Python Workshop: Basics II

Open In Colab


Based on:

this git of Zhiya Zuo

&

tutorials from tutorialspoint


Control Logics

In the following examples, we show examples of comparison, if-else loop, for loop, and while loop.

Comparison

Python syntax for comparison is the same as our hand-written convention:

  1. Larger (or equal): > (>=)
  2. Smaller (or equal): < (<=)
  3. Equal to: ==
  4. Not equal to: !=
In [1]:
3 == 5 
Out[1]:
False
In [2]:
72 >= 2
Out[2]:
True
In [3]:
test_str = "test"
test_str == "test" # can also compare strings
Out[3]:
True

If-Else

In [4]:
sum_ =0
In [5]:
if sum_ == 0:
    print("sum_ is 0")
elif sum_ < 0:
    print("sum_ is less than 0")
else:
    print("sum_ is above 0 and its value is " + str(sum_)) # Cast sum_ into string type.
sum_ is 0

Comparing strings are similar

In [6]:
store_name = 'Walmart'
In [7]:
if 'Wal' in store_name:
    print("The store is not Walmart. It's " + store_name + ".")
else:
    print("The store is Walmart.")
The store is not Walmart. It's Walmart.

For loop

In [8]:
for letter in store_name:
    print(letter)
W
a
l
m
a
r
t

range() is a function to create integer sequences:

In [9]:
a_range = range(5)
print(a_range)
print("range(5) gives" + str(list(range(5)))) # By default starts from 0
print("range(1,9) gives: " + str(list(range(1, 9)))) # From 1 to 8 (Again the end index is exclusive.)
range(0, 5)
range(5) gives[0, 1, 2, 3, 4]
range(1,9) gives: [1, 2, 3, 4, 5, 6, 7, 8]
In [10]:
for index in range(len(store_name)): # length of a sequence
    print("The %ith letter in store_name is: %s"%(index, store_name[index]))
The 0th letter in store_name is: W
The 1th letter in store_name is: a
The 2th letter in store_name is: l
The 3th letter in store_name is: m
The 4th letter in store_name is: a
The 5th letter in store_name is: r
The 6th letter in store_name is: t

List comprehensions

List comprehensions provides an easy way to create lists:

