Home  >  Article  >  Backend Development  >  What are the operators in Python and how to use them

What are the operators in Python and how to use them

WBOY
WBOYforward
2023-05-13 20:01:042196browse

Python Operators

Operators are used to perform operations on variables and values.

Python divides operators into the following groups:

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Identity operators

  • Member operators

  • Bitwise operators

Python arithmetic operators

Arithmetic operators are used with numerical values ​​to Perform common mathematical operations:

Operators:

Name: Add

Example: x y

Try it

x = 5
y = 3
print(x + y)

What are the operators in Python and how to use them

Operator: -

Name: Minus

Example: x - y

Try it

x = 5
y = 3

print(x - y)

What are the operators in Python and how to use them

Operator: *

Name: Multiply

Example: x * y

Try it

x = 5
y = 3

print(x * y)

What are the operators in Python and how to use them

Operator: /

Name: except

Instance: x / y

Try it

x = 15
y = 3

print(x / y)

What are the operators in Python and how to use them

Operator: %

Name: Modulo

Example: x % y

Try it

x = 5
y = 2

print(x % y)

What are the operators in Python and how to use them

Operator: **

Name: Power

Example: x ** y

Try it

x = 2
y = 5

print(x ** y) #same as 2*2*2*2*2

What are the operators in Python and how to use them

Operator: //

Name: floor division (dividing by integer)

Example: x // y

Try it

x = 15
y = 2

print(x // y)

#the floor division // rounds the result down to the nearest whole number

What are the operators in Python and how to use them

Python assignment operator

The assignment operator is used to assign values ​​to variables

Operator: =

Example: x = 5

is equivalent to: x = 5

Try it:

x = 5print(x)

What are the operators in Python and how to use them

Operator: =

Example :x = 3

is equivalent to: x = x 3

Try:

x = 5

x += 3

print(x)

What are the operators in Python and how to use them

Operator: -=

Example: x -= 3

Equivalent to: x = x - 3

Try it:

x = 5

x -= 3

print(x)

What are the operators in Python and how to use them

Operator: *=

Example: x *= 3

Equivalent to: x = x * 3

Try it:

x = 5

x *= 3

print(x)

What are the operators in Python and how to use them

Operator: /=

Example: x /= 3

Equivalent to: x = x / 3

Try it:

x = 5

x /= 3

print(x)

What are the operators in Python and how to use them

Operator: %=

Example: x %= 3

Equivalent to: x = x % 3

Try it:

x = 5

x%=3

print(x)

What are the operators in Python and how to use them

Operator: //=

Example: x //= 3

Equivalent to: x = x // 3

Try it:

x = 5

x//=3

print(x)

What are the operators in Python and how to use them

Operator: **=

Example: x **= 3

Equivalent to: x = x ** 3

Try it:

x = 5

x **= 3

print(x)

What are the operators in Python and how to use them

Operator: &=

Example: x &= 3

Equivalent to: x = x & 3

Try:

x = 5

x &= 3

print(x)

What are the operators in Python and how to use them

##Operator : |=

Example: x |= 3

is equivalent to: x = x | 3

Try it:

x = 5

x |= 3

print(x)

What are the operators in Python and how to use them

Operator: ^=

Example: x ^= 3

Equivalent to: x = x ^ 3

Try it:

x = 5

x ^= 3

print(x)

What are the operators in Python and how to use them

Operator: >>=

Example: x >>= 3

Equivalent to: x = x >> ; 3

Try it:

x = 5

x >>= 3

print(x)

What are the operators in Python and how to use them##Operator:

实例:x

等同于: x = x

试一试:

x = 5

x <<= 3

print(x)

What are the operators in Python and how to use them

The above is the detailed content of What are the operators in Python and how to use them. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete