Python进阶之路三(列表,字典,部分控制流)

上一篇 / 下一篇  2018-11-13 11:30:32 / 个人分类:Python

2018年11月13日
1.代码编译注意小点:
文本最上方添加# code=utf-8(预防格式异常,这个我操作的时候遇到该问题)
2.列表list []
3.列表增加list.append()
4.字典{}
对应1-4代码学习实例:
# code=utf-8
list_A=["1HBZ","OCEAN","xyzc","chsqb"]
print(list_A[0])
c=len(list_A)
print(list_A[-1])
print(list_A[c-1])
nfsdqc="nfsdqc"
list_A.append(nfsdqc)
#字典
print(list_A)
dict_A={}
dict_xyzc={"name":"xyzc","sex":"male","city":"shenzhen","hobby":"boy"}
print(dict_xyzc)
执行结果:
1HBZ
chsqb
chsqb
['1HBZ', 'OCEAN', 'xyzc', 'chsqb', 'nfsdqc']
{'name': 'xyzc', 'sex': 'male', 'city': 'shenzhen', 'hobby': 'boy'}
5.控制流 if elif else
money=700000
if money>100000:
    print("去美国旅游")
elif money>50000:
    print("去云南旅游")
else:
    print("家里蹲")
6.冒号:后面的代码行注意缩进,缩进代表范围内,后续定格代表进入另外一个范围;
7.多行注释使用''',也可以选中所需要注释的代码行,点击ctrl+?,所选内容同时采用#注释(小窍门)
8.控制流for in 表示循环  if else表示判断 break表示中断
for循环-遍历下标-通过下标来获取数据
for循环-遍历列表的值-直接取值
在for循环中使用and和or,break
square_account_list=[
    {"name":"John","age":22,"contury":"England","sex":"male","height":180,"weight":160},
    {"name":"Tom","age":30,"contury":"America","sex":"female","height":180,"weight":150},
    {"name":"Dick","age":27,"contury":"England","sex":"male","height":187,"weight":150},
    {"name":"Lucy","age":28,"contury":"America","sex":"female","height":180,"weight":150},
    {"name":"David","age":30,"contury":"America","sex":"male","height":120,"weight":150},
    {"name":"Bill","age":30,"contury":"America","sex":"male","height":130,"weight":150},
    {"name":"Jian","age":18,"contury":"China","sex":"female","height":180,"weight":150}]
# first_one=square_account_list[0]
# if first_one["name"]=="David":
#     print(first_one)
# else:
#     print("他不是David")

#for循环-遍历下标-通过下标来获取数据
#[0,1,2,3,4,5,6]
for index in range(0,len(square_account_list)):
    person_name=square_account_list[index]["name"]
    if person_name=="David":
        print(person_name)
        break
    else:
        print("他不是David")
'''
#for循环-遍历列表的值-直接取值
for item in square_account_list:
    if item["name"]=="David":
        print(item)
     
    else:
        print("他不是David")
'''
'''
for item in square_account_list:
    if item["age"]>20 and item["sex"]=="female":
        print(item["name"])
    else:
        print("====")
'''
for item in square_account_list:
    if item["age"]>20 or (item["sex"]=="female" and item["height"]>160):
        print(item["name"])
    else:
        print("====")
执行后的结果:
1HBZ
chsqb
chsqb
['1HBZ', 'OCEAN', 'xyzc', 'chsqb', 'nfsdqc']
{'name': 'xyzc', 'sex': 'male', 'city': 'shenzhen', 'hobby': 'boy'}



TAG:

 

评分:0

我来说两句

Open Toolbar