Operators are special symbols that perform operations on variables and values. Python has various types of operators, including arithmetic, comparison, logical, assignment, bitwise, and membership operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
- Addition (+):
result = 5 + 3 # Output: 8
Adds two values.
- Subtraction (-):
result = 10 - 6 # Output: 4
Subtracts the second value from the first.
- Multiplication (*):
result = 4 * 7 # Output: 28
Multiplies two values.
- Division (/):
result = 16 / 4 # Output: 4.0
Divides the first value by the second.
- Floor Division (//):
result = 17 // 3 # Output: 5
Divides the first value by the second and returns the largest integer less than or equal to the result.
- Modulus (%):
result = 10 % 3 # Output: 1
Returns the remainder of the division.
- Exponentiation ()**:
result = 2 ** 3 # Output: 8
Raises the first value to the power of the second.
Comparison Operators
Comparison operators compare two values and return True
or False
.
- Equal to (==):
result = (5 == 5) # Output: True
Checks if two values are equal.
- Not equal to (!=):
result = (5 != 3) # Output: True
Checks if two values are not equal.
- Greater than (>):
result = (7 > 3) # Output: True
Checks if the first value is greater than the second.
- Less than (<):
result = (4 < 9) # Output: True
Checks if the first value is less than the second.
- Greater than or equal to (>=):
result = (6 >= 6) # Output: True
Checks if the first value is greater than or equal to the second.
- Less than or equal to (<=):
result = (8 <= 10) # Output: True
Checks if the first value is less than or equal to the second.
Logical Operators
Logical operators are used to combine conditional statements.
- and:
result = (5 > 3) and (8 > 5) # Output: True
Returns True
if both conditions are true.
- or:
result = (5 > 3) or (8 < 5) # Output: True
Returns True
if at least one condition is true.
- not:
result = not (5 > 3) # Output: False
Reverses the result of the condition.
Assignment Operators
Assignment operators are used to assign values to variables.
- Assign (=):
x = 5
Assigns the value 5
to x
.
- Add and assign (+=):
x = 5 x += 3 # Equivalent to x = x + 3
Adds the right operand to the left operand and assigns the result to the left operand.
- Subtract and assign (-=):
x = 5 x -= 2 # Equivalent to x = x - 2
Subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiply and assign (*=):
x = 5 x *= 4 # Equivalent to x = x * 4
Multiplies the left operand by the right operand and assigns the result to the left operand.
- Divide and assign (/=):
x = 10 x /= 2 # Equivalent to x = x / 2
Divides the left operand by the right operand and assigns the result to the left operand.
- Modulus and assign (%=):
x = 10 x %= 3 # Equivalent to x = x % 3
Takes the modulus using two operands and assigns the result to the left operand.
- Exponentiation and assign (=)**:
x = 2 x **= 3 # Equivalent to x = x ** 3
Raises the left operand to the power of the right operand and assigns the result to the left operand.
Python Operators Example Code
Explanation of Code:
This code demonstrates various Python operators. It includes arithmetic, comparison, logical, and assignment operations. The results are printed for each operation type.
# Arithmetic Operators x = 5 y = 3 print("Addition:", x + y) # Output: 8 print("Subtraction:", x - y) # Output: 2 print("Multiplication:", x * y) # Output: 15 print("Division:", x / y) # Output: 1.666... # Comparison Operators print("Equal to:", x == y) # Output: False print("Not equal to:", x != y) # Output: True print("Greater than:", x > y) # Output: True print("Less than:", x < y) # Output: False # Logical Operators a = True b = False print("Logical AND:", a and b) # Output: False print("Logical OR:", a or b) # Output: True print("Logical NOT:", not a) # Output: False # Assignment Operators x = 10 x += 5 print("Add and assign:", x) # Output: 15 x *= 2 print("Multiply and assign:", x) # Output: 30