python 判断语句_Python 之if条件判断语句

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]"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值