python中基本运算符_Python中的基本运算符及示例

python中基本运算符

Operators are symbols which tells the interpreter to do a specific operation such as arithmetic, comparison, logical, and so on.

运算符是符号,它们告诉解释器执行特定的操作,例如算术,比较,逻辑等。

The different types of operators in Python are listed below:

以下列出了Python中不同类型的运算符:

  1. Arithmetic Operators

    算术运算符
  2. Relational Operators

    关系运算符
  3. Bitwise Operators

    按位运算符
  4. Assignment Operators

    赋值运算符
  5. Logical Operators

    逻辑运算符
  6. Membership Operators

    会员运营商
  7. Identity Operators

    身份运算符

算术运算符 (Arithmetic Operators)

An arithmetic operator takes two operands as input, performs a calculation and returns the result.

算术运算符将两个操作数作为输入,执行计算并返回结果。

Consider the expression, “a = 2 + 3”. Here, 2 and 3 are the operands and + is the arithmetic operator. The result of the operation is stored in the variable a.

考虑表达式“ a = 2 + 3” 。 在这里, 23操作数+算术运算符 。 运算结果存储在变量a

OperatorDescriptionUsage
+Performs Addition on the operands12 + 3 = 15
-Performs Subtraction on the operands12 - 3 = 9
*Performs Multiplication on the operands12 * 3 = 36
/Performs Division on the operands12 / 3 = 4
%Performs a Modulus on the operands16 % 3 = 1
**Performs an Exponentiation operation12 ** 3 = 1728
//Performs a Floor Division operation18 // 5 = 3
操作员 描述 用法
+ 对操作数执行加法 12 + 3 = 15
-- 对操作数执行减法 12-3 = 9
* 对操作数执行乘法 12 * 3 = 36
/ 对操作数执行除法 12/3 = 4
对操作数执行模量 16%3 = 1
** 执行幂运算 12 ** 3 = 1728
// 执行楼层划分操作 18 // 5 = 3

Note: To get the result in floating type, one of the operands must also be of float type.

注意:要以浮点类型获取结果,操作数之一也必须是浮点类型。

关系运算符 (Relational Operators)

A relational operator is used to compare two operands to decide a relation between them. It returns a boolean value (true or false) based on the condition.

关系运算符用于比较两个操作数,以确定它们之间的关系。 它根据条件返回布尔值(真或假)。

OperatorDescriptionUsage
>Returns True if the left operand is greater than the right operand12 > 3 returns True
<Returns True if the right operand is greater than the left operand12 < 3 returns False
==Returns True if both the operands are equal12 == 3 returns False
>=Returns True if the left operand is greater than or equal to the right operand12 >= 3 returns True
<=Returns True if the right operand is greater than or equal to the left operand12 <= 3 returns False
!=Returns True if both the operands are not equal12 != 3 returns True
操作员 描述 用法
> 如果左操作数大于右操作数,则返回True 12> 3返回True
< 如果右操作数大于左操作数,则返回True 12 <3返回False
== 如果两个操作数相等,则返回True 12 == 3返回False
> = 如果左操作数大于或等于右操作数,则返回True 12> = 3返回True
<= 如果右操作数大于或等于左操作数,则返回True 12 <= 3返回False
!= 如果两个操作数都不相等,则返回True 12!= 3返回True

按位运算符 (Bitwise Operators)

A bitwise operator performs operations on the operands bit by bit

按位运算符逐位对操作数执行运算

Consider a = 2 (in binary notation, 10) and b = 3 (in binary notation, 11) for the below usages.

对于以下用法,请考虑a = 2(以二进制符号10)和b = 3(以二进制符号11)。

OperatorDescriptionUsage
&Performs bitwise AND operation on the operandsa & b = 2 (Binary: 10 & 11 = 10)
|Performs bitwise OR operation on the operandsa | b = 3 (Binary: 10 | 11 = 11)
^Performs bitwise XOR operation on the operandsa ^ b = 1 (Binary: 10 ^ 11 = 01)
~Performs bitwise NOT operation on the operand. Flips every bit in the operand~a = -3 (Binary: ~(00000010) = (11111101))
>>Performs a bitwise right shift. Shifts the bits of left operand, right by the number of bits specified as the right operanda >> b = 0 (Binary: 00000010 >> 00000011 = 0)
<<Performs a bitwise left shift. Shifts the bits of left operand, left by the number of bits specified as the right operanda << b = 16 (Binary: 00000010 << 00000011 = 00001000)
操作员 描述 用法
对操作数执行按位与运算 a&b = 2(二进制:10&11 = 10)
| 对操作数执行按位或运算 一个| b = 3(二进制:10 | 11 = 11)
^ 对操作数执行按位XOR运算 a ^ b = 1(二进制:10 ^ 11 = 01)
对操作数执行按位NOT运算。 翻转操作数中的每一位 〜a = -3(二进制:〜(00000010)=(11111101))
>> 执行按位右移。 将左操作数的位数向右移动指定为右操作数的位数 a >> b = 0(二进制:00000010 >> 00000011 = 0)
<< 执行按位左移。 将左操作数的位数向左移动指定为右操作数的位数 a << b = 16(二进制:00000010 << 00000011 = 00001000)

