Python Workshop: Basics II¶
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:
- Larger (or equal):
>
(>=
) - Smaller (or equal):
<
(<=
) - Equal to:
==
- Not equal to:
!=
3 == 5
72 >= 2
test_str = "test"
test_str == "test" # can also compare strings
If-Else¶
sum_ = 0
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.
Comparing strings are similar
store_name = "Walmart"
if "Wal" in store_name:
print("The store is not Walmart. It's " + store_name + ".")
else:
print("The store is Walmart.")
For loop¶
for letter in store_name:
print(letter)
range()
is a function to create integer sequences:
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.)
for index in range(len(store_name)): # length of a sequence
print("The %ith letter in store_name is: %s" % (index, store_name[index]))
List comprehensions¶
List comprehensions provides an easy way to create lists:
x = [i for i in range(10)]
print(x)
a lot of cool things can be done in one line!
x = [i + 2 for i in range(10)]
print(x)
x = [i ** 2 for i in range(10) if i % 2 == 0]
print(x)
While loop¶
x = 2
while x < 10:
print(x)
x = x + 1
Notes on break
and continue
¶
break
means get out of the loop immediately. Any code after the break
will NOT be executed.
store_name = "Walmart"
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
continue
means get to the next iteration of loop. It will break the current iteration and continue to the next.
for letter in store_name:
if letter == "a":
continue # Not printing a
else:
print(letter)
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)
print(F(2))
print(F(3))
print(F(5))
print(F(7))
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}$
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
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
optional args¶
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
print(geo_seq_optional_args(2))
print(geo_seq_optional_args(2, n=2))
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:
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
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()
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:
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:
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)
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()
Also using with
:
with open("tmp2.csv", "r") as f:
print(f.readlines())
# delete the files...
import os
os.remove("tmp1.csv")
os.remove("tmp2.csv")
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
x = 3
print("e^x = e^3 = %f" % math.exp(x))
print("log(x) = log(3) = %f" % math.log(x))
You can also import one specific function:
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
Or all:
from math import * # Import all functions - not recommended do to overriding of functions
print(exp(x))
print(log(x)) # Before importing math, calling `exp` or `log` will raise errors
You can import a package with a shortened name:
import math as m
m.exp(3)
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.