In Python, as in every programming language, there are various operators for manipulating numbers, logical values, and comparing objects.
Arithmetic Operators
The most familiar ones are the mathematical operators.
Sum: +
Subtraction: -
Multiplication: *
Division: / or //
Exponentiation: **
Modulus: %
Let’s see an example for each operator:
print(10 + 3) # Addition: 13
print(10 - 3) # Subtraction: 7
print(10 * 3) # Multiplication: 30
print(10 / 3) # Division: 3.3333...
print(10 // 3) # Floor Division: 3
print(10 ** 3) # Exponentiation: 1000
print(10 % 3) # Modulus: 1
Of these, the one worth a quick note is the //
operator, also known as “Floor Division.” This operator simply rounds down the division result to the nearest integer.
Comparison Operators
These operators help determine the relationship between two objects.
Equal to: ==
Not equal to: !=
Less than: <
Greater than: >
Less than or equal to: <=
Greater than or equal to: >=
The result of each of these operators is a boolean (True
or False
), indicating if the condition is met.
Example for each operator:
print(10 == 3) # Equal to: False
print(10 != 3) # Not equal to: True
print(10 < 3) # Less than: False
print(10 > 3) # Greater than: True
print(10 <= 3) # Less than or equal to: False
print(10 >= 3) # Greater than or equal to: True
Logical Operators
These operators will be very useful in upcoming tutorials. For now, know that they are used in boolean operations.
Logical AND: and
Logical OR: or
Logical NOT: not
Example for each operator:
print(True and False) # Logical AND: False
print(True or False) # Logical OR: True
print(not True) # Logical NOT: False
Conclusion
In upcoming tutorials, we’ll delve deeper into using these operators, especially the logical ones. We’ll also see how both arithmetic and comparison operators can be applied to more than just numbers.
As always, find the sample code link here.