python - 逻辑判断

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 is  False
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 is 1
b is 0
a, b now is 0 1
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 is 5
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 = 3

if 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: 6
6 is divisible by 2 and 3
6 is divisible by 2 or 3
In [23]: x = -9

In [25]: if x > 0:  #条件表达式,达到实行后面的语句
    ...:     y = 10
    ...: else:
    ...:     y = 20
    ...:

In [26]: print(y)
20

In [27]: y = (10 if x > 0 else 20)   #一句也可以写完

In [28]: print(y)
20

In [29]: n=eval(input("enter a number:")); print("n is an even number" if n % 2 ==0 else "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 % 10
if 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值