this git of Zhiya Zuo
tutorials from tutorialspoint
In the following examples, we show examples of comparison,
for loop, and
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 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
store_name = 'Walmart'
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 letter in store_name: print(letter)
W a l m a r t
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.)
range(0, 5) range(5) gives[0, 1, 2, 3, 4] range(1,9) gives: [1, 2, 3, 4, 5, 6, 7, 8]
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
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!
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]
x = 2
while x < 10: print(x) x = x + 1
2 3 4 5 6 7 8 9
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
W End at a
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)
W l m r t
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))
1 2 5 13
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)) # get only second element
(2, 2.0) (4, 6.0) (4, 6.0) 14.0
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))
(2, 2.0) (4, 6.0)
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()
Name : Jhon, Salary: 10000 Name : Bob, Salary: 20000 Name : Bob, Salary: 30000 Name : Larry, Salary: 30000
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
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
with open("tmp2.csv", "a") as f: # 'a' == append to end of file for item in range(15, 19): f.write(str(item)+"\n")
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']
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']
# 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
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:
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
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.
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.