# this is a comment
```
Three single quotation marks can be used to make multi-line comments
This is a comment
print ('hello world!'')
# You can use single or double quotes throughout python
name = 'john'
authors_name = name
age = 30
print(authors_name)
# output is john
print(age)
# 30
x = 2
y = 4
print(x+y)
# output is 6
print(x-y)
# output is -2
print(x*y)
# output is 8
print(x/y)
# output is 0.5
x += 12 \n print(x)
# output is 14
x -= 11 \n print(x)
# output is 3
x *= 3 \n print(x)
# output is 9
x += y \n print(x)
# output is 13
print(x%y)
# output is 1
greeting = 'hello'
print(greeting + ' ' + name)
# output is hello john
user_fav = input(“what's your favorite ice cream?”)
print ('your favorite ice cream is ' + user_fav)
# output is your favorite ice cream is vanilla
str.lower
# makes string all lower case
str.upper
# makes string all upper case
str.title
# capitalizes every word
print(“what's your favorite ice cream?”)
# output is what's your favorite ice cream?
print('he said, “it is vanilla”')
# output is he said, “it is vanilla”
if ice_cream == 'vanilla':
...print('it is vanilla')
x == y
# equal to
x != y
# not equal to
x > y
# greater than
x < y
# less than
x >= y
# greater than or equal to
x <= y
# less than or equal to
elif ice_cream == 'chocolate':
...print('it is chocolate')
else:
...print('it is not chocolate or vanilla')
if tree_age > 100 and tree_height > 20:
...print('that is a tall, old tree')
if ice_cream == 'vanilla' or ice_cream == 'chocolate':
...print('that is a tasty ice cream flavor')
if (ice_cream == 'vanilla' or ice_cream == 'chocolate') and (ice_cream_size > 2):
...print('that is a lot of tasty ice cream')
...if ice_cream_size > 2:
......print('that is a large, vanilla ice cream')
for i in ice_cream_flavors:
...print(i)
...if i == 'vanilla':
......print('they have your favorite flavor!')
......break
...for t in ice_cream_toppings:
......print('do you want ' + i + ' ice cream with ' + t '?')
// output is "do you want vanilla ice cream with chocolate suace?", "do you want vanilla ice cream with sprinkles?", "do you want chocolate ice cream with chocolate suace?", "do you want chocolate ice cream with sprinkles?"
for x in range(2,6):
...print(x)
def play_game():
...user_input = input('Guess a number between 0 and 100')
...while user_input != 57:
......user_input = input('Wrong, guess again')
print('you got it right! It was 57!')
ice_cream_flavors = [] # this is an empty list
# this is an empty list
ice_cream_flavors = ['vanilla', 'chocolate', 'strawberry', 'banana']
ice_cream_prices = [2.00, 2.25, 2.50, 2.50]
print(ice_cream_flavors[0])
// output is 'vanilla'
print(ice_cream_flavors[2])
// output is 'strawberry'
ice_cream_flavors[2] = 'mint'
// output is 'mint'
ice_cream_flavors.append('cookie dough')
print(ice_cream_flavors)
// output is ['vanilla', 'chocolate', 'mint', 'banana', 'cookie dough']
ice_cream_prices.append(2.75)
print(ice_cream_prices)
// output is [2.00, 2.25, 2.50, 2.50, 2.75]
ice_cream_flavors.insert(2, “banana”)
// output is ['vanilla', 'chocolate', 'banana', 'mint', 'cookie dough']
ice_cream_flavors = ice_cream_flavors + ['apple', 'cherry']
// output is ['vanilla', 'chocolate', 'banana', 'mint', 'cookie dough', 'apple', 'cherry']
ice_cream_flavor_slice = ice_cream_flavors[1:3]
print(ice_cream_flavor_slice)
// output is ['chocolate', 'banana']
ice_cream_flavor_slice = ice_cream_flavors[:3]
// output is ['vanilla', 'chocolate', 'banana']
ice_cream_flavor_slice = ice_cream_flavors[4:]
// output is ['chocolate', 'apple', 'cherry']
del ice_cream_flavors[5]
// Delete by list index number
ice_cream_flavors.remove['apple']
Delete by list element value
del ice_cream_flavors[2]
// output is ['vanilla', 'chocolate', 'cookie dough']
retired_flavors = ice_cream_flavors.pop(2)
retired_flavors.append(ice_cream_flavors.pop(1))
// output is ['vanilla']
print(retired_flavors)
// output is ['cookie dough', 'chocolate']
my_favorite_ice_cream_flavors = ('vanilla', 'cookie dough')
user1 = {}
# this is an empty dictionary
user1 = {'first_name': 'john', 'last_name':'smith', 'age':30}
my_favorite_ice_creams = {1:'vanilla', 2:'cookie dough',3:'chocolate chip'}
print(user1['first_name'])
// output is john
user1['age'] = 31
del user1['age']
for each_value in user1.values():
...print(each_value)
// output is John Smith 30 76
for each_key in user1.keys():
...print(each_key)
// output is first_name last_name age height
for each_key, each value in user1.items():
...print(each_key + ':' + each_value)
// output is first_name:john last_name:picasso age:30 height:76
user1 = {'user_id': 1, 'first_name': 'john', 'last_name':'picasso', 'age':30, 'height': 76}
user2 = {'user_id': 2, 'first_name': 'taylor', 'last_name':'swift', 'age':30, 'height': 68}
user3 = {'user_id': 3, 'first_name': 'tom', 'last_name':'cruise', 'age':55, 'height': 69}
people_list = [user1, user2, user3]
people_list.append(new_user)
user1 = {'first_name': 'john', 'last_name':'picasso', 'age':30, 'height': 76, 'interests': ['coding', 'chess', 'cooking']}
people_list = {
...1: {'first_name': 'john', 'last_name':'picasso', 'age':30, 'height': 76},
...2: {'first_name': 'taylor', 'last_name':'swift', 'age':30, 'height': 68},
...3: {'first_name': 'tom', 'last_name':'cruise', 'age':55, 'height': 69},
}
print(people_list[2]['first_name'])
// output is taylor
def check_id():
...age = input('how old are you?')
...if age >=21:
......print('you can drink')
...else:
......print('you can't drink')
check_id()
def divide_numbers(x, y):
...result = x/y
...print result
...return result
def check_id(name, age):
......print(name + ', you can drink')
......print(name + ', you can't drink')
check_id(age=30, name=”john”)
// output is john, you can drink
def cash_income(salary, income_tax_rate=0.45):
...print(salary * (1 - income_tax_rate))
cash_income(salary=200000)
// output is 110000
cash_income(salary=200000, income_tax_rate=0.5)
// output is 100000
def display_user_info (first_name, last_name, **extra_info):
...print('Hello ' + first_name + ' ' + last_name)
...for key, value in extra_info.items():
......print(key + ': ' + value)
display_user_info(john,picasso,height=76, age=30)
// output is Hello john picasso height:76 age:30
def display_user_info (first_name, last_name, *age):
add_numbers(x,y,z):
...return x+y+z
multiply_numbers(x,y,z):
...return x*y*z
total = add_numbers(x,y,z) + multiply_numbers(x,y,z)
class User():
...def __init__(self, id, user_name, user_email, age):
......self.id = id
......self.user_name = user_name
......self.user_email = user_email
user1 = User(1,johnpicasso,jpicasso@gmail.com, 30)
user2 = User(2,taylorswift, tswift@gmail.com,30 )
user_age = user1.age
user1.age = 31
...def can_drink(self):
......if self.age <21:
.........print(“user can't drink”)
......else:
.........print('user can drink')
user1.can_drink()
// output is user can drink
from flask_sqlalchemy import SQLAlchemy
from .auth.auth import AuthError, requires_auth
from movie import Movie
def json(self):
...return {
......'id': self.id,
......'user_name': self. user_name,
......'user_email': self. user_email,
...}
import json
with open('my_file.txt', 'w' as f):
...json.dump(user.json(),f)
ord('a') // output is 97
// output is 97
chr(97)
// output is 'a'
len('apple')
// output is 5
varB = [False] * 10
s = 'apple'
s[0]
hash_table = [0]*126
2Darray = [[1034,2],[1035,6],[1035,6]]
hash_table = {}
for e in 2Darray:
...hash_table[e[0]] = 0
matrix_array= [[1,2,14],[1,12,3],[1,2,9]]
matrix_array[0]
// output is [1,2,14]
matrix_array[0][1]
// output is 2
class NodeLL:
...def __init__(self, data, next_node)
......self.data = data
......self.next_node = next_node
class LinkedList:
...def __init__(self):
......self.head = None
p1 = Node(“George Washington”)
p2 = Node(“John Adams”)
p3 = Node(“Thomas Jefferson”)
prez_list = LinkedList(p1)
p1.next_node = p2
p2.next_node = p3
class NodeBST:
...def __init__(self,data):
......self.l_child = None
......self.r_child = None
...def binary_insert(self, node):
......if self.data > node.data:
.........if self.l_child is None:
............self.l_child = node
.........else:
............self.l_child.binary_insert(node)
.........if self.r_child is None:
............self.r_child = node
............self.r_child.binary_insert(node)
def in_order_print(self):
...if self.l_child:
......self.l_child.in_order_print()
...print(self.data)
...if self.r_child:
......self.r_child.in_order_print()
ex_tree = NodeBST(3)
ex_tree.binary_insert(NodeBST(7))
ex_tree.binary_insert(NodeBST(1))
ex_tree.binary_insert(NodeBST(12))