赋值运算符 (Assignment Operators)

An assignment operator is used to assign values to a variable. This is usually combined with other operators (like arithmetic, bitwise) where the operation is performed on the operands and the result is assigned to the left operand.

赋值运算符用于为变量赋值。 通常将此方法与其他运算符(如算术,按位运算符)结合使用,在这些运算符上对操作数执行操作,并将结果分配给左操作数。

Consider the following examples,a = 18. Here = is an assignment operator, and the result is stored in variable a.a += 10. Here += is an assignment operator, and the result is stored in variable a. This is same as a = a + 10.

考虑以下示例, a = 18=是赋值运算符,结果存储在变量a中。 一个+ = 10+=是赋值运算符,结果存储在变量a中。 这与a = a + 10相同。

OperatorDescription
=a = 5. The value 5 is assigned to the variable a
+=a += 5 is equivalent to a = a + 5
-=a -= 5 is equivalent to a = a - 5
*=a *= 3 is equivalent to a = a * 3
/=a /= 3 is equivalent to a = a / 3
%=a %= 3 is equivalent to a = a % 3
**=a **= 3 is equivalent to a = a ** 3
//=a //= 3 is equivalent to a = a // 3
&=a &= 3 is equivalent to a = a & 3
|=a |= 3 is equivalent to a = a | 3
^=a ^= 3 is equivalent to a = a ^ 3
>>=a >>= 3 is equivalent to a = a >> 3
<<=a <<= 3 is equivalent to a = a << 3
操作员 描述
= a =5。将值5分配给变量a
+ = a + = 5等效于a = a + 5
-= a-= 5等效于a = a-5
* = a * = 3等效于a = a * 3
/ = / = 3等效于a = a / 3
%= a%= 3等效于a = a%3
** = a ** = 3等效于a = a ** 3
// = a // = 3等价于a = a // 3
&= a&= 3等效于a = a&3
| = | = 3等于a = a | 3
^ = a ^ = 3等效于a = a ^ 3
>> = a >> = 3等效于a = a >> 3
<< = a << = 3等效于a = a << 3

逻辑运算符 (Logical Operators)

A logical operator is used to make a decision based on multiple conditions. The logical operators used in Python are and, or and not.

逻辑运算符用于根据多个条件做出决策。 Python中使用的逻辑运算符为andornot

OperatorDescriptionUsage
andReturns True if both the operands are Truea and b
orReturns True if any one of the operands are Truea or b
notReturns True if the operand is Falsenot a
操作员 描述 用法
如果两个操作数均为True,则返回True a和b
要么 如果任何一个操作数为True,则返回True a或b
如果操作数为False,则返回True 不是

会员运营商 (Membership Operators)

A membership operator is used to identify membership in any sequence (lists, strings, tuples).

成员资格运算符用于标识任何顺序(列表,字符串,元组)的成员资格。

in and not in are membership operators.

成员运算符是innot in

in returns True if the specified value is found in the sequence. Returns False otherwise.

如果在序列中找到指定的值,则in返回True。 否则返回False。

not in returns True if the specified value is not found in the sequence. Returns False otherwise.

如果在序列中找不到指定的值,则not in返回True。 否则返回False。

a = [1,2,3,4,5]
  
#Is 3 in the list a?
print 3 in a # prints True 
  
#Is 12 not in list a?
print 12 not in a # prints True
  
str = "Hello World"
  
#Does the string str contain World?
print "World" in str # prints True
  
#Does the string str contain world? (note: case sensitive)
print "world" in str # prints False  

print "code" not in str # prints True

身份运算符 (Identity Operators)

An identity operator is used to check if two variables share the same memory location.

身份运算符用于检查两个变量是否共享相同的内存位置。

is and is not are identity operators.

isis not身份运算符。

is returns True if the operands refer to the same object. Returns False otherwise.

如果操作数引用相同的对象,则is返回True。 否则返回False。

is not returns True if the operands do not refer to the same object. Returns False otherwise.

如果操作数未引用同一对象,则is not返回True。 否则返回False。

Please note that two values when equal, need not imply they are identical.

请注意,两个值相等时,不一定意味着它们是相同的。

a = 3
b = 3  
c = 4
print a is b # prints True
print a is not b # prints False
print a is not c # prints True

x = 1
y = x
z = y
print z is 1 # prints True
print z is x # prints True

str1 = "FreeCodeCamp"
str2 = "FreeCodeCamp"

print str1 is str2 # prints True
print "Code" is str2 # prints False

a = [10,20,30]
b = [10,20,30]

print a is b # prints False (since lists are mutable in Python)

翻译自: https://www.freecodecamp.org/news/basic-operators-in-python-with-examples/

python中基本运算符

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值