【汇总】:https://blog.csdn.net/wistonty11/article/details/121348048
【2章:变量和简单数据类型】:https://blog.csdn.net/wistonty11/article/details/114553239
【3章:列表简介】:https://blog.csdn.net/wistonty11/article/details/114673314
【4章:列表操作】:https://blog.csdn.net/wistonty11/article/details/114684679
【5章:if语句】:https://blog.csdn.net/wistonty11/article/details/114932777
【6章:字典】https://blog.csdn.net/wistonty11/article/details/117432520
【7章:用户输入和while循环】:https://blog.csdn.net/wistonty11/article/details/117437656
【8章:函数】:https://blog.csdn.net/wistonty11/article/details/117448978
【9章:类】:https://blog.csdn.net/wistonty11/article/details/117521111
文章目录
# time:2021.06.01 儿童节快乐!!!!
# 收录在《口袋图书》系列:随时随地便于阅读
# 只节选了 重要/必要内容
# 本文为摘抄总结 望赞鼓励
【下载.ipynb】
7.1 函数input()
7.1.1 短句输入
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
Tell me something, and I will repeat it back to you: 杨鸽加油!
杨鸽加油!
- 引号里面是弹到显示器上的,而你输入的是要赋值给message
7.1.2 长句输入
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? 杨
Hello, 杨!
- 当你想弹到显示器上的话很长时,可用次方法。
- 前半句 存储在变量prompt,在第二行中**+=**在已经存储的prompt再加一段字符串
7.1.3 多个数值输入(补充)
用map( ) ,input( ) 可以实现
- map()
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。
a,b =map(int,input('输入a,b空格隔开:').split())
#此时a,b为int型
print("a = ", a,type(a))
print("b = ", b,type(b))
输入a,b空格隔开:2 4
a = 2 <class 'int'>
b = 4 <class 'int'>
- input( )
a,b =input('输入a,b空格隔开:').split()
#此时a,b为str型
print("a = ", a, type(a))
print("b = ", b, type(b))
输入a,b空格隔开:2 4
a = 2 <class 'str'>
b = 4 <class 'str'>
对比可得知:
① map 输出类型是自己定的,比如我们输入a,b时规定的是 int,input 默认为str
② 这里是以空格为分节符,遇到所有的空格都分开
- split()
用 split( ):拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)
函数接收两个参数,一个是分隔符,一个是序列:
① str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
② num – 分割次数。默认为 -1, 即分隔所有。如果参数 num 有指定值,则分隔 num+1 个子字符串
txt = "Google#Runoob#Taobao#Facebook"
# 第二个参数为 1,返回两个参数列表
x = txt.split("#", 1)
print (x)
['Google', 'Runoob#Taobao#Facebook']
-
#号是分词符
-
分割次数是num+1, 本示例是要分成两个参数列表,那么只把遇到的第一个分开 就够了。
7.2 while循环
7.2.1 满足运行条件循环
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
1
2
3
4
5
-
运行条件 : current_number ≤ \leq ≤ 5
-
开始是1, 进入循环,print后 变量为2 继续循环;
-
直到print(5)后 +1 :current_number = 6 不满足循环跳出
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. hello
hello
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. 杨鸽
杨鸽
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
-
做了把“钥匙”:active
-
当输入:message特定:quit词时,钥匙失败退出循环
-
其余时候继续循环
7.2.2 用break退出循环
break: 结束循环
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
message = input(prompt)
if message == 'quit':
break #!!!!!!!
else:
print(message)
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. hello
hello
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. 杨鸽
杨鸽
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
for循环也可以用break,也是直接退出循环。
7.2.3 用continue退出循环
continue:退出本次循环,开始下次循环
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
1
3
5
7
9
-
current_number 是 0 到10 之间(整数),进入循环
-
除2 没有余数,那么continue,结束本次循环
-
除2 有余数,那么输出这个数
-
这个实验其实做的就是输出奇数
7.3 使用while循环来处理列表和字典
7.3.1 在列表之间
unconfirmed_users = ['alice', 'brian', 'candace']
while unconfirmed_users:
print(unconfirmed_users)
unconfirmed_users.pop()
['alice', 'brian', 'candace']
['alice', 'brian']
['alice']
- pop()从末尾弹出,while循环条件:列表非空;空了就结束了
7.3.2 删除特定的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
print("\n")
while 'cat' in pets:
pets.remove('cat')
print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
- remove()函数可以去掉某个特定的值,但只能去掉第一个遇见的;
- 用while循环去,把列表中所有重复的cat都去掉
7.3.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? (yes/ no) ")
if repeat == 'no':
polling_active = False
# 调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")
What is your name? 杨鸽
Which mountain would you like to climb someday? 泰山
Would you like to let another person respond? (yes/ no) yes
--- Poll Results ---
杨鸽 would like to climb 泰山.
What is your name? 王尼玛
Which mountain would you like to climb someday? 嵩山
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
杨鸽 would like to climb 泰山.
王尼玛 would like to climb 嵩山.
-
创建了个空字典:responses = {}
-
字典的key:namel;value:response
-
字典名字[key] = value 写入字典
-
用polling_active这个变量控制是否要继续循环