This publication is for programmers who want to learn python. It is assumed that you know what variable, arrays, data structures, functions, etc. already are. You only need python’s syntax and tips for using them. So here, you will see more code and less explanation. If you are a programmer who only needs to fill in the blanks to become functional in python3, then you have come to the right place. If you need to learn programming, try googling “python for beginners”. There are many excellent free online sources to learn python. You don’t need to buy a book.

The best way to learn from this booklet is to copy paste the code, run it, modify it, and understand it.

python is:

  • an interpreting language
  • a fully object-oriented language
  • dynamically typed
  • strongly typed

Installing and running

If you are using Linux, python3 is probably already installed on your system. Type the following to check:

which python3

If you don’t have it installed, the command to install on Ubuntu is:

sudo apt-get install python3
sudo apt-get install python-pip3

On mac,

brew install python3
brew install pip3

To check if the python3 and pip3 was installed,

which python3
which pip3

To run a python program, simply type python3 and the file you want to run.

python3 hello.py

Variables

See the following program. Save as datatype.py:

a = 100    # integer
b = 1.23   # float
c = "python"    # string

# print variables
print(a)
print(b)
print(c)

# convert int to float
print(float(100))         

# convert float to int
print(int(3.14))

# convert string to int
d = "12"
e = "12.3"
print(int(d))
# print(int(e)) - this will generate an error

# convert string to float
print(float(d))
print(float(e))

# convert int to string
f = str(12)
print(type(f))

To run

chmod 755 datatype.py
python3 datatype.py

Output

100
1.23
python
100.0
3
12
12.0
12.3
<class 'str'>

Casting between string, int, and float is very simple is python. Simply use int(), float(), or str() functions.

Commandline Input

To take commandline input:

name = input("What is your name: ")
print(name)

output

What is your name:  mole
mole

Type casting

type() shows the data type. int() and float() casts to int and float, respectively.

age = input("What is your age")
print(type(age))
a = int(age)
print(type(a))

output

What is your age: 23
<class 'str'>
<class 'int'>

Numeric Operators

See the example:

print(2 + 2 * 2 - 2)
print(5 / 2)    # float division
print(5 // 2)   # integer division
print(5 % 2)  # modulus
print(5 ** 2)  # exponent

output

4
2.5
2
1
25

String operations

The following example covers the most common string manipulations.

print("hello")

# string vs character
name = "Justin"                    # string
achar = 'a'                        # character

# str() function
address = str()                    # empty string
address = str("25 Sussex Drive")   # create a list

# substrings
print(name[0])                     # J
print(name[4:6])                   # in
print(name[:4])                    # Just
print(name[3:])                    # tin
print(name[1:-1])                  # usti

# does the substring exist in string
print("tin" in name)               # True
print("xin" in name)               # False

# comparison
print(name == "Justin")            # True
print(name != "Justin")            # False

# commonly used functions
print(len(name))                   # 6
print(max(name))                   # u
print(min(name))                   # J
print('--------------------')

# ending strings, default is \n
print("python", end="")            # end without \n
print("python", end="|")           # end with |
print("\n")   # output for 3 lines # pythonpython|

# does the string contain
s1 = "abc123"                      # True
# true if string contains alphanumeric characters only
print(s1.isalnum())                # True
# digits only
print("123".isdigit())             # True
# lowercase English alphabet only
print("abc".islower())             # True
# uppercase English alphabet only
print("ABC".isupper())             # True
# whitespace characters only (space, \t, \n)
print("\t".isspace())              # True

Output of the code is in comments.

Lists

Lists can hold collection of values. The following example cover the common operations performed on lists.

# creating lists                   
a = list()         # empty list    # []
b = [1,2,3]                        # [1, 2, 3]
c = list(["python","programming"]) # ['python', 'programming']
# without [], each character becomes a separate element in the list
d = list("python")                 # ['p', 'y', 't', 'h', 'o', 'n']

print(a)
print(b)
print(c)
print(d)

# accessing list elements, indexing begins at 0
print(b[1])                        # 2

# concatenating lists
e = [4,5,6]
f = b + e
print(f)                           # [1, 2, 3, 4, 5, 6]

# replicating a list
g = b * 2
print(g)                           # [1, 2, 3, 1, 2, 3]

# using a for loop
for i in b:
    print(i, end=" ")              # 1 2 3

# list functions
print(1 in b)                      # True
print(4 not in b)                  # True
print(len(b))                      # 3
print(max(b))                      # 3
print(min(b))                      # 1
print(sum(b))                      # 6

# inserting values in list
b.append(4)
print(b)                           # [1, 2, 3, 4]
b.extend(e)
print(b)                           # [1, 2, 3, 4, 4, 5, 6]
b.insert(3,22)
print(b)                           # [1, 2, 3, 22, 4, 4, 5, 6]

# taking values out of list
b.pop()
print(b)                           # [1, 2, 3, 22, 4, 4, 5]
b.remove(4)
print(b)                           # [1, 2, 3, 22, 4, 5]

# sort and reverse the list
b.reverse()
print(b)                           # [5, 4, 22, 3, 2, 1]
b.sort()
print(b)                           # [1, 2, 3, 4, 5, 22]

Output is in the comments

Tuple

A tuple is an immutable list. Once created, it cannot be modified.

a = ()                              
b = (1, 2, 3)                      
c = [4, 5, 6]
d = tuple(c)
e = tuple("python")
print(a)                           # ()
print(b)                           # (1, 2, 3)
print(d)                           # (4, 5, 6)
print(e)                           # ('p', 'y', 't', 'h', 'o', 'n')

# substrings
print(b[0:2])                      # (1, 2)

# does a value exist in the tuple
print(2 in b)                      # True
print(5 not in b)                  # True

# basic functions
print(len(b))                      # 3
print(sum(b))                      # 6
print(min(b))                      # 1
print(max(b))                      # 3

# looping through tuple
for i in b:
print(i, end=" ")              # 1 2 3
print()

Output is in the comments.

Set

A set is an unordered collection on unique elements. Following example shows how to create and manipulate sets:

# create set
a = {'perl', 'php', 'python'}
print(a)          # {'python', 'perl', 'php'}

b = lang.copy()
print(b)          # {'python', 'perl', 'php'}

# add element to set
a.add('java')
print(a)          # {'python', 'perl', 'java', 'php'}

# difference between 2 sets
print(a.difference(b))    # {'java'}

# discard element from set, no error for missing element
# discard element from set, generate error for missing element
b.discard('php')
b.remove('perl')
print(b)          # {'python'}

# elements present in both sets
print(a.intersection(b))  # {'python'}

# return true for null intersection, false otherwise
print(a.isdisjoint(b))    # False

# is b a subset of a?
print(b.issubset(a))      # True
print(a.issubset(b))      # False

# is b a superset of a?
print(b.issuperset(a))      # False
print(a.issuperset(b))      # True

# empty set
b.clear()
print(b)          # set()

Output is in the comments.

Dictionary

A dictionary is an associated array. See the following code:

# create dictionary
emptydict = {}                     
print(emptydict)                   # {}

emergency = {
    'Canada' : '911',
'Switzerland' : '112'
}
print(emergency)                   # {'Canada': '911', 'Switzerland': '112'}

# retrieve and element
print(emergency['Canada'])         # 911

# add an element 
emergency['Seychelles'] = '999'
print(emergency)    
# {'Canada': '911', 'Seychelles': '999', 'Switzerland': '112'}

# delete an element
del emergency['Seychelles']
print(emergency)                   # {'Canada': '911', 'Switzerland': '112'}

# length of dictionary
print(len(emergency))              # 2

# in and not in operators
print('Canada' in emergency)       # True
print('USA' not in emergency)      # True

# empty the dictionary
deleteme = {
'useless' : 'information'      
}
print(deleteme)                        # {'useless' : 'information'}
deleteme.clear()                   
print(deleteme)                        # {}

# find key
print(emergency.get('Canada','not found'))  # 911
print(emergency.get('USA','not found'))     # not found

# get all keys or values
print(emergency.keys())            # dict_keys(['Switzerland', 'Canada'])
print(emergency.values())          # dict_values(['112', '911'])

The output is in the comments.

Conditionals

Python supports if-else conditions. See code below

a, b = 100, 200
if a < b:
    print('a ({}) < b ({})'.format(a, b))
else:
    print('a ({}) >= ({})'.format(a, b))

output

a (100) < b (200)

Loops

Python uses while and for loops. Python for loops are similar to foreach loop in PHP. See code below:

# loop through entire array
color = ['red','green','blue']
for i in color:
    print(i)

output

red
green
blue

Loop through part of the array

color = ['red','green','blue']

# skip first element
for i in color[1:]:
    print(i)

# only last 2 elements
for i in color[-2:]:
    print(i)

output

green
blue

Using while loop

count = 0 
while (count < 3):
    print(count)
    count += 1

output

0
1
2

Functions

Python functions are defined by def. Once again, indentation, not {}, are used to define blocks. See code below:

def xply(m,n):
    return m * n

print(xply(32,54))    # output = 1728

Python is a fully object-oriented programming language but it can also be used for scripting. It is assumed that you are already familiar with object-orientation concepts and have experience writing object-oriented code in another language. This page will show how to write object-oriented code in Python without explaining object-oriented concepts.

The following class computes the area of a rectangle. Classes are defined with class keyword. init is the constructor. length and width are initialized in the constructor. Area is calculated by calcArea().

# class definition
class Rectangle():

   # constructor
    def __init__(self, length, width):
        self.length = length
        self.width = width

    # calculate area
    def calcArea(self):
        return self.length * self.width

# run the code
r = Rectangle(23, 32)
print(r.calcArea())       # output = 736

To use the class, we first create and object, r and then call r.calcArea() function.

Inheritance

Following example shows how to use inheritance in Python3.

class Vehicle():

    def __init__(self, make, model):
        self.make = make
        self.model = model

    def __str__(self):
        return self.make + " " + self.model

class Car(Vehicle):

    def __init__(self, make, model, wheels):
        Vehicle.__init__(self, make, model)
        self.wheels = wheels

    def __str__(self):
        return Vehicle.__str__(self) + " has " + self.wheels + " wheels"

v = Vehicle('Toyota','Corolla')
print(v)             
m = Car('Toyota','Corolla','four')
print(m)

output

Toyota Corolla
Toyota Corolla has four wheels

Vehicle is the base class. Car is the subclass. Note that declaration of the class Car takes superclass name as input parameter. Also note the use of Vehicle.init in Car class to initialize superclass variables and the use of Vehicle.str(self) in the str method to print values from superclass.

Polymorphism

Polymorphism basically refers to the same method call doing different things for different subclasses. In the following example, seats() method returns different results based on which subclass is called. The property name in the super class is used by all subclasses.

class Vehicle():
    def __init__(self, name):
        self.name = name

    # abstract method
    def seats(self):
        raise NotImplementedError("Subclass must implement abstract method")

class Sedan(Vehicle):
    def seats(self):
        return '4'

class Minivan(Vehicle):
    def seats(self):
        return '7'

class Motorbike(Vehicle):
    def seats(self):
        return '2'

vehicles = [Sedan('Tesla Model S'),
            Minivan('Toyota Sedona'),
            Motorbike('Harley Davidson')]

for vehicle in vehicles:
    print(vehicle.name + ' has ' + vehicle.seats() + ' seats')

output

Tesla Model S has 4 seats
Toyota Sedona has 7 seats
Harley Davidson has 2 seats

In Python, you can handle errors with exceptions. The following code will generate an FileNotFound error because the file it is trying to open does not exist:

fh = open('data.txt')
for strline in fh.readlines():
    print(strline)

output

FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'

An error is generated and the program stop executing. Suppose, we want to program to continue because we will compensate for this error later in the code. We can use try: except:

try:
    fh = open('data.txt')
    for strline in fh.readlines():
        print(strline)
except:
    print('Error generated')
print('ignore and continue')

output

Error generated
continue regardless

If the code is try block generate an error, the action to be taken in response to this error is define in the except block. In this code, the error is caught is try and the action we defined is simply print and message and move one. If you come across this error while running a software, this error is not very helpful so let’s write a more useful error.

try:
    fh = open('data.txt')
    for strline in fh.readlines():
        print(strline)
except IOError as err:
    print("Error generated in file open and print code: ({})".format(err)) 
print('ignore and continue')

output

Error generated in file open and print code: ([Errno 2] No such file or directory: 'data.txt')
ignore and continue

Python uses re library for regular expressions. re comes with standard python installation. Regular expressions is a very big topic. This page only covers the basics of how to search and substitute. The syntax for patterns is the same as Perl. Consider the following example:

import re

line = "apple banana carrot date eggplant fig guava"

# is there a match?
if re.search("carrot", line):
    print('match found')
else:
    print('match not found')

# what was matched?
mat = re.search(r'(.*) date (.*)', line)
if mat:
    print(mat.group())
    print(mat.group(1))
    print(mat.group(2))
else:
    print('no')

# how to match and substitute a substring
res = re.sub(r'guava', "grapefruit", line)
if res:
    print(res)
else:
    print('not substitution')

output

match found
apple banana carrot date eggplant fig guava
apple banana carrot
eggplant fig guava
apple banana carrot date eggplant fig grapefruit

First, we need to import re. The two function of re that we are interested in a re are search and sub. Search finds the substring (m// in Perl). sub finds and substitutes the substring with another (s/// in Perl).

Unit testing functionality is built into Python3. Following is a very simple example:

def Arithmetic(a)
    return a * a

Save this file as mycode.py. Save the following code as mycode_unittest.py

import unittest
from mycode import Arithmetic

class ArithmeticTest(unittest.TestCase):

    def setUp(self):
        self.arr1 = ((0,0),(1,1))
        self.arr2 = ((1,2),(3,4))

    def testCalculation(self):
        for (i,val) in self.arr1:
            self.assertEqual(Arithmetic(i), val)
        for (i,val) in self.arr2:
            self.assertNotEqual(Arithmetic(i), val)

    def tearDown(self):
        self.arr1 = None
        self.arr2 = None

if __name__ == "__main__": 
    unittest.main()

When you run the unit test code, you will not get any errors. Change assertEqual to assertNotEqual. Then you will get unit test failure.

This page shows how to work with text files using Python3 code. Following is a sample txt file we will be using. Lets called it stocks.txt

bce.to
hnd.to 
mtl.to

Reading from File

You can use read(), readline(), or readlines() function to read from a file. This example uses read()

stocks = open("data/stocks.txt","r")
stocks.read()
stocks.close()

output:

'bce.to\nhnd.to\nmtl.to'

Using readline():

stocks = open("data/stocks.txt","r")
stocks.readline()
stocks.readline()
stocks.close()

output:

bce.to

hnd.to

Note that each readline() will print the next row in the file.

Using readlines(): Note the plural readlines as opposed to readline.

stocks = open("data/stocks.txt","r")
stocks.readlines()
stocks.close()

output:

['bce.to\n', 'hnd.to\n', 'mtl.to']

readlines() outputs each row as a list element

Easy way to remove newlines

stocks = open("data/stocks.txt","r")
for stocksymbol in stocks.read().splitlines():
    print(stocksymbol)
stocks.close()

output:

bce.to
hnd.to
mtl.to

Write to File

fh = open("data/stocks.txt", "w")
newstock = 'ta.to'
fh.write(newstock)
fh.close()

After you run the code, stocks.txt will contain

ta.to

Your code overwrote the existing contents of the file. To append the text rather than overwriting, use the “a” option:

fh = open("data/stocks.txt", "w")
newstock = 'ta.to'
fh.write(newstock)
fh.close()

After you run the code, stocks.txt will contain

bce.to
hnd.to
mtl.to
ta.to

NumPy is a python library that makes it easy to perform mathematical and logical operations on arrays. It provides high performance n-dimensional array object and tools to work with the arrays.

Why use numpy instead of a python list

  1. Takes less memory
  2. Faster than python lists
  3. More powerful
  4. Easier to use

What is my numpy version

import numpy as np
print (np.__version__)

output

1.9.3

Creating numpy arrays

There are many ways to create numpy arrays

# create arrays
avalues = [12, 34, 45]
a = np.array(avalues)
b = np.array([11.1,22.2,33.3])

# 2D array, matrix
c = np.array([(1, 2, 3), (4 , 5, 6)])

# print results
print('a = ', a)              
print('b = ', b)               
print(c)

output

a =  [12 34 45]
b =  [ 11.1  22.2  33.3]
[[1 2 3]
 [4 5 6]] 

Using arange() function to create arrays

d = np.arange(5)              # create array of 5 numbers starting from 0
e = np.arange(9)+1            # create array of 9 numbers starting from 1
f = np.arange(10, 20)         # array of numbers 10 to 19
g = np.arange(10, 31, 5)      # array 10 to 30 with 5 increments
h = len(np.arange(10,15))     # get size of array
i = np.arange(10, 20).size    # get size of array
j = np.arange(9).reshape(3,3) # create 2D array

# print results
print('d = ', d)
print('e = ', e)
print('f = ', f)
print('g = ', g)
print('h = ', h)
print('i = ', i)
print(j)

output

d =  [0 1 2 3 4]
e =  [1 2 3 4 5 6 7 8 9]
f =  [10 11 12 13 14 15 16 17 18 19]
g =  [10 15 20 25 30]
h =  5
i =  10
[[0 1 2]
 [3 4 5]
 [6 7 8]]

Using numpy function linspace()

d = np.linspace(10, 20, 5)                # create elements with 2.5 intervals
e = np.linspace(10, 20, 5, retstep=True)  # also shows the interval size
print(d)
print(e)
print(e[1])                               # get the interval size

output

[ 10.   12.5  15.   17.5  20. ]
(array([ 10. ,  12.5,  15. ,  17.5,  20. ]), 2.5)
2.5

Using zeros() and ones() functions

a = np.zeros(4)               # array of 4 zero elements
b = np.ones(4)                # array of 4 one elements
c = np.ones(5, dtype='int64') # create int64 array
d = np.zeros((4,3))           # 4 x 3 matrix of zeros
e = np.zeros((2,3,4))         # Two 3 x 4 matrices of zeros
print(a, "\n", b, "\n", c, "\n-----\n", d, "\n-----\n", e)

output

[ 0.  0.  0.  0.] 
 [ 1.  1.  1.  1.] 
 [1 1 1 1 1] 
-----
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]] 
-----
 [[[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]

 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]]

Array Type

A numpy array can only have one type. To find the type of an array,

print(a.dtype)         # int64
print(b.dtype)         # float64

output

type of a =  int64
type of b =  float64

Since every element of a numpy array must have the same data type, in the following example, int and float elements will be converted to complex numbers:

tpl = (10, -1.23, 2+3j)
c = np.array(tpl)
print(c)

output

[ 10.00+0.j  -1.23+0.j   2.00+3.j]

Using arrays

Working with 1D array

a = np.array([-3, -2, 0, 1, 2])
print(a)
print(a[1])
a[2] = 100
print(a)
print(a.size)

output

[-3 -2  0  1  2]
-2
[ -3  -2 100   1   2]
5

Working with 2D array

b = np.arange(21)
b.shape = (3, 7)
print(b)
print('-----')
print(b[2])
print(b[2,1])
print(b[2][1])

output

[[ 0  1  2  3  4  5  6]
 [ 7  8  9 10 11 12 13]
 [14 15 16 17 18 19 20]]
-----
[14 15 16 17 18 19 20]
15
15

Working with 3D array

c = np.arange(27)
c.shape = (3,3,3)
print(c)
print('-----')
print(c[0])
print('-----')
print(c[0,1])
print('-----')
print(c[0,1,2])
print('-----')
c[0,1,2] = 999
print(c)

output

[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
-----
[[0 1 2]
 [3 4 5]
 [6 7 8]]
-----
[3 4 5]
-----
5
-----
[[[  0   1   2]
  [  3   4 999]
  [  6   7   8]]

 [[  9  10  11]
  [ 12  13  14]
  [ 15  16  17]]

 [[ 18  19  20]
  [ 21  22  23]
  [ 24  25  26]]]

Arithmetic operations on arrays

Note that numpy overloads operators operate differently on python arrays and numpy arrays.

# subtract all values
print(a - b)           # [  0.9  11.8  11.7]
# cube all values
print(a**3)            # [ 1728 39304 91125]
print(2 * np.cos(a))   # [  1367.631  10941.048  36926.037]
print(a < 30)          # [ True False False]
# multiply values
print(a * b)           # [  133.2   754.8  1498.5]
# matrix product
print(a.dot(b))        # 2386.5

Output is in the code comments.