message='Albert Einstein once said,"A person who never made a mistake never tried anything new"' #单双引号混用 print(message)
执行结果
2.6 名言2
具体代码
1 2 3 4 5
famous_person="Albert" #单双引号混用 message=f"{famous_person} "'Einstein once said,"A person who never made a mistake never tried anything new"' print(message)
foods=['tomato','bacon','cheese'] for food in foods: print(f"I like {food}!") print("I really love pizza!")
执行结果
4.2 动物
具体代码
1 2 3 4 5 6
animals=['monkey','mouse','elephant'] for animal in animals: print(animal) print(f"A {animal} would make a great pet") print("Any of these animals would make a great pet!")
执行结果
4.3 for循环输出1-20
具体代码
1 2 3
for value in range(1,21): print(value)
执行结果
4.4 一百万
具体代码
1 2 3 4
numbers=list(range(1,1000001)); for value in numbers: print(value)
jiabins=['宋亚翔','python','java','c++','c语言'] for jiabin in jiabins[0:3]: print(jiabin) print("--------------------") for jiabin in jiabins[-3:]: print(jiabin)
执行结果
4.11 你的比萨,我的比萨
具体代码
1 2 3 4 5 6 7 8 9 10 11 12
foods=['tomato','bacon','cheese'] friend_pizzas=foods[:] #复制副本 #两个列表都添加新的比萨 foods.append('abs') friend_pizzas.append('bbb') #循环遍历输出 for food in foods: print(food) print("\n") for friend in friend_pizzas: print(friend)
执行结果
4.13 自助餐
具体代码
1 2 3 4 5 6 7 8 9
foods=('aaa','bbb','ccc','ddd','eee') for food in foods: print(food) # foods[0]='fff' #元组不允许修改 print("\n") foods=('aaaa','bbbb','ccc','ddd','eee') #只能所有的重新赋值 for food in foods: print(food)
执行结果
if语句
5.3 外星人颜色
具体代码
1 2 3 4 5 6
alien_color='green' if alien_color == 'green': print("是绿色,恭喜您获得五分!") if alien_color == 'yellow': print("是黄色,恭喜您获得五分!")
执行结果
5.4 外星人颜色2
具体代码
1 2 3 4 5 6
alien_color='green' if alien_color == 'green': print("是绿色,恭喜您获得5分!") else : print("不是绿色,恭喜您获得10分!")
age=120 if age<2: print("此人是婴儿") elif age>2 and age <4 : print("此人是幼儿") elif age>4 and age<13 : print("此人是儿童") elif age>13 and age<20 : print("此人是青少年") elif age>20 and age<65 : print("此人是成年人") else : print("此人是老年人")
执行结果
5.7 喜欢的水果
具体代码
1 2 3 4 5 6 7 8 9 10 11 12
favorite_fruits=['apple','orange','bananas',] if 'apple' in favorite_fruits : print("apple在此列表内!") if 'oranges' in favorite_fruits : print("oranges在此列表内!") if 'orange' in favorite_fruits : print("orange在此列表内!") if 'bananas' in favorite_fruits : print("bananas在此列表内!") if 'applebet' in favorite_fruits : print("applebet在此列表内!")
执行结果
5.8 以特殊方式跟管理员打招呼
具体代码
1 2 3 4 5 6 7
users=['admin','bbb','ccc','ddd','eee_dasd'] for user in users: if user == 'admin' : print("Hello admin,would you like to see a status report?") else : print(f"Hello {user},thank you for logging in again.")
执行结果
5.9 处理没有用户的情形
具体代码
1 2 3 4 5 6 7
users=['admin','bbb','ccc','ddd','eee_dasd'] for user in users: if user == 'admin' : print("Hello admin,would you like to see a status report?") else : print(f"Hello {user},thank you for logging in again.")
current_users=['admin','John','CCC','ddd','EeE_dasd'] new_users=['aaa','bbb','JOHN','ddd','eee'] #创建当前用户副本 current_users_xiaoxie=current_users[:] #将当前用户小写形式存储 i=0; for user in current_users_xiaoxie: current_users_xiaoxie[i]=user.lower() #转为小写 i=i+1
#遍历查看是否注册过 for user in new_users: if user.lower() in current_users_xiaoxie : print(f"{user}注册过,请重新输入") else : print(f"{user}可以使用")
执行结果
5.11 序数
具体代码
1 2 3 4 5 6 7 8 9 10 11
numbers=['1','2','3','4','5','6','7','8','9']; for number in numbers: if number == '1' : print("1st") elif number == '2' : print("2nd") elif number == '3' : print("3rd") else : print(f"{number}th")
执行结果
字典
6.1 人
具体代码
1 2 3 4
peoples={'frist_name':'宋','last_name':'亚翔','age':'23','city':'西安'} for jian,zhi in peoples.items(): print(f"{jian}: {zhi}")
执行结果
6.2 喜欢的数
具体代码
1 2 3 4
peoples={'宋亚翔':'7','荔湾':'34636','王五':'23','赵四':'5498'} for jian,zhi in peoples.items(): print(f"{jian}: {zhi}")
执行结果
6.3 词汇表
具体代码
1 2 3 4
peoples={'JAVA':'大二学的','C':'大一学的','C#':'大二学的','C++':'还没学的','Python':'正在学的'} for jian,zhi in peoples.items(): print(f"{jian}: {zhi}")
rivers={'the Yangtze River':'China','Yellow River':'China','Nile':'Egypt'} #打印对应河流和国家 for key,value in rivers.items(): print(f"The {key} runs through {value}") print("\n") #打印所有河流名称 for key in rivers.keys(): print(key) print("\n") #打印所有国家名称 for value in rivers.values(): print(value)
执行结果
6.6 调查
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13
favorite_languages={ 'jen':'python', 'sarah':'c', 'edward':'ruby', 'phil':'python' } people={'jen','ww','syx','zs'} for key in people: if(key in favorite_languages): print(f"{key}恭喜你!") else : print(f"{key}抱歉!")
执行结果
6.7 人们
具体代码
1 2 3 4 5 6 7
peoples_one={'frist_name':'宋','last_name':'亚翔','age':'23','city':'西安'} peoples_two={'frist_name':'李','last_name':'五','age':'12','city':'武汉'} peoples_three={'frist_name':'赵','last_name':'四','age':'22','city':'长沙'} peoples=[peoples_one,peoples_two,peoples_three] for people in peoples: print(people)
执行结果
6.8 宠物
具体代码
1 2 3 4 5 6 7 8
#列表里面存储字典 peoples_one={'frist_name':'欢欢','last_name':'宋亚翔'} peoples_two={'frist_name':'乐乐','last_name':'李五',} peoples_three={'frist_name':'憨憨','last_name':'赵四'} pets=[peoples_one,peoples_two,peoples_three] for pet in pets: print(pet)
执行结果
6.9 喜欢的地方
具体代码
1 2 3 4 5 6
#字典里面存储列表 syx_place=['上海','北京','哈尔滨'] favorite_places={'宋亚翔':syx_place,'王五':'武汉','赵四':'上海',} for name,place in favorite_places.items(): print(f"{name}: {place}")
执行结果
6.10 喜欢的数2
具体代码
1 2 3 4 5 6 7
#字典里面存储列表 syx_number=['12','77','7777'] lz_number=['408','555'] peoples={'宋亚翔':syx_number,'荔湾':lz_number,'王五':'23','赵四':'5498'} for jian,zhi in peoples.items(): print(f"{jian}: {zhi}")
执行结果
6.11 城市
具体代码
1 2 3 4 5 6 7 8
#字典里面存储字典 xian={'country':'china','population':'250.6w','fact':'陕西省省会'} shanghai={'country':'china','population':'555.3w','fact':'中国的一线城市'} beijing={'country':'china','population':'105.8w','fact':'中国的首都'} cities={'xian':xian,'shanghai':shanghai,'beijing':beijing} for city,xx in cities.items(): print(city,xx)
message=input("请输入1个数判断是否为10的整数倍: ") if int(message)%10 == 0: print("是10的整数倍!") else : print("不是10的整数倍!")
执行结果
7.4 比萨配料
具体代码
1 2 3 4 5
message='' while message != 'quit' : message=input("请输入需要添加的比萨配料:"); print(message)
执行结果
7.5 电影票
具体代码
1 2 3 4 5 6 7 8 9 10
price='' while price != 0 : price=input("请输入观看者年龄:"); if int(price) <= 3 : print("免费") elif int(price)> 3 and int(price) < 12 : print("10美元") else : print("15美元")
执行结果
7.8 熟食店
具体代码
1 2 3 4 5 6 7 8
sandwich_orders=['aaa','bbb','ccc'] finished_sandwiches=[] for sandwich in sandwich_orders: print("I made your tuna sandwich") finished_sandwiches.append(sandwich)
print(finished_sandwiches)
执行结果
7.9 五香烟熏牛肉卖完了
具体代码
1 2 3 4 5 6 7 8 9 10 11
sandwich_orders=['aaa','pastrami','bbb','ccc','pastrami','pastrami','pastrami'] print("删除前的列表:") print(sandwich_orders) print("熏牛肉已经卖完了!") #要删除的变量名 current='pastrami' while current in sandwich_orders : sandwich_orders.remove(current) print("删除后的列表:") print(sandwich_orders)
执行结果
7.10 梦想的度假胜地
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#获取最终调查结果 result={} #结束的标志符 flag=True while flag: name=input("请问您的姓名:") respnse=input("请问您最想去的城市:") result[name]=respnse #将姓名和所想去的城市存入字典 repeat=input("还有人参加吗yes/no?") #如果没人参加了就设置flag为false if repeat == 'no': flag=False print(result)
from random import sample lottery=['1','2','3','4','5','6','7','8','9','10','a','b','c','d','e'] result=sample(lottery,4) print(result)
执行结果
9.15 彩票分析
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13
from random import sample lottery=['1','2','3','4','5','6','7','8','9','10','a','b','c','d','e'] result=sample(lottery,4) print(f"中大奖的序列是:{result}") current=[] sum=0 while result!=current: current=sample(lottery,4) print(f"当前随机的序列是:{current}") sum+=1
print(f"一共要{sum}多次才可以中大奖!")
执行结果
文件和异常
10.1 python学习笔记
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#.txt文件和.py文件在一个目录里面!!!
#1.全文读取 with open('learning_python.txt',encoding='utf-8') as files: contents=files.read() print(contents)
#2.逐行读取 with open('learning_python.txt',encoding='utf-8') as files: for file in files: print(file.strip()) #去除print打印多余空行
#3.使用列表读取 with open('learning_python.txt',encoding='utf-8') as files: lines=files.readlines() #with语句外 for line in lines: print(line.strip()) #去除print打印多余空行
执行结果
10.2 C语言学习笔记
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#1.一行一行修改 with open('learning_python.txt',encoding='utf-8') as files: lines=files.readlines()
for line in lines: line=line.replace('Python','JAVA') print(line.strip()) #删除print打印多余空格
#2.整段文直接修改 with open('learning_python.txt',encoding='utf-8') as files: lines=files.read() lines=lines.replace('Python','JAVA') print(lines)
执行结果
10.3 访客
具体代码
1 2 3 4 5 6
#1.open()里面写是w 写入模式 #2.使用write()写入文件 with open('learning_python.txt','w',encoding='utf-8') as files: writes=input("请输入用户名:") files.write(writes)
执行结果
10.4 访客名单
具体代码
1 2 3 4 5 6 7
with open('learning_python.txt','w',encoding='utf-8') as files: writes='' while writes != 'quit': writes=input("请输入用户名:") #输入用户名 print(f"{writes}欢迎您!") #打印问候语 files.write(f"{writes}\n") #自己在f字符串里面加\n换行
执行结果
10.5 调查
具体代码
1 2 3 4 5 6 7
with open('learning_python.txt','w',encoding='utf-8') as files: writes='' while writes != 'quit': print("为什么您喜欢编程?") writes=input("请输入原因:") #输入用户名 files.write(f"{writes}\n") #自己在f字符串里面加\n换行