Operators & ExpressionsPython Basics

Python Operators Explained: Arithmetic, Logical, and Comparison

Operators are the building blocks of any logic in programming. They allow you to perform calculations, make comparisons, and execute conditional checks in your code. Mastering Python operators is essential for writing dynamic scripts, algorithm design, and handling control flow.

This practical guide breaks down the core Python operators—arithmetic, comparison, and logical—with simple code examples you can test right away.

Key Takeaways

  • Arithmetic operators handle standard mathematical calculations like addition, division, and modulo operations.
  • Comparison operators evaluate conditions and always return a boolean value (True or False).
  • Logical operators (and, or, not) combine multiple boolean conditions.
  • Understanding operator precedence ensures your mathematical expressions calculate in the expected order.

1. Python Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric values.

OperatorNameDescriptionExample (x = 10, y = 3)Result
+AdditionAdds two valuesx + y13
-SubtractionSubtracts second value from firstx - y7
*MultiplicationMultiplies two valuesx * y30
/DivisionDivides first value by second (returns float)x / y3.3333
%ModuloReturns remainder of divisionx % y1
**ExponentiationRaises first value to power of secondx ** y1000
//Floor DivisionDivides and rounds down to nearest whole numberx // y3

Python

# Modulo example: Checking if a number is even or odd
number = 14
if number % 2 == 0:
    print("Even Number") # Outputs: Even Number

2. Python Comparison Operators

Comparison operators evaluate the relationship between two values and always return either True or False.

  • == (Equal to): Returns True if both operands are equal (5 == 5 $\rightarrow$ True).
  • != (Not equal to): Returns True if operands are not equal (5 != 3 $\rightarrow$ True).
  • > (Greater than): Returns True if left value is larger (10 > 5 $\rightarrow$ True).
  • < (Less than): Returns True if left value is smaller (3 < 8 $\rightarrow$ True).
  • >= (Greater than or equal to): (5 >= 5 $\rightarrow$ True).
  • <= (Less than or equal to): (4 <= 2 $\rightarrow$ False).
Python Operators Explained

3. Python Logical Operators

Logical operators combine conditional statements to control program decisions.

The and Operator

Returns True only if both conditions evaluate to True.

Python

age = 22
has_license = True

if age >= 18 and has_license:
    print("Allowed to drive.")

The or Operator

Returns True if at least one condition evaluates to True.

Python

is_admin = False
is_editor = True

if is_admin or is_editor:
    print("Access granted to dashboard.")

The not Operator

Reverses the boolean result of a condition (turns True into False and vice versa).

Python

logged_in = False

if not logged_in:
    print("Please log in to continue.")

Operator Precedence in Python

When combining multiple operators in a single expression, Python evaluates them based on operator precedence (similar to standard algebra rules / PEMDAS):

  1. Parentheses () (Highest precedence)
  2. Exponentiation **
  3. Multiplication, Division, Modulo *, /, //, %
  4. Addition, Subtraction +, -
  5. Comparison Operators ==, !=, >, <
  6. Logical Operators not, and, or (Lowest precedence)

Python

# Example of operator precedence
result = 10 + 2 * 5  # Evaluates as 10 + 10 = 20 (Multiplication first)
result_with_parentheses = (10 + 2) * 5  # Evaluates as 12 * 5 = 60

Conclusion

Mastering Python operators allows you to perform precise calculations, formulate conditional statements, and streamline complex application logic. Whether you are building financial tools using arithmetic operators or regulating user authorization with logical checks, these core symbols form the backbone of Python programming.

Frequently Asked Questions

What is the difference between = and == in Python?

= is the assignment operator used to assign a value to a variable, whereas == is a comparison operator used to check if two values are equal.

What does the % (Modulo) operator do in Python?

The modulo operator calculates and returns the remainder after dividing the left number by the right number (e.g., 10 % 3 returns 1).

What is floor division in Python?

Floor division (//) divides two numbers and rounds the result down to the nearest whole integer, dropping any decimal places.

Leave a Reply

Your email address will not be published. Required fields are marked *