Python跑路到突然想学习了
2021-06-19 08:03
标签:条件 zha 比较 ann magic 5.2.1 lis [] aci Python跑路到突然想学习了 第4章 操作列表 4.1 遍历列表 利用for循环 进行遍历 eg:magicians = [‘alice’,’dacid’,carolina’] for magician in magicians: print(magician) # 把列表内容传输给新变量。 # 输出语句缩进。 # 注意冒号的使用。 4.1.2 for循环中更多操作 eg:magicians = [‘alice’,’dacid’,carolina’] for magician in magicians: print(magician.title()+”,that was a great trick!”) print("I can’t wait to see your next trick,”+magician.title()+”.\n”) # 不缩进输出错误。 # 循环输出三次。 4.3 创建数值列表 4.3.1 使用range()函数 Eg:for value in range(1,5): print(value) 1 2 3 4 列表创建 Eg: numbers = list(range(1,5)) print(numbers) [1,2,3,4] 4.3.2 对数字列表的简单统计计算 Eg: digits = [1,2,3,4,5,6,7,8,9] min(digits) #最小值 max(digits) #最大值 sum(digits) #总和 4.4切片 Eg:magicians = [‘alice’,’dacid’,carolina’] print([0:2]) 4.4.1 遍历切片 eg:magicians = [‘alice’,’dacid’,carolina’] for magician in magicians[0:1]: print(magician.title()+”,that was a great trick!”) 4.4.2 复制列表 eg:magicians = [‘alice’,’dacid’,carolina’] my_friends = magicians[:] print(my_friends) 4.5元组 4.5.1 定义 Eg: dimensions = [200,50] print(dimensions[0]) print(dimensions[1]) # 元组元素通过给存储元组的变量赋值来改变其值的大小 Eg: dimensions = [200,50] print(“Origina dimensions:”) for dimension indimensions: print(dimension) dimensions = [400,100] print(“\nModified dimensions:”) for dimension indimensions: print(dimension) 第五章 if语句 5.1 一个简单的事例 cars = [‘audi’,’bmw’,’subaru’,toyota’] for car in cars: if car == ‘bmw’ print(car.upper()) # 全大写 else print(car.title()) # 第一个字母大写 5.2条件测试 car = ‘bwm’ car == ‘bwm’ true # 简单的赋值与比较 # 且字母大小不同输出False 可使用lower()和upper()函数进行转换 后输出true # 数字比较 数字的大小和 大于小于都可以进行比较 # 可使用 or Eg: Age_0 = 11 Age_1 = 22 Age_0 >= 20 or Age_1 >=20 ture # or 一个为真都为真 and 必须两个都为真 5.2.1 检测元素 是否在列表中 eg: user = ‘ma’ if user not in banned_users: print(user.title()+”,you can
post a response if you wish.”) 5.2.2 布尔表达式 Game_active = true Can_edit = false # 游戏是否进行 用户是否可以编辑内容 5.3 if 语句 age = 19 if age >= 18 print(“you are old enough
to vote!”) print(“Have you registered to vote yet?”) # 如果 if语句成立则输出 两句缩进语句 5.3.1 if-else语句 Eg: age = 19 if age >= 18 print(“you are in room!”) else: print(“you are
not in room!”) 5.3.2 if-elif-else Eg: age = 12 if age
price = 0 elif age
price = 1 else age
price = 2 print(“you admission need is
$”+ str(price)+”.”) # 注意引用数字 str() # 注意语句缩减 # 执行一个代码段可以使用 if-elif-else结构 # 多个代码段可以使用一些了独立if语句 5.4 if语句处理列表 banned_users= [‘tao’,’li’,’zhang’] for banned_user in banned_users: print(banned_user.title()+”,you
can post a response if you wish.”) # 检查特殊元素 banned_users= [] for banned_user in banned_users: if banned_users: for banned_user in banned_users: print(banned_user.title()+”,you
can post a response if you wish.”) else: print(”are you sure
use he?”) # 使用多个列表 # * 注意else以后无语句和for语句后面
则 加 : 冒号 # 注意 for 和print都要缩进 Python跑路到突然想学习了 标签:条件 zha 比较 ann magic 5.2.1 lis [] aci 原文地址:https://www.cnblogs.com/9a3a/p/9690909.html
banned_users= [‘tao’,’li’,’zhang’]