python中基本运算符_Python中的运算符

python中基本运算符

In every programming language, operators are used for performing various types of operations on any given operand(s). They are used to evaluate different types of expressions and to manipulate the values of operands or variables by performing different operations on them.

在每种编程语言中,运算符用于对任何给定的操作数执行各种类型的运算。 它们用于评估不同类型的表达式,并通过对它们执行不同的操作来操纵操作数或变量的值。

In python, we have 7 different types of operators, they are:

在python中,我们有7种不同类型的运算符,它们是:

  1. Arithmetic Operators

    算术运算符

  2. Comparison Operators

    比较运算符

  3. Assignment Operators

    赋值运算符

  4. Logical Operators

    逻辑运算符

  5. Bitwise Operators

    按位运算符

  6. Membership Operators

    会员运营商

  7. Identity Operators

    身份运营商

While the first five are commonly used operators the last two i.e Membership and Identity operators are exclusive to python.

前五个是常用的运算符,后两个即MembershipIdentity运算符是python专有的。

Python:算术运算符 (Python: Arithmetic Operators)

Arithmetic operators are the operators used for performing arithmetic operations on numeric operands like division, subtraction, addition etc.

算术运算符是用于对数字操作数执行算术运算的运算符,例如除法,减法,加法等。

Following are different arithmetic operators:

以下是不同的算术运算符:

OperatorUsageDescription
+a + bAdds values on either side of the operator.
-a - bSubtracts the right hand operand from the left hand operand.
*a*b
To multiply values on either side of the operator.
/a/bDivides left hand operand by right hand operand(returns float value).
%a % bReturns the remainder from dividing left hand operand by right hand operand.
**a**bReturns Exponent – left operand raised to the power of right
//a//bFloor Division – The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero(towards negative infinity).
操作员 用法 描述
+ a + b 在运算符的任一侧添加值。
- a - b 从左手操作数中减去右手操作数。
* a*b
在运算符的任一侧乘以值。
/ a/b 将左操作数除以右操作数(返回浮点值)。
% a % b 返回左操作数除以右操作数的余数。
** a**b 返回指数-左操作数提高到右方的幂
// a//b 底数除法 –操作数的除法,其结果是除去小数点后的数字的商。 但是,如果其中一个操作数为负,则结果是下限的,即从零开始舍入(朝负无穷大)。

Let's have a few code examples:

让我们看一些代码示例:

>>>3+2
5
>>>4-3
1
>>>3/2
1.5

Python:比较运算符 (Python: Comparison Operators)

These operators are used to compare the values of operands on either side of this type of operators. These operators return true or false Boolean values. They return true if the condition is satisfied otherwise the return false.

这些运算符用于比较此类运算符两侧的操作数的值。 这些运算符返回truefalse布尔值。 如果满足条件,则它们返回true ,否则返回false

Following are different comparison operators:

以下是不同的比较运算符:

OperatorUsageDescription
==a == bReturns True if the values on the either side of the operator is equal otherwise False.
!=a != bReturns True if the values on either sides of the operator is not equal to each other otherwise False.
>a > bReturns True if the value of the operand on the left of the operator is greater than the value on the right side of the operator.
<a < bReturns True if the value of the operand on the left of the operator is less than the value on the right side of the operator.
>=a >= bReturns True if the value of the operand on the left of the operator is greater than or equal to the value on the right side of the operator.
<=a <= bReturns True if the value of the operand on the left of the operator is less than or equal to the value on the right side of the operator.
操作员 用法 描述
== a == b 如果运算符两侧的值相等,则返回True,否则返回False。
!= a != b 如果运算符两侧的值彼此不相等,则返回True;否则返回False。
> a > b 如果运算符左侧的操作数的值大于运算符右侧的值,则返回True。
< a < b 如果运算符左侧的操作数的值小于运算符右侧的值,则返回True。
>= a >= b 如果运算符左侧的操作数的值大于或等于运算符右侧的值,则返回True。
<= a <= b 如果运算符左侧的操作数的值小于或等于运算符右侧的值,则返回True。

Let's have a few code examples:

让我们看一些代码示例:

>>> 3 == 2
False
>>> 3 != 2
True
>>> 2<=4
True

Python:赋值运算符 (Python: Assignment Operators)

The assignment operator is used to assign a specific value to a variable or an operand.

赋值运算符用于将特定值赋给变量或操作数。

The equal to (=) operator is used to assign a value to an operand directly, if we use any arithmetic operator (+, -, /, etc.) along with the equal to operator then it will perform the arithmetic operation on the given variable and then assign the resulting value to that variable itself.

等于( = )运算符用于直接为操作数赋值,如果我们将任何算术运算符( +-/等)与等于运算符一起使用,则它将对给定值执行算术运算变量,然后将结果值分配给该变量本身。

Let's have a few code examples:

让我们看一些代码示例:

>>> x=2
>>> x
2
>>> x+=1
>>> x
3
>>> x-=1
>>> x
2

Python:逻辑运算符 (Python: Logical Operators)

The logical operators are used to perform logical operations (like and, or, not) and to combine two or more conditions to provide a specific result i.e. true or false.

逻辑运算符用于执行逻辑运算(如不是 ),并结合两个或多个条件以提供特定的结果,即true或false。

Following are different logical operators:

以下是不同的逻辑运算符:

OperatorUsageDescription
andx and yTrue if both sides of the operator is True
orx or yTrue if either of the operand is True
notnot xComplements the operand
操作员 用法 描述
and x and y 如果运算符双方均为True,则为True
or x or y 如果任一操作数为True,则为True
not not x 补数

Python:按位运算符 (Python: Bitwise Operators)

Bitwise operator acts on the operands bit by bit. These operators take one or two operands. Some of the the bitwise operators appear to be similar to logical operators but they aren't.

按位运算符逐位作用于操作数。 这些运算符采用一个或两个操作数。 某些按位运算符似乎与逻辑运算符相似,但与之不同。

Following are different bitwise operators:

以下是不同的按位运算符:

OperatorUsageDescription
& Binary AND(a & b)Operator copies a bit to the result if it exists in both operands.
| Binary OR(a | b)
It copies a bit if it exists in either operand.
^ Binary XOR(a ^ b)It copies the bit if it is set in one operand but not both.
~ 1's complement(~a)It is unary and has the effect of 'flipping' bits.
<< Binary Left Shifta << 2The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shifta >> 2The left operands value is moved right by the number of bits specified by the right operand.
操作员 用法 描述
&二进制AND (a & b) 如果两个操作数中都存在运算符,则运算符会将其复制到结果中。
| 二进制或 (a | b)
如果在任何一个操作数中存在,它都会复制一点。
^二进制异或 (a ^ b) 如果在一个操作数中设置了该位,但不是在两个操作数中都设置了,则它将复制该位。
~的补码 (~a) 它是一元的,具有“翻转”位的作用。
<<二进制左移 a << 2 左操作数的值向左移动右操作数指定的位数。
>>二进制右移 a >> 2 左操作数的值向右移动右操作数指定的位数。

Let's have a few code examples:

让我们看一些代码示例:

>>> a = 3
>>> b = 4
>>> a = 3   # equivalent binary is 0011
>>> b = 4   # equivalent binary is 0100
>>> a & b
0
>>> a | b
7
>>> # 7 is equivalent to 0111 in binary

Python:成员运算符 (Python: Membership Operators)

The membership operators are used to test whether a value is in a specific sequence or not like in lists, tuples, string, etc. It returns True or False as output.

成员运算符用于测试值是否按特定顺序排列,例如列表,元组,字符串等。它返回True或False作为输出。

Following are different membership operators:

以下是不同的成员运算符:

OperatorUsageDescription
inx in yTrue if the value/operand in the left of the operator is present in the sequence in the right of the operator.
not inx not in yTrue if the value/operand in the left of the operator is not present in the sequence in the right of the operator.
操作员 用法 描述
in x in y 如果运算符左侧的值/运算符出现在运算符右侧的序列中,则为true。
not in x not in y 如果运算符左侧的序列中不存在运算符左侧的值/操作数,则为True。

Let's have a few code examples:

让我们看一些代码示例:

>>> lst=[2,3,6,7,9,1]
>>> x=2
>>> y=5
>>> x in lst
True
>>> y in lst
False

Python:身份运算符 (Python: Identity Operators)

The identity operators are used to test whether a variable refers to same value/object or not. It returns True or False as output.

身份运算符用于测试变量是否引用相同的值/对象。 它返回True或False作为输出。

Following are different identity operators:

以下是不同的身份运算符:

OperatorUsageDescription
isx is TrueTrue if both the operands refer to the same object.
is notx is not TrueEvaluates to false if the variables on either side of the operator point to the same object and true otherwise.
操作员 用法 描述
is x is True 如果两个操作数都引用同一个对象,则为true。
is not x is not True 如果运算符两侧的变量指向同一个对象,则结果为false,否则为true。

Let's have a few code examples:

让我们看一些代码示例:

>>> x=4
>>> y=4
>>> z=2
>>> x is y
True
>>> z is x
False

翻译自: https://www.studytonight.com/python/operators-in-python

python中基本运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值