In [1]: a =20
In [2]: b =5
In [3]: a < b
Out[3]:False#逻辑判断的结果布尔值
In [4]: a >= b
Out[4]:True
In [5]: a ==20#等于20 为真
Out[5]:True
In [6]: b !=10#不等于10 为真
Out[6]:True
In [7]:int(True)#布尔值 转换 为整型
Out[7]:1
In [8]:int(False)
Out[8]:0
In [9]:bool(10)#只要是非零 布尔值都为真
Out[9]:True
In [10]:bool(0)
Out[10]:False
In [11]:bool(-1)#包括负数 也是真
Out[11]:True
In [12]:bool('a')#包括字符,浮点
Out[12]:True
In [13]:import random
In [14]: random.randint(0,100)#不超过0 100范围的随机数
Out[14]:71
In [15]: random.randint(50,100)
Out[15]:90
import random #判断人工计算两个随机数相机是否有误
a = random.randint(0,1000)
b = random.randint(0,1000)
sum1 = a + b
sum2 =eval(input(str(a)+" + "+str(b)+" = "))#有双引号的符号是字符,没有双引号的+是运算符,把字符拼接起来,数字要变成字符才可以拼接print("the answer is ", sum1 == sum2)
runfile('C:/pycode/untitled0.py', wdir='C:/pycode')600+768=110
the answer isFalse
In [16]: a =1#if语句,注意格式 4个空格
In [17]: b =10
In [18]:if a < b:...:print("a<b")...:
a<b
import random #if的另一个示例
a = random.randint(0,10)print('a is',a)
b = random.randint(0,10)print('b is',b)if a > b:
a, b = b, a
print('a, b now is',a,b)
runfile('C:/pycode/untitled2.py', wdir='C:/pycode')
a is1
b is0
a, b now is01
import random #if - else的示例
a = random.randint(0,100)if a >50:print("a is greater than 50.")else:print("a is smaller than 50.")print('a is',a)
runfile('C:/pycode/untitled3.py', wdir='C:/pycode')
a is smaller than 50.
a is5
import random #elif的示例
a = random.randint(0,10)if a >5:print("a > 5, a is",a)elif a <5:print("a < 5, a is",a)else:print("a = 5")
runfile('C:/pycode/untitled4.py', wdir='C:/pycode')
a =5
a =1
b =5
c =3if c > a:#if的嵌套if c > b:print("c greater than a and b")else:print("c greater than a, but not greater than b")else:print("c not greater than a and b")
runfile('C:/pycode/untitled5.py', wdir='C:/pycode')
c greater than a, but not greater than b
n =eval(input("enter a number: "))if(n %2==0)and(n %3==0):#逻辑和,两个条件都达到 为真print(n,"is divisible by 2 and 3")if(n %2==0)or(n %3==0):#逻辑或,达到一个条件 为真print(n,"is divisible by 2 or 3")if(not(n %2==0))and(not(n %3==0)):#not 逻辑否;不能被2整除,也不能被3整除print(n,"is not divisible by 2 and 3")
runfile('C:/pycode/untitled6.py', wdir='C:/pycode')
enter a number:66is divisible by 2and36is divisible by 2or3
In [23]: x =-9
In [25]:if x >0:#条件表达式,达到实行后面的语句...: y =10...:else:...: y =20...:
In [26]:print(y)20
In [27]: y =(10if x >0else20)#一句也可以写完
In [28]:print(y)20
In [29]: n=eval(input("enter a number:"));print("n is an even number"if n %2==0else"n is an odd numbere")#可读性差一些
enter a number:123
n is an odd numbere
import random #一个彩票程序,输入数字等于随机数获奖,倒过来的二奖,中一个三奖
rn = random.randint(10,99)
un =eval(input("enter your number(10-99): "))print("the random number is: ", rn)
rn1 = rn //10#十 位
rn2 = rn %10#个 位
un1 = un //10
un2 = un %10if rn == un:print("you get 10000")elif(rn1 == un2)and(rn2 == un1):print("you get 5000")elif(rn1 == un1)or(rn1 == un2)or(rn2 == un1)or(rn2 == un2):print("you get 1000")else:print("sorry, you get nothing")
runfile('C:/pycode/untitled7.py', wdir='C:/pycode')
enter your number(10-99):45
the random number is:49
you get 1000