Here’s a precise manual I put together for convenient learning of Python programming language for a beginner and as an easy reference. Bookmark or re-download periodically as I will keep updating this page for improvement. Installation and some other instructions are currently only for Mac OS X operating system.


\[\\[0.2in]\]

Online documentation

https://docs.python.org/3/

\[\\[0.4in]\]

Install python

brew install python3  # for latest version of python 3

IPython is probably the best command line REPL application you can find (provides some nice features like syntax highlighting, code completion, and much more). Install it using commandline and open as follows:

pip install ipython
ipython

\[\\[0.4in]\]

Install pip and packages

sudo easy_install pip
python3 -m pip install    # install for python3 version of python
python3 -m pip install --upgrade pip  # upgrade pip

pip install <package> #install a package
pip install <package>==<version>
python -m pip install <package>
pip install –U <package> #upgrade a package
pip uninstall <package> #uninstall a package
pip show <package> #show package details

\[\\[0.4in]\]

Open python (interactively)

Open Terminal (in Mac). Type python3 to open Python REPL (Read-Evaluate-Print-Loop). Type quit() to close REPL. The same interactive prompt can be accessed by using the IDLE application that’s installed by Python automatically (in Applications/Python directory in Mac).

name = "Nilotpal"
print(name)
## Nilotpal

\[\\[0.4in]\]

Run saved python script

To run, a python program from file, save the script into prog.py and then run in commandline using

python3 prog.py

For shell script, write prog.py as

#!/usr/bin/python3
name = "Nilotpal"
print(name)

Then, in the terminal

chmod u+x program.py. #set execution permission
./program.py

\[\\[0.4in]\]

Indentation

Indentation in Python is meaningful. You cannot indent randomly like this (will give error):

name = "Flavio"
    print(name)

\[\\[0.4in]\]

Commenting

# This is commeting
"""This is called docstring""" #(can be multiline)
## 'This is called docstring'

\[\\[0.4in]\]

Data types

Strings (str)

name = "Nilotpal"
type(name) #<class 'str'>
type(name)==str #True
isinstance(name, str) #True
name = str("Nilotpal")  #create a variable of a specific type
name[0] #'N'
name[1] #'i'
name[-1] #'l'
name[-2] #'a'
name[0:2] #"Ni"
name[:2] #"Ni"
name[2:] #"lotpal"
## multiline strings ussing """
print("""Roger is
    8
years old
""")
## Built-in methods for strings. Use like this example: name.isalpha(), name.lower()
name.isalpha() #True #to check if a string contains only characters and is not empty 
name.isalnum() #True #to check if a string contains characters or digits and is not empty
name.isdecimal() #False #to check if a string contains digits and is not empty 
name.lower() #'nilotpal' #to get a lowercase version of a string
name.islower() #False #to check if a string is lowercase
name.upper() #'NILOTPAL' #to get an uppercase version of a string
name.isupper() #False #to check if a string is uppercase
name.title() #'Nilotpal' #to get a capitalized version of a string
name.startswith("a") #False #to check if the string starts with a specific substring 
name.endswith("a") #False #to check if the string ends with a specific substring 
name.replace("a","b") #'Nilotpbl' #to replace a part of a string
name.split("l") #['Ni', 'otpa', ''] #to split a string on a specific character separator
name.strip() #'Nilotpal' #to trim the whitespace from a string
name.join("##") #'#Nilotpal#Nilotpal#' #to append new letters to a string
name.find("lo") #2 #to find the position of a substring

Integers (int), Floats (float)

age = 1
type(age) == int #True
age = int("20")
print(age) #20
fraction = 0.1
type(fraction) == float #True
intFraction = int(fraction)
print(intFraction) #0

Range (range)

range(4) #creates a sequence that starts from 0 and contains 4 items: [0, 1, 2, 3].
## range(0, 4)
range(10,13) #[10,11,12,13]
## range(10, 13)
a = [3,6,9]
for i in enumerate(a):  #to get index
    print(i)
## (0, 3)
## (1, 6)
## (2, 9)

Complex numbers (complex)

complexNumber = 2+3j
complexNumber = complex(2, 3)

Boolean (book)

done = False
done = True

Lists (list)

days = ["Monday", "Tuesday"]  #can also use single quotes
items = ["Monday", 4, "Tuesday", True]
null = []
items[0] #"Monday"
items.index("Monday") #0
len(items) #4
items.append("Test") #["Monday", 4, "Tuesday", True, 'Test']
items += ["Test1"] #["Monday", 4, "Tuesday", True, 'Test', 'Test1']
items += "Test" #['Monday', 4, 'Tuesday', True, 'Test', 'Test1', 'T', 'e', 's', 't']
items.remove("Tuesday") #['Monday', 4, True, 'Test', 'Test1', 'T', 'e', 's', 't']
items.insert(1, "Hello") # add "Test" at index 1 #['Monday', 'Hello', 4, True, 'Test', 'Test1', 'T', 'e', 's', 't']
items1 = [items[i] for i in [0,1,4]] #['Monday', 'Hello', 'Test']
items1.sort() #['Hello', 'Monday', 'Test']
sorted(items1, key=str.lower) #['Hello', 'Monday', 'Test']
names = ("Roger", "Syd")
names += ('hi','gu') #('Roger', 'Syd', 'hi', 'gu')
names += ('ho','gr') #error, only tuples can be added

numbers = [1, 2, 3, 4, 5]
numbers_power_2 = [n**2 for n in numbers] #[1, 4, 9, 16, 25]

Tuples (tuple)

Once a tuple is created, it can’t be modified. You can’t add or remove items.

names = ("Roger", "Syd")
names[0] #"Roger"
sorted(names)

Dictionaries (dict)

While lists allow you to create collections of values, dictionaries allow you to create collections of key / value pairs.

dog = { 'name': 'Roger' }
dog = { 'name': 'Roger', 'age': 8 }
dog['name'] #'Roger'
dog['age']  #8
dog['name'] = 'Syd'
dog.get('name') #'Roger'
dog.get('test', 'default') #'default' #add a default value if missing
dog.pop('name') #'Roger' #retrieves the value of a key, and subsequently deletes the item from the dictionary
dog #{'age': 8}
dog.popitem() #retrieves and removes the last key/value pair inserted into the dictionary
dog #{}
dog = { 'name': 'Roger', 'age': 8 }
'name' in dog #True
list(dog.keys()) #['name', 'age']
list(dog.values()) #['Roger', 8]
list(dog.items()) #[('name', 'Roger'), ('age', 8)]
del dog['age'] #[('name', 'Roger')]

Sets (set)

Sets work like tuples, but they are not ordered, and they are mutable. Or we can say they work like dictionaries, but they don’t have keys.

set1 = {"Roger", "Syd"}
set2 = {"Roger"}
intersect = set1 & set2 #{'Roger'}
union = set1 | set2 #{'Roger', 'Syd'}
difference = set1 - set2 #{'Syd'}
isSuperset = set1 > set2 # True
list(set1) #['Syd', 'Roger']

\[\\[0.4in]\]

Operators

# Assignment and Arithemtic operators
a = 2
"Roger" + " is a good dog"  #'Roger is a good dog'
"Roger" + str(8)  # 'Roger8' #concatenate a number to a string
age = 8
age += 1 # age is now 9

## other such operators: -=, *=, /=, %=, etc.

# Boolean operators: or (or |), and ( or &), not (or ~)
condition1 = True
condition2 = False
not condition1 #False
condition1 and condition2 #False
condition1 or condition2 #True
## `or` used in an expression returns the value of the first operand that is not a falsy value ( False , 0 , '' , [] ..). Otherwise it returns the last operand.
print(0 or 1) #1
print(False or 'hey') #'hey'
print('hi' or 'hey') #'hi'
print([] or False) #'False'
print(False or []) #'[]'
## `and` only evaluates the second argument if the first one is true. So if the first argument is falsy ( False , 0 , '' , [] ..), it returns that argument. Otherwise it evaluates the second argument:
print(0 and 1) #0
print(1 and 0) #0
print(False and 'hey') #False
print('hi' and 'hey') #'hey'
print([] and False ) #[]
print(False and [] ) #False
# in
print("nil" in name) #True

# The Ternary Operator
## Instead of writing:
def is_adult(age):
    if age > 18:
        return True
    else:
        return False

## you can write
def is_adult(age):
    return True if age > 18 else False

\[\\[0.4in]\]

If, while, for loops

a = 0            
while a < 10:   
    a = a + 1    
print(a) # 10
x = 4
if x>2: print(2) #2
if x>2:
    print(2)   
    print("hi")
else:
    print(3) #2 #hi
if x == 2:
    print("bla")
elif name == "Nil":
    print("Hello Nil")
else:
    print("okay") #okay
result = 2 if a == 0 else 3  # ternary

for i in (1,2,3):
    print(i)
for item in range(4):  #sequence from 0 with length 4, i.e., [0,1,2,3]
    print(item)

#break, continue works as in R.

\[\\[0.4in]\]

Various inbuilt functions

any([2>1,3<2]) #True
all([2>1,3<2]) #False
abs(-2.3) #2.3
round(0.12, 1) #0.1
len([1,2]) #2

\[\\[0.4in]\]

Functions

def hello(name):
    print('Hello ' + name + '!')

def hello(name):
    if not name:
        return
    print('Hello ' + name + '!')

def hello(name):
    print('Hello ' + name + '!')
    return name, 'Roger', 8 #the return value is a tuple containing those 3 values

