LEARNING AREA: CODING
LANGUAGE: PYTHON PROGRAMMING
GRADE 8
PREPARED BY: OSCAR ARTHUR.
Introduction to Python
What is Python?
Python is a programming language that was created by Guido van Rossum, and
released in 1991.
Python is a popular programming language and it is used to create games, apps,
websites, data analytics and automation and robotics (control robots).
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
Why Learn Python?
Easy to Learn: Python uses simple English words and clear instructions, making it a
great first programming language.
Powerful: Python is a powerful programming language that is used by companies like
Google and NASA.
Problem Solving: Learning Python helps you think logically and solve problems,
skills that are useful in everyday life.
Getting Started with Python
1. Installing Python
To write and run Python code on your computer, you need to install Python.
Go to the Python website and download the version for your computer.
Follow the instructions to install it. If you are not sure how to go about the installation
process, consult an adult.
2. Setting Up Your Code Editor
A code editor is a place where you write your Python code.
While you can use a simple text editor like Notepad, it's easier to use an Integrated
Development Environment (IDE) like Thonny, which is designed for beginners.
Other Integrated Development Environment (IDE) include: IDLE, PyCharm, Visual
Studio Code, Sublime Text 3, Atom etc.
Python Basics
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
Python uses indentation to indicate a block of code and will give you an error if you skip
indentation.
Comments
Comments are used to explain the code and for purposes of documentation.
Comments are also used to make the code more readable.
Comments start with a #, and Python will render the rest of the line as a comment:
Comments are not executed as part of the code.
#This is a comment
print("Hello, World!")
1. Syntax.
"Hello, World!" is the first code written by programmers.
This helps you see that your code works and that Python is set up correctly.
print("Hello, World!")
Typing this code and running it gives the computer instructions to display Hello, World! on
the computer screen.
2. Variables
Variables are containers that store data values. They can hold different types of data, like
names or numbers.
Variables are like labels that are used to refer to data.
They are created the moment you assign value to it.
x = 5
y = "John"
print(x)
print(y)
Examples:
python
Copy code
name = "Alice" # String variable
age = 13 # Integer variable
height = 1.60 # Float variable
is_student = True # Boolean variable
name stores the text "Alice".
age stores the number 13.
height stores the number 1.60.
is_student stores the boolean value True.
You can change the value stored in a variable:
python
Copy code
age = 14
print(age) # Output: 14
3. Data Types
The following are some of the types of data that can be handled by Python:
1. Strings (Text): Strings are written inside quotes. They are used for text.
python
greeting = "Hello, World!"
print(greeting) # Output: Hello, World!
2. Integers: These are whole numbers for example, 0, 1, 2, 3, 4, e.t.c. They are used for
counting or indexing.
python
number_of_students = 25
print(number_of_students) # Output: 25
3. Floats: These are decimal numbers. They are used for precise measurements.
python
price = 19.99
print(price) # Output: 19.99
4. Booleans: Used for logical operations (True/ False).
python
is_raining = False
print(is_raining) # Output: False
5. Basic Operations
Python can be used to perform various operations on numbers and strings:
Examples:
python
# Arithmetic Operations
sum = 5 + 3 # Addition
difference = 10 - 4 # Subtraction
product = 2 * 3 # Multiplication
quotient = 8 / 2 # Division
remainder = 7 % 3 # Modulus (Remainder)
exponent = 2 ** 3 # Exponentiation (2 raised to the power of 3)
# String Operations
greeting = "Hello" + " " + "World!" # Concatenation (Joining strings)
repeat = "Ha" * 3 # Repetition
Each of these lines does a calculation and stores the result in a variable.
ADDITION: PROMPTING THE USER TO ENTER TWO NUMBERS.
We can prompt the user to enter values in python by using the ‘input’ command.
Example: input(“Enter your name: “)
The command above will prompt a user to enter their name.
Below is an example of how we can prompt the user to enter the value of two numbers and print the
sum of those two numbers:
5. Control Structures
1. If Statements:
Used to make decisions in your code based on conditions.
They let your program do different things based on different conditions.
Examples:
python
age = 16
if age < 18:
print("You are a minor.")
else:
print("You are an adult.")
1. Loops: These allow you to repeat actions multiple times.
There are two main types of loops in Python: for loops and while loops.
For Loops:
o Used to repeat actions a specific number of times.
Examples:
Python
# For loop: repeats a fixed number of times
for i in range(5): # Range from 0 to 4
print("This is loop number", i)
While Loops:
o Used to repeat actions as long as a condition is true.
Examples:
python
count = 0
# While loop: repeats as long as a condition is true
while count < 5:
print("Counting", count)
count += 1 # Increment count by 1
The for loop runs the code inside it 5 times, printing the loop number each time.
The while loop keeps counting up to 5, printing the current count each time.
6. Functions
Functions are blocks of code that perform a specific task. They help you reuse code and
make your programs more organized.
Examples:
python
def greet(name):
print("Hello, " + name + "!")
greet("Alice") # Output: Hello, Alice!
greet("Bob") # Output: Hello, Bob!
This function greet takes a name as an input and prints a greeting. You can call this function
with different names to greet different people.
You can also have functions that return values:
python
def add(a, b):
return a + b
result = add(3, 4)
print(result) # Output: 7
7. Lists
Lists are used to store multiple items in a single variable. They are ordered and changeable.
python
# Creating a list
fruits = ["apple", "banana", "cherry"]
# Accessing list items
print(fruits[0]) # Output: apple
Here, fruits is a list that contains three items: "apple", "banana", and "cherry". The code
print(fruits[0]) prints the first item in the list.
# Changing list items
fruits[1] = "blueberry"
print(fruits) # Output: ["apple", "blueberry", "cherry"]
# Adding items to a list
[Link]("orange")
print(fruits) # Output: ["apple", "blueberry", "cherry", "orange"]
# Looping through a list
for fruit in fruits:
print(fruit)
# Output:
# apple
# blueberry
# cherry
# orange
Practice Exercises
1. Simple Calculator:
Write a program that asks for two numbers and then prints their sum, difference, product,
and quotient.
python
Copy code
# Simple Calculator
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2
print("Sum:", sum)
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)
2. Favorite Fruit:
Write a program that asks for your favorite fruit and then prints a message saying "I like
[fruit] too!"
python
Copy code
# Favorite Fruit
favorite_fruit = input("What is your favorite fruit? ")
print("I like " + favorite_fruit + " too!")
3. Count to Ten:
Write a program that prints the numbers from 1 to 10 using a loop.
python
Copy code
# Count to Ten
for i in range(1, 11):
print(i)