Imagine you are buying a notebook for โฆ2,000 and a pen for โฆ500. How do you calculate the total amount you need to pay?
You use addition:
โฆ2,000 + โฆ500 = โฆ2,500
Python can perform this calculation for you. It uses special symbols and words called operators to perform calculations, compare values, and make decisions.
In this module, you will learn how to use Python operators to solve simple mathematical and programming problems.
By the end of this module, you should be able to:
Python operators are special symbols or keywords that tell Python what operation to perform on values or variables.
For example:
number1 = 10
number2 = 5
result = number1 + number2
print(result)
Output:
15
In this example:
number1 stores the value 10.number2 stores the value 5.+ is the addition operator.result stores the answer.Python has several types of operators. The major types you will learn in this module are:
Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication and division.
Python supports the following arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 5 | 2.0 |
| // | Floor division | 11 // 2 | 5 |
| % | Modulus | 11 % 2 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 |
The addition operator adds two or more numbers together.
price1 = 2000
price2 = 1500
total = price1 + price2
print(total)
Output:
3500
This program adds โฆ2,000 and โฆ1,500 to produce โฆ3,500.
The subtraction operator removes one number from another.
money = 5000
expense = 2000
balance = money - expense
print(balance)
Output:
3000
If you have โฆ5,000 and spend โฆ2,000, your remaining balance is โฆ3,000.
The multiplication operator multiplies numbers.
price = 500
quantity = 4
total = price * quantity
print(total)
Output:
2000
If one notebook costs โฆ500 and you buy four notebooks, the total cost is โฆ2,000.
The division operator divides one number by another. In Python, ordinary division returns a floating-point number, even when the answer is a whole number.
total_money = 10000
number_of_students = 4
share = total_money / number_of_students
print(share)
Output:
2500.0
Each student receives โฆ2,500.
Floor division divides two numbers and returns the whole-number result rounded down to the nearest integer.
result = 11 // 2
print(result)
Output:
5
Although 11 divided by 2 is 5.5, floor division returns 5.
The modulus operator returns the remainder after division.
result = 11 % 2
print(result)
Output:
1
When 11 is divided by 2, the remainder is 1.
Example:
number = 8
if number % 2 == 0:
print("This is an even number")
else:
print("This is an odd number")
Output:
This is an even number
The exponentiation operator raises a number to the power of another number.
result = 2 ** 3
print(result)
Output:
8
This means 2 multiplied by itself three times: 2 ร 2 ร 2 = 8.
Assignment operators are used to give values to variables or update the values already stored in them.
The most common assignment operator is the equal sign (=).
age = 13
print(age)
Output:
13
Python also provides shortcut assignment operators.
| Operator | Meaning | Example | Equivalent Operation |
|---|---|---|---|
| = | Assign | x = 10 | x = 10 |
| += | Add and assign | x += 5 | x = x + 5 |
| -= | Subtract and assign | x -= 3 | x = x - 3 |
| *= | Multiply and assign | x *= 2 | x = x * 2 |
| /= | Divide and assign | x /= 2 | x = x / 2 |
score = 50
score += 10
print(score)
Output:
60
The statement score += 10 adds 10 to the existing score.
Comparison operators are used to compare two values. They help Python determine whether something is true or false.
Python returns either True or False when you use a comparison operator.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 10 == 10 | True |
| != | Not equal to | 10 != 5 | True |
| > | Greater than | 10 > 5 | True |
| < | Less than | 5 < 10 | True |
| >= | Greater than or equal to | 10 >= 10 | True |
| <= | Less than or equal to | 5 <= 10 | True |
student_score = 75
pass_mark = 50
print(student_score > pass_mark)
Output:
True
The result is True because 75 is greater than 50.
age = 12
print(age == 13)
print(age != 13)
print(age < 18)
Output:
False
True
True
= with ==. The single equal sign assigns a value, while the double equal sign checks whether two values are equal.
Logical operators allow you to combine two or more conditions. They are useful when a program needs to check multiple requirements.
Python has three main logical operators:
andornotThe and operator returns True only when both conditions are True.
age = 15
has_ticket = True
print(age >= 13 and has_ticket == True)
Output:
True
Both conditions are True, so the final result is True.
The or operator returns True when at least one of the conditions is True.
has_username = True
has_email = False
print(has_username or has_email)
Output:
True
Only one condition needs to be True for the result to be True.
The not operator reverses a Boolean value. It changes True to False and False to True.
is_raining = False
print(not is_raining)
Output:
True
| Condition A | Condition B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
An expression is a combination of values, variables and operators that Python evaluates to produce a result.
For example:
10 + 5
This is an expression because Python evaluates it and produces 15.
Another example:
price = 2000
quantity = 3
total = price * quantity
print(total)
Output:
6000
The expression price * quantity calculates the total cost.
Operator precedence describes the order in which Python performs operations when an expression contains more than one operator.
For example:
result = 10 + 5 * 2
print(result)
Output:
20
Python performs multiplication before addition. Therefore:
You can use parentheses to tell Python which calculation should happen first.
result = (10 + 5) * 2
print(result)
Output:
30
Python calculates the expression inside the parentheses first.
You can combine different operators to solve real-life problems.
Consider a student who buys three notebooks at โฆ800 each and two pens at โฆ300 each.
notebook_price = 800
notebook_quantity = 3
pen_price = 300
pen_quantity = 2
notebook_total = notebook_price * notebook_quantity
pen_total = pen_price * pen_quantity
total_bill = notebook_total + pen_total
print("Notebook total:", notebook_total)
print("Pen total:", pen_total)
print("Total bill:", total_bill)
Output:
Notebook total: 2400
Pen total: 600
Total bill: 3000
The program calculates the cost of each item and adds the amounts to produce the final bill of โฆ3,000.
Python also allows you to use some operators with text, which is called a string.
The plus sign can join two or more strings together. This is called string concatenation.
first_name = "Tunde"
last_name = "Adebayo"
full_name = first_name + " " + last_name
print(full_name)
Output:
Tunde Adebayo
The multiplication operator can repeat a string a specified number of times.
message = "Welcome! "
print(message * 3)
Output:
Welcome! Welcome! Welcome!
You are building a simple calculator for a school shop. The calculator should calculate the total cost of items purchased by a student and the balance remaining after payment.
Project Requirements:
notebook_price = 1000
quantity = 4
amount_paid = 5000
total_cost = notebook_price * quantity
balance = amount_paid - total_cost
print("Total cost:", total_cost)
print("Amount paid:", amount_paid)
print("Balance:", balance)
Output:
Total cost: 4000
Amount paid: 5000
Balance: 1000
The student purchased four notebooks for โฆ4,000, paid โฆ5,000 and received โฆ1,000 as the remaining balance.
Write a Python program that stores the scores of a student in three subjects and calculates the average score.
Use addition and division operators.
Create a variable called number. Use the modulus operator to determine whether the number is even or odd.
Create two variables and use comparison operators to check whether the first number is greater than, less than or equal to the second number.
Create a variable called score and give it an initial value of 40. Use the += operator to add 20 points. Display the new score.
| Term | Meaning |
|---|---|
| Operator | A symbol or keyword used to perform an operation. |
| Arithmetic Operator | An operator used for mathematical calculations. |
| Assignment Operator | An operator used to assign or update variable values. |
| Comparison Operator | An operator used to compare two values. |
| Logical Operator | An operator used to combine or reverse conditions. |
| Expression | A combination of values, variables and operators that produces a result. |
| Modulus | An operator that returns the remainder after division. |
| Precedence | The order in which Python evaluates operators. |
In this module, you learned that Python operators help computers perform calculations, compare values and work with conditions.
You explored arithmetic operators, assignment operators, comparison operators and logical operators. You also learned about expressions, operator precedence and string operations.
These concepts are important because they help you build programs that can calculate prices, process scores, compare information and solve everyday problems.
Test your understanding of this module by answering the following questions.
1. What is a Python operator?
2. Which operator is used for multiplication in Python?
3. What is the result of 10 + 5?
4. Which operator returns the remainder after division?
5. What is the result of 2 ** 3?
6. Which operator checks whether two values are equal?
7. Which logical operator requires both conditions to be True?
8. What is the result of 11 // 2?
9. Which operator is used to add a value to an existing variable and update it?
10. What does operator precedence determine?
Module 6: Python Input and Output
In the next module, you will learn how to make Python programs receive information from users and display useful results on the screen.
You will explore the input() and print() functions and build interactive programs that can ask users questions and respond to their answers.