This article unlocks the full potential of Python’s arithmetic operators with a comprehensive guide. Learn how to perform addition, subtraction, multiplication, division, and more. Master essential techniques and optimize your code for efficient computations. Dive into the world of Python arithmetic and boost your programming skills.
Table of Contents
Arithmetic Operators
Arithmetic Operators are Mathematical operators such as addition, subtraction, multiplication, division, and more that perform various operations on numerical values to compute arithmetic solutions.
- Addition (+): Adds two operands. In other words, adds two numbers.
- Subtraction (-): Subtracts the right operand from the left operand.
- Multiplication (*): Multiplies two operands. In other words, multiplies two numbers.
- Division (/): Divides the left operand by the right operand and returns results in a float.
- Floor Division (//): Divides the left operand by the right operand and terminates the decimal part and returns results in an integer.
- Modulus (%): Returns the remainder of the division of the left operand by the right operand.
- Exponentiation (**): Raises the left operand to the power of the right operand.
Example: Addition
result_addition = 5 + 3
print(result_addition)
# Output: 8
Example: Subtraction
result_subtraction = 10 - 4
print(result_subtraction)
# Output: 6
Example: Multiplication
result_multiplication = 3 * 7
print(result_multiplication)
# Output: 21
Example: Division
result_division = 15 / 4
print(result_division)
# Output: 3.75
Example: Floor Division
result_floor_division = 15 // 4
print(result_floor_division)
# Output: 3
Example: Modulus
result_modulus = 15 % 4
print(result_modulus)
# Output: 3
Example: Exponentiation
result_exponentiation = 2 ** 3
print(result_exponentiation)
# Output: 8
Comparison Operators
Comparison Operators are used to compare two values or expressions. These operators return a Boolean value (True or False) based on the result of the comparison.
Comparison operators in Python:
- Equal to (==): Checks if the left operand is equal to the right operand.
- Not equal to (!=): Checks if the left operand is not equal to the right operand.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Example: Equal to
result_equal = 5 == 5
print(result_equal)
# Output: True
Example: Not equal to
result_not_equal = 5 != 10
print(result_not_equal)
# Output: True
Example: Greater than
result_greater_than = 10 > 5
print(result_greater_than)
# Output: True
Example: Less than
result_less_than = 3 < 7
print(result_less_than)
# Output: True
Example: Greater than or equal to
result_greater_equal = 8 >= 8
print(result_greater_equal)
# Output: True
Example: Less than or equal to
result_less_equal = 6 <= 10
print(result_less_equal)
# Output: True
These operators are commonly used in conditional statements and loops to control the flow of the program based on certain conditions.
Logical Operators
Logical operators are used to combine or modify the results of Boolean expressions. They operate on Boolean values (True or False) and return a new Boolean value as a result.
Logical operators in Python:
- Logical AND (and): Returns True if both the operands are True, otherwise returns False.
- Logical OR (or): Returns True if at least one of the operands is True, otherwise returns False.
- Logical NOT (not): Returns the opposite Boolean value of the operand. If the operand is True, it returns False, and vice versa.
Example: Logical AND
result_and = True and False
print(result_and)
# Output: False
Example: Logical OR
result_or = True or False
print(result_or)
# Output: True
Example: Logical NOT
result_not = not True
print(result_not)
# Output: False
Logical operators are often used in conditional statements and decision-making constructs to evaluate multiple conditions.
Example:
age = 18
allow_drive = False
# Using logical AND and logical OR in a conditional statement
if age >= 18 and not allow_drive:
print("You are allowed to drive.")
elif age < 18 or allow_drive:
print("You are a minor. Not allowed to drive")
else:
print("Invalid input.")
In this example, the logical AND operator (and) is used to check if the age is greater than or equal to 18 and the logical NOT operator (not) is used to check if the person is not allowed to drive. The logical OR operator (or) is used to check if the age is less than 18 or if the person is allowed to drive. Based on the condition, the appropriate message will be printed.
Assignment Operators
Assignment operators are used to assign values to variables. They combine the assignment operation with other operations like addition, subtraction, multiplication, division, etc. This allows you to perform an operation on the variable and then store the result back in the same variable.
Assignment operators in Python:
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 stores the result in the variable.
Subtraction assignment (-=): Subtracts the value on the right from the variable on the left and stores the result in the variable.
Multiplication assignment (*=): Multiplies the variable on the left by the value on the right and stores the result in the variable.
Division assignment (/=): Divides the variable on the left by the value on the right and stores the result in the variable.
Modulus assignment (%=): Performs the modulus operation on the variable on the left with the value on the right and stores the result in the variable.
Exponentiation assignment (**=): Raises the variable on the left to the power of the value on the right and stores the result in the variable.
Floor division assignment (//=): Performs the floor division on the variable on the left with the value on the right and stores the result in the variable.
Example: Assignment
x = 10
print(x)
# Output: 10
Example: Addition assignment
x += 5 # equivalent to x = x + 5
print(x)
# Output: 15
Example: Subtraction assignment
x -= 3 # equivalent to x = x - 3
print(x)
# Output: 12
Example: Multiplication assignment
x *= 2 # equivalent to x = x * 2
print(x)
# Output: 24
Example: Division assignment
x /= 4 # equivalent to x = x / 4
print(x)
# Output: 6.0
Example: Modulus assignment
x %= 5 # equivalent to x = x % 5
print(x)
# Output: 1.0
Example: Exponentiation assignment
x **= 3 # equivalent to x = x ** 3
print(x)
# Output: 1.0
Example: Floor division assignment
x //= 0.5 # equivalent to x = x // 0.5
print(x)
# Output: 2.0
Conclusion
Python’s arithmetic operators play a fundamental role in performing various mathematical operations on numerical data types. Understanding and utilizing these operators effectively is essential for any Python programmer.
Python’s arithmetic operators are not only limited to basic calculations but are also widely used in complex mathematical computations, conditional statements, loops, and various other programming tasks.
As you continue your journey in Python programming, keep in mind the significance of arithmetic operators in daily coding tasks and the role they play in making your programs more robust and flexible. By mastering these operators, you’ll have a solid foundation for exploring more advanced concepts and building powerful applications with Python.