DEV Community

Divya Dixit
Divya Dixit

Posted on

Python Operators: Arithmetic and Logical Operators Explained

An operator is something that performs an operation on values. Just like in mathematics, where we use + for addition, Python provides various types of operators, including:

  1. Arithmetic Operators
  2. Relational Operators
  3. Conditional Operators
  4. Ternary Operators
  5. Logical Operators

In this first part, we will focus on Arithmetic and Logical operators.

Arithmetic Operators in Python

Arithmetic operators are used to perform basic mathematical operations. Here’s a list of them:

+

Adds two numbers (e.g., 5 + 3 = 8)

Subtraction

-

Subtracts the right operand from the left operand (e.g., 10 - 4 = 6)

Multiplication

*

Multiplies two numbers (e.g., 7 * 2 = 14)

Division

/

Divides the left operand by the right operand (e.g., 10 / 2 = 5.0)

Floor Division

//

Returns the quotient, discarding the decimal part (e.g., 12 // 5 = 2)

Modulus

%

Returns the remainder (e.g., 12 % 5 = 2)

Exponentiation

**

Raises one number to the power of another (e.g., 2 ** 3 = 8)

Understanding Floor Division and Modulus

Floor Division (//): This operator returns the quotient but removes the decimal part. Example:

print(12 // 5)  # Output: 2

Enter fullscreen mode Exit fullscreen mode

Here, 12 / 5 would normally give 2.4, but // only takes 2.

Modulus (%): This operator returns the remainder after division. Example:

print(12 % 5)  # Output: 2

Enter fullscreen mode Exit fullscreen mode

Since 12 divided by 5 gives 2 remainder 2, % returns 2.

Exponentiation ()**: This operator raises a number to a power. Example:

print(2 ** 2)  # Output: 4
print(3 ** 2)  # Output: 9
Enter fullscreen mode Exit fullscreen mode

Logical Operators in Python

Logical operators include and, or, and not. They operate on conditions, not numbers. For example, if we want to check whether 4 > 3 and 4 > 1 simultaneously, we use logical operators. These conditions evaluate to True or False.

AND (and): Returns True only if both conditions are True. If any condition is False, the result will be False.

OR (or): Returns True if at least one condition is True. The result is False only when both conditions are False.

NOT (not): This operator negates the result. If a condition is True, not makes it False, and vice versa.

In computer language, we often denote True with 1 and False with 0.

*AND behaves like multiplication:
*

1 and 1 β†’ 1

1 and 0 β†’ 0

0 and 1 β†’ 0

0 and 0 β†’ 0

*OR is like choosing the larger value:
*

1 or 1 β†’ 1

1 or 0 β†’ 1

0 or 1 β†’ 1

0 or 0 β†’ 0

*NOT reverses the condition:
*

not True β†’ False

not False β†’ True

Example Usage:

x = 4
y = 3

# AND operator
print(x > 3 and x > 1)  # Output: True

# OR operator
print(x > 10 or x > 3)  # Output: True

# NOT operator
print(not (x > 3))  # Output: False
Enter fullscreen mode Exit fullscreen mode

This concludes our first part on Arithmetic and Logical Operators. In the next part, we will explore Relational, Conditional, and Ternary Operators in Python. Stay tuned!

Happy coding! πŸš€

Top comments (0)