Python学习日志
布尔值+str浮点型
price=1000000
good_credit = False
bad_credit = True
if good_credit:
down_payment = 0.1*price
print('you need to put down ' + str(down_payment))
elif bad_credit:
down_payment = 0.2*price
print('you need to put down ' + str(down_payment))
if函数
price = 1000000
good_credit = True
if good_credit:
down_payment = 0.1*price
else:
down_payment = 0.2*price
print(f'Down payment: ${down_payment}')
if函数和else,else
Weight = int(input("Weight: "))
unit = input("(L)bs or (K)g: ")
if unit.upper() == "L":
converted = Weight * 0.45
print(f"You are {converted} kilos")
elif unit.upper() == "K":
converted = Weight / 0.45
print(f"You are {converted} pounds")
else:
print("Please input the right unit again.")
while循环
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess = int(input("Guess: "))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
print("Sorry, you failed.")
高阶while循环,嵌套循环
i = ""
started = False
print("Input help to get help.")
while True:
i = input("> ").lower()
if i == "help":
print(''' input start - to start the car
stop - to stop the car
quit - to exit''')
elif i == "start":
if started :
print("Car is already started!")
else:
started = True
print("Car started ... Ready to go!")
elif i == "stop":
if not started:
print("Car is already stopped!")
else:
started = False
print("Car stopped.")
elif i == "quit":
break
else:
print("Sorry,I don't understand that!")
简单for循环
prices = [10,20,30]
total = 0
for price in prices:
total += price
print(f'The prices is {total}')
嵌套循环
for x in range(4):
for y in range(3):
print(f'({x},{y})')
嵌套练习cheat
numbers = [5, 2, 5, 2, 2, ]
for x_count in numbers:
print('x' * x_count)
双层嵌套循环
numbers = [5, 2, 5, 2, 2]
for x_count in numbers:
output = ''
for count in range(x_count):
output += 'x'
print(output)
列表基础
names = ['a', 'b', 'c', 'd']
names[0] = 'x'
print (names)
寻找列表中最大的数
numbers = [3, 6, 2, 8, 4, 10]
max = numbers[0]
for number in numbers:
if number > max:
max = number
print(max)
二维列表
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix [0] [1] = 20
print(matrix[0][1])
列表查重
numbers = [2, 2, 4, 6, 3, 6, 4, 1]
uniques = []
for number in numbers:
if number not in uniques:
uniques.append(number)
print(uniques)
元组
numbers =(1, 2, 3)
x, y, z = numbers
print(x* y* z )
字典:输入密匙返回相应预设值
customer = {
"name ": "Jhon Smith",
"age": 30,
"is_verified": True
}
print(customer["name"])
get方法
print(customer.get("birthdate","jAN 1 1988"))
字典基本方法
customer = {
"name ": "Jhon Smith",
"age": 30,
"is_verified": True
}
customer ["name "] = "jai"
customer ["birthdate"] = "jAN 1 1988"
print(customer["name "])
print(customer["birthdate"])
利用字典翻译,get方法添加不匹配返回值
phone = input("Phone:")
digits_mapping = {
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four"
}
output = ""
for character in phone:
output += digits_mapping.get(character,"mmp") + " "
print(output)
练习,注意get方法中默认返回值
message = input(">")
words = message.split(' ')
emojis = {
":)" : "🙂",
":(" : "😖"
}
output = ""
for word in words:
output += emojis.get(word,word) + " "
print(output)