0% found this document useful (0 votes)
14 views14 pages

REVISION G8 Python

The document is a guide for Grade 8 students at Pui Kiu College on Python programming, covering fundamental concepts such as output statements, variables, data types, and operators. It includes detailed explanations of selection statements, boolean conditions, and iteration statements like for loops. The content aims to provide a foundational understanding of programming with Python, emphasizing practical coding examples.

Uploaded by

zuma.deshaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views14 pages

REVISION G8 Python

The document is a guide for Grade 8 students at Pui Kiu College on Python programming, covering fundamental concepts such as output statements, variables, data types, and operators. It includes detailed explanations of selection statements, boolean conditions, and iteration statements like for loops. The content aims to provide a foundational understanding of programming with Python, emphasizing practical coding examples.

Uploaded by

zuma.deshaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pui Kiu College

G7 ICT 2012-2013 – Programming with Kodu

REVISION GRADE 8 PYTHON

Name: ______________________

Class: ______________________

Class No.: ______________________

Pui Kiu College | Information and Communication Technology | Chapter 5: Programming with Kodu 0
GRADE 8 PYTHON

CONTENTS

INTRODUCTION TO PYTHON PROGRAMMING 2

OUTPUT STATEMENT 2
PYTHON COMMENTS 2
INPUT STATEMENT 2
VARIABLES 3
DATA TYPES IN PYTHON 3
PYTHON CASTING (TYPE CONVERSION) 4
PYTHON ARITHMETIC OPERATORS 4
PYTHON ASSIGNMENT OPERATORS 5
INPUT AND DATA CONVERSION 6

SELECTION STATEMENTS 6

BOOLEAN CONDITIONS 6
BOOLEAN OPERATORS 7
IF STATEMENT 7
IF...ELSE STATEMENT 8
IF...ELIF...ELSE STATEMENT 8
NESTED IF STATEMENTS 9
LOGICAL OPERATORS 10
ITERATION STATEMENTS (I): FOR LOOP 11
NESTED FOR LOOP 13

2025-2026 Term 1 Grade 9 ICT Page 1


GRADE 8 PYTHON

Python is a widely-used, high-level programming language known for its simplicity and flexibility

INTRODUCTION TO PYTHON PROGRAMMING

OUTPUT STATEMENT

The print() function is used to display output on the screen. Always use lowercase print, followed by
parentheses () containing the text or variables you want to display. You can print strings, numbers, or even
multiple variables by separating them with commas.

print("Hello, World!") # Prints a string

print(5) # Prints a number

print("My age is", 16) # Prints a string and a number

Strings must be enclosed in either single (') or double (") quotation marks. When printing multiple items,
commas ensure that each item is separated by a space in the output.

PYTHON COMMENTS

Comments are used to explain code and improve its readability.

# This is a comment explaining the code below

print("Hello, World!") # This prints a message to the screen

# print("This line is disabled and won't run")

Comments start with # and are ignored by Python. They can be placed above a line of code or at the end
of it. Comments are useful for adding documentation or for disabling code without having to delete it.

INPUT STATEMENT

The input() function allows you to receive input from the user during program execution. The input is
always returned as a string, so you may need to convert it to the appropriate data type for further
operations.

name = input("Enter your name: ") # Prompts the user to input

print("Hello, " + name + "!") # Greets the user with their name

2025-2026 Term 1 Grade 9 ICT Page 2


GRADE 8 PYTHON

VARIABLES

In Python, there is no need to declare variable types, as the language automatically determines the type
based on the value assigned. Additionally, variables can change their type during runtime. However, it's
important to follow naming rules:

 Must start with a letter or an underscore (_).


 Can only contain letters, numbers, and underscores (_).
 Case-sensitive: age, Age, and AGE are considered different variables

name = "Alice" # A string variable

age = 16 # An integer variable

age = "sixteen" # Python allows changing the type of a variable

DATA TYPES IN PYTHON

In Python, there are two main types of numbers:

 Integer (int): Represents whole numbers, which can be positive or negative (e.g., 5, -10).
 Floating Point (float): Represents numbers with decimal points, allowing for fractional values (e.g.,
3.14, -0.5).

A string is a sequence of characters enclosed in single or double quotes.

Strings are used to represent text in Python. You can also concatenate (combine) strings using the +
operator.

name = "Alice" # String

greeting = "Hello, " + name # Concatenation

print(greeting) # Output: Hello, Alice

In Python, you can check the type of a variable using the type() function. This is useful when you want to
confirm the data type of a variable.

x = 3.14

print(type(x)) # Output: <class 'float'> (Floating-point number)

print(type(x)) # Output: <class 'int'> (Integer)


2025-2026 Term 1 Grade 9 ICT Page 3
print(type(y)) # Output: <class 'float'> (Floating-point
number)
GRADE 8 PYTHON

