Category: tutorials
-
Python Tutorial Series – Exceptions
Exceptions are used to catch errors during program execution. Catching errors with exceptions during program execution helps prevent a program from crashing. Exception handling is performed in Python as follows: try: do_something() except: print (‘Exception occurred’) Catch a specific exception: try: do_something() except ValueError: print (‘ValueError Exception occurred’) Catch multiple exceptions:…
-
Python Tutorial Series – Functions
In Python, functions are defined as follows: def addTwoNumbers(a, b): return a + b x = 3 y = 4 result = addTwoNumbers(x, y) print(‘The sum of {} and {} is {}’.format(x, y, result)) Python also supports setting default values for the input parameters. Example: def addTwoNumbers(a = 3, b = 2): return…
-
Python Tutorial Series – Dictionaries
Another type of collection that Python provides support for is dictionaries. Dictionaries Dictionaries are used to store comma separated key, value pairs enclosed in curly brackets, example: to_do_dict = {‘a’:’groceries’, ‘b’:’laundry’, ‘c’:’wash car’, ‘d’:’vacuum’} Dictionaries do not have any order and may be changed. Elements are accessed by using their key value. For example this…
-
Python Tutorial Series – Loops
Python has two ways to loop through data. For Loop The first is the for loop. The for loop is used when you know how many iterations to make. Example to loop through a dictionary using a for loop: Show keys: to_do_dict = {‘a’:’groceries’, ‘b’:’laundry’, ‘c’:’wash car’, ‘d’:’vacuum’} print (“TO DO ITEM KEYS:”) for key…
-
Python Tutorial Series – Tuples
Another type of collection that Python provides support for is tuples. Tuples Tuples are used to store comma separated values enclosed in parentheses, example: to_do_tuple = (‘groceries’, ‘laundry’, ‘wash car’, ‘vacuum’) Note: If the tuple has only one element, a comma after the element is needed, example: to_do_tuple = (‘groceries’,) Tuples retain their order and,…
-
Python Tutorial Series – Variables
In Python, variable names must start with a letter or underscore, are case sensitive and each word should be separated by an underscore. Variable names may only contain letters, numbers and underscores. Python automatically interprets what type a variable is. A variable declaration is just: diameter = 13 # An integer radius = 6.5 #…
-
Python Tutorial Series – Lists
Python provides support for several types of collections. A collection holds a group of items. An item may be a number, string, object or another collection. One Python collection type is a list. Each item in the list may be a different data type. Lists Lists are used to store comma separated values enclosed in…
-
Python Tutorial Series – Style Guide
A best practice in programming is to adhere to a consistent style guide. It allows for more readable code and easier maintenance. Python actually has it’s own suggested style guide. It is online at: https://www.python.org/dev/peps/pep-0008/. Some highlights include: – Indentation is 4 spaces – This is really important. If a file has a line with…
-
Python Tutorial Series – Comments
Comments are used to make notes in source code about what a piece of code does or to not include executable lines of code when the application is run. Comments are created in Python by adding a hash tag symbol in front of the code to exclude. Example: # This is a comment. There is…
-
Python Tutorial Series – IDE
Python source files end in .py. Python source files can be written in a simple text editor. Python comes installed with a tool called IDLE, Integrated Development and Learning Environment. It is an editor with a shell window. It also has a debugger. A popular alternative is Anaconda. Anaconda is a huge all in one…