Python运算子

Python operators allow us to do common processing on variables. We will look into different types of python operators with examples and also operator precedence.

Python运算符允许我们对变量进行通用处理。 我们将通过示例和运算符优先级研究不同类型的python运算符。

Python运算子 (Python Operators)

Python Operators are the special symbols that can manipulate values of one or more operands.

Python运算符是可以操纵一个或多个操作数的值的特殊符号。

Python运算子类型 (Python Operator Types)

Python operators can be classified into several categories.

Python运算符可分为几类。

  • Arithmetic Operators

    算术运算符
  • Logical Operators

    逻辑运算符
  • Comparison Operators

    比较运算符
  • Bitwise Operators

    按位运算符
  • Assignment Operators

    赋值运算符

Python算术运算符 (Python Arithmetic Operators)

OperatorDescriptionExample
+used to add two numberssum = a + b
used for subtractiondifference = a – b
*used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times.mul = a*b

>>> “Hi”*5
‘HiHiHiHiHi’

/used to divide two numbersdiv = b/a
%modulus operator, returns the remainder of divisionmod = a%b
**exponent operator
操作员 描述
+ 用于加两个数字 总和= a + b
用于减法 差异= a – b
* 用于将两个数字相乘。 如果将字符串和int相乘,则将字符串重复int次。 mul = a * b

>>>“嗨” * 5
'HiHiHiHiHi'

/ 用于除以两个数字 div = b / a
模运算符,返回除法的余数 mod = a%b
** 指数算子
#create two variables
a=100
b=200

# addition (+) operator
print(a+b) 

# subtraction (-) operator
print(a-b) 

# multiplication (*) operator
print(a*b)

# division (/) operator
print(b/a)

# modulus (%) operator
print(a%b) # prints the remainder of a/b

# exponent (**) operator
print(a**b) #prints a^b

Output:

输出:

Python Arithmetic Operators

Python Arithmetic Operators

Python算术运算符

Python比较运算符 (Python Comparison Operators)

OperatorDescriptionExample
==returns True if two operands are equal, otherwise False.flag = a == b
!=returns True if two operands are not equal, otherwise False.flag = a != b
>returns True if left operand is greater than the right operand, otherwise False.flag = a > b
<returns True if left operand is smaller than the right operand, otherwise False.flag = a < b
>=returns True if left operand is greater than or equal to the right operand, otherwise False.flag = a > b
<=returns True if left operand is smaller than or equal to the right operand, otherwise False.flag = a < b
操作员 描述
== 如果两个操作数相等,则返回True,否则返回False。 标志= a == b
!= 如果两个操作数不相等,则返回True,否则返回False。 标志= a!= b
> 如果左操作数大于右操作数,则返回True,否则返回False。 标志= a> b
< 如果左操作数小于右操作数,则返回True,否则返回False。 标志= a <b
> = 如果左操作数大于或等于右操作数,则返回True,否则返回False。 标志= a> b
<= 如果左操作数小于或等于右操作数,则返回True,否则返回False。 标志= a <b
# create two variables
a=100
b=200

# (==) operator, checks if two operands are equal or not
print(a==b)

# (!=) operator, checks if two operands are not equal
print(a!=b)

# (>) operator, checks left operand is greater than right operand or not
print(a>b)

# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)

# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)
Python Comparison Operators

Python Comparison Operators

Python比较运算符

Python按位运算符 (Python Bitwise Operators)

OperatorDescriptionExample
&Binary AND Operatorx = 10 & 7 = 2
|Binary OR Operatorx = 10 | 7 = 15
^Binary XOR Operatorx = 10 ^ 7 = 13
~Binary ONEs Compliment Operatorx = ~10 = -11
<<Binary Left Shift operatorx = 10<<1 = 20
>>Binary Right Shift Operatorx = 10>>1 = 5
操作员 描述
二进制与运算符 x = 10&7 = 2
| 二进制或运算符 x = 10 | 7 = 15
^ 二进制XOR运算符 x = 10 ^ 7 = 13
二进制ONE补码运算符 x =〜10 = -11
<< 二进制左移运算符 x = 10 << 1 = 20
>> 二元右移运算符 x = 10 >> 1 = 5
#create two variables
a=10 # binary 1010
b=7  # binary 0111

# Binary AND (&) operator, done binary AND operation
print(a&b)

# Binary OR (|) operator, done binary OR operation
print(a|b)

# Binary XOR (^) operator, done binary XOR operation
print(a^b)

# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)

# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1) 
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)
Python Bitwise Operators

Python Bitwise Operators

Python按位运算符

Python逻辑运算符 (Python Logical Operators)

OperatorDescriptionExample
andLogical AND Operatorflag = exp1 and exp2
orLogical OR Operatorflag = exp1 or exp2
notLogical NOT Operatorflag = not(True) = False
操作员 描述
逻辑与运算符 标志= exp1和exp2
要么 逻辑或运算符 标志= exp1或exp2
逻辑非运算符 标志=不(True)= False
#take user input as int
a=int(input())

# logical AND operation

if a%4==0 and a%3==0:
    print("divided by both 4 and 3")

# logical OR operation
if a%4==0 or a%3==0:
    print("either divided by 4 or 3")

# logical NOT operation
if not(a%4==0 or a%3==0):
    print("neither divided by 4 nor 3")
Python Logical Operators

Python Logical Operators

Python逻辑运算符

Python赋值运算符 (Python Assignment Operators)

OperatorDescription
+=a+=b is equivalent to a=a+b
*=a*=b is equivalent to a=a*b
/=a/=b is equivalent to a=a/b
%=a%=b is equivalent to a=a%b
**=a**=b is equivalent to a=a**b (exponent operator)
//=a//=b is equivalent to a=a//b (floor division)
操作员 描述
+ = a + = b等效于a = a + b
* = a * = b等效于a = a * b
/ = a / = b等效于a = a / b
%= a%= b等效于a = a%b
** = a ** = b等效于a = a ** b(指数运算符)
// = a // = b等于a = a // b(底数除法)
# take two variable, assign values with assignment operators
a=3
b=4

print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a+b
a+=b

print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a**b ( exponent operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))

# it is equivalent to a=a//b ( floor division)
a//=b
print("a: "+str(a))
print("b: "+str(b))
Python Assignment Operators

Python Assignment Operators

Python赋值运算符

Python运算符优先级 (Python Operator Precedence)

Precedence of python operators means the priority level of operators. This becomes vital when an expression has multiple operators in it. For example consider the following expression:

python运算符的优先级是指运算符的优先级。 当表达式中包含多个运算符时,这一点至关重要。 例如,考虑以下表达式:

>>> 2+3*4

Now, what do you think the series of operation would be? We can add 2 and 3, then multiply the result by 4. Also, we can multiply 3 and 4 first, then add 2 with it. Here we can see that the operators’ precedence is important.

现在,您认为这一系列操作是什么? 我们可以将2和3相加,然后将结果乘以4。此外,我们可以先将3和4相乘,然后再加上2。 在这里我们可以看到,运算符的优先级很重要。

Below is a list of operators indicating the precedence level. It’s in descending order. That means the upper group has more precedence than that of the lower group.

以下是指示优先级的运算符列表。 它是降序排列的。 这意味着上层人群比下层人群具有更高的优先级。

  1. Parenthesis – ()

    括号– ()
  2. Exponentiation – **

    求幂– **
  3. Compliment, unary plus and minus – ~, +, -

    恭维,一元加减~+-
  4. Multiply, Divide, modulo – *, /, %

    乘,除,取模– */%
  5. Addition and Subtraction – +, -

    加减法– +-
  6. Right and Left Shift – >>, <<

    左右移动– >><<
  7. Bitwise AND – &

    按位与– &
  8. Bitwise OR and XOR – |, ^

    按位“或”和“异或” – |^
  9. Comparison Operators – ==, !=, >, <, >=, <=

    比较运算符– ==!=><>=<=
  10. Assignment Operator- =

    分配运算符- =
GitHub Repository. GitHub存储库中签出更多Python示例。

Reference: Official Python Documentation

参考: 官方Python文档

翻译自: https://www.journaldev.com/14082/python-operators

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值