Python 学习第四天
2018-01-07 09:58
573 查看
(四)字典
4.1 使用键值
在Python 中,字典是一系列键---值对。每个键都与一个值相关联,与键相关联的值可以是数字、字符串、列表乃至字典。在Python 中,字典用放在花括号{ }里的一系列键---值对表示。
alien_0 = {'color':'green','point':5} print(alien_0)
{'color': 'green', 'point': 5}
4.2 访问字典的值
alien_0 = {'color':'green','point':5} print(alien_0['color']) print(alien_0['point'])
green 5
4.3 添加键值对
字典是一种动态结构,可随时在其中添加键值对。要添加键值对,可依次指定字典名、用方括号括起来的键和相关联的值。alien_0 = {'color':'green','point':5} print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
{'color': 'green', 'point': 5} {'color': 'green', 'point': 5, 'x_position': 0, 'y_position': 25}
4.4 创建一个空字典
alien_0 = {} print(alien_0) alien_0['color'] = 'green' alien_0['points'] = 5 print(alien_0)
{} {'color': 'green', 'points': 5}
4.5 修改字典里的值
alien_0 = {'color':'green','points':5} print(alien_0) alien_0['color'] = 'red' print(alien_0)
{'color': 'green', 'points': 5} {'color': 'red', 'points': 5}
4.6 删除键值对
对于字典里不再需要的信息,可使用del语句将对对应的键值对彻底删除。alien_0 = {'color':'green','points':5}
print(alien_0)
del alien_0['color']
print(alien_0)
{'color': 'green', 'points': 5} {'points': 5}注意:删除的键值对永远消失了。
(五)用户输入和while循环
5.1 函数input()的工作原理
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python 将其存储在一个变量中,以方便使用。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: hello python hello python
5.1.1 使用int()来获取数值输入
使用函数input()时,Python 将用户输入解读为字符串,例如:
age = input("How old are you ?") print(age) print(type(age))
How old are you ?26 26 <class 'str'>
由上述程序可知,输入是数字是26,但是age返回的类型是str字符串类型。
age = input("How old are you ?") print(type(age)) age = int(age) print(type(age))
How old are you ?26 <class 'str'> <class 'int'>为了解决上述问题,使用函数int(),函数int()将数字的字符串表示转换为数值表示。
5.1.2 求模运算符
print(4 % 3) print(5 % 3) print(6 % 3) print(7 % 3)
1 2 0 1
5.2 while循环简介
5.2.1 使用while循环current_number = 1 while current_number <=5: print(current_number) current_number += 1
1 2 3 4 5
5.2.2 使用break退出循环
要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句。
promp = "\nPlease enter the name of a city you have visited:" promp += "\n(Enter 'quit' when you are finished.)" while True: city = input(promp) if city =='quit': print("quit finished") break else: print("I'd love to go to " + city.title()+ "!")
Please enter the name of a city you have visited: (Enter 'quit' when you are finished.)quit quit finished
Please enter the name of a city you have visited: (Enter 'quit' when you are finished.)xuzou I'd love to go to Xuzou!
5.2.3 在循环中使用continue
要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句。
current_number = 0 while current_number <10: current_number += 3 if current_number % 2 == 0: continue print(current_number)
3 9
# 3 -> print(3) # 6 ->continue # 9 -> print(9)
5.2.4 删除包含特定值的所有列表元素
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] ['dog', 'dog', 'goldfish', 'rabbit']
相关文章推荐
- python学习第四天 --字符编码 与格式化及其字符串切片
- Python 学习第四天
- python 学习第四天
- Python学习日记 第四天
- Python学习实践,第四天
- Python 学习第四天
- Python学习第四天
- Python快速学习第四天
- python 第四天学习笔记
- Python学习第四天
- 人生苦短,我用Python 学习笔记——第四天
- python学习之路-第四天-模块
- Python学习 第四天
- python学习——第四天
- 学习大数据第四天:最小二乘法的Python实现
- 学习python的第四天
- python基础学习——第四天(决策树)
- Codecademy网学习Python第四天
- 据廖雪峰python3教程----python学习第四天
- Python之学习总结第四天