今日内容:
1.列表
2.元组
3.py2与py3的区别
4解释器/编译器
5.练习题
1.列表
想要表示多个”事物“,可以使用列表
users = ["李邵奇","奇航",99]
#列表生成
[x * x for x in range(1, 11)]#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
-
-
len
users = ["李邵奇","奇航",99] val = len(users) print(val) # 3
-
索引
users = ["李邵奇","奇航",99] val = users[0] print(val)
- 切片
users = ["李邵奇","奇航",99] val = users[0:2]
- 删除(数字/布尔/字符串除外)
users = ["李邵奇","奇航",99] # 方式一 users.pop(1) print(users) # 方式二: del users[1] print(users)
-
字符串本身不能修改或删除【不可变类型】 v1 = "alex".upper()
-
列表是可变类型。
-
- 修改(字符串/数字/布尔除外)
users = ["李邵奇","奇航",99] users[2] = 66 users[0] = '李杰' users[0][1]
- 步长
users = ["李邵奇","奇航",99] val = users[0:2:2]
- 练习题
#list/tuple v = ['alex','oldboy','藏老四','利奇航'] if "利奇航" in v: print('含敏感')
# 让用户输入任意字符串,然后判断此字符串是否包含指定的敏感字符。 char_list = ['利奇航','堂有光','炸展会'] content = input('请输入内容:') # 我叫利奇航 / 我是堂有光 / 我要炸展会 success = True for v in char_list: if v in content: success = False break if success: print(content) else: print('包含铭感字符')
View Code - for循环
View Code
-
- 独有功能
- append,在列表的最后追加一个元素
users = [] users.append('alex') print(users)
""" 示例一: users = [] while True: name = input('请输入姓名:') users.append(name) print(users) """ """ 示例二: # 录入用户和密码 users = [] for i in range(0,3): name = input('请输入用户名和密码:') users.append(name) print(users) # ['alex,123', 'oldboy,888', 'lishaoqi,123'] # 用户和密码校验 username = input('请输入要登陆用户名:') password = input('请输入要登陆密码:') for item in users: result = item.split(',') # ['alex','123'] user = result[0] pwd = result[1] if user == username and pwd == password: print('登陆成功') break """
- reverse
v1 = [1,2,3111,32,13] print(v1) v1.reverse() print(v1)
- sort
v1 = [11,22,3111,32,13] print(v1) # v1.sort(reverse=False) # 从小到大(默认) # v1.sort(reverse=True) # 从大到小 # print(v1)
- insert
- remove
- pop
- append,在列表的最后追加一个元素
- 总结
- 增:append/insert
- 删:remove/pop/clear/del users[2]
- 改:users[3]="新值
- 查:索引/切片
- 列表嵌套
users = ["alex",0,True,[11,22,33,"老男孩"],[1,['alex','oldboy'],2,3]] users[0] users[2] users[0][2] users[3] # [11,22,33,"老男孩"] users[3][-1] # "老男孩" users[3][-1][1] # '男' users[3] = 666
2.元组
- 元组书写规范
users = [11,22,33,"老男孩"] # 列表(可变) users = (11,22,33,"老男孩") # 元组(不可变)
- 公共功能
- 索引(排除:int/bool)
users = (11,22,33,"老男孩") print(users[0]) print(users[-1])
- 切片排除:(int/bool)
users = (11,22,33,"老男孩")
print(users[0:2]) - 步长(排除:int/bool)
users = (11,22,33,"老男孩") print(users[0:2:2])
- 删除(排除:tuple/str/int/bool)
- 修改(排除:tuple/str/int/bool)
- for循环(排除:int/bool)
users = (11,22,33,"老男孩") for item in users: print(item)
- len(排除:int/bool)
users = (11,22,33,"老男孩") print(len(users))
- .独有功能(无)
- 特殊:元组中的元素(儿子)不可被修改/删除。
# 示例一: v1 = (11,22,33) v1[1] = 999 # 错误 v1 = 999 # 正确 # 示例二:可以嵌套 v1 = (11,22,33,(44,55,66),(11,2,(99,88,),3)) # 示例三:嵌套 v2 = [11,22,33,(11,22,33)] v2[-1][1] = 99 # 错误 v2[-1] = 123 # 正确 # 示例四:嵌套 v3 = (11,[1,2,3],22,33) v3[1] = 666 # 错误 v3[1][2] = 123
3.py2与py3的区别
1.默认编码器
py2:ascii
py3:utf-8
2.输入
py2:raw_input
py3:input
3.输出
py2:print ""
py3:print()
4.数据类型
py2:有int/long;做除法要多加一行代码
py3:只有int
4.解释器/编译器
# 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行。 # 解释型:写完代码交给解释器,解释器会从上到下一行行代码执行:边解释边执行。 【实时翻译】
5.练习题
#!/usr/bin/env python # -*- coding:utf-8 -*- # #######################1. 步长 """ name = 'alex' # val = name[0:-1:2] # val = name[1:-1:2] # val = name[1::2] # val = name[::2] # val = name[-1:0:-2] # print(val) # 笔试题:请将字符串反转。
#顺序相反操作 val = name[::-1]#顺序相反操作 print(val) """ # ####################### 2. for循环 # name = 'alex' # for item in name: # print(item) # continue # print('123') # # print('结束') # 1. for循环打印字符串的每个元素: for > while # 2. 请打印: 1 - 10 for循环 + range # for i in range(1,11): # "12345678910" # print(i) # 3. 请打印: 1 2 3 4 5 6 8 9 10 """ for i in range(1,11): if i == 7: pass else: print(i) """
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 实现一个整数加法计算器(两个数相加): 如:content = input("请输入内容:") 用户输入:5+9或5+ 9或5 + 9(含空白),然后进行分割转换最终进行整数的计算得到结果。 """ # 思路一: """ content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ] content = content.strip() # [5+9] 或 [5 +9] 或者 [5 + 9] v1 = int(content[0]) v2 = int(content[-1]) v3 = v1 + v2 """ # 思路二: """ content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ] content_len = len(content) index = 0 total = 0 while True: char = content[index] if char.isdigit(): total += int(char) index += 1 if index == content_len: break print(total) """ # 思路三: """ content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ] result = content.split('+') # print(result) # ['55 ', ' 99 '] v1 = int(result[0]) # "55" v2 = int(result[1]) # " 99 " v3 = v1 + v2 print(v3) """