\[\\[0.4in]\]

Define new class

class Dog:  # the Dog class
    def bark(self):
        print('WOF!')
roger = Dog()
type(roger)  #<class '__main__.Dog'>

# A Sample class with init method
class Person: 
    # init method or constructor
    def __init__(self, name):
        self.name = name
    # sample method
    def say_hi(self):
        print('Hello, my name is', self.name)
p = Person('Nikhil')
p.say_hi() #Hello, my name is Nikhil

# Inheritance
class Animal:
    def walk(self):
      print('Walking..')
class Dog(Animal):
    def bark(self):
      print('WOF!')
roger = Dog()
roger.walk() # 'Walking..'
roger.bark() # 'WOF!'

\[\\[0.4in]\]

Modules

Every Python file is a module.

import dog
dog.bark()
#or
from dog import bark  # lets us pick the things we need.
bark()

# Suppose you put dog.py in a subfolder. In that folder, you need to create an empty file named __init__.py. This tells Python the folder contains modules.
from lib import dog
dog.bark()
#or
from lib.dog import bark
bark()

The Python standard library is a huge collection of all sort of utilities, ranging from math utilities to debugging to creating graphical user interfaces. The full list of standard library modules is here: https://docs.python.org/3/library/index.html. Some of the important modules are:

  • math for math utilities
  • re for regular expressions
  • json to work with JSON
  • datetime to work with dates
  • sqlite3 to use SQLite
  • os for Operating System utilities
  • random for random number generation statistics for statistics utilities
  • requests to perform HTTP network requests http to create HTTP servers
  • urllib to manage URLs

\[\\[0.4in]\]

Input and import modules and data

# user input
age = input()
print('Your age is ' + age)

# import module/function
import math
math.sqrt(4) # 2.0
from math import sqrt

#import data

\[\\[0.4in]\]

with function

# Instead of writing:
filename = '/Users/flavio/test.txt'
try:
    file = open(filename, 'r')
    content = file.read()
    print(content)
finally:
    file.close()

#You can write:
filename = '/Users/flavio/test.txt'
with open(filename, 'r') as file:
    content = file.read()
    print(content)

\[\\[0.4in]\]

Debugging

You debug by adding one/more breakpoints into your code:

breakpoint()

When the Python interpreter hits a breakpoint in your code, it will stop, and wait for your instruction.

  • Press c to continue the execution of the program normally.
  • Type the name of any variable to inspect its value.
  • Press s to step to the next line in the current function. If the next line is a function, the debugger goes into that, and you can then run one instruction of that function at a time.
  • Press n to step to the next line in the current function. If the code calls functions, the debugger does not get into them, and consider them “black boxes”.
  • Press q to stop the execution of the program.

\[\\[0.4in]\]

Closure

If a function returns another function defined inside it, then the returned function has access to the variables defined in the first function even if the first function is not active any more.

def counter():
    count = 0
    def increment():
        nonlocal count
        count = count + 1
        return count
    return increment
increment = counter()
print(increment()) # 1
## 1
print(increment()) # 2
## 2
print(increment()) # 3
## 3
#increment() function still has access to the state of the count variable even though the counter() function has ended.

\[\\[0.4in]\]

Error-catching

try:
    # some lines of code

try:
    # some lines of code
except <ERROR1>:
    # handler <ERROR1>
except <ERROR2>:
    # handler <ERROR2>
except:
    # catch all other exceptions
else:
    # ran if no exceptions were found
finally:
    # do something in any case, regardless if an error occurred or not

try:
    result = 2 / 0
except ZeroDivisionError:
    print('Cannot divide by zero!')
finally:
    result = 1
print(result) # Cannot divide by zero! # 1

\[\\[0.4in]\]

The PEP8 Python style guide

Python defines its conventions in the PEP8 (Python Enhancement Proposals) style guide. Its full content is here: https://www.python.org/dev/peps/pep- 0008/ but here’s a quick summary of the important points to start with:

  • Indent using spaces, not tabs
  • Indent using 4 spaces.
  • Python files are encoded in UTF-8
  • Use maximum 80 columns for your code
  • Write each statement on its own line
  • Functions, variable names and file names are lowercase, with underscores between words (snake_case)
  • Class names are capitalized, separate words are written with the capital letter too, (CamelCase)
  • Package names are lowercase and do not have underscores between words
  • Variables that should not change (constants) are written in uppercase Variable names should be meaningful
  • Add useful comments, but avoid obvious comments
  • Add spaces around operators
  • Do not use unnecessary whitespace
  • Add a blank line before a function
  • Add a blank line between methods in a class
  • Inside functions/methods, blank lines can be used to separate related blocks of code to help readability

\[\\[0.4in]\]

\[\\[1in]\]