Contents
13 If Statements and Comparisons
14 Building a Better Calculator
9 Tuples元组
coordinates = (4,5)
print(coordinates[0])
# 4
coordinates = (4,5)
coordinates[1] = 10
print(coordinates[0])
# error / 'tuple' object does not support item assignment
coordinates = [4,5]
coordinates[1] = 10
print(coordinates[1])
# 10
coordinates = [(4,5), (6,7), (80,34)]
10 Functions
def sayhi():
print("Hello User")
# blank
def sayhi():
print("Hello User")
sayhi()
# Hello User
def sayhi():
print("Hello User")
print('Top')
sayhi()
print("Bottom")
# Top
# Hello User
# Bottom
def say_hi(name):
print("Hello " + name)
say_hi("Mike")
say_hi("Steve")
# Hello Mike
# Hello Steve
def say_hi(name, age):
print("Hello " + name +" , you are " + age)
say_hi("Mike", "35")
say_hi("Steve", "70")
# Hello Mike , you are 35
# Hello Steve , you are 70
def say_hi(name, age):
print("Hello " + name +" , you are " + age)
say_hi("Mike", 35)
say_hi("Steve", 70)
# error can only concatenate str (not "int") to str
def say_hi(name, age):
print("Hello " + name +" , you are " + str(age))
say_hi("Mike", 35)
say_hi("Steve", 70)
# Hello Mike , you are 35
# Hello Steve , you are 70
11 Return Statement
def cube(num):
num*num*num
print(cube(3))
# None
def cube(num):
return num*num*num
print(cube(3))
# 27
def cube(num):
return num*num*num
result = cube(4)
print(result)
# 64
12 If Statements
is_male = True
if is_male:
print("You are a male")
# You are a male
is_male = False
if is_male:
print("You are a male")
# blank
is_male = True
is_tall = True
if is_male or is_tall:
print("You are a male or tall or both")
else:
print("You are neither male od tall")
# You are a male or tall or both
is_male = True
is_tall = False
if is_male or is_tall:
print("You are a male or tall or both")
else:
print("You are neither male od tall")
# You are a male or tall or both
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 od tall")
# You are neither male od tall
is_male = True
is_tall = False
if is_male and is_tall:
print("You are a tall male")
elif is_male and not(is_tall):
print("You are a short male")
elif not(is_male) and is_tall:
print("You are not a male but tall")
else:
print("You are neither male od tall")
# You are a short male
is_male = True
is_tall = True
if is_male and is_tall:
print("You are a tall male")
elif is_male and not(is_tall):
print("You are a short male")
elif not(is_male) and is_tall:
print("You are not a male but tall")
else:
print("You are neither male od tall")
# You are a tall male
is_male = False
is_tall = False
if is_male and is_tall:
print("You are a tall male")
elif is_male and not(is_tall):
print("You are a short male")
elif not(is_male) and is_tall:
print("You are not a male but tall")
else:
print("You are neither male od tall")
# You are neither male od tall
13 If Statements and Comparisons
def max_num(num1, num2, num3):
if num1 >= num2 and num >= num3:
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
print(max_num(3, 4, 5))
# 5
# ==等于 !=不等于 >= <=
14 Building a Better Calculator
num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter 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 operator")
Enter first number: 5
Enter operator: +
Enter second number: 23
28.0
num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter 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 operator")
Enter first number: 6
Enter operator: *
Enter second number: 5.23
31.380000000000003
Enter first number: 5
Enter operator: ()
Enter second number: 4
Invalid operator
15 Dictionaries
monthConversion = {
"Jan": "January",
"Feb": "February",
"Mar": "March",
"Jun": "June",
"Jul": "July",
"Aug": "August",
"Sep": "September",
"Oct": "October",
"Nov": "November",
"Dec": "December",
}
print(monthConversion["Nov"])
print(monthConversion.get("Nov"))
print(monthConversion.get("Luv"))
print(monthConversion.get("Luv", "Not a valid key"))
November
November
None
Not a valid key