Python中and,or,&,| 以及三目运算符

a,b是数值变量

python中的&符号延续的了C/C++的含义,指的是位运算;
而用and、or来代替C/C++中的&&、||,来进行逻辑运算。

  • &, |:
#2在二进制里面是10,1在二进制中是01
1 & 2         # 输出为 0, 
1 | 2          # 输出为3
  • and 、or
    在python中,由于短路效应:a and b,a为False,b不会被判断,直接返回a;a为True,表达式的值就取决于b,因此就返回b 。or运算同理,a or b,a为Ture,b不会被判断,直接返回a;a为False,表达式的值就取决于b,因此就返回b 。
# 判断变量是否为0, 是0则为False,非0判断为True,
 # and中含0,返回0; 均为非0时,返回后一个值, 
2 and 0   # 返回0
2 and 1   # 返回1
1 and 2   # 返回2

# or中, 至少有一个非0时,返回第一个非0,
2 or 0   # 返回2
2 or 1   # 返回2
0 or 1   # 返回1 

a,b是逻辑变量

则两类的用法基本一致

(2>0) | (2<1)
 True
(2>0) and (2<1)
 False
(2>0) or (2<1)
 True
(2>0) & (2<1)
 False

三目运算符

在C/C++中,有 ? : 三目运算符,在python中,也有同样的实现

为真时的结果 if 判断条件 else 为假时的结果(没有冒号

def Sum(n):
    return n + Sum(n) if n > 0 else 0

附上Codeforces上一道题:

B. Square Filling
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0.

You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2×2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1≤x<n and 1≤y<m, and then set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1.

Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B.

Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don’t have to minimize the number of operations.

Input
The first line contains two integers n and m (2≤n,m≤50).

Then n lines follow, each containing m integers. The j-th integer in the i-th line is Ai,j. Each integer is either 0 or 1.

Output
If it is impossible to make B equal to A, print one integer −1.

Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k — the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1). The condition 0≤k≤2500 should hold.

Examples
inputCopy
3 3
1 1 1
1 1 1
0 1 1
outputCopy
3
1 1
1 2
2 2
inputCopy
3 3
1 0 1
1 0 1
0 0 0
outputCopy
-1
inputCopy
3 2
0 0
0 0
0 0
outputCopy
0
Note
The sequence of operations in the first example:

000000000→110110000→110110110→110111111

n, m = map(int, input().split())
r = [0]
a = b = 0
for i in range(n):
    c, d = int(input().replace(' ', ''), 2), 0
    for j in range(1, m):
        k = 3 << m-j-1
        if a & c & k == k:
            r[0] += 1;r += i, j; b |= k; d |= k
    if a != b: break
    a, b = c, d
print(*(r, [-1])[a != b])



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值