笔记分为六小节如下:
《python crash course》笔记(1):主要包括变量、字符串和列表及其操作等
《python crash course》笔记(2):包含列表操作、元组和if语句等
《python crash course》笔记(3):包含字典及其操作等
《python crash course》笔记(4):包含用户输入以及while循环
《python crash course》笔记(5):包括函数等
《python crash course》笔记(6):包括类及其操作等
1. 用户输入
1.1 input()
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
input()接受一个参数,用来向用户的显示或说明,然后用户输入内容,以string(字符串)的形式存储到message变量中,并且以 回车结束,下面看一个高级一点的例子
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")
输出为
If you tell us who you are, we can personalize the messages you see.
What is your first name? Eric
Hello, Eric!
1.2 int()
获取数值输入用Int()而非input()
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
print("\nYou're tall enough to ride!")
else:
print("\nYou'll be able to ride when you're a little older.")
输出如下
How tall are you, in inches? 71
You're tall enough to ride!
2. while循环
2.1 简单例子
current_number = 1
while current_number <= 5: # 注意加上冒号!
print(current_number)
current_number += 1
输出结果如下
1
2
3
4
5
注意使用while循环一个典型的逻辑错误就是自增表达式的位置问题,如下
current_number = 1
while current_number <= 5: # 注意加上冒号!
current_number += 1
print(current_number)
这种情况输出如下
2
3
4
5
6
2.2 让用户选择何时退出
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message='' # define a string
while message != 'quit':
message = input(prompt)
print(message)
但这样会把quit也打印出来,改善方法就是加个if语句判断
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message='' # define a string
while message != 'quit': # 注意加冒号(colon)
message = input(prompt)
if message != 'quit': # 注意加冒号
print(message)
2.3 使用标志(flag)
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True # define a flag
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
2.4 使用break
prompt = "\nPlease tell me a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
city = input(prompt)
if city == 'quit':
break # 执行break后会立即退出while循环
else:
print("I'd love to go to " + city.title() + "!")
2.5 使用break
current_number = 0
while current_number < 10:
current_number += 1
if current_number %2 == 0:
continue # 执行continue后将忽略循环中剩下的代码而开继续新的循环
print(current_number)
输出结果如下
1
3
5
7
9
2.6 使用while循环处理列表和字典
2.6.1 在列表之间移动元素
unconfirmed_users=['alice','brian','candace']
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print("Verifying user: "+current_user.title())
confirmed_users.append(current_user)
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
2.6.2 删除包含特定值的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
2.6.3 使用用户输入来填充字典
responses = {}
# Set a flag to indicate that polling is active.
polling_active = True
while polling_active:
# Prompt for the person's name and response.
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
# Store the response in the dictionary:
responses[name] = response
# Find out if anyone else is going to take the poll.
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False
# Polling is complete. Show the results.
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")