PYTHON CASTING (TYPE CONVERSION)

Casting in Python is used to convert a variable's data type. This is useful when you want to change the type
of a value for operations that require specific data types, such as performing math on numbers or
concatenating strings.

Common Casting Functions:

 int(): Converts strings or floats to integers (decimals are truncated).


 float(): Converts integers or strings to floating-point numbers.
 str(): Converts numbers (integers or floats) or other data types to strings.

# Integer to float

x = 5

y = float(x) # Converts 5 to 5.0

print(y) # Output: 5.0

# String to integer

age = "18"

age_num = int(age) # Converts the string "18" to the integer 18

print(age_num) # Output: 18

PYTHON ARITHMETIC OPERATORS

Arithmetic operators in Python are used to perform basic mathematical calculations.

Operator Description

+ Addition: Adds two values

- Subtraction: Subtracts right from left value

* Multiplication: Multiplies two values

/ Modulus: Returns remainder of division

% Modulus: Returns remainder of division

** Exponent: Raises left value to the power of righ

// Floor Division: Divides and removes decimal par

2025-2026 Term 1 Grade 9 ICT Page 4


GRADE 8 PYTHON

a = 10

b = 20

print(a + b) # Output: 30 (Addition)

print(a - b) # Output: -10 (Subtraction)

print(a * b) # Output: 200 (Multiplication)

print(b / a) # Output: 2.0 (Division)

print(b % a) # Output: 0 (Modulus)

print(a ** 2) # Output: 100 (Exponent)

print(b // 3) # Output: 6 (Floor Division)

PYTHON ASSIGNMENT OPERATORS

The basic assignment operator (=) is used to assign a value to a variable. The value on the right-hand side
of the = is stored in the variable on the left-hand side.

x = 15 # Assigns the value 15 to the variable x

y = 2 * 3 # The expression 2 * 3 is evaluated, and 6 is assigned to y.

z = (x + y) # The sum of x and y is assigned to z.

print(z) # Output: 21

Compound assignment operators (+=, -=, *=, /=) combine arithmetic operations with assignment, allowing
you to both perform an operation and update the variable in a single step.

x = 10

x += 5 # Equivalent to x = x + 5

print(x) # Output: 15

y = 20

y -= 7 # Equivalent to y = y - 7

print(y) # Output: 13

2025-2026 Term 1 Grade 9 ICT Page 5


GRADE 8 PYTHON

a = 4

a *= 3 # Equivalent to a = a * 3

print(a) # Output: 12

b = 15

b /= 3 # Equivalent to b = b / 3

print(b) # Output: 5.0

INPUT AND DATA CONVERSION

The input() function returns the user's input as a string by default. To perform calculations or operations
with this input, it needs to be converted to the appropriate data type, such as an integer (int) or a floating-
point number (float).

age = input("Enter your age: ") # Input is stored as a string

age = int(age) # Convert the input to an integer

print("You will be " + str(age + 1) + " next year!")

The input() function can also be wrapped inside int(), which converts the string entered by the user into an
integer immediately. This makes the code more concise and efficient.

age = int(input("Enter your age: "))

print("You will be " + str(age + 1) + " next year!")

SELECTION STATEMENTS

Selection, or decision-making, is key in both daily life and programming. Just like we make choices based
on conditions (e.g., "If it’s sunny, wear a hat; if it’s raining, take an umbrella"), programs use selection
statements to execute specific code when certain conditions are met.

BOOLEAN CONDITIONS

Boolean conditions are the foundation of selection statements. These conditions evaluate to either True
or False, and based on the result, the program will decide whether to execute the code beneath (indented
under) the condition.

2025-2026 Term 1 Grade 9 ICT Page 6


GRADE 8 PYTHON

BOOLEAN OPERATORS

Boolean conditions are usually written using logical operators to compare values. These operators include:

Operator Description

== Equal to

!= Not equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

age = 18

if age == 18: # True because age is exactly 18.

print("You are 18 years old.")

if age >= 20: # Nothing is printed since this condition is False.

print("You're old enough to drive.")

if age != 21: # True because age is not equal to 21.

print("You're not 21 yet.")

IF STATEMENT

The if statement is used to execute a block of code only when a specified


condition is True. If the condition is False, the code inside the if block is
skipped.

Syntax:

IF Condition:

Block of code to execute if condition is


True

2025-2026 Term 1 Grade 9 ICT Page 7


GRADE 8 PYTHON

num = 3

if num > 0:

print("Positive number") # It is executed since num > 0 is True

IF...ELSE STATEMENT

The if...else statement allows the program to run one block of code when the condition is True, and
another block when the condition is False

Syntax:

if Condition:

# Block of code to execute if


condition is True

else:

# Block of code to execute if


condition is False

num = -5

if num > 0:

print("Positive number")

else:

print("Negative number") # It is executed since num > 0 is False

IF...ELIF...ELSE STATEMENT

The if...elif...else statement allows checking for multiple conditions.


The program will evaluate each condition one by one.

Once it finds a True condition, it will execute the corresponding block and
skip the remaining conditions.

2025-2026 Term 1 Grade 9 ICT Page 8


GRADE 8 PYTHON

if Condition 1:

# Block of code to execute if condition1 is True

elif Condition 2:

# Block of code to execute if condition2 is True

else:

# Block of code to execute if all conditions are False

num = 0
if num > 0:
print("Positive number")
elif num == 0:
print("Zero") # This line is executed because num == 0 is True
else:
print("Negative number")

NESTED IF STATEMENTS

A nested if statement is an if statement placed inside another if, elif, or else block. This allows for more
complex decision-making.

num = 10
if num > 0:
print("Positive number")
if num % 2 == 0:
print("Even number") # Executed if num > 0 and num is even
else:
print("Odd number") # Executed if num > 0 but num is odd
else:
print("Negative number or Zero") # Executed if num is not greater
than 0

2025-2026 Term 1 Grade 9 ICT Page 9


GRADE 8 PYTHON

LOGICAL OPERATORS

Logical operators are used to combine multiple conditions into a single condition. Python provides three
logical operators:

Operator Description

and True if both conditions are True

or True if at least one condition is True

not Reverses the result, True becomes False, and vice versa

age = 20
if age > 18 and age < 30:
print("You are a young adult")
# This will run because both conditions are True
else:
print("You are not a young adult")
# This will run if either condition is False

age = 18
if age == 18 or age == 21:
print("You are either 18 or 21 years old.")
# Runs because age == 18 is True
else:
print("You are neither 18 nor 21.")
# Runs if both conditions are False

is_raining = False
# Boolean variable indicating whether it is raining or not
if not is_raining: # Runs because is_raining is False
print("You don't need an umbrella.")

2025-2026 Term 1 Grade 9 ICT Page 10


GRADE 8 PYTHON

ITERATION STATEMENTS (I): FOR LOOP

In computer programming, iteration refers to the process of repeating a block of instructions. In Python,
the for loop is commonly used to iterate through a sequence of numbers, and one of the most efficient
ways to do this is by using the range() function.

for variable in range(start, stop, step):

# Code block to be executed

The range() function returns a sequence of numbers


starting from 0 by default, increments by 1 by default,
and stops before a specified number. Parameters of
range():

 start (optional): The starting number of the


sequence (default is 0).
 stop (required): The number at which the sequence stops (not included in the output).
 step (optional): The amount by which the sequence increments (default is 1).

Example 1: One Parameter (stop)

for i in range(5):
print(i)

Output:
0
1
2
3
4

Example 2: Two Parameters (start, stop)

for i in range(5, 12):


print(i, end="")

Output:

567891011

2025-2026 Term 1 Grade 9 ICT Page 11


GRADE 8 PYTHON

Example 3: Three Parameters (start, stop, step)

for i in range(1, 20, 3):


print(i)

Output:

1
4
7
10
13
16
19

for i in range(5, 0, -1):


print(i, end=" ")

Output:

54321

In Python, you can use the else block with a for loop. The else part of a for loop will be executed after the
loop finishes all iterations.

for i in range(1, 6):


print(i, end=" ")
else:
print("\nLoop finished successfully!")

Output:

12345
Loop finished successfully!

In Python, \n is a newline character. It is used to insert a new line in a string.

2025-2026 Term 1 Grade 9 ICT Page 12


GRADE 8 PYTHON

NESTED FOR LOOP

A nested for loop is a for loop inside another for loop. The inner loop runs completely every time the outer
loop runs once.

for outer_variable in range(outer_start, outer_stop, outer_step):

for inner_variable in range(inner_start, inner_stop, inner_step):

# Code block to execute for every combination of


outer_variable and inner_variable

for i in range(1, 3): # Outer loop (controls rows)


for j in range(1, 4): # Inner loop (controls columns)
print("i = " + str(i) + ", j = " + str(j))

Output:

i = 1, j = 1

i = 1, j = 2

i = 1, j = 3

i = 2, j = 1

i = 2, j = 2

i = 2, j = 3

for i in range(1, 4): # Outer loop (number of rows)


for j in range(i): # Inner loop (number of stars in each row)
print("*", end="")
print() # Moves to the next line after each row

Output:

**

***

2025-2026 Term 1 Grade 9 ICT Page 13

You might also like