w1w 发表于 2017-5-1 09:59:14

python入门(三)条件表达式

python入门(三)条件表达式

参考书籍 python_note_pdq.pdf
4.1 布尔表达式
真值为1,假值为0
4.2 逻辑操作符
and or not
a = 1
b = 0
print(a and b)   ---------- 0
print(a or b)    ---------- 1
print(not a)   ----------- False
if(a or b) :
print(a or b)------------1

空字符串为假,非空字符串为真,非零的数为真
and左边为真就显示右边的值
print("9" and "test")   ----------test
print("test" and "haha")-----haha
or 显示真值
print("9" or "test")         -----9
print("test" or "9")         -----test
print(0 or "test")             ------test
print("" or "9")                ------9
print("===",0 or "","====")---- ============
not 相反
print(not 0)               ------- True
print(not "")      ------True

4.3 条件语句
Header:
    First Statement
    Second Statement
    ......
    Last Statement
Other Statement
eg:
x = 16
if(x % 2 ):
    print(x," is even")
else :
    print(x," is odd")--------16 is even

eg:
x = 15
y = 12
if(x > y):
    print("big")
elif(x == y):
    print("equal")
else:
    print("small")    ---------big

4.4 while语句
#中文注释
x = 15
result = 0
while(x):
    result = result + x
    x = x - 1
print(result)

4.5 条件嵌套
eg1:
x = 12
y = 11
if(x == y):
    print("equals")
else:
    if(x > y):
      print("large")
    else:
      print("small");   --------large
eg2:
x = 11
if(0<x<20):
    print(x,"介于0--20之间。") ------- 11 介于0--20之间。

4.6 return语句
4.7 键盘输入
raw_input 报错如下:
NameError: name 'raw_input' is not defined
原来是python的后续版本将名字修改了为input了。
eg1:
name = input("please input something:")
print(name)
返回:
please input something:sillycat
sillycat
页: [1]
查看完整版本: python入门(三)条件表达式