操作列表
遍历整个列表
systems = ['andriod','linux','harmonyos']
for system in systems: #注意这个冒号
print(system.title() + "Hello System")
/*数字列表*/
number_list = list(range(2,11,2)) #最后一个2为步长
print(number_list)
/*计算与统计*/
squares = []
for value in range(1,11)
square = value ** 2 #平方
squares.append(square) #放置到末尾
print(squares)
/*列举一些简单的数学计算*/
max()
min()
sum()
/*列表解析*/
giao_giao = [education**2 for value in range(1,11)]
print(giao_giao)
使用列表的一部分
剖析一下重要人物
1、切片:
kunkun = ['basketball','dance','rap','sing','chicken']
print(kunkun[0:3])
print(kunkun[:3])
print(kunkun[3:]) #从第三个开始
print(kunkun[-3:]) #往后查三个
2、复制列表:kun_kun = kunkun[:]
小黑子们注意了,下面这种行不通
/*这是一种关联*/
kun_kun = kunkun
/*思考并运行下面代码*/
kunkun = ['basketball','dance','rap','sing','chicken']
kun_kun = kunkun[:] #分别运行这两句
#kun_kun = kunkun
kunkun.append('litchi')
kun_kun.append('little')
print(kunkun)
print(kun_kun)
注意:python对缩进很敏感,所以注意忘记缩进、不必要的缩进、循环后的缩进
If语句
1、大小写:Python==python(这个是错的)
2、检查多个条件:这里呢要用用keywordand 和 or 和 in
/*试试下面,用终端嗷*/
》》kunkun = ['basketball','dance','rap','sing','chicken']
》》year_0 = 20
》》year_1 = 21
》》year_0 >= 21 or year_1 >= 21
》》year_0 >= 21 and year_1 >= 21
》》'basketball' in kunkun
》》'girl' in kunkun
3、if的简单形式:
if condition_test:
do something
else:
do something
/*********************/
if condition_test:
do something
elif condition_test:
do something
else:
do something
4、检查特殊元素:思考一下就行
5、对于列表
power_babys = []
if power_babys :
for power_baby in pow_baby:
print("OKOK" + power_baby + ".")
print("\nI buy a power_baby")
else :
print("Are you kiddng me?")
字典, 注意逗号
1、一个简单的字典:这里找找外星人儿
/*字典是一系列键对值*/
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
print(alien_0['points'])
/*传递*/
new_point = alien_0['point']
print("You just earned" + str(nes_point) + "points")
/*添加*/
alien_0['x_position'] = 0
alien_0['y_position'] = 25
/*修改*/
alien_0['color'] = 'yellow'
/*删除键-值对*/
del alien_0['point']
/*遍历*/
name_list{
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in name_list.items(): #items()的作用就是返回键-值对列表,列表,列表~
print("\nKey" + key)
print("Value" + value)
/*不需要值*/
for key in name_list.keys():
print("\nKey" + key.title())
/*哎,就要值*/
for key in name_list.values():
print("\nKey" + key.title())
不需要值的情况特别有意思,遍历字典时,会默认遍历所有的键,不信你试试
就要值的情况特别有意思,遍历字典时,无法删除重复值,不信你试试
/*四十是十四,十四是四十*/
for key in name_list.keys():
print("\nKey" + key.title())
/*不能把四十说成十四,也不能十四说成四十*/
for key in set(name_list.values()):
print("\nKey" + key.title())
2、按顺序遍历字典
猜猜是什么顺序
for key in sorted(name_list.keys()):
print("\nKey" + key.title())
3、嵌套
字典存储在列表中,列表存储在字典中
/*字典存储在列表中*/
alien_0 = {'color': 'green', 'points': 5}
alien_2 = {'color': 'yellow', 'points': 10}
alien_3 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien)
/*列表存储在字典中*/
pizza = {
'crust': 'thick',
'toppings': ['mushrooms', 'extra cheese'],
}
print("You orderd a " + pizza['crust'] + "-crust pizza" +
"with the following toppings:")
for topping in pizzal['toppings']:
print("\t" + topping)
/*字典存储在字典中*/
users = {
'banana':{
'first': 'ban',
'last': 'ana',
'location': 'fruit'
},
'watermelon':{
'first': 'water',
'last': 'melon',
'location': 'favorite_fruit'
},
}
for username, user_info in users.items():
print("\nUsername: " + username)
full_name = user_info['first'] + user_info['last']
location = user_info['location']
print("\tFull name: " + full_name.title())
print("\tlocation: " + location.title())
书山有路勤为径,学海无涯苦作舟