In this section, we'll discuss operators in Python, which are symbols or combinations of symbols that perform operations on variables and values. Understanding operators is crucial for working with expressions and performing calculations in Python.
- Operators are non-alphanumeric characters and combinations of characters that Python uses to perform operations on data.
- Python recognizes a variety of operators, each with its specific functionality.
Here is a list of the most commonly used operators in Python:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder after division)**
: Exponentiation (power)//
: Floor division (division that results in the largest whole number less than or equal to the result)
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333...
print(a % b) # 1
print(a ** b) # 1000
print(a // b) # 3
<
: Less than<=
: Less than or equal to>
: Greater than>=
: Greater than or equal to==
: Equal to!=
: Not equal to
a = 5
b = 3
print(a < b) # False
print(a <= b) # False
print(a > b) # True
print(a >= b) # True
print(a == b) # False
print(a != b) # True
Python provides a wide range of operators that you can use for arithmetic calculations, bitwise operations, comparisons, and more. In Python v3, the @
operator was introduced for matrix multiplication, expanding the capabilities of Python in handling mathematical operations efficiently.