Home  >  Article  >  Backend Development  >  Python operators explained

Python operators explained

WBOY
WBOYOriginal
2023-09-12 21:53:13805browse

The

Python 运算符解释

operators are used to perform operations on variables and values. Python has the following operators:

arithmetic operators

x = 5

y = 2

print(x+y)

o/p = 7

Print (x - y)

o/p = 3

Print (x * y)

o/p = 10

Print (x/y)

o/p = 2.5

Print (x%y)

o/p = 1

Print the power of (x ** 2) operation

o/p = 25

print ( x // 2) performs integer division

o/p = 2

comparison operator

Print (x > y)

o/p = true

Print (x

o/p = false

Print (x >= y)

o/p = true

Print (x

o/p = false

Print (x==y)

o/p = false

Print (x!=y)

o/p = true

Logical operators

x = true

y = false

Print (x or y)

o/p = true

Print (x and y)

o/p = false

Print (not x)

o/p = false

Print (not y)

o/p = true

Bitwise operators

They handle binary values

x = 2

y = 3

Print (x & y)

o/p = 2

Binary 2 = 010

Binary 3 = 011

Bitwise operation on 010 and 011 gives 010 (decimal 2)

Print (x | y)

o/p = 3

Bitwise OR of 010 and 011 gives 011 (decimal 3)

Print (x >> 2)

o/p = 0

Print(y

o/p = 24

print (~x) # 1’s complement

o/p = -3

assignment operator

a = 3

Print (1)

o/p = 3

a += 3

o/p = 6

a =- 3

o/p = 3

a *= 3

o/p = 9

a &= 3

a++ and ++a do not exist in Python. Using it will produce a syntax error.

identity operator

Check if 2 variables are in the same memory location.

a = 3

b = 3

Print (a is b)

o/p = true

a = [1,2,3]

b = [1,2,3]

Print (a is b)

o/p = false

Print (a is not b)

o/p = true

Member operator

x="Delhi"

Print ("D" in x)

o/p = true

Print ("D" is not in x)

o/p = false

x = [1,2,3]

Print (5 inches x)

o/p = false

The above is the detailed content of Python operators explained. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn