当前位置: 首页 > news >正文

《Python编程从入门到实践》学习笔记06字典

alien_0={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])

green
5

alien_0={'color':'green','points':5}
new_points=alien_0['points']
print(f'you just earned {new_points} points!')

you just earned 5 points!

#添加键值对
alien_0={'color':'green','points':5}
print(alien_0)
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)

{‘color’: ‘green’, ‘points’: 5}
{‘color’: ‘green’, ‘points’: 5, ‘x_position’: 0, ‘y_position’: 25}

alien_0={}
alien_0['color']='green'
alien_0['points']=5
print(alien_0)

{‘color’: ‘green’, ‘points’: 5}

alien_0={'color':'green','points':5}
print(f"the alien is {alien_0['color']}")

the alien is green

alien_0['color']='yellow'
print(f"the alien is {alien_0['color']}")

the alien is yellow

alien_0={'x_position':0,'y_position':25,'speed':'medium'}
print(f"Original x-position:{alien_0['x_position']}")if alien_0['speed']=='slow':x_increment=1
elif alien_0['speed']=='medium':x_increment=2
else:x_increment=3alien_0['x_position']=alien_0['x_position']+x_increment
print(f"New x-position:{alien_0['x_position']}")

Original x-position:0
New x-position:2

#删除键值对
alien_0={'color':'green','points':5}
print(alien_0)del alien_0['points']
print(alien_0)

{‘color’: ‘green’, ‘points’: 5}
{‘color’: ‘green’}

favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}language=favourite_languages['sarah'].title()
print(f"Sarah's favourite language is {language}")

Sarah’s favourite language is C

alien_0={'color':'green','speed':'slow'}
point_value=alien_0.get('points','No points value assigned.')
print(point_value)

No points value assigned.

alien_0={'color':'green','speed':'slow'}
point_value=alien_0.get('points')
print(point_value)

None

#遍历字典
user_0={'username':'efermi','first':'enrico','last':'fermi'
}for a,b in user_0.items():print(f'\nKey:{a}')print(f'Key:{b}')

Key:username
Key:efermi

Key:first
Key:enrico

Key:last
Key:fermi

#不加item()
user_0={'username':'efermi','first':'enrico','last':'fermi'
}for a,b in user_0:print(f'\nKey:{a}')print(f'Key:{b}')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_47956\556717232.py in <module>6 }7 
----> 8 for a,b in user_0:9     print(f'\nKey:{a}')10     print(f'Key:{b}')ValueError: too many values to unpack (expected 2)
favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}for name,language in favourite_languages.items():print(f"{name.title()}'s favourite language is {language.title()}'")

Jen’s favourite language is Python’
Sarah’s favourite language is C’
Edward’s favourite language is Ruby’
Phil’s favourite language is Python’

#keys()
favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}
for name in favourite_languages.keys():print(name.title())

Jen
Sarah
Edward
Phil

favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}friends=['phil','sarah']
for name in favourite_languages.keys():print(f'{name.title()}')if name in friends:language=favourite_languages[name].title()print(f'\t{name.title()},i see you love {language}!')
Jen
SarahSarah,i see you love C!
Edward
PhilPhil,i see you love Python!
favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}
for name in sorted(favourite_languages.keys()):print(f'{name.title()},thank you for taking the poll')

Edward,thank you for taking the poll
Jen,thank you for taking the poll
Phil,thank you for taking the poll
Sarah,thank you for taking the poll

favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}
print('the following languages have been metioned:')
for language in favourite_languages.values():print(language.title())

the following languages have been metioned:
Python
C
Ruby
Python

favourite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}
print('the following languages have been metioned:')
for language in set(favourite_languages.values()):print(language.title())

the following languages have been metioned:
C
Ruby
Python

alien_0={'color':'green','points':5}
alien_1={'color':'green','points':10}
alien_2={'color':'green','points':15}aliens=[alien_0,alien_1,alien_2]
for alien in aliens:print(alien)

{‘color’: ‘green’, ‘points’: 5}
{‘color’: ‘green’, ‘points’: 10}
{‘color’: ‘green’, ‘points’: 15}

#{'color':'green','points':5,'speed':'slow'}
#{'color':'green','points':5,'speed':'slow'}
#{'color':'green','points':5,'speed':'slow'}
#{'color':'green','points':5,'speed':'slow'}
#{'color':'green','points':5,'speed':'slow'}
#...
#Total number of aliens:30
aliens=[]
for alien_number in range(30):new_alien={'color':'green','points':5,'speed':'slow'}aliens.append(new_alien)for alien in aliens[:5]:print(alien)print('...')
print(f'total number of aliens:{len(aliens)}')

