python编程:从入门到实践----第五章:if语句>练习
food = 'meat'
print("Is food == 'meat'? I predict True")
print(food =='meat')
print("\nIs food == 'apple'? I predict False")
print(food =='apple') fruit = 'banana'
print("\nIs fruit == 'banana'? I predict True")
print(fruit =='banana')
print("\nIs fruit == 'apple'? I predict False")
print(fruit =='apple') #输出结果:
Is food == 'meat'? I predict True
True Is food == 'apple'? I predict False
False Is fruit == 'banana'? I predict True
True Is fruit == 'apple'? I predict False
False
'''
i1='apple'
i2='Apple'
print(i1 == i2)
print(i1 != i2)
print(i1 == i2.lower())
'''
'''
i3=20
i4=30
print(i3 == i4)
print(i3 != i4)
print(i3 > i4)
print(i3 >= i4)
print(i3 < i4)
print(i3 <= i4)
print(i3<=20 and i4<=20)
print(i3<=20 or i4<=20)
'''
j=['apple','banan','orange']
j1 = 'apple'
print(j1 in j)
print(j1 not in j)
'''
alien_color = 'green'
if alien_color =='green':
print("You get 5 points")
#输出结果:You get 5 points
'''
alien_color = 'green'
if alien_color =='orange':
print("You get 5 points")
#输出结果:空
alien_color = 'green'
if alien_color =='green':
print("You shoot alien. You get 5 points")
'''
else:
print("You get 10 points")
'''
5-5 外星人颜色#3 :将练习5-4中的if-else 结构改为if-elif-else 结构。
alien_color = 'green'
if alien_color =='green':
print("You got 5 points")
elif alien_color =='yellow':
print("You got 10 points")
else:
print("You got 15 points") #输出结果:You got 5 points
备注:黄色和红色打印的消息的代码省略,只要把变量中的值改为黄色和红色即可。
age = 64
if age <2:
print("He is baby")
elif 2<=age<4 :
print("He just learn to walk")
elif 4<=age <13:
print("He is a child")
elif 13<=age <20:
print("He is teenager")
elif 20<= age <65:
print("He is a adult")
else:
print("He is a elderly") #输出结果:He is a adult
favorite_fruit = ['apple','orange','banana']
if 'apple' in favorite_fruit:
print("You really like apple!")
if 'orange' in favorite_fruit:
print("You really like orange!")
if 'banana' in favorite_fruit:
print("You really like bananas!")
if 'pear' in favorite_fruit:
print()
if 'watermelon' in favorite_fruit:
print() #输出结果:
You really like apple!
You really like orange!
You really like bananas!
users=["admin","crystal","James","Lily","Lucy"]
for user in users:
if user == "admin":
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user + ",thank you for logging in again.") #输出结果:
Hello admin, would you like to see a status report?
Hello crystal,thank you for logging in again.
Hello James,thank you for logging in again.
Hello Lily,thank you for logging in again.
Hello Lucy,thank you for logging in again.
users=[]
if users:
for user in users:
if user == "admin":
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user + ",thank you for logging in again.")
else:
print("We need to find some users!") #输出结果:
We need to find some users!
current_users =['admin','crystal','James','Lucy','Lily']
new_users = ['Admin','Crystal','Jim','Candy','lily']
for new_user in new_users:
if new_user.lower() in [current_user.lower() for current_user in current_users]:
#[current_user.lower() for current_user in current_users] 列表不区分大小写,也就是列表解析
print(new_user + ",Please input other user")
else:
print(new_user + ",This user is not used") #输出结果:
Admin,Please input other user
Crystal,Please input other user
Jim,This user is not used
Candy,This user is not used
lily,Please input other user
5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
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(str(number)+"th")
#输出结果
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
python编程:从入门到实践----第五章:if语句>练习的更多相关文章
- #Python编程从入门到实践#第四章笔记
#Python编程从入门到实践#第四章笔记 操作列表 1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...
- python编程:从入门到实践----第五章>if 语句
一.一个简单示例 假设有一个汽车列表,并想将其每辆汽车的名称打印出来.遇到汽车名‘bmw’,以全大写打印:其他汽车名,首字母大写 cars=['audi','bmw','subaru','toyota ...
- 《Python编程从入门到实践》第二章_变量和简单数据类型
什么是变量呢? 举例: >>> message = "Hello,Python!" >>> print (message) Hello,Pyth ...
- Python:从入门到实践--第五章--if语句--练习
#1.编写一系列条件测试:将每个测试以及结果打印出来 car = '宝马' if car == "宝马": print("预测正确") print(car) e ...
- #Python编程从入门到实践#第三章笔记
列表简介 1.什么是列表 列表:由一系列按也顶顺序排列的元素组成.元素之间可以没有任何关系. 列表:用方括号[]表示,并用逗号分隔其中元素.名称一般为复数 2.访问元素 (1)列表是有序集合 ...
- Python编程从入门到实践笔记——异常和存储数据
Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...
- Python编程从入门到实践笔记——文件
Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...
- Python编程从入门到实践笔记——类
Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
随机推荐
- [题解] LuoguP6071 [MdOI2020] Treequery
传送门 感觉这是一个写的很舒服的题? 树上路径的交什么的就很想树上差分?发现根本没法做...它还要求在线.... 好先来看\(Subtask\)吧\(qwq\)... Subtask 1 \(l=r\ ...
- Linux用户管理 权限管理 内存管理 网络管理命令 (第四天)
默认添加的用户会自动加入和用户名一样的组中su 切换用户查看当前登陆的用户: whoami id`查看当前用户属于哪个组:groupsgroupadd 组名 添加组groupdel 组名 删除组gre ...
- 基础语法-其它流程控制语句break和continue
基础语法-其它流程控制语句break和continue 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.break语句 /** * break语句 * @author 尹正杰 * ...
- java切换jdk版本
目的:将jdk1.7切换为jdk1.6 1.原本安装了jdk1.7,环境变量也是配置的1.7相关路径,在cmd下输入[java -version]后,显示 [ C:\Users\Administrat ...
- BZOJ:2190: [SDOI2008]仪仗队
题解:欧拉函数 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...
- java百货中心供应链管理系统 源码
开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 百货中心供应链管理系统主要用于实现了企业管理数据统计等.本系统结构如 ...
- 在线答题APP动工前的小总结
7月26号,确定了我们这个暑假的学习任务——做一个答题软件.目的就是为了让我们在做项目中学习,在学习中提高做项目的能力.但是,由于基础知识都还没掌握牢固,所以从本周三确定项目以来,基本没有一点进展. ...
- Activity LauchMode设置
lauchMode: standard: 标准模式,每次调用startActivity()方法就会产生一个新的实例. singleTop: 如果已经有一个实例位于Activit ...
- 2020/2/13 bluecmsv1.6sp1代码审计
0x00 前言 从今天开始审计一些小的cms,一周内至少审计一种,中间可能会写点别的有趣的东西 0x01 安装好后,看到登陆框,用万能密码打一发,无果,尝试重装,可以重装.有robots.txt 看u ...
- Tensorflow学习教程------tensorboard网络运行和可视化
tensorboard可以将训练过程中的一些参数可视化,比如我们最关注的loss值和accuracy值,简单来说就是把这些值的变化记录在日志里,然后将日志里的这些数据可视化. 首先运行训练代码 #co ...