Home  >  Article  >  Backend Development  >  A Complete Guide to Python Operators

A Complete Guide to Python Operators

王林
王林Original
2024-01-20 09:21:07544browse

A Complete Guide to Python Operators

Python is a simple and easy-to-learn programming language that provides a rich set of operators for performing various mathematical and logical operations. This article will introduce commonly used operators in Python and provide specific code examples.

  1. Arithmetic operators
    Arithmetic operators are used to perform basic mathematical operations, including addition, subtraction, multiplication, division, remainder, and exponentiation.
    Sample code:

a = 10
b = 3

print(a b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a ** b) # Output: 1000

  1. Comparison operators
    Comparison operators are used to compare two values ​​and return a Boolean value (True or False).
    Sample code:

a = 10
b = 5

print(a == b) # Output: False
print(a != b ) # Output: True
print(a > b) # Output: True
print(a print(a >= b) # Output: True
print(a

  1. Assignment operator
    The assignment operator is used to assign a value to a variable.
    Sample code:

a = 10
b = 5

a = b # Equivalent to a = a b
print(a) # Output: 15

  1. Bit operators
    Bit operators are used to operate on binary numbers.
    Sample code:

a = 2 # Binary representation is 10
b = 3 # Binary representation is 11

print(a & b) # Output: 2 , bitwise AND operation
print(a | b) #Output: 3, bitwise OR operation
print(a ^ b) #Output: 1, bitwise XOR operation
print(~a) # Output: -3, negation operation
print(a print(b >> 1) # Output: 1, shift one to the right Bit

  1. Logical operators
    Logical operators are used to operate on Boolean values.
    Sample code:

a = True
b = False

print(a and b) # Output: False, logical AND operation
print(a or b) #Output: True, logical OR operation
print(not b) #Output: True, logical NOT operation

  1. Member operator
    Member operator is used to check a certain Whether the value belongs to a collection.
    Sample code:

a = [1, 2, 3, 4, 5]

print(2 in a) # Output: True
print(6 not in a) # Output: True

  1. Identity operator
    The identity operator is used to compare the memory addresses of objects.
    Sample code:

a = 10
b = 10

print(a is b) # Output: True
print(a is not b) # Output: False

The above are commonly used operators in Python. Of course, there are other operators, such as conditional operators (ternary operators), string operators, etc. Mastering these operators is very important for writing efficient Python code. I hope this article can help you understand and use Python operators.

The above is the detailed content of A Complete Guide to Python Operators. 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