Python Learning Note Day 03
今天是Python学习第三天,主要学习内容是:元组、代码格式、if语句相关、if语句处理列表、if语句的格式等。同时,今天正式接触Github 通过git commit和push上了Python学习笔记的代码和大纲,项目地址: https://github.com/LittleFee/python-learning-note .
大纲
相关练习代码
# Python将不能修改的值称为不可变的,而不可变的列表被称为元组。
# 定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。
# 例如一个边长不可变三角形
triangle=(3,4,5)
print(triangle[0])
print(triangle[1])
print(triangle[2])
#试图修改元组内容
# triangle[0]=12
# print(triangle[0])
# File "D:/python-learning-note/day03/0-yuanzu.py", line 9, in <module>
# triangle[0]=12
# TypeError: 'tuple' object does not support item assignment
for item in triangle :
print(item)
# 虽然不能修改元组的元素,但可以给存储元组的变量赋值。也就是重新定义元组
triangle=(9,16,25)
for item in triangle :
print(item)
triangle=(1,1)
for item in triangle :
print(item)
# 4-13 自助餐:有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食 # 品,并将其存储在一个元组中。 # 使用一个for 循环将该餐馆提供的五种食品都打印出来。 # 尝试修改其中的一个元素,核实Python 确实会拒绝你这样做。 # 餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块: # 给元组变量赋值,并使用一个for 循环将新元组的每个元素都打印出来。 foods=('noddles','rice','hotpot','sandwich','Cola') for food in foods : print(food.title()) # foods[2]='watermelon' foods=('poke','fruits','hotpot','sandwich','Cola') for food in foods : print(food.title())
# PEP 8建议每级缩进都使用4个空格,这既可提高可读性,又留下了足够的多级缩进空间。 # 如果喜欢使用tab,则可以把编辑器(IDE)的tab设置成为4个空格 # 每行不超过80字符,注释的行长都不超过72字符,在pycharm中可以在:File>Setting>Editor>Cod # estyle>Default Option 里设置Hard wrap at 为80,在编辑器的右侧就会有一条参考线,提醒你 # 适当的空行有助于代码阅读,但是请不要滥用空行 # 在诸如==、>=和<=等比较运算符两边各添加一个空格,例如,if age < 4:要比if age<4:好。
cars=['toyota','bmw','honda','audi'] for car in cars : if car=='bmw': print(car.upper()) else: print(car.title()) # Python 大小写敏感 word='WORD' if word=='word': print('True') else: print('False') # 可以用.lower()来 if word.lower()=='word': print('True') else: print('False') # 不相等 用 ! word='word' if word!='word': print('!Equal') else: print('Equal')
# 数字比较有:> < >= <= == bigger=12 smaller=2 if bigger==smaller : print('equal') if bigger>=smaller: print('>=') if smaller<=bigger: print('<=')
# 检查多个条件 # 你可能想同时检查多个条件,例如,有时候你需要在两个条件都为True时才执行相应的操作, # 而有时候你只要求一个条件为True时就执行相应的操作。在这些情况下,关键字and和or可助你 # 一臂之力。 # and number=input('give me a number : ') if (int(number)>=12) and (int(number)<=17): print('In the range') else : print('try again') # or number=input('Please give me a number') if (int(number)>100) or (int(number)<22): print('In the range') else : print('Out of range')
#有时候,执行操作前必须检查列表是否包含特定的值 可以用 in # 检查不包含的话用not in things=['ball','tool','car','GF'] if 'GF' in things : print('Ha? you have a GF?') if 'BF' not in things : print('Ha! you dont have a BF!')
# 布尔表达式 # 与条件表达式一样,布尔表达式的结果要么为True,要么为False。 status_1=True # status_1=0 status_2=False if status_1 : print('True') if status_2 : print('True') else: print('False')
# 条件测试:编写一系列条件测试;将每个测试以及你对其结果的预测和实际结 # 果都打印出来。你编写的代码应类似于下面这样: # car = 'subaru' # print("Is car == 'subaru'? I predict True.") # print(car == 'subaru') # print("\nIs car == 'audi'? I predict False.") # print(car == 'audi') # 详细研究实际结果,直到你明白了它为何为True 或False。 # 创建至少10 个测试,且其中结果分别为True 和False 的测试都至少有5 个。 baby='FYX' print("baby is FYX ? I think so!") print(baby=='FYX') print("baby is QW ? I think not!") print(baby=='QW') cars=['toyota','honda','volvo','benz'] print('I think benz is in the list') if 'benz' in cars : print('True') print('I think lotus is not in the list') if 'lotus' not in cars : print('True') print('I think benz and volvo both in the list') if ('benz' in cars) and ('volvo' in cars) : print('True') print('I think benz or lotus in the list') if ('benz' in cars) or ('lotus' in cars) : print('True') number=input('Input a number plz') if int(number) > 0 : print('True') else: print('False')
# 对于下面列出的各种测试, # 至少编写一个结果为True 和False 的测试。 # 检查两个字符串相等和不等。 # 使用函数lower()的测试。 # 检查两个数字相等、不等、大于、小于、大于等于和小于等于。 # 使用关键字and 和or 的测试。 # 测试特定的值是否包含在列表中。 # 测试特定的值是否未包含在列表中。 baby='FYX' if baby=='FYX' : print('True') if baby=='QW' : print('True') else: print('False') if baby.lower()=='fyx': print('True') baby='QW' if baby.lower()!='qw': print('True') else: print('False') num_1=23 if num_1==23: print('=23') if num_1 > 0: print('>0') if num_1 < 24 : print('<24') if num_1<=23 : print('<=23') if num_1>=23: print('>=23') if (num_1>1) and (num_1<24): print('1<num_1<24') if (num_1>0) or (num_1<23): print('true') lists=['baby','fyx','qw'] if 'fyx' in lists : print('in') if 'pop' not in lists: print('not in')
age = int(input('How old are you ?')) if age < 10 : print('You can do it free') elif age <= 18 : print('You can get a 50 % discount.') else: print('Opps...') words=['You can do it free','You can get a 50 % discount.','Opps...'] age = int(input('How old are you ?')) level=0 if age < 10 : level=0 elif age <= 18 : level=1 else: level=2 print(words[level]) # 省略else 代码块 age = 12 if age < 4: price = 0 elif age < 18: price = 5 elif age < 65: price = 10 elif age >= 65: price = 5 print("Your admission cost is $" + str(price) + ".") # 代码块在顾客的年龄超过65(含)时,将价格设置为5美元,这比使用else代码 # 块更清晰些。经过这样的修改后,每个代码块都仅在通过了相应的测试时才会执行。 # else是一条包罗万象的语句,只要不满足任何if或elif中的条件测试,其中的代码就会执行, # 这可能会引入无效甚至恶意的数据。如果知道最终要测试的条件,应考虑使用一个elif代码块来 # 代替else代码块。这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行。
# 外星人颜色#1:假设在游戏中刚射杀了一个外星人,请创建一个名为 # alien_color 的变量,并将其设置为'green'、'yellow'或'red'。 # 编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出 # 玩家获得了5 个点。 # 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中 # 未通过(未通过测试时没有输出)。???没看懂,翻译问题? alien_color='green' if alien_color=='green': print('You got 5 points') if alien_color != 'green' : print('You do not got point') # 外星人颜色#2:像练习5-3 那样设置外星人的颜色,并编写一个if-else 结构。 # 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5 个 # 点。 # 如果外星人不是绿色的,就打印一条消息,指出玩家获得了10 个点。 # 编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执 # 行else 代码块。 if alien_color=='green': print('You got 5 points') else: print('you got 10 points') if alien_color!='green': print('you got 10 points') else: print('you got 5 points') # 外星人颜色#3:将练习5-4 中的if-else 结构改为if-elif-else 结构。 # 如果外星人是绿色的,就打印一条消息,指出玩家获得了5 个点。 # 如果外星人是黄色的,就打印一条消息,指出玩家获得了10 个点。 # 如果外星人是红色的,就打印一条消息,指出玩家获得了15 个点。 # 编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条 # 消息。 if alien_color=='green': print('you got 5 points') elif alien_color=='yellow': print('you got 10 points') else: print('you got 15 points') if alien_color=='green': print('you got 5 points') if alien_color=='yellow': print('you got 10 points') if alien_color=='red': print('you got 15 points') # 人生的不同阶段:设置变量age 的值,再编写一个if-elif-else 结构,根据age # 的值判断处于人生的哪个阶段。 # 如果一个人的年龄小于2 岁,就打印一条消息,指出他是婴儿。 # 如果一个人的年龄为2(含)~4 岁,就打印一条消息,指出他正蹒跚学步。 # 如果一个人的年龄为4(含)~13 岁,就打印一条消息,指出他是儿童。 # 如果一个人的年龄为13(含)~20 岁,就打印一条消息,指出他是青少年。 # 如果一个人的年龄为20(含)~65 岁,就打印一条消息,指出他是成年人。 # 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。 age=int(input('How old are you ?\n')) if age<2: print('you are a baby') elif age <4: print('you are learning walk') elif age<13: print('you are a children') elif age<20: print('you are a teenager') elif age <65: print('you are a adult') elif age>=65: print('you are old man') # 喜欢的水果:创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if # 语句,检查列表中是否包含特定的水果。 # 将该列表命名为favorite_fruits,并在其中包含三种水果。 # X 编写5 条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中, # 就打印一条消息,如“You really like bananas!”。 favorite_fruits=['Bananer','Apple','orange','mango'] if 'Apple' in favorite_fruits: print('you are really like apple') if 'Apple' not in favorite_fruits: print('Opps......')
# 制作一杯柠檬水,每添加一个材料打印一个成功,但是店里的糖用完了,要提示没有盐,然后打印完成 needs=['salt','water','sugar','lemon'] for need in needs: if need=='sugar': print("Don't have enough sugar!") else: print(need+' is done') print('Done!\n') # 确定列表不是空的 # needs=[] if needs: for need in needs: if need == 'sugar': print("Don't have enough sugar!") else: print(need + ' is done') print('Done!') else: print('List is empy !') all_needs=['salt','water','sugar','watermelon','apple'] for need in needs: if need in all_needs: print('add '+need+' and done !') else: print(need+' is not available ') print('everting is ok')
# 以特殊方式跟管理员打招呼:创建一个至少包含5 个用户名的列表,且其中一 # 个用户名为'admin'。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。 # 遍历用户名列表,并向每位用户打印一条问候消息。 # 如果用户名为'admin',就打印一条特殊的问候消息,如“Hello admin, would you # like to see a status report?”。 # 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again” admins=['Pete','amdin','Kiwi','Baobao'] for admin in admins: if admin == 'amdin': print("Hello admin, would you like to see a status report?") else: print('Hello '+admin+', thank you for logging in again') # 处理没有用户的情形:在为完成练习5-8 编写的程序中,添加一条if 语句,检 # 查用户名列表是否为空。 # 如果为空,就打印消息“We need to find some users!”。 # 删除列表中的所有用户名,确定将打印正确的消息。 admins=[] if admins: for admin in admins: if admin == 'amdin': print("Hello admin, would you like to see a status report?") else: print('Hello ' + admin + ', thank you for logging in again') else: print('We need to find some users!') # 检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名 # 都独一无二的方式。 # 创建一个至少包含5 个用户名的列表,并将其命名为current_users。 # 再创建一个包含5 个用户名的列表,将其命名为new_users,并确保其中有一两 # 个用户名也包含在列表current_users 中。 # 遍历列表new_users,对于其中的每个用户名,都检查它是否已被使用。如果是 # 这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 # 出这个用户名未被使用。 # 确保比较时不区分大小写;换句话说,如果用户名'John'已被使用,应拒绝用户 # 名'JOHN'。 current_users=['tom','jerry','john','lina','jerff','ben'] new_users=['tom','kiwi','emily','tina','liam','BEN'] for user in new_users: if user.lower() in current_users: print('Name '+user+' already exist !') else: print('Name '+user+' is avialable') # 序数:序数表示位置,如1st 和2nd。大多数序数都以th 结尾,只有1、2 和3 # 例外。 # 在一个列表中存储数字1~9。 # 遍历这个列表。 # 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容 # 应为1st、2nd、3rd、4th、5th、6th、7th、8th 和9th,但每个序数都独占一行。 nums=list(range(1,10)) for num in nums: if num == 1: print(str(num)+'st') elif num == 2: print(str(num)+'nd') elif num == 3: print(str(num)+'rd') else: print(str(num)+'th')
今天完成第五章内容。
版权声明:
作者:Kiwi
链接:https://www.qingwei.tech/programe-develops/886.html
来源:清渭技术小站
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
共有 0 条评论