Category: python tutorials

  • Python Tutorial Series – Packages

    Python modules can be organized into packages. A package is a namespace which contains a set of modules. Packages are useful for organizing modules, especially when an application is large and contains a lot of modules. To create a package, create a directory and add python files to it. The directory must contain an __init__.py…

  • Python Tutorial Series – Classes

    In object oriented programming, classes are used as a blueprint to define objects in an application. Classes are a way to organize code, provide the ability to easily extend functionality and allow for reuse. In Python, a class is defined as follows: class BaseClass:   # constructor   def __init__(self, param1):     self.param1 =…

  • Python Tutorial Series – Modules

    A Python module is composed of the classes, functions and variables contained in a single python file. A python file can contain multiple classes, not just a single class like in Java. Module names should be simple and ideally kept to one word. Below is an example module, which is contained in a file called…

  • Python Tutorial Series – Files

    In Python, files are opened with the open command. The open command takes two parameters. The first parameter is the file name. The second parameter is the mode to open the file in: r – Open the file for reading. An error is thrown if the file does not exist. a – Open the file…

  • 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   #…