Unit 1
Programming
Basics
Learning Outcomes
GC1.1 Write comments to make the code more readable and sharable
by others.
GC1.2 Create variables to store and process values.
GC1.3 Create string variable using single quotes and assign multiline
string to variable using three quotes.
GC1.4 Use Python- built in data types and practice the concept of
casting by creating variables and specifying their data types such as
str(), int(), float()
GC1.5 Perform common mathematical operations using the main
arithmetic operators +,-,*,/ on numeric values and variables.
What is Python?
Python is a popular programming language. It was created by
a dutch programmer, Guido van Rossum, and released in
1991.
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
Python Syntax
The Python syntax defines a set of rules that are used to
create a Python Program.
Python is a case sensitive programming language.
Thus, ATS and ats are two different variables in Python.
Python is very similar to English language and is very easy to
learn and understand.
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Python uses indentation to indicate a block of code.
Example:
Indentation error – check your gaps and spaces in the block of code.
Variables
In Python, variables are created when you assign a value to it:
Example
Variables do not need to be declared with any particular type,
and can even change type after they have been set.
Watch the video
Variables
ActivityPart A: Complete the table below:
Statement Variable Name Data Value store
name = “Ali” name Ali
score = 7.5 score 7.5
thisYear = 2024 thisYear 2024
yearBorn = 2010 yearBorn 2010
age = thisYear - yearBorn age 14
Activity Part B: Complete the table below:
Data Value
Problem description Python Statement
stored
weight = 4000 4000
1. A car weighs 4000 kg. How would you store the
weight value in a variable?
2. Ali had three lives at the start of a game. He lives = 3 - 1 2
just lost one life. How many lives does Ali have
left?
3. There are 30 students in grade 7. Store the students = 30 30
total number of students in a variable.
A=L*W 50
4. Calculate the area (A) of the shape by
multiplying 10 cm length (L) and 5 cm width (W).
Word Wall Activity
https://wordwall.net/play/21764/435/943
Python Operators
Operator Operation Example
+ Addition z=x+y
- Subtraction Answer = 4 - 2
Average = total /
/ Division
number
* Multiplication Total = cost * price
Exponent (or power
** Num2**2
of)
= Assignment number Num = 23
Operand & Operator
The instruction you give a computer has two parts; An operand and an operato
Operator – tells the computer what to do with the data
Operand – Is the data it works on
Data Types
Data types:
Data types are used to classify the data used in a computer program.
Every value in Python has a datatype. Since everything is an object in
Python programming, data types are actually classes and variables are
instance (object) of these classes.
Changing Data Types
When you enter data using a keyboard, the interpreter will usually store
it as a string. You need to tell the interpreter to treat it as an integer or
a real number if that is what you need in your program. The function
below shows you how to do that.
Activity Part A: Match the data types:
Activity Part B: Fill the table with what type
of data type would you use
Data to store Data Type
“Laptop” String
1200 Integer
0.001 Float
True Boolean
Activity Part C: Identify python program
Can you identify :
• variables names and data types
• Python operators and calculations
What is the expected output?
Choose the correct output when you run the program:
What is the expected output?
Choose the correct output when you run the program:
Project (5.1 –
5.5)
Sales Data
Analysis
Classkic
k
Expected Output:
In this program,
identify:
• Comment
• Variables
• Data type
• Print command
Now write this program
without any errors in
any python compiler and
paste the code + result in
classkick – Simple
Calculator
Remember if the teacher
fixes your errors you will
lose 1 mark.
Learning Outcomes
GC1.6 Use for loop to print each item in a given list.
GC1.7 Use for loop to print each letter in a string.
GC1.8 Loop through a set of code a specified number of times using range()
function
GC1.9 Use while loop to repeat a set of statements as long as a condition is true.
GC1.10 Use input() function to read the user input and use it in the program
GC1.11 Use If statement to perform simple conditional statement using the basic
logical operations as <,>,==,!= .
GC1.12 Write an if else statement using proper syntax to create a conditional
statement that trigger a sequence of commands based on a Boolean condition
GC1.13 Write code to create a function and call it using proper syntax.
Loops
Python programming language provides two types of loops
– For loop and While loop to handle looping requirements.
Python provides three ways for executing the loops.
While all the ways provide similar basic functionality, they
differ in their syntax and condition-checking time.
While Loop
While loop is used to execute a block of statements
repeatedly until a given condition is satisfied. When the
condition becomes false, the line immediately after the loop in
the program is executed.
All the statements indented by the same number of character
spaces after a programming construct are considered to be
part of a single block of code. Python uses indentation as its
While Loop Syntax
Let’s see a simple example of while loop in Python. The given Python
code uses a ‘while' loop to print “Hello Grade 8” three times by
incrementing a variable called ‘count' from 1 to 3.
While Loop with else
statement:
The code prints “Hello Grade 8” three times using a ‘while' loop
and then, after the loop, it prints “Enjoy Python
Programming” because there is an “else” block associated with
the ‘while' loop.
Infinite While Loop:
The code uses
a ‘while' loop with the
condition (count ==
0). This loop will only
run as long as count is
equal to 0.
Since count is initially
set to 0, the loop will
execute indefinitely
because the condition
is always true.
What is the expected output?
Choose the correct output when you run the program:
What is the expected output?
Choose the correct output when you run the program:
For Loops
For loop is used for iterating over a sequence (that is either
a list, a tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once
for each item in a list, tuple, set etc.
For Loop Syntax
The code uses a Python for loop that iterates over the values from 0
to 3 (not including 4), as specified by the range(0, n) construct. It
will print the values of ‘i' in each iteration of the loop.
Match the for loop with the correct
output:
Project (1.6 –
1.13)
Password
Strength Checker
Classkic
k
Expected Output:
In this program,
identify:
• Comment
• Variables
• Data type
• Loops
Now write this program
without any errors in
any python compiler and
paste the code + result in
classkick – Simple
Calculator
Remember if the teacher
fixes your errors you will
lose 1 mark.
Self Learning