In [11]:
x = [i for i in range(10)]
print(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

a lot of cool things can be done in one line!

In [12]:
x = [i + 2 for i in range(10)]
print(x)

x = [i ** 2 for i in range(10) if i%2==0]
print(x)
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[0, 4, 16, 36, 64]

While loop

In [13]:
x = 2
In [14]:
while x < 10:
    print(x)
    x = x + 1
2
3
4
5
6
7
8
9

Notes on break and continue

break means get out of the loop immediately. Any code after the break will NOT be executed.

In [15]:
store_name = 'Walmart'
In [16]:
index = 0
while True:
    print(store_name[index])
    index += 1 # a += b means a = a + b
    if store_name[index] == "a":
        print("End at a")
        break # instead of setting flag to False, we can directly break out of the loop
        print("Hello!") # This will NOT be run
W
End at a

continue means get to the next iteration of loop. It will break the current iteration and continue to the next.

In [17]:
for letter in store_name:
    if letter == "a":
        continue # Not printing a
    else:
        print(letter)
W
l
m
r
t

Functions

Structure of a function

def func_name(arg1, arg2, arg3, ...):
    #####################
    # Do something here #
    #####################
    return output

return output` is NOT required

One input one output

In [18]:
def F(n): # bonus- what does this function do? 
    if n<0: 
        print("Incorrect input") 
    elif n==0: 
        return 0
    elif n==1: 
        return 1
    else: 
        return F(n-1)+F(n-2) 
In [19]:
print(F(2))

print(F(3))

print(F(5))

print(F(7))
1
2
5
13

Multiple outputs

reference- geometric sequence equations:

$a_n = a_1 \cdot q^{n-1}$

$S_n = \frac{a_1\cdot(q^n-1)}{q-1}$

In [20]:
def geo_seq(a_1, q, n):
    a_n = a_1 * (q ** (n-1))
    S_n = (a_1 * (q ** n - 1)) / (q - 1)
    return a_n, S_n
In [21]:
print(geo_seq(2, 2, 1)) # multiple outputs returns as a tuple

print(geo_seq(2, 2, 2))

print(geo_seq(2, 2, 2))

print(geo_seq(2, 2, 3)[1]) # get only second element
(2, 2.0)
(4, 6.0)
(4, 6.0)
14.0

optional args

In [22]:
def geo_seq_optional_args(a_1, q=2, n=1):
    a_n = a_1 * (q ** (n-1))
    S_n = (a_1 * (q ** n - 1)) / (q - 1)
    return a_n, S_n
In [23]:
print(geo_seq_optional_args(2))

print(geo_seq_optional_args(2, n=2))
(2, 2.0)
(4, 6.0)

Classes

As been said before- Python is object oriented programing (OOP) language, so every variable is actually an instance of some class.

Here are some class basics:

In [24]:
class Employee:

  # the function that is being called each time a new instance is created
  def __init__(self, name="Jhon", salary=10000):
    # per instance variables
    self.name = name
    self.salary = salary

  def display_employee(self):
    print("Name : " + self.name + ", Salary: "+ str(self.salary))
    
  def change_salary(self, new_salary):
    self.salary = new_salary
In [25]:
emp1 = Employee() #create new instance
emp1.display_employee()

emp2 = Employee("Bob", salary=20000) #create new instance
emp2.display_employee()

emp2.change_salary(30000)
emp2.display_employee()

# instance variables are also accessible - no such thing private/public vars
emp2.name = "Larry"
emp2.display_employee()
Name : Jhon, Salary: 10000
Name : Bob, Salary: 20000
Name : Bob, Salary: 30000
Name : Larry, Salary: 30000

FIle I/O

This section is about some basics on reading and writing data, in Python native style

Write data to a file

In [26]:
f = open("tmp1.csv", "w") # f is a file handler, while "w" is the mode (w for write)
for item in range(6):
    f.write(str(item) + "\n") 
f.close() # close the filer handler for security reasons.

Note that without the typecasting from int to str, an error will be raised. A more commonly used way:

In [27]:
with open("tmp2.csv", "w") as f:
    for item in range(4):
        f.write(str(item))
        f.write("\n") 
# no need to close file, when out of 'with' scope the file closes automatically

Occasionally, we need to append new elements instead of overwriting existing files. In this case, we should use a mode in our open function:

In [28]:
with open("tmp2.csv", "a") as f: # 'a' == append to end of file
    for item in range(15, 19):
        f.write(str(item)+"\n")

Read data to a file

To read a text file into Python, we use r mode (for read)

In [29]:
f = open("tmp1.csv", "r") # this time, use read mode
contents = [item.strip("\n") for item in f] # list comprehension. This is the same as for-loop but more concise + stripping newline
print(contents)
f.close()
['0', '1', '2', '3', '4', '5']

Also using with:

In [30]:
with open("tmp2.csv", "r") as f:
  print(f.readlines())
['0\n', '1\n', '2\n', '3\n', '15\n', '16\n', '17\n', '18\n']
In [31]:
# delete the files...
import os
os.remove("tmp1.csv")
os.remove("tmp2.csv")

Packages

Often times, we need either internal or external help for complicated computation tasks. In these occasions, we need to import packages.

Built-in packages

Python provides many built-in packages to prevent extra work on some common and useful functions

We will use math as an example.

In [32]:
import math # use import to load a library

To use functions from the library, do: library_name.function_name. For example, when we want to calculate the logarithm using a function from math library, we can do math.log

In [33]:
x = 3
print("e^x = e^3 = %f"%math.exp(x))
print("log(x) = log(3) = %f"%math.log(x))
e^x = e^3 = 20.085537
log(x) = log(3) = 1.098612

You can also import one specific function:

In [34]:
from math import exp # You can import a specific function
print(exp(x)) # This way, you don't need to use math.exp but just exp
20.085536923187668

Or all:

In [35]:
from math import * # Import all functions - not recommended do to overriding of functions
In [36]:
print(exp(x))
print(log(x)) # Before importing math, calling `exp` or `log` will raise errors
20.085536923187668
1.0986122886681098

You can import a package with a shortened name:

In [37]:
import math as m
m.exp(3)
Out[37]:
20.085536923187668

Depending on what you want to achieve, you may want to choose between importing a few or all (by *) functions within a package.

External Packages

There are times you'll want some advanced utility functions not provided by Python. There are many useful packages by developers.

We'll use numpy as an example. (numpy, scipy, matplotlib,and probably pandas will be of the most importance to you for data analyses.

Installation of packages for Python is the easiest using pip:

~$ pip install numpy

We'll see use for external packages later on.

In [ ]: