Python 之if条件判断语句
2019-06-27 21:52
337 查看
1.if 用法举例:
if语句写法: if 表达式: xxxx
(1)条件为真true (非空的量(string,tuple,list ,set,dictonary),所有非零的数):
if 1: print 'hello world!' print 'True' if 'aaa': print 'hello world!' print 'True'
(2)条件为假 faulse(0,None,空的量):
if 0: print 'hello world!' print 'True'if None: print 'hello world!' print 'True' if '': print 'hello world!' print 'True' if 1>2: print 'hello world!' print 'True'
(3)组合条件及其他(and /or ):
if not 1>2: print 'hello world!' print 'True' if not 1>2 and 1 == 1: print 'hello world!' print 'True'
2.if else 举例:
if else写法:
else语句: if expression: statement(s) else: statement(s) if 1 < 2: print 'hello world' else: print 'Oh,no,fourse!' print 'main'
3.if elif else写法:
elfi 语句:
if expression1: statement1(s) elif expression2: statement2(s) else: statement3(s) if 1 < 2: print 'hello world' elif 'a': print 'aaaaa' else: print 'Oh,no,fourse!'
4.举例1:
#!/usr/bin/env python score =int( raw_input(‘Please input a num:’)) if score >= 90: print 'A' print 'Very good' elif score >=80: print 'B' print 'good' elif score >=60: print 'C' print 'pass' else: print 'D'print 'END'
5.举例2:and or 应用: 多个条件下判断: 转换大小写: a.lower() a.upper()#!/usr/bin/env pythonyn = raw_input("Please input [Yes/No]:") yn = yn.lower()if yn == 'y' or yn == 'yes': print "Programe is running..."elif yn == 'n' or yn == 'no': print "Programe is exit."else: print "Error,Please input [Yes/No]"
相关文章推荐
- 【学习笔记】Python条件判断-If语句
- DayDayUP_Python自学记录[3]_Python条件判断语句(if while for)
- Python基础1:if条件判断 break/continue语句
- python基础:条件判断语句if
- python if条件判断语句
- Python基础--if条件判断语句
- 浅谈Python的条件判断语句if/else语句
- Python:条件判断if语句概述和基本用法
- Python之 If条件判断、While循环语句
- python 列表处理,for循环语句,if条件判断
- 零基础学python-2.10 条件判断 if语句
- 零基础学python-2.10 条件判断 if语句
- 零基础学python-2.10 条件判断 if语句
- Python 条件判断语句(if ,elif, else)
- 不用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)求1+2+…+n
- shell基础之二:条件判断语句if、case及read命令,
- python条件判断和循环语句
- python的for、while循环和if条件语句
- 智力题总结——求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- javascript: if语句中的判断条件,什么时候为假?