Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† PYTHON FUNDAMENTALS LEVEL 1 PROFESSIONAL ยท Lesson 6 of 9

Module Five

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
6
Module 5: Python Operators and Expressions Module 5: Python Operators and Expressions

MODULE 5: PYTHON OPERATORS AND EXPRESSIONS

Course: Python Fundamentals Level 1 Professional
Module: 5 of 8
Level: Beginner

1. Introduction to Python Operators

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.

2. Learning Objectives

By the end of this module, you should be able to:

  • Explain what Python operators are.
  • Identify the different types of Python operators.
  • Perform mathematical calculations using Python.
  • Compare values using comparison operators.
  • Use logical operators to combine conditions.
  • Understand assignment operators.
  • Write simple Python expressions.
  • Build a basic shopping calculator using Python.

3. What Are Python Operators?

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.

4. Types of Python Operators

Python has several types of operators. The major types you will learn in this module are:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators

5. Arithmetic Operators

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

5.1 Addition Operator (+)

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.

5.2 Subtraction Operator (-)

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.

5.3 Multiplication Operator (*)

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.

5.4 Division Operator (/)

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.

5.5 Floor Division Operator (//)

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.

5.6 Modulus Operator (%)

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.

Remember: The modulus operator is useful when checking whether a number is even or odd.

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

5.7 Exponentiation Operator (**)

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.

6. Assignment Operators

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

Example of Assignment Operators

score = 50

score += 10

print(score)

Output:

60

The statement score += 10 adds 10 to the existing score.

7. Comparison Operators

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

Example: Comparing Students' Scores

student_score = 75
pass_mark = 50

print(student_score > pass_mark)

Output:

True

The result is True because 75 is greater than 50.

Another Example

age = 12

print(age == 13)
print(age != 13)
print(age < 18)

Output:

False
True
True
Important: Do not confuse = with ==. The single equal sign assigns a value, while the double equal sign checks whether two values are equal.

8. Logical Operators

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:

  • and
  • or
  • not

8.1 AND Operator

The 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.

8.2 OR Operator

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.

8.3 NOT Operator

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

Logical Operators Truth Table

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

9. What Are Python Expressions?

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.

10. Operator Precedence

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:

  1. 5 ร— 2 = 10
  2. 10 + 10 = 20

Using Parentheses

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.

Tip: Use parentheses when an expression looks complicated. They make your calculations easier to read and understand.

11. Combining Operators in a Program

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.

12. String Operators

Python also allows you to use some operators with text, which is called a string.

12.1 Joining Strings with +

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

12.2 Repeating Strings with *

The multiplication operator can repeat a string a specified number of times.

message = "Welcome! "

print(message * 3)

Output:

Welcome! Welcome! Welcome! 

13. Practical Project: Simple Shopping Calculator

Project Description

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:

  • Create a variable for the price of a notebook.
  • Create a variable for the number of notebooks purchased.
  • Calculate the total cost using multiplication.
  • Create a variable for the amount paid.
  • Calculate the balance using subtraction.
  • Display the total cost and balance.

Sample Solution

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.

14. Common Mistakes to Avoid

  • Confusing = and ==: Remember that = assigns a value, while == compares two values.
  • Using the wrong operator: Use * for multiplication, not x.
  • Dividing by zero: Python cannot perform ordinary division by zero.
  • Ignoring operator precedence: Remember that multiplication and division are performed before addition and subtraction.
  • Mixing strings and numbers: Convert values to the correct data type before performing mathematical calculations.

15. Practical Activities

Activity 1: Calculate a Student's Average

Write a Python program that stores the scores of a student in three subjects and calculates the average score.

Use addition and division operators.

Activity 2: Check an Even or Odd Number

Create a variable called number. Use the modulus operator to determine whether the number is even or odd.

Activity 3: Compare Two Numbers

Create two variables and use comparison operators to check whether the first number is greater than, less than or equal to the second number.

Activity 4: Update a Score

Create a variable called score and give it an initial value of 40. Use the += operator to add 20 points. Display the new score.

16. Key Terms to Remember

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.

17. Module Summary

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.

18. Knowledge Check

Test your understanding of this module by answering the following questions.

1. What is a Python operator?

  1. A symbol or keyword used to perform an operation
  2. A computer keyboard
  3. A type of computer
  4. A Python file

2. Which operator is used for multiplication in Python?

  1. +
  2. *
  3. /
  4. -

3. What is the result of 10 + 5?

  1. 10
  2. 5
  3. 15
  4. 50

4. Which operator returns the remainder after division?

  1. //
  2. **
  3. %
  4. /

5. What is the result of 2 ** 3?

  1. 5
  2. 6
  3. 8
  4. 9

6. Which operator checks whether two values are equal?

  1. =
  2. ==
  3. !=
  4. >

7. Which logical operator requires both conditions to be True?

  1. or
  2. not
  3. and
  4. +

8. What is the result of 11 // 2?

  1. 5
  2. 5.5
  3. 6
  4. 11

9. Which operator is used to add a value to an existing variable and update it?

  1. ==
  2. +=
  3. !=
  4. %

10. What does operator precedence determine?

  1. The colour of a Python program
  2. The name of a variable
  3. The order in which operators are evaluated
  4. The size of a computer
Click here to reveal the answers
  1. A - A symbol or keyword used to perform an operation.
  2. B - * is the multiplication operator.
  3. C - 10 + 5 = 15.
  4. C - % returns the remainder after division.
  5. C - 2 raised to the power of 3 equals 8.
  6. B - == checks whether two values are equal.
  7. C - and requires both conditions to be True.
  8. A - 11 // 2 returns 5.
  9. B - += adds a value to the existing variable.
  10. C - Operator precedence determines the order of evaluation.

19. What Is Next?

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.

Congratulations! You have completed Module 5 of Python Fundamentals Level 1 Professional. Keep practising the examples and activities before moving to the next module.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’