nicks =['zhangsan','lisi','wangwu','zhaoliu'] for nick in nicks: print(nick) 得到: zhangsan lisi wangwu zhaoliu
for cat in cats: for dog in dogs for item in list_of_items
使用单数和复数的式名称可帮助判断代码段处理的是单个列表元素还是整个列表。
1.在for循坏环中执行更多的操作
在每条记录中打印一条消息。
1 2 3 4 5 6 7 8 9 10
nicks =['zhangsan','lisi','wangwu','zhaoliu']
for nick in nicks: print(nick.title()+", welcome to china") 得到: Zhangsan, welcome to china Lisi, welcome to china Wangwu, welcome to china Zhaoliu, welcome to china
执行多行代码,这里需要注意一下,接下来的代码都是需要缩进的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
nicks =['zhangsan','lisi','wangwu','zhaoliu']
for nick in nicks: print(nick.title()+", welcome to china") print("hello,python") 得到: Zhangsan, welcome to china hello,python Lisi, welcome to china hello,python Wangwu, welcome to china hello,python Zhaoliu, welcome to china hello,python
for nick in nicks: print(nick.title()+", welcome to china") print("hello,python")
print("print all message and print finish!")
得到: Zhangsan, welcome to china hello,python Lisi, welcome to china hello,python Wangwu, welcome to china hello,python Zhaoliu, welcome to china hello,python print all message and print finish!
可以看到最后一条要打印的消息只打印一次,最后一条没有缩进,因此只打印一次
####2.避免缩进错误
忘记缩进
1 2 3 4 5 6 7 8 9 10
nicks =['zhangsan','lisi','wangwu','zhaoliu']
for nick in nicks: print(nick.title()+", welcome to china")
得到: File "/Users/liuking/Documents/python/python_learn/test.py", line 22 print(nick.title()+", welcome to china") ^ IndentationError: expected an indented block
for nick in nicks: print(nick.title()+", welcome to china") print("hello,python")
print("print all message and print finish!") 得到: Zhangsan, welcome to china hello,python print all message and print finish! Lisi, welcome to china hello,python print all message and print finish! Wangwu, welcome to china hello,python print all message and print finish! Zhaoliu, welcome to china hello,python print all message and print finish!
for value in sorted(values.keys()): print(value) 得到: first second three
完
7.用户输入和while循环
1.函数input()工作原理
注意:用户输入只能从终端运行,不能直接通过sublime来运行。
os x系统从终端运行python程序:
1 2 3 4 5 6
1. liukingdeMacBook-Pro:~ liuking$ cd Desktop 2. liukingdeMacBook-Pro:Desktop liuking$ ls 3. input.py 4. python3 input.py 5. 输出得到结果 6.
首先:写一段python 文件
1 2 3 4 5 6 7 8 9
name = input("Please enter your name: ") print("Hello,"+name)
在终端中运行得到:
liukingdeMacBook-Pro:Desktop liuking$ python3 input.py Please enter your name: kobe bryant Hello,kobe bryant liukingdeMacBook-Pro:Desktop liuking$
多行输入展示:
多行展示可以用+=来追加字符串。
1 2 3 4 5 6 7 8 9 10 11 12 13
prompt = "If you tell us who you are,we can personalize the message you see." prompt += "\nWhat is your first name?" name = input(prompt)
print("\n Hello,"+name)
得到: liukingdeMacBook-Pro:Desktop liuking$ python3 input.py If you tell us who you are,we can personalize the message you see. What is your first name?zhang
Hello,zhang liukingdeMacBook-Pro:Desktop liuking$
注意以下几点:
使用int()来获取数值输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
height = input("How tall are you ,in inches? ") height = int(height)
if height >= 36: print("\n you're tall enought to ride") else: print("\nyou'll be able to ride when you're a little older.") 得到: liukingdeMacBook-Pro:Desktop liuking$ python3 input.py How tall are you ,in inches? 43
you're tall enought to ride liukingdeMacBook-Pro:Desktop liuking$
The following users have been confirmed: Three Two One
1 2
#####2.使用用户输入来填充字典
responses = {}
设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat is your name?")
response = input("Which mountain would you like to climb someday?")
# 将答案存在字典中
responses[name] = response
# 看看是否还有人要参加调查
repeat = input("would you like to let another person respond?(Y/N)")
if repeat == 'N':
polling_active = False
nicks =['zhangsan','lisi','wangwu','zhaoliu'] for nick in nicks: print(nick) 得到: zhangsan lisi wangwu zhaoliu
for cat in cats: for dog in dogs for item in list_of_items
使用单数和复数的式名称可帮助判断代码段处理的是单个列表元素还是整个列表。
1.在for循坏环中执行更多的操作
在每条记录中打印一条消息。
1 2 3 4 5 6 7 8 9 10
nicks =['zhangsan','lisi','wangwu','zhaoliu']
for nick in nicks: print(nick.title()+", welcome to china") 得到: Zhangsan, welcome to china Lisi, welcome to china Wangwu, welcome to china Zhaoliu, welcome to china
执行多行代码,这里需要注意一下,接下来的代码都是需要缩进的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
nicks =['zhangsan','lisi','wangwu','zhaoliu']
for nick in nicks: print(nick.title()+", welcome to china") print("hello,python") 得到: Zhangsan, welcome to china hello,python Lisi, welcome to china hello,python Wangwu, welcome to china hello,python Zhaoliu, welcome to china hello,python
for nick in nicks: print(nick.title()+", welcome to china") print("hello,python")
print("print all message and print finish!")
得到: Zhangsan, welcome to china hello,python Lisi, welcome to china hello,python Wangwu, welcome to china hello,python Zhaoliu, welcome to china hello,python print all message and print finish!
可以看到最后一条要打印的消息只打印一次,最后一条没有缩进,因此只打印一次
####2.避免缩进错误
忘记缩进
1 2 3 4 5 6 7 8 9 10
nicks =['zhangsan','lisi','wangwu','zhaoliu']
for nick in nicks: print(nick.title()+", welcome to china")
得到: File "/Users/liuking/Documents/python/python_learn/test.py", line 22 print(nick.title()+", welcome to china") ^ IndentationError: expected an indented block
for nick in nicks: print(nick.title()+", welcome to china") print("hello,python")
print("print all message and print finish!") 得到: Zhangsan, welcome to china hello,python print all message and print finish! Lisi, welcome to china hello,python print all message and print finish! Wangwu, welcome to china hello,python print all message and print finish! Zhaoliu, welcome to china hello,python print all message and print finish!
for value in sorted(values.keys()): print(value) 得到: first second three
完
7.用户输入和while循环
1.函数input()工作原理
注意:用户输入只能从终端运行,不能直接通过sublime来运行。
os x系统从终端运行python程序:
1 2 3 4 5 6
1. liukingdeMacBook-Pro:~ liuking$ cd Desktop 2. liukingdeMacBook-Pro:Desktop liuking$ ls 3. input.py 4. python3 input.py 5. 输出得到结果 6.
首先:写一段python 文件
1 2 3 4 5 6 7 8 9
name = input("Please enter your name: ") print("Hello,"+name)
在终端中运行得到:
liukingdeMacBook-Pro:Desktop liuking$ python3 input.py Please enter your name: kobe bryant Hello,kobe bryant liukingdeMacBook-Pro:Desktop liuking$
多行输入展示:
多行展示可以用+=来追加字符串。
1 2 3 4 5 6 7 8 9 10 11 12 13
prompt = "If you tell us who you are,we can personalize the message you see." prompt += "\nWhat is your first name?" name = input(prompt)
print("\n Hello,"+name)
得到: liukingdeMacBook-Pro:Desktop liuking$ python3 input.py If you tell us who you are,we can personalize the message you see. What is your first name?zhang
Hello,zhang liukingdeMacBook-Pro:Desktop liuking$
注意以下几点:
使用int()来获取数值输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
height = input("How tall are you ,in inches? ") height = int(height)
if height >= 36: print("\n you're tall enought to ride") else: print("\nyou'll be able to ride when you're a little older.") 得到: liukingdeMacBook-Pro:Desktop liuking$ python3 input.py How tall are you ,in inches? 43
you're tall enought to ride liukingdeMacBook-Pro:Desktop liuking$
while unconfirmed_users: current_user = unconfirmed_users.pop() print("verifying User:"+current_user) confirmed_users.append(current_user)
# 显示所有已验证用户 ```python print("\n The following users have been confirmed: ") for user in confirmed_users: print(user.title()) 得到: verifying User:three verifying User:two verifying User:one
The following users have been confirmed: Three Two One
while polling_active: # 提示输入被调查者的名字和回答 name = input("\nWhat is your name?") response = input("Which mountain would you like to climb someday?")
# 将答案存在字典中 responses[name] = response
# 看看是否还有人要参加调查 repeat = input("would you like to let another person respond?(Y/N)") if repeat == 'N': polling_active = False
# 调查结果,显示结果 ```python print("\n----Poll results-----") for name,response in responses.items(): print(name+" would like to climb "+ response+".") 在终端运行得到: