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

What are the operators in Python and how to use them

WBOY
Release: 2023-05-13 20:01:04
forward
2214 people have browsed it

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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
Copy after login

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
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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

实例:x <<= 3

等同于: x = x << 3

试一试:

x = 5

x <<= 3

print(x)
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template