PYTHON
PYTHON
MAD LIBS
color = input("Enter a color: ")
plural_noun = input("Enter a plural noun: ")
celebrity = input("Enter a celebrity: ")
LIST
friends = ["kiki", "lala", "hehe", "loli"]
print(friends[2])
->Python will print “hehe”
LIST FUNCTIONS
EMERGE
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "loli"]
friends.extend(lucky_numbers)
print(friends)
->Python will print ['kiki', 'lala', 'hehe', 'loli', 2, 3, 4,
5, 6, 7, 8]
THÊM VÀO 1 PHẦN TỬ
friends = ["kiki", "lala", "hehe", "loli"]
friends.append("lulu")
print(friends)
->Python will print ['kiki', 'lala', 'hehe', 'loli', 'lulu']
THẾ MỘT PHẦN TỬ VÀO MỘT VỊ TRÍ CỤ THỂ
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "loli"]
friends.insert(1, "kelly")
print(friends)
->['kiki', 'kelly', 'lala', 'hehe', 'loli']
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "loli"]
friends.remove("hehe")
print(friends)
->['kiki', 'lala', 'loli']
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "loli"]
friends.clear()
print(friends)
->[]
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "loli"]
friends.pop()
print(friends)
->['kiki', 'lala', 'hehe']
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "loli"]
print(friends.index("lala"))
->1
lucky_numbers = [2, 3, 4, 5, 6, 7, 8]
friends = ["kiki", "lala", "hehe", "hehe", "loli"]
print(friends.count("hehe"))
->2
friends2 = friends.copy()
print(friends2)
->['kiki', 'lala', 'hehe', 'hehe', 'loli']
RETURN STATEMENT
def cube(num):
return num*num*num
result = cube(3)
print(result)
->27
IF STATEMENT
is_male = False
is_tall = False
if is_male or is_tall:
print("You are a male or tall or both")
else:
print("You are neither male nor tall")
->You are neither male nor tall
is_male = True
is_tall = False
is_male = False
is_tall = True
== bằng
!= khác
TÌM SỐ KHÁC NUM2
def equal_num(num1, num2, num3):
if num1 != num2:
return num1
else:
return num3
print(equal_num(4, 4, 6))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
BUILDING A BETTER CALCULATOR
num1 = float(input("Enter the first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter the second number: "))
if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "/":
print(num1 / num2)
elif op == "*":
print(num1 * num2)
else:
print("Invalid result")
OR
num1 = input("Enter the first number: ")
op = input("Enter operator: ")
num2 = input("Enter the second number: ")
if op == "+":
print(float(num1) + float(num2))
elif op == "-":
print(float(num1) - float(num2))
elif op == "/":
print(float(num1) / float(num2))
elif op == "*":
print(float(num1) * float(num2))
else:
print("Invalid result")
DICTIONARIES
monthConversions = {
"Jan": "January",
"Feb": "February",
"Mar": "March",
"Apr": "April",
"Ma": "May",
"Jun": "June",
"Jul": "July",
"Aug": "August",
"Sep": "September",
"Oct": "October",
"Nov": "November",
"Dec": "December",
}
print(monthConversions["Nov"]
->Python will print november
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
monthConversions = {
"Jan": "January",
"Feb": "February",
"Mar": "March",
"Apr": "April",
"Ma": "May",
"Jun": "June",
"Jul": "July",
"Aug": "August",
"Sep": "September",
"Oct": "October",
"Nov": "November",
"Dec": "December",
}
print(monthConversions.get("Jul"))
->July
*********************************************************
*********
WHILE LOOP
i = 1
while i <= 10:
print(i)
i += 1
->1
2
3
4
5
6
7
8
9
10
Done with loop
i = 10
while i >= 0:
print(i)
i -= 1
print("You win!")
->
Enter guess: hh
Enter guess: jj
Enter guess: mixhunk
You win!
secret_word = "mixhunk"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != secret_word and
not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count +=1
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, YOU LOSE")
else:
print("You win!")
if out_of_guesses == True:
print("You lose")
else:
print("You win")
FOR LOOP
for letter in "Hello":
print(letter)
->
H
e
l
l
o
EXPONENT FUNCTION
def raise_to_power(base_num, pow_num):
result = 1
for index in range(pow_num):
result = result * base_num
return result
print(raise_to_power(3, 4))
-> 81
2D loops
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
print(number_grid[0])
-> [1, 2, 3]
print(number_grid[0][2])
->3
NESTED LOOPS
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
for row in number_grid:
for col in row:
print(col)
->1
2
3
4
5
6
7
8
9
0
TRANSLATE A PHRASE
def translate(phrase):
translation = ""
for letter in phrase:
if letter in "AEIOUaeiou":
translation = translation + "g"
else:
translation = translation + letter
return translation
print(translate(input("Enter a phrase: ")))
TRY EXCEPT
try:
number = int(input("Enter a number: "))
print(number)
except:
print("Invalid number")
try:
hihi = 2/0
except ZeroDivisionError as err:
print(err)
-> division by zero
try:
hihi = 26/11
number = int(input("Enter a number: "))
print(number)
except ZeroDivisionError:
print("Divided by zero")
except ValueError:
print("I know hehee")
READING FILES
You have to create a file
jjjj
kkkkkk