Welcome to Module 2! In the previous module, you learned what Python is and how it helps people analyse data. Now, you will learn how to write basic Python instructions and work with different types of information.
Think of Python as a language you use to communicate with a computer. When you write instructions, the computer follows them and gives you results.
Syntax means the rules you must follow when writing instructions in a programming language.
Just like English has grammar rules, Python has rules that help the computer understand your instructions.
For example:
print("Hello, World!")
Output:
Hello, World!
This instruction tells Python to display the words Hello, World! on the screen.
Look at another example:
print("I am learning Python")
print("Python is interesting")
Output:
I am learning Python
Python is interesting
Important Python Rules:
A variable is a named storage location used to keep information in a computer program.
Think of a variable as a labelled box. You put information inside the box and give it a name so you can use it later.
For example, imagine you want Python to remember your name:
name = "David"
print(name)
Output:
David
In this example:
Another example:
age = 13
city = "Ibadan"
print(age)
print(city)
Output:
13
Ibadan
You can also change the information stored in a variable.
score = 50
score = 80
print(score)
Output:
80
The new value replaces the old value.
Data types describe the kind of information stored in a variable. Python supports different data types for working with numbers, text, and other information.
Here are four important data types beginners should understand:
An integer is a whole number without a decimal point.
Examples include 10, 25, 100, and 500.
age = 13
students = 40
print(age)
print(students)
Output:
13
40
A float is a number that contains a decimal point.
Examples include 12.5, 45.75, and 100.50.
price = 1500.50
average_score = 75.5
print(price)
print(average_score)
Output:
1500.5
75.5
A string is text made up of letters, words, numbers, or symbols. Strings are written inside quotation marks.
name = "Grace"
country = "Nigeria"
message = "Welcome to Python"
print(name)
print(country)
print(message)
Output:
Grace
Nigeria
Welcome to Python
A Boolean is a data type that has only two possible values: True or False.
It is useful when a computer needs to make decisions.
is_student = True
is_married = False
print(is_student)
print(is_married)
Output:
True
False
Quick Comparison of Python Data Types
| Data Type | Meaning | Example |
|---|---|---|
| Integer (int) | Whole numbers | 25 |
| Float (float) | Decimal numbers | 25.5 |
| String (str) | Text or characters | "Hello" |
| Boolean (bool) | True or False values | True |
Python allows you to work with numbers and text. You can perform calculations, combine text, and display results.
Example 1: Adding Numbers
number_one = 20
number_two = 30
total = number_one + number_two
print(total)
Output:
50
Example 2: Combining Text
first_name = "John"
last_name = "Peter"
full_name = first_name + " " + last_name
print(full_name)
Output:
John Peter
When you use the plus sign (+) with numbers, Python adds them. When you use it with strings, Python joins the text together. This is called string concatenation.
Python can work like a calculator. You can use it to add, subtract, multiply, divide, and perform other mathematical operations.
Here are some important mathematical operators:
| Operator | Meaning | Example | Answer |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 5 | 2.0 |
| // | Floor division | 11 // 2 | 5 |
| % | Remainder | 11 % 2 | 1 |
| ** | Power | 2 ** 3 | 8 |
Example: Calculating Total Sales
Imagine a shop sells three products:
You can use Python to calculate the total sales.
rice = 5000
beans = 3000
garri = 2000
total = rice + beans + garri
print(total)
Output:
10000
The total sales amount is โฆ10,000.
A comment is a note written inside a computer program to explain what the code does. Python ignores comments when running a program.
Comments help programmers understand their code, especially when working on large projects.
In Python, you use the hash symbol (#) to write a comment.
# This program calculates total sales
rice = 5000
beans = 3000
total = rice + beans
print(total)
Output:
8000
The line beginning with # is a comment. It explains what the program does.
Sometimes, you may want to know the type of information stored in a variable. Python provides a function called type() to help you do this.
Example:
age = 13
price = 2500.50
name = "David"
is_student = True
print(type(age))
print(type(price))
print(type(name))
print(type(is_student))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
This shows that Python can identify whether information is a whole number, decimal number, text, or Boolean value.
In this module, you have learned:
Open Google Colab or another Python environment and complete these activities:
Imagine you have โฆ10,000 to buy school supplies. You purchase:
Your Task:
Remember: Learning Python becomes easier when you practise regularly. Try changing the numbers in the examples and observe how the results change.