Python---if语句
# 参考《Python编程:从入门到实践》进行学习
# 有C或C++基础
# 大多代码运行结果不给出,可copy
示例:
cars = ['audi','bmw','subaru','toyota']for car in cars:if car == 'bmw':print(car.upper())else:print(car.title())
对于"bmw"进行全大写打印,其余首字母大写打印。
检查多个条件
1. and
age = 20
print(age>18 and age<22)
2. or
age = 20
print(age>22 or age<22)
检查特定值是否包含在列表中
用关键词in
requested_toppings = ['mushrooms','onions','pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
检查特定值是否不包含在列表中
用关键词not in
banned_users = ['andrew','carolina','david']
user = 'marie'
if user not in banned_users:print(user.title()+", you can post a response if you wish.")
if-elif-else结构
age = 12
if age < 4:price=0
elif age < 18:price=5
else:price=10
print("Your admission cost is $"+str(price)+'.')
可使用多个elif代码块。
可省略else代码块。
检查特殊元素
requested_toppings = ['mushrooms','green peppers','extra cheese']for requested_topping in requested_toppings:if requested_topping == 'green peppers':print("Sorry, we are out of green peppers right now.")else:print("Adding "+requested_topping + '.')
print("\nFinished making your pizza!")
确定列表不为空
requested_toppings = []
if requested_toppings:for requested_topping in requested_toppings:print("Adding "+requested_topping+".")print("\nFinished making your pizza!")
else:print("Are you sure you want a plain pizza?")
使用多个列表
available_toppings = ['mushrooms','olives','green peppers','pepperoni',
'pineapple','extra cheese']requested_toppings = ['mushrooms','french fries','extra cheese']for requested_topping in requested_toppings:if requested_topping in available_toppings:print("Adding "+requested_topping+".")else:print("Sorry, we don't have "+requested_topping+".")
print("\nFinished making your pizza!")
current_users = ['Mike','admin','Eric','Jack','Bay']
new_users = ['Mary','Mike','John','Bay','Yahoo']
for new_user in new_users:if new_user in current_users:print("Please input other user name:")else:print("This name can be used.")
Python---if语句相关推荐
- 忘了python关键语句?这份备忘录拯救你的记忆
忘了Python关键语句?这份备忘录拯救你的记忆 今天要介绍的 Python 3 Cheat Sheet 由法国国家科学研究中心(CNRS)的法国机械工程与信息技术实验室(LIMSI)的工程师 Lau ...
- 包教包会!7段代码带你玩转Python条件语句(附代码)
来源:大数据 本文约5200字,建议阅读10分钟. 本文介绍了Python条件语句常用的7段代码. [ 导 读 ]条件语句通过一个或多个布尔表达式的执行结果(真值或假值)决定下一步的执行方向.所谓布尔 ...
- python入门语句_Python 快速入门笔记(5):语句
本系列随笔是本人的学习笔记,初学阶段难免会有理解不当之处,错误之处恳请指正.转载请注明出处: https://www.cnblogs.com/itwhite/p/12297769.html. 简介 p ...
- python“ with”语句的用途是什么?
今天,我第一次碰到了Python with语句. 我已经使用Python几个月了,甚至都不知道它的存在! 考虑到它的地位有些晦涩,我认为值得一问: Python with语句旨在用于什么? 你用它来做 ...
- python循环语句-Python-循环语句及循环控制语句
循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式: Python 提供了 for 循环和 while 循环(在 Python 中没有 do..while 循环): ...
- python基本语法语句-Python基本语句
一.Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非 ...
- python基本语法语句-python基本语句有哪些
Python是一种计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的开发.下面我们就为 ...
- python输入语句-python输入语句
广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. python条件语句目录:1. 分支语句(if...else...)2. ...
- python条件语句-Python3 条件控制
Python3 条件控制 Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: 代码执行过程: if 语句 ...
- python条件语句-Python 条件语句
Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.高佣联盟 www.cgewang.com 可以通过下图来简单了解条件语句的执行过 ...
最新文章
- 成年人过敏的N种变态表现
- Android 使用反射机制获取或设置系统属性(SystemProperties)
- [YTU]_2630 ( E2 驾驭const)
- JAVA 注解的基本原理
- re:Invent 大会第一天,看看AWS有哪些最新进展?\n
- android 4.0.4 usb调试,安卓手机4.0怎么打开usb调试?图文详解附视频
- View、Text、Button的drawableLeft左侧图片自定义大小
- [概率论]如何通俗地理解“最大似然估计法”?
- 给C#学习者的建议 - CLR Via C# 读后感
- ucenter 用户加密方式
- MATLAB直方图均衡化函数的计算及使用
- 电脑ping服务器显示传输失败,Win10电脑ping时显示“传输失败常见故障”的最佳解决方案...
- 【期末复习之路】JAVA(二)A
- pytorch 定义torch类型数据_PyTorch 使用 TorchText 进行文本分类
- 单片机c语言篮球比分_基于单片机的篮球计时计分器设计(C语言编程、含proteus仿真图)...
- 有关人工智能的硕士点整理
- linux内核配置nfs,【参赛手记】开启Digilent提供的Linux内核的NFS支持
- 7-3 计算正弦函数值 (10 分)
- dom4j实现XML操作
- rundll32.exe命令使用简介