Python -- 4. if()操作
来源:互联网 发布:软件开发策划书 编辑:程序博客网 时间:2024/06/04 23:34
1. 条件测试
每条if 语句的核心都是一个值为True 或False 的表达式,这种表达式被称为条件测试 。
- ==, !=, <, <=, >, >=
- and, or
- 检查特定值是否包含在列表中, in / 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.")
2. if 语句
(1).简单的if 语句
age = 19if age >= 18: print("You are old enough to vote!")
(2).if-else 语句
age = 17if age >= 18: print("You are old enough to vote!") print("Have you registered to vote yet?")else: print("Sorry, you are too young to vote.") print("Please register to vote as soon as you turn 18!")
(3).if-elif-else 结构
age = 12if age < 4: print("Your admission cost is $0.")elif age < 18: print("Your admission cost is $5.")else: print("Your admission cost is $10.")
(4).使用多个elif 代码块
age = 12if age < 4: price = 0elif age < 18: price = 5elif age < 65: price = 10else: price = 5print("Your admission cost is $" + str(price) + ".")
(5).省略else 代码块
Python并不要求if-elif 结构后面必须有else 代码块。
3. 使用if 语句处理列表
确定列表不是空的
在if 语句中将列表名用在条件表达式中时,Python将在列表至少包含一个元素时返回True ,并在列表为空时返回False 。
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?")
0 0
- Python -- 4. if()操作
- python BIF /操作符 / If else / assert
- struts2 if标签操作
- smarty if 操作符
- <s:if> 字符串操作
- Python的if语句
- Python-if语句
- python学习:if 语法
- Python if and input
- python if语句语法
- python if语句
- python——if
- python if while用法
- 【Python】if.. elif..
- Python-if语句
- python 多重if
- Python学习:if语句
- Python-if语句
- Android中SoundPool播放音频示例
- java 基础知识一 初识java
- 面向对象的“形”与“本”
- CSMA/CA与CSMA/CD
- 一步一步超级详细的zabbix安装教程
- Python -- 4. if()操作
- 解决Maven项目Update Project自动恢复为JRE1.5的问题
- mysql数据库文件位置
- POJ 3488 Arne Saknussemm 可能会
- AWR解读
- (Android新手上路)actionbar完全自定义标题布局,告别繁琐的actionbar设置,看别人的然后自己琢磨出来的
- 常量函数成员奈我何
- Hive扩展功能(三)--使用UDF函数将Hive中的数据插入MySQL中
- android 调用第三方QQ、微信、新浪微博、腾讯微博等聊天方法