Virtual Environment

To create virtual environment, for Linux, install virtual environment utility:

sudo apt install python3-virtualenv

change directory into project directory and create virtual environment with following command:

virtualenv venv (where venv is the folder name for the virtual environment)

To run the virtual environment, go to:

../venv/bin and type

source activate to enable virtual environment.

Program organization

For Python program, each file with .py extension is consider a module. Example: is a math module.

To create a package to further group the modules together, create a directory along with a file. The directory will be consider as a package.


Four different scopes exist in Python.

  1. Local – the innermost one and contains local names.
  2. Enclosing – any function within the enclosing scope where it contains non-local naming and non-global names.
  3. Global – global names.
  4. Built-in – Set of functions that are available from default. Such as print(), all, abs, etc.

In Python, scopes are also defined by indentation as well. Each indentation is a scope of its own.

When identing in Python, there is no hard rule of how many spaces but in general, people uses 4 spaces to indent Python programs.

During execution, Python will try to find the variables using LEGB rule where it will try to find the variables from local scope first and go down to build-in scope. If Python couldn’t find the variable anywhere, it will raise NameError exception.


Basic class structure

class ClassName:

def __init__(self, var1, var2):

self.var1 = var1

self.var2 = var2

def myFunc(self):

… declaration

def __init__(self, var1, var2): is a initializer method. The purpose of this method is to set up the object with the values passed in such as var1 and var2.

Class attribute and Instance attribute

class Person:
species = ‘Human’

Person.isAlive = True # this will add a class attribute to Person class that will be share among all Person objects.
man = Person() = ‘John’ # this will add an instance attribute to man object that belongs to man only.



No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Cookie Consent with Real Cookie Banner