In Python, operators are symbols or special characters that perform specific operations on one or more operands. They are an essential part of any programming language and allow us to manipulate data and perform various calculations. Python provides a wide range of operators that can be used for arithmetic, comparison, logical, assignment, and other operations.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations on numerical values. Python supports the following arithmetic operators:
Addition (+): Adds two operands.
Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides the first operand by the second.
Modulus (%): Returns the remainder of the division.
Exponentiation (**): Raises the first operand to the power of the second.
Floor Division (//): Returns the quotient of the division, discarding the remainder.
These operators can be used with both integers and floating-point numbers. Let's see some examples:
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3
Comparison Operators
Comparison operators are used to compare two values and return a Boolean result (True or False). Python supports the following comparison operators:
Equal to (==): Returns True if the operands are equal.
Not equal to (!=): Returns True if the operands are not equal.
Greater than (>): Returns True if the first operand is greater than the second.
Less than (<): Returns True if the first operand is less than the second.
Greater than or equal to (>=): Returns True if the first operand is greater than or equal to the second.
Less than or equal to (<=): Returns True if the first operand is less than or equal to the second.
These operators can be used with any data type, including numbers, strings, and even objects. Here are some examples:
x = 5
y = 10
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True
print(x >= y) # Output: False
print(x <= y) # Output: True
Logical Operators
Logical operators are used to combine multiple conditions and evaluate the result. Python supports the following logical operators:
Logical AND (and): Returns True if both operands are True.
Logical OR (or): Returns True if at least one of the operands is True.
Logical NOT (not): Returns the opposite of the operand's value.
These operators are commonly used in conditional statements and loops to control the flow of the program. Let's see some examples:
x = 5
y = 10
z = 3
print(x < y and y > z) # Output: True
print(x < y or y < z) # Output: True
print(not x < y) # Output: False
Assignment Operators
Assignment operators are used to assign values to variables. They combine the assignment (=) operator with other arithmetic or logical operators. Python supports the following assignment operators:
Assignment (=): Assigns the value on the right to the variable on the left.
Addition assignment (+=): Adds the value on the right to the variable on the left and assigns the result to the variable.
Subtraction assignment (-=): Subtracts the value on the right from the variable on the left and assigns the result to the variable.
Multiplication assignment (*=): Multiplies the variable on the left by the value on the right and assigns the result to the variable.
Division assignment (/=): Divides the variable on the left by the value on the right and assigns the result to the variable.
Modulus assignment (%=): Performs modulus operation on the variable on the left with the value on the right and assigns the result to the variable.
Exponentiation assignment (**=): Raises the variable on the left to the power of the value on the right and assigns the result to the variable.
Floor division assignment (//=): Performs floor division on the variable on the left with the value on the right and assigns the result to the variable.
These operators provide a concise way to update the value of a variable based on its current value. Here are some examples:
x = 5
x += 3 # Equivalent to x = x + 3
print(x) # Output: 8
x -= 2 # Equivalent to x = x - 2
print(x) # Output: 6
x *= 4 # Equivalent to x = x * 4
print(x) # Output: 24
x /= 6 # Equivalent to x = x / 6
print(x) # Output: 4.0
x %= 3 # Equivalent to x = x % 3
print(x) # Output: 1.0
x **= 2 # Equivalent to x = x ** 2
print(x) # Output: 1.0
x //= 0.5 # Equivalent to x = x // 0.5
print(x) # Output: 2.0
These are just a few examples of the operators available in Python. Operators are fundamental building blocks of any programming language, and understanding how to use them effectively is crucial for writing efficient and concise code. In the next section, we will explore control flow statements, which allow us to control the execution of our code based on certain conditions.