Fundamentals 8 min read

Python Operators: Arithmetic, Comparison, Assignment, Logical, Membership, Identity, Bitwise, and Operator Precedence

This article explains Python’s arithmetic, comparison, assignment, logical, membership, identity, and bitwise operators, demonstrates their usage with example code, and outlines operator precedence, providing clear outputs for each operation in a concise tutorial format.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Python Operators: Arithmetic, Comparison, Assignment, Logical, Membership, Identity, Bitwise, and Operator Precedence

Arithmetic Operators

+   加法运算<br/>-   减法运算<br/>*   乘法运算<br/>/   除法运算<br/>%   模运算<br/>**  幂运算<br/>//  整除运算

Experiment Code

numOne = 10<br/>numTwo = 20<br/><br/>print("numOne + numTwo = %d" %(numOne + numTwo) )<br/>print("numOne - numTwo = %d" %(numOne - numTwo) )<br/>print("numOne * numTwo = %d" %(numOne * numTwo) )<br/>print("numOne / numTwo = %d" %(numOne / numTwo) )<br/>print("numOne % numTwo = {0}".format(numOne % numTwo ) )<br/>print("numOne ** numTwo = %d" %(numOne ** numTwo))<br/>print("numTwo  // numOne = %d" %(numTwo // numOne) )

Comparison Operators

==  等于运算符<br/>!=  不等于<br/>>   大于<br/><   小于<br/>>=  大于等于<br/><=  小于等于<br/><br/>返回值是布尔类型  True False

Experiment Code

#比较运算符<br/>number1 = 123<br/>number2 = 456<br/><br/>print("number1 == number2 is ---> {0}".format(number1 == number2))<br/>print("number1 != number2 is ---> {0}".format(number1 != number2))<br/>print("number1 > number2 is ---> {0}".format(number1 > number2))<br/>print("number1 < number2 is ---> {0}".format(number1 < number2))<br/>print("number1 >= number2 is ---> {0}".format(number1 >= number2))<br/>print("number1 <= number2 is ---> {0}".format(number1 <= number2))

Assignment Operators

=  将右侧的值分配给左侧<br/>+=  先相加然后将结果赋值给左侧<br/>-=  先相减然后将结果赋值给左侧<br/>*=  先相乘然后将结果赋值给左侧<br/>/=  先相除然后将结果赋值给左侧<br/>%=  先求模然后将结果赋值给左侧<br/>**=  先幂运算然后将结果赋值给左侧<br/>//=  先整除然后将结果赋值给左侧

Experiment Code

result = 10<br/><br/>print(result)<br/><br/>num1 = 10<br/>num2 = 20<br/><br/>num1 = num2<br/>print("num1 = num2 ---> {0} ".format(num1))<br/><br/>num1 += num2<br/>print("num1 += num2 ---> {0}".format(num1))<br/><br/>num1 -= num2<br/>print("num1 -= num2 ---> {0}".format(num1))<br/><br/>num1 *= num2<br/>print("num1 *= num2 ---> {0}".format(num1))<br/><br/>num1 /= num2<br/>print("num1 /= num2 ---> {0}".format(num1))<br/><br/>num1 %= num2<br/>print("num1 %= num2 ---> {0}".format(num1))<br/><br/>num1 **= num2<br/>print("num1 **= num2 ---> {0}".format(num1))<br/><br/>num1 //= num2<br/>print("num1 //= num2 ---> {0}".format(num1))

Console Output

10<br/>num1 = num2 ---> 20<br/>num1 += num2 ---> 40<br/>num1 -= num2 ---> 20<br/>num1 *= num2 ---> 400<br/>num1 /= num2 ---> 20.0<br/>num1 %= num2 ---> 0.0<br/>num1 **= num2 ---> 0.0<br/>num1 //= num2 ---> 0.0

Logical Operators

and  逻辑与  两边所有为True 则True 否则 False<br/>or   逻辑或  两边任意为True 则True 否则False<br/>not  逻辑非  如果为True 则为False 变量为False 返回True

Experiment Code

A = True<br/>B = False<br/><br/>print("A and B ---> {0}".format(A and B))<br/>print("A or B ---> {0}".format( A or B))<br/>print("not A ---> {0}".format(not A))<br/>print("not B ---> {0}".format(not B))

Console Output

A and B ---> False<br/>A or B ---> True<br/>not A ---> False<br/>not B ---> True

Membership Operators

in  存在 返回True False<br/>not in  不存在 True False

Experiment Code

# 成员运算符<br/><br/>student = ["zhangsan","lisi","wangwu"]<br/><br/>print("xiaoming is my student ? -->{0}".format("xiaoming" in student))<br/>print("xiaohua is not my student ? -->{0}".format("xiaohua" not in student))<br/>print("zhangsan is my student? -->{0}".format("zhangsan" in student))

Console Output

xiaoming is my student ? -->False<br/>xiaohua is not my student ? -->True<br/>zhangsan is my student? -->True

Identity Operators

is  如果两个对象为同一个内存地址返回True false<br/>is not  不相同返回True  false

Experiment Code

#身份运算符<br/><br/>num1 = 10<br/>num2 = 10<br/><br/>print("num1 is num2 --> {0}".format(num1 is num2))<br/>print("num1 is not num2 --> {0}".format(num1 is not num2))<br/><br/>a = "123"<br/>b = "123"<br/>print("a  is b --> {0}".format(a is b))

Console Output

num1 is num2 --> True<br/>num1 is not num2 --> False<br/>a  is b --> True

Bitwise Operators

&  位于运算<br/>|  位或运算<br/>^  异或运算<br/>~  位取反

Experiment Code

#位运算符<br/>num1 = 1000101<br/>num2 = 10101<br/><br/>print("num1 & num2 = {0}".format(num1&num2))<br/>print("num1 | num2 = {0}".format(num1|num2))<br/>print("num1 ^ num2 = {0}".format(num1^num2))<br/>print("~num1 = {0}".format(~num1))<br/>print("~num2 = {0}".format(~num2))

Console Output

num1 & num2 = 549<br/>num1 | num2 = 1009653<br/>num1 ^ num2 = 1009104<br/>~num1 = -1000102<br/>~num2 = -10102

Operator Precedence

**<br/>~ + -<br/>* / % //<br/>+ -<br/>>> <<<br/>&<br/>^ |<br/><= <> >=<br/>= %= /+= //= -+ += *= **=<br/>is is not<br/>in not in<br/>not or and
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonCode Examplesoperatorsprogramming basics
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.