Python 基础(四)-if语句
2018-02-19 19:43
288 查看
推荐一个编辑器
Sublime Text 超级好用,而且可以直接运行 Python 代码,在编辑器页面按下ctrl + B即可运行,厉害了我的ST一个简单的示例
cars = ['audi', 'bmw', 'subaru', 'tiyota'] for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title())
输出结果
分析
声明一个列表,通过 for 循环判断列表中是否存在一个值和bmw一样,是的话将bmw转为大写BMW,否则将列表中其他的字符串首字母大写
检查字符串是否相等、不等
car = 'audi' car == 'bmw' 输出 False car = 'audi' car != 'bmw' 输出 True
字符串比较是区分大小写的,’audi’ 不等于’Audi’
比较数字,=,<=,>=,和Java中一样
age = 18 age == 18 输出 True
检查多个条件,and
age_0 = 22 age_1 = 18 age_0 >= 21 and age_1 >=21 输出False
检查特定值是否在列表中,是否不在列表中
cars = ['audi', 'bmw', 'subaru', 'toyota'] 'audi' in cars 输出True cars = ['audi', 'bmw', 'subaru', 'toyota'] 'mini' not in cars 输出True
if-elif-else
age = 12 if age > 4 print("大于4") elif age < 18: print("小于18") else: print("大于4小于18")
确定列表不是空的
cars = [] if cars: print("列表非空") else: print("列表是空的")
使用多个列表
cars = ['audi', 'bmw', 'subaru', 'toyota'] types = ['small', 'middle', 'big', 'toyota'] for type in types: if type in cars: print("tpyes 列表中有 cars 列表中的值") else: print("tpyes 列表没中有 cars 列表中的值")
相关文章推荐
- Python学习入门基础教程(learning Python)--3.3.4 Python的if-elif-else语句
- Python语法基础(四):if-else语句
- Python学习入门基础教程(learning Python)--3.2 if-else分支语句
- Python基础1:if条件判断 break/continue语句
- Python基础05 for,if 语句 【例1:数学计算】
- Python学习入门基础教程(learning Python)--3.1Python的if分支语句
- Python新手学习基础之条件语句——if/else语句
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- python基础学习(四)if判断语句
- 重学Python - Day 02 - python基础 -> if语句应用 + while循环应用
- Python学习入门基础教程(learning Python)--3.1Python的if分支语句 .
- python基础------if条件语句
- Python基础(2)if语句
- Python学习入门基础教程(learning Python)--3.2 if-else分支语句 .
- Python新手学习基础之条件语句——if/else语句
- Python语言基础--1(if‘、while条件语句)
- Python基础入门-IF语句
- Python新手学习基础之条件语句——if/else语句
- python基础选择结构if语句判断三角形
- 2-python基础python的基本语法之if判断语句