{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}

total number of aliens:30

aliens=[]
for alien_number in range(30):new_alien={'color':'green','points':5,'speed':'slow'}aliens.append(new_alien)for alien in aliens[:3]:if alien['color']=='green':alien['color']='yellow'alien['speed']='medium'alien['points']=10for alien in aliens[:5]:print(alien)print('...')
print(f'total number of aliens:{len(aliens)}')

{‘color’: ‘yellow’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘yellow’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘yellow’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}

total number of aliens:30

aliens=[]
for alien_number in range(30):new_alien={'color':'green','points':5,'speed':'slow'}aliens.append(new_alien)for alien in aliens[:3]:if alien['color']=='green':alien['color']='yellow'alien['speed']='medium'alien['points']=10elif alien['color']=='yellow':alien['color']='red'alien['speed']='fast'alien['points']=15for alien in aliens[:5]:print(alien)print('...')
print(f'total number of aliens:{len(aliens)}')

{‘color’: ‘yellow’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘yellow’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘yellow’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}
{‘color’: ‘green’, ‘points’: 5, ‘speed’: ‘slow’}

total number of aliens:30

#在字典中存储列表
pizza={'crust':'thick','toppings':['mushrooms','extra cheese'],
}
print(f"you orderes a {pizza['crust']}-crut pizza with the following toppings:")for topping in pizza['toppings']:print('\t'+topping)
you orderes a thick-crut pizza with the following toppings:mushroomsextra cheese
favourite_languages={'jen':['python','ruby'],'sarah':'c','edward':['ruby','go'],'phil':['python','haskell'],
}
for name,languages in favourite_languages.items():print(f"\n{name.title()}'s favourite languages are:")for language in languages:print(f'\t{language.title()}')
Jen's favourite languages are:PythonRubySarah's favourite languages are:CEdward's favourite languages are:RubyGoPhil's favourite languages are:PythonHaskell
users={'aeinstein':{'first':'albert','last':'einstein','location':'princeton',},'mcurie':{'first':'marie','last':'curie','location':'paris'},
}for username,user_info in users.items():print(f"\nUsername:{username}")full_name=f"{user_info['first']}{user_info['last']}"location=user_info['location']print(f"\tFull name:{full_name.title()}")print(f"\tLocation:{location.title()}")
Username:aeinsteinFull name:AlberteinsteinLocation:PrincetonUsername:mcurieFull name:MariecurieLocation:Paris
http://www.lryc.cn/news/91886.html

相关文章:

  • 为什么说程序员和产品经理一定要学一学PMP
  • LearnOpenGL-高级OpenGL-9.几何着色器
  • 8.视图和用户管理
  • bootstrapvue上传文件并存储到服务器指定路径及从服务器某路径下载文件
  • Qt OpenGL(四十二)——Qt OpenGL 核心模式-GLSL(二)
  • C++基础讲解第八期(智能指针、函数模板、类模板)
  • JMeter 测试 ActiveMq
  • 2023年4月和5月随笔
  • 新Linux服务器安装Java环境[JDK、Tomcat、MySQL、Nacos、Redis、Nginx]
  • 精简总结:一文说明软件测试基础概念
  • 通过 Gorilla 入门机器学习
  • 【二叉树】298. 二叉树最长连续序列
  • Matlab论文插图绘制模板第100期—紧凑排列多子图(Tiledlayout)
  • [2.0快速体验]Apache Doris 2.0 日志分析快速体验
  • MySQL学习-数据库创建-数据库增删改查语句-事务-索引
  • 浏览器渗透攻击-渗透测试模拟环境(9)
  • MySQL数据库基础(基础命令详解)
  • 企业培训直播场景下嘉宾连线到底是如何实现的?
  • 五、JSP05 分页查询及文件上传
  • 一起看 I/O | 借助 Google Play 管理中心价格实验,优化定价策略
  • hexview 命令行操作使用说明
  • vue3+element plus,使用分页total修改成中文
  • RPC、HTTP、DSF、Dubbo,每个都眼熟,就是不知道有什么联系?
  • java.security.MessageDigest的用法
  • 3.2 分析特征间的关系
  • Numpy学习
  • IDC机房相电压与线电压的关系
  • chatgpt赋能python:Python如何设置输入的SEO
  • Spring Cloud Alibaba — Nacos 构建服务注册中心
  • 4.2 Spark SQL数据源 - 基本操作