• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 2.4: Operators: Arithmetic, Comparison, Logical, Assignment

Lesson 2.4: Operators: Arithmetic, Comparison, Logical, Assignment

4 min read

Operators are special symbols that perform operations on values and variables. Understanding them is key to performing calculations, making decisions, and assigning values in your programs.

Python supports several types of operators:

  1. Arithmetic Operators These are used for mathematical calculations:
  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division (always returns a float)
  • %: Modulus (returns the remainder of a division)
  • **: Exponentiation (power)
  • //: Floor division (returns the integer part of the division, rounds down)
python
a = 10 b = 3 print(f"a + b = {a + b}") # Output: 13 print(f"a - b = {a - b}") # Output: 7 print(f"a * b = {a * b}") # Output: 30 print(f"a / b = {a / b}") # Output: 3.3333333333333335 (float division) print(f"a % b = {a % b}") # Output: 1 (remainder of 10 / 3) print(f"a ** b = {a ** b}") # Output: 1000 (10 to the power of 3) print(f"a // b = {a // b}") # Output: 3 (floor division, discards fractional part)

  1. Comparison (Relational) Operators These operators compare two values and return a Boolean result (True or False).
  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to
python
x = 10 y = 12 print(f"x == y: {x == y}") # Output: False print(f"x != y: {x != y}") # Output: True print(f"x > y: {x > y}") # Output: False print(f"x < y: {x < y}") # Output: True print(f"x >= y: {x >= y}") # Output: False print(f"x <= y: {x <= y}") # Output: True

  1. Logical Operators These operators combine conditional statements (Boolean expressions).
  • and: Returns True if both statements are True.
  • or: Returns True if at least one statement is True.
  • not: Reverses the logical state of its operand.
python
p = True q = False r = 5 s = 10 print(f"p and q: {p and q}") # Output: False print(f"p or q: {p or q}") # Output: True print(f"not p: {not p}") # Output: False print(f"r < 10 and s > 5: {r < 10 and s > 5}") # Output: True (5<10 is True, 10>5 is True) print(f"r > 10 or s > 5: {r > 10 or s > 5}") # Output: True (5>10 is False, 10>5 is True)

  1. Assignment Operators These operators are used to assign values to variables. The simple assignment operator is =. Compound assignment operators combine an arithmetic operation with assignment.
  • =: Assign value
  • +=: Add and assign (x += 5 is x = x + 5)
  • -=: Subtract and assign
  • *=: Multiply and assign
  • /=: Divide and assign
  • %=: Modulus and assign
  • **=: Exponentiate and assign
  • //=: Floor divide and assign
python
num = 10 print(f"Initial num: {num}") # Output: Initial num: 10 num += 5 # num = num + 5 print(f"num after += 5: {num}") # Output: num after += 5: 15 num *= 2 # num = num * 2 print(f"num after *= 2: {num}") # Output: num after *= 2: 30 num /= 3 # num = num / 3 print(f"num after /= 3: {num}") # Output: num after /= 3: 10.0 num %= 4 # num = num % 4 print(f"num after %= 4: {num}") # Output: num after %= 4: 2.0

Operator Precedence

Just like in mathematics, operators have an order of precedence. For example, multiplication and division are performed before addition and subtraction. Parentheses () can be used to override the default precedence.

python
result1 = 5 + 2 * 3 # Multiplication first: 5 + 6 = 11 result2 = (5 + 2) * 3 # Parentheses first: 7 * 3 = 21 print(f"Result 1: {result1}") # Output: 11 print(f"Result 2: {result2}") # Output: 21

Understanding and correctly applying these operators is fundamental to writing functional and logical Python programs. They allow you to process data, make comparisons, and control program flow.

Key Takeaways:

  • Arithmetic operators perform mathematical calculations.
  • Comparison operators (==, !=, <, >) return True or False.
  • Logical operators (and, or, not) combine or modify Boolean expressions.
  • Assignment operators (=, +=, -=, etc.) assign values to variables.
  • Operator precedence determines the order of operations; use parentheses to clarify or change it.
End of lesson
👏Well done!
Previous Lesson
Lesson 2.3: String Data Type and Basic Operations
Next Lesson
Quiz: Module 2: Variables, Data Types, and Operators

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Lesson 2.1: Variables and Naming Conventions

Lesson 2.2: Numeric Data Types (Integers, Floats, Complex) and Type Conversion

Lesson 2.3: String Data Type and Basic Operations

Lesson 2.4: Operators: Arithmetic, Comparison, Logical, Assignment

Quiz

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Lesson 2.1: Variables and Naming Conventions

Lesson 2.2: Numeric Data Types (Integers, Floats, Complex) and Type Conversion

Lesson 2.3: String Data Type and Basic Operations

Lesson 2.4: Operators: Arithmetic, Comparison, Logical, Assignment

Quiz