python基础知识之条件语句

1 条件和条件语句

1.1 布尔变量

在作为布尔表达式时,解释器会将下面值看作假:
False None 0    “"  ()  []   {}
其中0包括任意类型的数字0,包括浮点型、长整型等,空字符串、空元组、空序列、空字典都为假。其他一切都为真。

>>> True
True
>>> False
False
>>> True==1
True
>>> False==0
True
>>> True+False+42
43
>>>   

布尔值 Ture 和 False 都是布尔类型,bool函数用来转换其他值。

>>> bool('fine')
True
>>> bool(42)
True
>>> bool('')
False
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool([])
False
>>> bool({})
False
>>> bool(())
False
>>>  

1.2 条件执行和 if语句

if 语句实现条件执行。条件为真,执行后面的语句块;条件为假,不执行后面的语句块。条件为 if 和 冒号:之间的表达式。
脚本里输入:如果用户以 wang为结尾,那么 if 进入语句块,打印问候语。

name=raw_input('what is your name?')
if name.endswith('wang'):
    print 'hello,Mrs.wang'

shell里输出:

>>> =============== RESTART ================
>>> 
what is your name?wang
hello,Mrs.wang
>>> 

1.3 else 子句

在上一节例子中用 else子句增加一种选择。之所以叫子句是因为它是 if 语句的一部分,不是独立语句。

name=raw_input('what is your name?')
if name.endswith('wang'):
    print 'hello,Mrs.wang'
else:
    print 'hello,stranger'
>>> ===================== RESTART =====================
>>> 
what is your name?cui
hello,stranger
>>> ===================== RESTART =====================
>>> 
what is your name?wang
hello,Mrs.wang
>>> 

1.4 elif 子句

elif 是else if 的缩写。
脚本输入:

num=input('enter the number:')
if num > 0:
    print 'the number is positive'
elif num < 0:
    print 'the number is negative'
else :
    print 'the number is zero'

shell 输出

>>> ======================= RESTART ======================
>>> 
enter the number:0
the number is zero
>>> ======================== RESTART =====================
>>> 
enter the number:9
the number is positive
>>> ====================== RESTART =======================
>>> 
enter the number:-1
the number is negative
>>> 

现在注意我将输入函数变为 raw_input后,shell中的输出为:

>>> ===================== RESTART =====================
>>> 
enter the number:9
the number is positive
>>> ====================== RESTART ====================
>>> 
enter the number:-1
the number is positive
>>> =================== RESTART ======================
>>> 
enter the number:0
the number is positive
>>> 

发现无论你输入什么数字,结果永远显示该数是正数。
原因就在 input 和 raw_input 的区别:

>>> num=raw_input('enter the number:')
enter the number:9
>>> num
'9'
>>> num=input('enter the number:')
enter the number:9
>>> num
9
>>> 

raw_input 输入的9被存储为字符‘9’,同理‘0’、‘9’、‘-1’都成字符数了,所以 num>0始终成立。

>>> if '9'>0:print 1

1
>>> if '0'>0:print 1

1
>>> if '-1'> 0:print 1

1
>>> 

这样大家对input 和 raw_input 的区别更理解了吧。

1.5 if 语句嵌套 if语句

脚本里输入:

name=raw_input('what is your name?')
if name.endswith('wang'):
    if name.startswith('Mr.'):
        print 'hello,Mr.wang'
    elif name.startswith('Mrs.'):
        print 'hello,Mrs.wang'
    else:
        print 'hello,wang'
else:
    print 'hello,stanger'

shell输出:

>>> ================== RESTART =====================
>>> 
what is your name?
>>> ====================== RESTART ================
>>> 
what is your name?Mr.wang
hello,Mr.wang
>>> ===================== RESTART ================
>>> 
what is your name?Mrs.wang
hello,Mrs.wang
>>> ===================== RESTART ================
>>> 
what is your name?wang
hello,wang
>>> ================= RESTART ====================
>>> 
what is your name?cui
hello,stanger
>>> 

1.6 复杂的条件

1. 比较运算符

x==y                   # x等于y
x<y                    # x小于y
x>y                    # x大于y
x>=y                   # x大于等于y
x<=y                   # x小于等于y
x!=y                   # x不等于y
x is y                 # x和y是同一个对象
x is not y             # x和y不是同一个对象
x in y                 # x 是y的成员
x not in y             #x不是y 的成员

比较运算符和赋值运算符可以连接使用,如

0<age<10

2. 相等运算符 ==

>>> 'foo'=='foo'
True
>>> 'foo'=='faa'
False
>>> 

3. 同一性运算符 is
同一性和相等是不同的,两个变量被绑定到同一个列表时,这两个变量才是同一性。

>>> x=y=[1,2,3] # x 与 y绑定在同一个列表上
>>> z=[1,2,3]   # z绑定在另一列表上,该列表与上一个列表相等但不同一
>>> x==y
True
>>> x==z
True
>>> y==z
True
>>> x is y
True
>>> x is z
False
>>> y is x
True
>>> y is z
False
>>> x=[1,2,3]  
>>> y=[2,4]     #开始x和y绑定列表就不同,虽然结果列表相同但不同一
>>> x is not y
True
>>> del x[2]
>>> x
[1, 2]
>>> y[1]=1
>>> y
[2, 1]
>>> y.reverse()
>>> y
[1, 2]
>>> x==y
True
>>> x is y
False
>>> 

4. in 成员资格运算符

条件语句中的 in 的使用。
脚本中输入:

name=raw_input('what is your name?')
if 's' in name:
    print 'your name contains letter "s"'
else:
    print 'your name does not contain letter "s"'

shell输出:

>>> ====================== RESTART ====================
>>> 
what is your name?wang
your name does not contain letter "s"
>>> ====================== RESTART ====================
>>> 
what is your name?san
your name contains letter "s"
>>> 

5. 字符串和序列比较

>>> 'abc'<'bbc'
True
>>> 'abc'<'acc'
True
>>> 'abc'<'abd'
True
>>> 

6. 布尔运算符 and or not
脚本输入:

number=input('enter a number between 1 and 10:')
if number <=10 and number >=1:
    print 'ok'
else:
    print 'no'

shell输出:

>>> ============= RESTART ========================
>>> 
enter a number between 1 and 10:0
no
>>> ============ RESTART =======================
>>> 
enter a number between 1 and 10:6
ok
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值