Course contentsShow
← Python
Lesson 13 of 1,0601. Foundations and EnvironmentFree lesson

Performing Arithmetic and Logical Operations

You will learn how to use operators to manipulate numerical data and evaluate conditions to influence the flow of your program.

In this lesson, you will learn how to use Python operators to perform math on your data and make logical decisions that control your program.

Arithmetic Operations

Arithmetic operators allow you to manipulate numerical data just like a calculator. Python supports the standard operations you likely already know, plus a few extras for programming.

Consider these examples:

x = 10
y = 3

print(x + y)  # Addition: 13
print(x - y)  # Subtraction: 7
print(x * y)  # Multiplication: 30
print(x / y)  # Division: 3.333...
print(x % y)  # Modulo (remainder): 1

The modulo operator (%) is particularly useful; it returns only the remainder of a division. It is commonly used in programming to check if a number is even or odd.

Logical and Comparison Operations

Computers become powerful when they can evaluate conditions. Comparison operators compare two values and return a True or False result.

  • == checks if two values are equal.
  • != checks if they are not equal.
  • > and < check if one value is greater or less than another.

You can also use logical operators to combine these conditions:

  • and: Returns True if both conditions are true.
  • or: Returns True if at least one condition is true.
  • not: Reverses the result (turns True to False).
age = 20
has_id = True

# We can check if someone is an adult AND has an ID
can_enter = (age >= 18) and (has_id == True)

print(can_enter)  # Output: True

By combining arithmetic to calculate values and logical operators to compare them, you provide the "brain" for your code. These operations allow your variables to interact with one another rather than just sitting in memory as static values.

Key Takeaway: Arithmetic operators perform calculations on numerical data, while logical and comparison operators evaluate conditions to return Boolean values, allowing your program to make decisions.