PYTHON
Tutorials
- W3C
- MIT 6.100L Introduction to CS and Programming using Python, Fall 2022
- Special method names
- Jupyter_notebook_practice
ENV setup/install
data types
category | type |
---|---|
Text Type | str |
Numeric Types | int , float , complex |
Sequence Types | list , tuple , range |
Mapping Type | dict |
Set Types | set , frozenset |
Boolean Type | bool |
Binary Types | bytes , bytearray , memoryview |
None Type | NoneType |
example
Example Data | Type | cast |
---|---|---|
"Hello World" |
str |
str("Hello World") |
20 |
int |
int(20) |
20.5 |
float |
float(20.5) |
1j |
complex |
complex(1j) |
["apple", "banana", "cherry"] |
list |
list(("apple", "banana", "cherry")) |
("apple", "banana", "cherry") |
tuple |
tuple(("apple", "banana", "cherry")) |
range(6) |
range |
range(6) |
{"name" : "John", "age" : 36} |
dict |
dict(name="John", age=36) |
{"apple", "banana", "cherry"} |
set |
set(("apple", "banana", "cherry")) |
frozenset({"apple", "banana", "cherry"}) |
frozenset |
frozenset(("apple", "banana", "cherry")) |
True |
bool |
bool(5) |
b"Hello" |
bytes |
bytes(5) |
bytearray(5) |
bytearray |
bytearray(5) |
memoryview(bytes(5)) |
memoryview |
memoryview(bytes(5)) |
None |
NoneType |
variables
import random
print(random.randrange(1, 10))
# Many Values to Multiple Variables
x, y, z = "Orange", "Banana", "Cherry"
# One Value to Multiple Variables
x = y = z = "Orange"
# Unpack a Collection
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
# Unpack a tuple
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
string
# Multiline Strings
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
# Strings are Arrays
a = "Hello, World!"
print(a[1])
# Looping Through a String
for x in "banana":
print(x)
print(len(a))
txt = "The best things in life are free!"
print("free" in txt)
print("expensive" not in txt)
# slicing
b = "Hello, World!"
print(b[2:5])
# slicing from the start
print(b[:5])
# slicing to the end
print(b[2:])
# Use negative indexes to start the slice from the end of the string
print(b[-5:-2])
print(b.upper())
print(b.lower())
a = " Hello, World! "
print(a.strip())
# returns "Hello, World!"
print(a.replace("H", "J"))
print(a.split(","))
# returns ['Hello', ' World!']
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
myorder2 = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
# The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
string methods
Method | Description |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isascii() | Returns True if all characters in the string are ascii characters |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
islower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Converts the elements of an iterable into a string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
List
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
thislist.insert(2, "watermelon")
thislist.append("orange")
tropical = ["mango", "pineapple", "papaya"]
# The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.)
thislist.extend(tropical)
thislist.remove("banana")
thislist.pop(1)
del thislist[0]
# Delete the entire list
del thislist
# empties the list
thislist.clear()
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
# comprehension syntax: newlist = [expression for item in iterable if condition == True]
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
# this above is same as the following:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
thislist.sort(reverse = True)
def myfunc(n):
return abs(n - 50)
thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
List methods
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Tuple
tuple1 = ("abc", 34, True, 40, "male")
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
# Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
(green, *tropic, red) = fruits
Tuple methods
Method | Description |
---|---|
count() | Returns the number of times a specified value occurs in a tuple |
index() | Searches the tuple for a specified value and returns the position of where it was found |
Sets
thisset = {"apple", "banana", "cherry"}
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
thisset.add("orange")
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
# If the item to remove does not exist, remove() will raise an error.
thisset.remove("banana")
# If the item to remove does not exist, discard() will NOT raise an error.
thisset.discard("banana")
x = thisset.pop()
thisset.clear()
del thisset
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
# Both union() and update() will exclude any duplicate items.
set3 = set1.union(set2)
set1.update(set2)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# Keep ONLY the Duplicates
x.intersection_update(y)
z = x.intersection(y)
# Keep All, But NOT the Duplicates
x.symmetric_difference_update(y)
z = x.symmetric_difference(y)
Set Methods
Method | Description |
---|---|
add() | Adds an element to the set |
clear() | Removes all the elements from the set |
copy() | Returns a copy of the set |
difference() | Returns a set containing the difference between two or more sets |
difference_update() | Removes the items in this set that are also included in another, specified set |
discard() | Remove the specified item |
intersection() | Returns a set, that is the intersection of two other sets |
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | Returns whether two sets have a intersection or not |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
pop() | Removes an element from the set |
remove() | Removes the specified element |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | Return a set containing the union of sets |
update() | Update the set with the union of this set and others |
Dictionaries
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
# As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
print(len(thisdict))
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
x = thisdict.get("model")
x = thisdict.keys()
# The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] = "white"
print(x) #after the change
# The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.
x = thisdict.values()
# The returned list is a view of the items of the dictionary, meaning that any changes done to the dictionary will be reflected in the items list.
x = thisdict.items()
thisdict.update({"year": 2020})
thisdict["color"] = "red"
thisdict.pop("model")
# the `popitem()` method removes the last inserted item (in versions before 3.7, a random item is removed instead):
thisdict.popitem()
del thisdict["model"]
thisdict.clear()
del thisdict
for x in thisdict.keys():
print(x)
for x, y in thisdict.items():
print(x, y)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
mydict = dict(thisdict)
Dictionary Methods
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key value pair |
keys() | Returns a list containing the dictionary’s keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |
#
dict.get(key, default=None)
# delete key
del(dict[key])
# transform dict to list
list(dict.items())
Arrays
- List is a collection which is ordered and changeable. Allows duplicate members.
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
- Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
- Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
- Dictionary is a collection which is ordered** and changeable. No duplicate members.
Array methods
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
if
elif
else
a = 33
b = 200
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
# if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
if b > a:
pass
# Ternary Operators
print("A") if a > b else print("=") if a == b else print("B")
# With the break statement we can stop the loop even if the while condition is true:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
# With the continue statement we can stop the current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
# With the else statement we can run a block of code once when the condition no longer is true:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
# The range() function returns a sequence of numbers, starting from 0 by default,
# and increments by 1 (by default), and ends at a specified number.
# range(6) is not the values of 0 to 6, but the values 0 to 5.
for x in range(6):
print(x)
# If the number of arguments is unknown, add a * before the parameter name:
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
# If the number of keyword arguments is unknown, add a double ** before the parameter name:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
x = "hello"
if not type(x) is int:
raise TypeError("Only integers are allowed")
# user input
# python 3.6
username = input("Enter username:")
print("Username is: " + username)
# python 2.7
username = raw_input("Enter username:")
print("Username is: " + username)
lambda - A lambda function is a small anonymous function lambda arguments : expression
x = lambda a : a + 10
print(x(5))
x = lambda a, b : a * b
print(x(5, 6))
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Iterators
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)
classes
class Coordinate(object):
'''special method to create an instance
`self` allows you to create variables that belong to this object
`self` parameter to refer to an instance of the class without having created one yet
without `self`, you are just createing regular variables!
'''
def __init__(self, xval, yval):
self.x = xval
self.y = yval
''' Returns euclidean dist between to Coord obj '''
def distance(self, other):
x_diff_sq = (self.x - other.x)**2
y_diff_sq = (self.y - other.y)**2
return (x_diff_sq + y_diff_sq)**0.5
def __str__(self):
return "<" + str(self.x) + "," + str(self.y) + ">"
def reset(self):
self.x = 0
self.y = 0
origin = Coordinate(0, 0)
point = Coordinate(1, 1)
print(origin.x)
print(orgin.distance(point))
print(origin)
# <0, 0>
print(Coordinate)
# <class __main__.Coordinate>
print(type(Coordinate))
# <type 'type'>
print(isinstance(point, Coordinate))
# True
class Circle(object):
def __init__(self, center, radius):
if type(center) != Coordinate:
raise ValueError
if type(radius) != int:
raise ValueError
self.center = center
self.radius = radius
def is_inside(self, point):
if type(point) != Coordinate:
raise ValueError
return point.distance(self.center) < self.radius
my_circle = Circle(origin, 2)
print(my_circle.is_inside(point))
class SimpleFraction(object):
def __init__(self, n, d):
self.num = n
self.denom = d
def times(self, other):
num = self.num * other.num
denom = self.denom * other.denom
return num/denom
def plus(self, other):
num = self.num * other.denom + self.denom * other.num
denom = self.denom * other.denom
return num / denom
def inverse(self):
""" Returns a float representing 1/self """
return self.denom/self.num
# pass
def invert(self):
""" Returns self's num to denom and vice versa. returns None. """
(self.num, self.denom) = (self.denom, self.num)
# pass
def __str__(self):
if self.denom == 1:
return str(self.num)
else:
return str(self.num) + "/" + str(self.denom)
def __mul__(self, other):
num = self.num * other.num
denom = self.denom * other.denom
return SimpleFraction(num, denom)
def __float__(self):
return self.num/self.denom
def reduce(self):
def gcd(n, d):
while d != 0:
(d,n) = (n%d, d)
return n
if self.denom == 0:
return None
elif self.denom == 1:
# return self.num
return SimpleFraction(self.num, 1)
else:
greatest_common_divisor = gcd(self.num, self.denom)
num = int(self.num/greatest_common_divisor)
denom = int(self.denom/greatest_common_divisor)
return SimpleFraction(num, denom)
f1 = SimpleFraction(3, 4)
f2 = SimpleFraction(1, 4)
print(f1.num)
print(f1.denom)
print(f1.plus(f2))
print(f1.times(f2))
# the following 3 lines are equivalent
print(f1 * f2)
print(f1.__mul__(f2))
print(SimpleFraction.__mul__(f1, f2))
class Animal(object):
def __init__(self, age):
self.age = age
self.name = None
def __str__(self):
return "animal:" + str(self.name) + ":" + str(self.age)
def get_age(self):
return self.age
def get_name(self):
return self.name
def set_name(self, newname = ""):
self.name = newname
def set_age(self, newage):
self.age = newage
class Cat(Animal):
def speak(self):
print("meow")
def __str__(self):
return "cat:" + str(self.name)+ ":"+str(self.age)
class Person(Animal):
def __init__(self, name, age):
Animal.__init__(self, age)
self.set_name(name)
self.friends = []
def get_friends(self):
return self.friends.copy()
def add_friend(self, fname):
if fname not in self.friends:
self.friends.append(fname)
def speak(self):
print("Hello")
def age_diff(self, other):
diff = self.age - other.age
print(abs(diff), "year difference")
def __str__(self):
return "person:"+str(self.name)+":"+str(self.age)
import random
class Student(Person):
def __init__(self, name, age, major = None):
Person.__init__(self, name, age)
self.major = major
def change_major(slef, major):
self.major = major
def speak(self):
r = random.random()
if r < 0.25:
print("i have homework")
elif 0.25 <= r < 0.5:
print("i need sleep")
elif 0.5 <= 4 < 0.75:
print('i should eat')
else:
print("i'm still zooming")
def __str__(self):
return "student:"+str(self.name)+":"+str(self.age)+":"+str(self.major)
class SimpleWorkout(object):
""" A simple class to keep track of workouts """
def __init__(self, start, end, calories):
self.start = start
self.end = end
self.calories = calories
self.icon = '/poker face'
self.kind = 'Workout'
def get_calories(self):
return self.calories
def get_start(self):
return self.start
def get_end(self):
return self.end
def set_calories(self, calories):
self.calories = calories
def set_start(self, start):
self.start = start
def set_end(self, end):
self.end = end
# inspect internal state of classes
print(SimpleWorkout.__dict__.keys())
'''
dict_keys(['__module__', '__doc__', '__init__', 'get_calories', 'get_start',
'get_end', 'set_calories', 'set_start', 'set_end', '__dict__', '__weakref__'])
'''
pritn(SimpleWorkout.__dict__.values())
my_workout = SimpleWorkout('9/30/2021 1:35PM', '9/30/2021 1:57 PM', 200)
from dateutil import parser
start = '9/30/2021 1:35 PM'
end = '9/30/2021 1:45 PM'
start_date = parser.parse(start)
end_date = parser.parse(end)
print(type(start_date))
# <class 'datetime.datetime'>
print((end_date - start_date).total_seconds())
# 600.0
class Workout:
cal_per_hr = 200
def __init__(self, start, end, calories = None):
self.start = parser.parse(start)
self.end = parser.parse(end)
self.calories = calories
self.icon = '/poker face'
self.kind = 'Workout'
def get_start(self):
return self.start
def set_start(self, val):
self.start = parser.parse(val)
def get_end(self):
return self.end
def set_end(self, val):
self.end = parser.parse(val)
def get_kind(self):
return self.kind
def set_kind(self, val):
self.kind = val
def set_calories(self, val):
self.calories = val
def get_calories(self):
if (self.calories == None):
return Workout.cal_per_hr * (self.end - self.start).total_seconds() /3600
else:
return self.calories
def get_duration(self):
return self.end - self.start
def __eq__(self, other):
return type(self) == type(other) and \
self.start == other.start and \
self.end == other.end and \
self.kind == other.kind and \
self.get_calories() == other.get_calories()
def __str__(self):
width = 16
retstr = f"|{'-'*width}|\n"
retstr += f"|{' '*width}|\n"
iconLen = 0
retstr += f"|{self.icon}{' '*(width-3)}|\n"
retstr += f"|{self.kind}{' '*(width - len(self.kind) - 1)}|\n"
retstr += f"|{' '*width}|\n"
duration_str = str(self.get_duration())
retstr += f"|{duration_str}{' '*(width - len(duration_str) -1)}|\n"
cal_str = f"{self.get_calories():.0f}"
retstr += f"|{cal_str} Calories {' '*(width - len(cal_str)-11)}|\n"
retstr += f"|{' '*width}|\n"
retstr += f"|{'_'*width}|\n"
return retstr
w_one = Workout('9/30/2021 1:35 PM', '9/30/2021 1:45 PM', 300)
print(Workout)
# <class '__main__.Workout'>
print(w_one)
#|----------------|
#| |
#|/poker face |
#|Workout |
#| |
#|0:10:00 |
#|300 Calories |
#| |
#|________________|
from math import sin, cos, sqrt, atan2, radians
def gpsDistance(p1, p2):
""" Given tow points, respresented as (lat,lng) tuples, return
distance between them in kilometers"""
R = 6373.0
lat1 = radians(p1[0])
lon1 = radians(p1[1])
lat2 = radians(p2[0])
lon2 = radians(p2[1])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlon/2)**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
return R*c
class RunWorkout(Workout):
cals_per_km = 100
def __init__(self, start, end, elev = 0, calories = None)
# Workout.__init__(start, end, calories)
super().__init__(start, end, calories)
self.icon = '/run face'
self.kind = 'Running'
self.elev = elev
def get_elev(self):
return self.elev
def set_elev(self, e):
self.elev = e
def get_calories(self):
if (self.route_gps_points != None):
dist = 0
lastP = self.routeGpsPoints[0]
for p in self.routeGpsPoints[1:]:
dist += gpsDistance(lastP, p)
lastP = p
return dist * RunWorkout.cals_per_km
else:
return super().get_calories()
def __eq__(self, other):
return super().__eq__(other) and self.elev == other.elev
def celsius_to_fahrenheit(c):
counter = 3
return (counter, c*9.0/5 + 32)
def mysum(x):
counter = 1
total = 0
for i in range(x + 1):
counter += 3
total += 1
return (counter, total)
def square(n):
counter = 1
mysum = 0
for i in range(n):
counter += 1
for j in range(n):
counter += 3
mysum += 1
return (counter, mysum)
def count_wrapper(f, L):
""" helper function to show number of operations """
print('Counting', f.__name__)
for i in L:
counter = f(i)[0]
if i == min(L):
multiplier = 1.0
else:
multiplier = counter/float(prev)
prev = counter
print(f"{f.__name__}({i}): {counter} ops, {multiplier} x more")
L1 = [100]
for i in range(5):
L1.append(L1[-1]* 10)
L2_a = [128, 256, 512, 1024, 2048, 4096, 8192]
L2_b = [1, 10, 100, 1000, 10000]
count_wrapper(celsius_to_fahrenheit, L1)
count_wrapper(mysum, L1)
count_wrapper(square, L2_a)
count_wrapper(square, L2_b)
import time
def convert_to_km(m):
return m * 1.609
def compound(invest, interest, n_months):
total = 0
for i in range(n_months):
total = total * interest + invest
return total
# start clock
t0 = time.perf_counter()
# call function
convert_to_km(100000)
# stop clock
dt = time.perf_counter() - t0
print("t =", dt, "s,")
L_N = [1]
for i in range(7):
L_N.append(L_N[-1]* 10)
for N in L_N:
t = time.perf_counter()
km = convert_to_km(N)
dt = time.perf_counter() - t
print(f"convert_to_km({N}) took {dt:.2e} seconds ({1/dt:,.2f}/sec)")
for N in L_N:
t = time.perf_counter()
km = compound(10.0, 1.05, N)
dt = time.perf_counter() - t
print(f"compound({N}) took {dt:.2e} seconds ({1/dt:,.2f}/sec)")
import time
from math import sqrt, sin, cos
def is_in(L, x):
for elt in L:
if elt == x:
return True
return False
def binary_search(L, x):
""" search by bisecting the sorted list"""
low = 0
high = len(L)
while high - low > 1:
mid = (high + low) // 2 # interger division, round down
if L[mid] <= x:
low = mid
else:
high = mid
return L[low] == x
L_N = [1]
for i in range(8):
L_N.append(L_N[-1]* 10)
for N in L_N:
t = time.perf_counter()
km = binary_search(L_N, 10)
dt = time.perf_counter() - t
print(f"compound({N}) took {dt:.2e} seconds ({1/dt:,.2f}/sec)")
import time
from math import sqrt, sin, cos
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = sqrt((p1[0] - p2[0])**2 + (p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
def create_list_of_2D_points(N):
L = []
for i in range(N):
x = cos(i)*i
y = sin(i)*i
L.append((x, y))
return L
L_N = [100, 200, 400, 800, 1600, 3200, 6400]
diameter_times = []
last = 0
for N in L_N:
L = create_list_of_2D_points(N)
t = time.perf_counter()
d = diameter(L)
dt = time.perf_counter() - t
diameter_times.append(dt)
print("diameter of", N, "points took", dt, "seconds")
if (last != 0):
print(f" delat = {dt/last:.1f}x for 2x as many points")
last = dt
def fact_recur(n):
if (n < 1):
return 1
else:
return n*fact_recur(n-1)
def fib_iter(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib_recur(n-1) + fib_recur(n-2)
# fib_i = 0
# fib_ii = 1
# for i in range(n -1):
# tmp = fib_i
# fib_i = fib_ii
# fib_ii = tmp + fib_ii
# return fib_ii
def gen_subsets(L):
if len(L) == 0:
return [[]]
extra = L[-1:]
smaller = gen_subsets(L[:-1])
new = []
for small in smaller:
new.append(small + extra)
return smaller + new
def digit_add(n):
"""" assume n an int >= 0 """
answer = 0
s = str(n)
for c in s[::-1]:
answer += int(c)
return answer
print(digit_add(1234))
def bisect_serach1(L, e):
if L == []:
return False
elif len(L) == 1:
return L[0] == e
else:
half = len(L) // 2
if L[half] > e:
return bisect_serach1(L[:half], e)
else:
return bisect_serach1(L[half:], e)
def bisect_search2(L, e):
def bisect_search_helper(L, e, low, high):
if high == low:
return L[low] == e
mid = (low + high) // 2
if L[mid] == e:
return True
elif L[mid] > e:
if low == mid: # nothing left to search
return False
else:
return bisect_search_helper(L, e, low, mid - 1)
else:
return bisect_search_helper(L, e, mid + 1, high)
if len(L) == 0:
return False
else:
return bisect_search_helper(L, e, 0, len(L) - 1)
import time
import random
def bogo_sort(L):
count = 0
while not is_sorted(L):
random.shuffle(L)
count += 1
return count
def is_sorted(L):
i = 0
j = len(L)
while i + 1 < j:
if L[i] > L[i + 1]:
return False
i += 1
return True
L = []
for i in range(0, 9):
L.append(random.randint(1, 100))
print("--- BOGO SORT ---")
print('L: ', L)
t0 = time.time()
count = bogo_sort(L)
print('Sorted L:', L)
print(count, "shuffles and sorting took: ", time.time() - t0, 's')
def bubble_sort(L, detail = False):
did_swap = True
while did_swap:
did_swap = False
for j in range(1, len(L)):
if L[j-1] > L[j]:
did_swap = True
L[j],L[j-1] = L[j-1],L[j]
if detail:
print(L)
print()
print("------ BUBBLE SORT -----")
L = [8, 4, 1, 6, 5, 11, 2, 0]
print('L: ', L)
bubble_sort(L, True)
print('Sorted L:', L)
def selection_sort(L):
count = 0
for i in range(len(L)):
for j in range(i, len(L)):
if L[j] < L[i]:
L[i],L[j] = L[j],L[i]
count += 1
return count
print("------ SELECTION SORT -----")
L = [8, 4, 1, 6, 5, 11, 2, 0]
print('L: ', L)
c = selection_sort(L)
print('Sorted L:', L, 'count:', c)
def merge(left, right):
result = []
i,j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
j += 1
while (i < len(left)):
result.append(left[i])
i += 1
while (j < len(right)):
result.append(right[j])
j += 1
return result
def merge_sort(L):
if len(L) < 2:
return L[:]
else:
middle = len(L) // 2
left = merge_sort(L[:middle])
right = merge_sort(L[middle:])
return merge(left, right)
print("------ MERGE SORT -----")
L = [8, 4, 1, 6, 5, 11, 2, 0]
print('L: ', L)
c = merge_sort(L)
print('Sorted L:', c)
import matplotlib.pyplot as plt
nVals = []
linear = []
quadratic = []
cubic = []
exponential = []
for n in range(0, 30):
nVals.append(n)
linear.append(n)
quadratic.append(n**2)
cubic.append(n**3)
exponential.append(1.5**n)
plt.figure('lin')
plt.plot(nVals, linear)
plt.figure('quad')
plt.plot(nVals, quadratic)
plt.figure('cube')
plt.plot(nVals, cubic)
plt.figure('expo')
plt.plot(nVals, exponential)
months = range(1, 13, 1)
boston = [28, 32, 39, 48, 59, 68, 75, 73, 66, 54, 45, 34]
phoenix = [55, 57, 61, 68, 77, 86, 91, 90, 84, 73, 61, 54]
msp = [16, 19, 34, 48, 59, 70, 75, 73, 64, 60, 37, 21]
plt.plot(months, boston, color='b', linestyle='-', linewidth=2, marker='.', label='Boston')
plt.plot(months, phoenix, 'or--', label='Phoenix')
plt.plot(months, msp, '*g-.', label='Minneaplois')
plt.title('Ave. Temperature in Boston')
plt.xlabel('Month')
plt.ylabel('Degrees f')
plt.xlim(1, 12)
plt.xticks((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), ('Jan', 'Feb', 'Mar',
'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
plt.legend(loc='best', fontsize=12)
import matplotlib.pyplot as plt
def getUSPop(fileName):
inFile = open(fileName, 'r')
dates, pops = [], []
for l in inFile:
line = ''
for c in l:
if c in '0123456789 ':
line += c
line = line.split(' ')
dates.append(int(line[0]))
pops.append(int(line[1]))
return dates, pops
dates, pops = getUSPop('lec25_USPopulation.txt')
plt.plot(dates, pops)
plt.title('Population in What is Now U.S.\n' +\
'(Native Am. Excluded Before 1860)')
plt.xlabel('Year')
plt.ylabel('Population')
plt.semilogy()
def getCountryPops(fileName):
inFile = open(fileName, 'r')
pops = []
for l in inFile:
line = l.split('\t')
l = line[2]
pop = ''
for c in l:
if c in '0123456789':
pop += c
pops.append(int(pop))
return pops
pops = getCountryPops('lec25_countryPops.txt')
plt.plot(pops)
plt.title('Population in What is Now U.S.\n' +\
'(Native Am. Excluded Before 1860)')
plt.xlabel('Year')
plt.ylabel('Population')
plt.semilogy()
firstDigits = []
for p in pops:
firstDigits.append(int(str(p)[0]))
# plt.plot(firstDigits)
plt.hist(firstDigits, bins = 9)
# Benford's law
œ
Special method names
Method | Description |
---|---|
__add__(self, other) |
self + other |
__sub__(self, other) |
self - other |
__mul__(self, other) |
self * other |
__truediv__(self, other) |
self / other |
__eq__(self, other) |
self == other |
__lt__(self, other) |
self < other |
__len__(self) |
len(self) |
__str__(self) |
print(self) |
__float__(self) |
float(self) i.e cast |
__pow__ |
self ** other |
Modules
import platform # built-in modules
import mymodule as mx # Create an alias for mymodule called mx
from mymodule import person1 # Import only the `person1` dictionary from the module
x = platform.system()
y = dir(platform)
print(x)
print(y)
dates
import datetime
x = datetime.datetime.now()
# 2022-06-08 23:43:54.190024
print(x.year)
# 2022
print(x.strftime("%A"))
# Wednesday
x = datetime.datetime(2020, 5, 17)
print(x)
# 2020-05-17 00:00:00
date format
Directive | Description | Example |
---|---|---|
%a | Weekday, short version | Wed |
%A | Weekday, full version | Wednesday |
%w | Weekday as a number 0-6, 0 is Sunday | 3 |
%d | Day of month 01-31 | 31 |
%b | Month name, short version | Dec |
%B | Month name, full version | December |
%m | Month as a number 01-12 | 12 |
%y | Year, short version, without century | 18 |
%Y | Year, full version | 2018 |
%H | Hour 00-23 | 17 |
%I | Hour 00-12 | 05 |
%p | AM/PM | PM |
%M | Minute 00-59 | 41 |
%S | Second 00-59 | 08 |
%f | Microsecond 000000-999999 | 548513 |
%z | UTC offset | +0100 |
%Z | Timezone | CST |
%j | Day number of year 001-366 | 365 |
%U | Week number of year, Sunday as the first day of week, 00-53 | 52 |
%W | Week number of year, Monday as the first day of week, 00-53 | 52 |
%c | Local version of date and time Mon Dec 31 17:41:00 | 2018 |
%C | Century | 20 |
%x | Local version of date | 12/31/18 |
%X | Local version of time | 17:41:00 |
%% | A % character | % |
%G | ISO 8601 year | 2018 |
%u | ISO 8601 weekday (1-7) | 1 |
%V | ISO 8601 weeknumber (01-53) | 01 |
Math methods
Method | Description |
---|---|
math.acos() | Returns the arc cosine of a number |
math.acosh() | Returns the inverse hyperbolic cosine of a number |
math.asin() | Returns the arc sine of a number |
math.asinh() | Returns the inverse hyperbolic sine of a number |
math.atan() | Returns the arc tangent of a number in radians |
math.atan2() | Returns the arc tangent of y/x in radians |
math.atanh() | Returns the inverse hyperbolic tangent of a number |
math.ceil() | Rounds a number up to the nearest integer |
math.comb() | Returns the number of ways to choose k items from n items without repetition and order |
math.copysign() | Returns a float consisting of the value of the first parameter and the sign of the second parameter |
math.cos() | Returns the cosine of a number |
math.cosh() | Returns the hyperbolic cosine of a number |
math.degrees() | Converts an angle from radians to degrees |
math.dist() | Returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point |
math.erf() | Returns the error function of a number |
math.erfc() | Returns the complementary error function of a number |
math.exp() | Returns E raised to the power of x |
math.expm1() | Returns Ex - 1 |
math.fabs() | Returns the absolute value of a number |
math.factorial() | Returns the factorial of a number |
math.floor() | Rounds a number down to the nearest integer |
math.fmod() | Returns the remainder of x/y |
math.frexp() | Returns the mantissa and the exponent, of a specified number |
math.fsum() | Returns the sum of all items in any iterable (tuples, arrays, lists, etc.) |
math.gamma() | Returns the gamma function at x |
math.gcd() | Returns the greatest common divisor of two integers |
math.hypot() | Returns the Euclidean norm |
math.isclose() | Checks whether two values are close to each other, or not |
math.isfinite() | Checks whether a number is finite or not |
math.isinf() | Checks whether a number is infinite or not |
math.isnan() | Checks whether a value is NaN (not a number) or not |
math.isqrt() | Rounds a square root number downwards to the nearest integer |
math.ldexp() | Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i |
math.lgamma() | Returns the log gamma value of x |
math.log() | Returns the natural logarithm of a number, or the logarithm of number to base |
math.log10() | Returns the base-10 logarithm of x |
math.log1p() | Returns the natural logarithm of 1+x |
math.log2() | Returns the base-2 logarithm of x |
math.perm() | Returns the number of ways to choose k items from n items with order and without repetition |
math.pow() | Returns the value of x to the power of y |
math.prod() | Returns the product of all the elements in an iterable |
math.radians() | Converts a degree value into radians |
math.remainder() | Returns the closest value that can make numerator completely divisible by the denominator |
math.sin() | Returns the sine of a number |
math.sinh() | Returns the hyperbolic sine of a number |
math.sqrt() | Returns the square root of a number |
math.tan() | Returns the tangent of a number |
math.tanh() | Returns the hyperbolic tangent of a number |
math.trunc() | Returns the truncated integer parts of a number |
JSON
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
RegEx
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
RegEx Functions
Function | Description |
---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
numpy
- Indexing NumPy elements using conditionals
numbers = np.array([-5, 4, 0, 2, 3])
positives = numbers[numbers > 0]
print(positives)
# array([4, 2, 3])
- NumPy element-wise logical operations
numbers = np.array([-5, 4, 0, 2, 3])
is_positive = numbers > 0
print(is_positive)
# array([False, True, False, True, True], dtype=bool)
- Creating NumPy Arrays from files
imported_csv = np.genfromtxt("filename.txt", delimiter=",")
- NumPy Arrays
import numpy as np
# NumPy Arrays can be created with a list
my_array = np.array([1, 2, 3, 4])
- Accessing NumPy Array elements by index
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(matrix[0, 0])
# 1
print(matrix[1,:])
# array([4, 5, 6])
- NumPy element-wise arithmetic operations
odds = np.array([1, 3, 5, 7, 9])
evens = odds + 1
print(evens)
# array([2, 4, 6, 8, 10])
array1 = np.array([1, 2, 3])
array2 = np.array([4, 3, 2])
new_array = array1 + array2
print(new_array)
# array([5, 5, 5])
- NumPy’s Mean and Axis
ring_toss = np.array([[1, 0, 0],
[0, 0, 1],
[1, 0, 1]])
np.mean(ring_toss, axis=1)
# Output: array([ 0.33333333, 0.33333333, 0.66666667])
- Conditions in Numpy.mean()
import numpy as np
a = np.array([1,2,3,4])
np.mean(a)
# Output = 2.5
np.mean(a>2)
# The array now becomes array([False, False, True, True])
# True = 1.0,False = 0.0
# Output = 0.5
# 50% of array elements are greater than 2
- NumPy Percentile Function - In statistics, a data set’s Nth percentile is the cutoff point demarcating the lower N% of samples.
d = np.array([1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8])
np.percentile(d, 25)
# Output: 3.5
np.percentile(d, 40)
# Output: 4.00
np.percentile(d, 75)
# Output: 6.5
- NumPy’s Sort Function
heights = np.array([49.7, 46.9, 62, 47.2, 47, 48.3, 48.7])
np.sort(heights)
# Output: array([ 46.9, 47. , 47.2, 48.3, 48.7, 49.7, 62])
- Normal Distribution -
np.random.normal(loc, scale, size)
import numpy as np
mu = 0 #mean
sigma = 0.1 #standard deviation
np.random.normal(mu, sigma, 1000)
- Histogram Visualization
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.hist(x_axis,bins=10,color='blue')
plt.xlabel('Age bins')
plt.ylabel('Frequency')
plt.title('Age Distribution')
plt.show()
debug
python -m pdb python-script.py
file operation
import os
f = open("demo.log", 'w')
f.write("Hello! Welcome to demofile.txt\n")
f.close()
f = open("demo.log", 'a')
f.write("This file is for testing purposes.\n")
f.write("Good Luck!\n")
f.close()
f = open('demo.log', 'rt')
print(f.read(5))
print(f.read())
print(f.readline())
for x in f:
print(x)
f.close()
if os.path.exists("demo.log"):
os.remove("demo.log")
else:
print('file not exists')
pip
# Environment Management and Introspection
pip install
pip uninstall
pip inspect
pip list
# python -m pip show [options] <package> ...
pip show
pip freeze
pip check
# requirements.txt describes your Python dependencies. You can fill this file in automatically with
pip freeze > requirements.txt
# Handling Distribution Files
pip download
pip wheel
pip hash
# Package Index information
pip search
# Managing pip itself
pip cache
pip config
pip debug
mongo
import os
import pymongo
mongo_url = os.environ.get('MONGO_URL')
mongo_client = pymongo.MongoClient(mongo_url)
print(mongo_client.list_database_names())
db = mongo_client['eecs']
table = db['users']
user = {"name": "Jean", "city": "Cosmos"}
res = table.insert_one(user)
print(res.inserted_id)
list = [{"name": "Amy", "address": "Apple st 652"},
{"name": "Hannah", "address": "Mountain 21"},
{"name": "Michael", "address": "Valley 345"},
{"name": "Sandy", "address": "Ocean blvd 2"},
{"name": "Betty", "address": "Green Grass 1"},
{"name": "Richard", "address": "Sky st 331"},
{"name": "Susan", "address": "One way 98"},
{"name": "Vicky", "address": "Yellow Garden 2"},
{"name": "Ben", "address": "Park Lane 38"},
{"name": "William", "address": "Central st 954"},
{"name": "Chuck", "address": "Main Road 989"},
{"name": "Viola", "address": "Sideway 1633"}]
res = table.insert_many(list)
print(res.inserted_ids)
for x in table.find({}, {"_id": 0, "name": 1, "address": 1}):
print(x)
print('\r\n')
cursor = table.find({"address": {"$gt": "S"}}).sort("name", -1).limit(2)
res = table.update_one({"address": "Valley 345"}, {
"$set": {"address": "Canyon 123"}})
res = table.update_many({"address": {"$regex": "^S"}},
{"$set": {"name": "Minnie"}})
res = table.delete_one({"address": "Mountain 21"})
res = table.delete_many({"address": {"$regex": "^S"}})
print(res.deleted_count)
table.drop()
mysql
import os
import mysql.connector
mydb = mysql.connector.connect(
host=os.environ.get('MYSQL_HOST'),
user=os.environ.get('MYSQL_USER'),
password=os.environ.get('MYSQL_PASSWD')
)
mycursor = mydb.cursor()
dbnames = mycursor.execute("SHOW DATABASES")
dbname = 'houses'
tablename = 'pacifics'
sql = "CREATE TABLE pacific (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), meta VARCHAR(255), address VARCHAR(255), room INT, hall INT, floor INT, area FLOAT, price INT)"
if type(dbnames) != None and dbname in dbnames:
mycursor.execute(sql)
else:
mycursor.execute("CREATE DATABASE houses")
mycursor.execute(sql)
# ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY
def testDb():
database = 'houses'
mycursor.execute('SELECT VERSION()')
data = mycursor.fetchone()
print("Database version: %s" % data)
def insertOne(record):
sql = 'INSERT INTO pacifics (name, address) VALUES (%s, %s)'
mycursor.execute(sql, record)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
print("1 record inserted, ID:", mycursor.lastrowid)
def insertMany(list):
sql = 'INSERT INTO pacifics (title, meta, address, room, hall, floor, area, price) VALUES (%s, %s, %s, %d, %d, %d, %f, %d)'
mycursor.executemany(sql, list)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
def query(sql='SELECT * FROM customers'):
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
def queryColumn(sql='SELECT name, address FROM customers'):
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
# myresult = mycursor.fetchone()
# print(myresult)
# sql = "SELECT * FROM customers ORDER BY name DESC"
def queryOrder(sql='SELECT name, address FROM customers ORDER BY name'):
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
# myresult = mycursor.fetchone()
# print(myresult)
def queryDel(sql="DELETE FROM customers WHERE address = 'Mountain 21'"):
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
# DROP TABLE IF EXISTS customers
def dropTable(sql="DROP TABLE customers"):
mycursor.execute(sql)
# SELECT * FROM customers LIMIT 5 OFFSET 2
def queryUpdate(sql="UPDATE customers SET address = %s WHERE address = %s", val=("Valley 345", "Canyon 123")):
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
# sql = "SELECT \
# users.name AS user, \
# products.name AS favorite \
# FROM users \
# INNER JOIN products ON users.fav = products.id"
selenium
- batch download files
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from urllib.parse import urlparse
url = 'https://example.com/some/resource/page'
bin_path='/path/to/bin/chromedriver'
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument("--disable-extensions")
# chrome_options.add_argument("--disable-gpu")
prefs = {
'download.default_directory': os.getenv('OS_LOG_PATH')
}
chrome_options.add_experimental_option('prefs', prefs)
# make chrome log requests
capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = { "performance": "ALL"}
browser = webdriver.Chrome(
bin_path, chrome_options=chrome_options, desired_capabilities=capabilities)
browser.get(url)
browser.implicitly_wait(2)
tags = browser.find_elements(By.CSS_SELECTOR, 'a')
downloads = []
for tag in tags:
if tag.text == 'MP3 audio':
downloads.append('https:' + tag.get_dom_attribute('href'))
print(len(downloads))
for (index, link) in enumerate(downloads):
filename = str(index) + '-' + urlparse(link, scheme='https').path.split('/')[-1]
req = requests.get(link, allow_redirects=True)
fd = open('./path/to/save/' + filename, 'wb')
fd.write(req.content)
fd.close()
print(filename, 'done')
print('over')
browser.quit()