Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to Python for Data Analysis ยท Lesson 3 of 9

Module Two

๐Ÿ“– 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.
3
Module 2: Python Basics for Beginners Introduction to Python for Data Analysis | Beginner-friendly lesson for 13-year-olds

Module 2: Python Basics for Beginners

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.

2.1 Understanding Python Syntax

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:

  • Python instructions are usually written one per line.
  • Text must be placed inside quotation marks when used as a string.
  • Brackets must be opened and closed correctly.
  • Python is case-sensitive. This means Print and print are not the same.
  • Spaces and indentation matter in some Python instructions.

2.2 Understanding Variables

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:

  • name is the variable.
  • David is the information stored inside it.
  • The equals sign (=) assigns the information to the variable.

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.

2.3 Understanding Python Data Types

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:

1. Integer (int)

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

2. Float (float)

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

3. String (str)

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

4. Boolean (bool)

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 numbers25
Float (float)Decimal numbers25.5
String (str)Text or characters"Hello"
Boolean (bool)True or False valuesTrue

2.4 Working with Numbers and Text

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.

2.5 Basic Mathematical Operations in Python

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
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52.0
//Floor division11 // 25
%Remainder11 % 21
**Power2 ** 38

Example: Calculating Total Sales

Imagine a shop sells three products:

  • Rice: โ‚ฆ5,000
  • Beans: โ‚ฆ3,000
  • Garri: โ‚ฆ2,000

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.

2.6 Writing Comments in Python

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.

2.7 Checking the Type of Information

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.

2.8 Summary of the Module

In this module, you have learned:

  • Python syntax refers to the rules for writing Python instructions.
  • Variables are used to store information.
  • Integers are whole numbers.
  • Floats are numbers with decimal points.
  • Strings are used to store text.
  • Booleans contain True or False values.
  • Python can perform mathematical calculations.
  • Comments help explain what a program does.
  • The print() function displays information, while type() identifies a value's data type.

2.9 Practical Exercise

Open Google Colab or another Python environment and complete these activities:

  1. Create a variable called your_name and store your name inside it.
  2. Create a variable called age and store your age.
  3. Create a variable called school and store the name of your school.
  4. Write a program that adds 150 and 250.
  5. Write a program that subtracts 50 from 200.
  6. Create a variable containing the price of a school bag and another containing the price of a pair of shoes. Calculate their total cost.
  7. Use the type() function to identify the data types of three different variables.

Practical Challenge: Calculate a Student's Shopping Expenses

Imagine you have โ‚ฆ10,000 to buy school supplies. You purchase:

  • Exercise books: โ‚ฆ2,500
  • Pens: โ‚ฆ1,000
  • A school bag: โ‚ฆ4,000

Your Task:

  1. Create a variable for each item.
  2. Calculate the total amount spent.
  3. Calculate the money remaining from โ‚ฆ10,000.
  4. Use print() to display the total expenses and remaining balance.

Remember: Learning Python becomes easier when you practise regularly. Try changing the numbers in the examples and observe how the results change.

๐Ÿ† 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.
โ†’