python运算符and_Python AND运算子

python运算符and

Python operators can be divided into multiple categories. Two of them are – bitwise operators and logical operators. Bitwise operator performs operations on integers in binary format whereas logical operators perform operations on boolean values.

Python运算符可以分为多个类别。 其中两个是–按位运算符和逻辑运算符。 按位运算符对二进制格式的整数执行运算,而逻辑运算符对布尔值执行运算。

Python AND运算子 (Python AND Operator)

There are two types of Python AND Operators.

有两种类型的Python AND运算符。

  1. Bitwise AND Operator: It’s denoted by & and work with integers. The numbers are converted into binary format and bitwise AND operation is performed. Finally, the output is returned in the decimal format.

    按位AND运算符 :用&表示,并与整数一起使用。 将数字转换为二进制格式,然后执行按位与运算。 最后,输出以十进制格式返回。
  2. Logical AND Operator: It’s denoted by and and work with boolean values. The output is boolean – either True or False.

    逻辑AND运算符 :用and表示, and与布尔值一起使用。 输出为布尔值TrueFalse

Python按位与运算符示例 (Python Bitwise AND Operator Example)

Let’s look at an example of bitwise and operator. We will ask the user to enter two numbers and print their binary and operation output.

让我们看一下按位和运算符的示例。 我们将要求用户输入两个数字并打印其二进制和运算输出。

a = int(input('Please enter an integer:\n'))
b = int(input('Please enter another integer:\n'))

print(f'{a} in binary is {str(bin(a))[2:]}')
print(f'{b} in binary is {str(bin(b))[2:]}')
print(f'Binary AND of {a} and {b} is {a&b}')

Output:

输出

Python And Operator Bitwise Binary Integers

Python And Operator – Bitwise

Python和运算符–按位

Python逻辑AND运算子范例 (Python Logical AND Operator Example)

Let’s look at an example of logical and operator. We will ask the user to enter a single digit number and print if it’s positive or negative. If the user doesn’t enter single digit integer, then we will print it too.

让我们看一个逻辑和运算符的例子。 我们将要求用户输入一个数字并打印它的正数还是负数。 如果用户没有输入一位整数,那么我们也将其打印出来。

x = int(input('Please enter a single digit integer:\n'))

if x > 0 and x < 10:
    print('You entered positive single digit number')
elif x  -10:
    print('You entered negative single digit number')
else:
    print('You did not entered single digit integer')

Output:

输出

Python Logical And Operator

Python Logical And Operator

Python逻辑和运算符

摘要 (Summary)

Python provides a lot of operators to work with different kinds of data types. Here we learned how to use Python and operators with boolean and bitwise binary integers.

Python提供了许多运算符来处理不同类型的数据类型。 在这里,我们学习了如何对布尔值和按位二进制整数使用Python和运算符。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/26700/python-and-operator

python运算符and

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值