Teach Computer
Science
GCSE Python
Introduction to
Python
teachcomputerscience.co
m
2
Lesson Objectives
Students will learn about:
▪ Using Trinket for Python programming
▪ Placing comments in a program to improve readability
▪ Assigning values to variables and printing them
▪ Simple procedures in Trinket
teachcomputerscience.co
m
1.
Content
teachcomputerscience.co
m
4
Introduction to
programming
▪ A computer program is a detailed, step-by-step set of instructions
telling a computer exactly what to do.
▪ The programs are executed on a computer to obtain the desired
results.
▪ For example, consider a program that calculates nutritional
requirements. It requires input data such as age, height, weight
and information about physical activity. Based on these
parameters, the program calculates the nutrition requirements
personalised for each user. teachcomputerscience.co
m
5
Programming
▪ In computing, software controls the hardware components of a
computer. The process of creating software is called programming.
▪ Before writing a program, the problem is broken into a series of
logical steps called an algorithm.
▪ An algorithm is represented in the form of pseudocode or a
flowchart.
teachcomputerscience.co
m
6
Linear search algorithm
position = 0
item=enter (“enter the item to be found”)
len=numberOfItems-1
while (position<len and list[position]!= item)
position = position + 1
endwhile
if position> len then
print (“item not found”)
else
print (“item found at position ” position)
endif
Note: != means not equal to teachcomputerscience.co
m
7
Python
▪ Python is a high-level, open-source, interpreted and interactive
programming language.
▪ It also supports an object-oriented programming technique.
▪ Python is designed to be easily understandable.
▪ Some of the features of Python are:
• Python is interpreted
• Python has a simple syntax
• Python is interactive
• Python is a dynamically typed language
• Python code is portable
teachcomputerscience.co
m
8
Python is interpreted
▪ Programs in some
programming languages are
to be compiled before
executing them. However,
Python is an interpreted
language.
▪ There is no need to compile
the program before
executing it.
▪ Python is processed at
teachcomputerscience.co
runtime by the interpreter. m
9
Compiler
▪ A compiler is a computer program that translates a program written in
a high-level language into a machine code that can be directly used
by a computer to perform required tasks.
▪ Once compiled, the same code can be used again multiple times
without recompilation.
▪ A compiler optimises the code and errors are picked up only after the
complete compilation of a program.
teachcomputerscience.co
m
10
Interpreter
▪ An interpreter is a computer program that reads a statement from a
program written in a high-level language, performs the action
specified by the statement and then proceeds to the next statement
and so on.
▪ In case an error is found, the process stops there, and the interpreter
prompts the user to correct the error.
▪ Code does not get optimised.
▪ No object file is produced. Hence, the program must be interpreted
every time it is run.
▪ Unlike compilers, the program runs only in the presence of its teachcomputerscience.co
m
interpreter.
11
Python has a simple
syntax
▪ The simple syntax in Python enables Program that finds the greatest
us to use some of the features in a of two numbers (Python)
simpler and more understandable
manner compared to other num1 = int(input("Enter num1:
programming languages. "))
num2 = int(input("Enter num2:
▪ Other programming languages use "))
punctuation marks such as curly if num1>num2:
brackets {} for defining the control print(num1, "is greater")
flow. else:
▪ In Python, indentation is used to define print(num2, "is greater")
teachcomputerscience.co
the control flow. m
12
Python has a simple
syntax
▪ The first two statements are input Program that finds the greatest
statements, that receive values of two numbers (Python)
entered by the user.
num1 = int(input("Enter num1:
▪ The if statement selects which
"))
statement has to be printed. num2 = int(input("Enter num2:
▪ Please note the indentation in the if… "))
else structure. if num1>num2:
print(num1, "is greater")
▪ Some programming languages require
else:
curly brackets for this purpose.
print(num2, "is greater")
teachcomputerscience.co
m
C program C++ program 13
#include<stdio.h> #include<iostream>
int main() using namespace std;
{ int main()
▪ Program to
int num1, num2; {
find the
scanf(“%d int num1, num2;
greatest of
%d”,&num1,&num2); cin >> num1 >> num2;
two
if(num1 > num2) if(num1 > num2)
numbers in
{ {
C & C++.
printf(“%d is cout << num1 << ” is greater”
greater”,num1); << endl;
} }
else else
{ {
printf(“%d is cout << num2 << ” is greater”
greater”,num2); << endl;
} }
return 0; return 0;
teachcomputerscience.co
} } m
14
Java program
import java.util.*;
public class Main
▪ Program to find {
the greatest of two public static void main(String[] args)
numbers in Java. {
Scanner sc = new
Scanner(System.in);
int num1,num2;
num1 = sc.nextInt();
Comparing these
num2 = sc.nextInt();
codes with Python
if(num1 > num2)
code, we can say that
System.out.print(num1 + ” is
Python is simpler and
greater”);
easier to understand.
else
System.out.print(num2 + ” is
greater”);
} teachcomputerscience.co
m
}
15
Features of Python
iii. Python is interactive
The Python prompt allows its users to interact with the
interpreter directly to design programs. This feature allows us
to test and debug snippets of code.
teachcomputerscience.co
m
16
Features of Python
iv. Python is a dynamically typed language
▪ Variables are labels that are used to represent values stored in computer
memory. In some languages, it is essential to declare variables before using
them. In Python, we do not declare variables before using them.
▪ Please revisit the codes given to find the greatest of two numbers in C, C+
+ and Java. The statement to declare variables is:
int num1, num2;
▪ However, in Python, a variable is automatically assigned a data type (a
data type is a type of data. For example, integer, string, etc.) when it gets
its first value. For example, num1 = int(input("Enter num1: "))
teachcomputerscience.co
m
17
Features of Python
v. Python code is portable
▪ Python code runs on a wide variety of hardware platforms.
The user interface in all these platforms is similar. Python’s
library is also portable.
▪ A disadvantage of Python is that sometimes it is slower than
compiled languages. For example, in large-scale simulations,
compiled languages such as C, C++ or Fortran is preferred.
teachcomputerscience.co
m
18
Learning Python using
Trinket
▪ Trinket is an online tool that allows users to write and run codes in
any browser. Throughout this course, we will be using Trinket to learn
Python.
▪ The Python interactive tool of Trinket can be accessed using this URL:
https://trinket.io/python3
teachcomputerscience.co
m
19
Menu bar
▪ The leftmost option is a menu that allows us to download, share,
embed or email the code. Options are also provided to change the
font size.
▪ The button similar in shape to a play button allows us to execute the
codes. Options are given to switch to interactive/ console mode.
▪ The rightmost option is the share button.
teachcomputerscience.co
m
20
Menu bar
▪ main.py is the file that is opened by default. The program codes are
typed here. Once we press the run option, the code is executed.
▪ To begin, we can start using Python interpreter in interactive mode.
This mode is called console in Trinket. This mode is used to try
snippets of code.
teachcomputerscience.co
m
21
Trinket in console mode
▪ In the Python prompt, >>> symbol is displayed, denoting that the
Python interpreter is waiting for the user’s command.
▪ In programming language, a complete command is called a
statement.
teachcomputerscience.co
m
22
Trinket in console mode
▪ Let us begin with a simple print statement.
>>> print(“Hello!, World”)
▪ When we type this statement and press the Enter key, the Python
interpreter prints the text stated within the quotes and gets ready to
receive the next command.
teachcomputerscience.co
m
23
Trinket in console mode
▪ Let us now try some simple mathematical calculations.
▪ Python offers several operators to perform mathematical operations.
We will learn about them in detail in further topics.
teachcomputerscience.co
m
24
Comments
▪ A comment is text placed in a program for the benefit of human
readers. Comments are ignored by the computer.
▪ Comments start with the # character. For example, let us insert this
code in main.py,
print(“Hello!, World”) #displays the text within the brackets
▪ As we type this statement, we notice that comments are displayed in a
different colour.
teachcomputerscience.co
m
25
Comments
▪ For inserting multiline comments, comments are inserted within triple
single or double quotes. In this program, name is a variable that stores
the value “Harper”. The output of this program is,
teachcomputerscience.co
m
26
Simple procedures
▪ A procedure allows us to group a block of
code under a name. This block of code
can be called anywhere in the program to #procedure Hello
execute the instructions in it. def Hello():
▪ Defining a simple procedure named Hello. print("Hello!, Welcome
The def keyword denotes the start of a to Trinket")
procedure or function definition. When
called, this procedure displays the text
stated.
teachcomputerscience.co
m
27
Simple procedures
▪ It is important to note the
indentation used.
▪ The statement to call this
procedure is very simple as
given,
teachcomputerscience.co
m
28
Simple procedures
#procedure Hello
def Hello(name):
▪ A procedure or function may also print("Hello! “
have a list of parameters that allows +name+ “, Welcome
data to be passed to it. to Trinket")
▪ The procedure Hello is now modified
to print the statement along with a #calling procedure
name. user=“Harper”
▪ The variable user is assigned a Hello(user)
value outside the procedure and is
passed to the procedure.
teachcomputerscience.co
m
29
Simple procedures #procedure Hello
def Hello(name):
▪ print("Hello! “
Procedures can be called multiple
+name+ “, Welcome
times and allow the programmer to
to Trinket")
reuse codes wherever possible.
▪ We will learn more about procedures #calling procedure
later in this course. user=“Harper”
Hello(user)
teachcomputerscience.co
m
30
Let’s review some
concepts
Program Algorithm Python
A detailed, step-by-step set of A problem is broken into a Python is a high-level, open-
instructions telling a computer series of logical steps called source, interpreted and
exactly what to do. an algorithm. An algorithm is interactive programming
represented in the form of language.
The process of creating
pseudocode or a flowchart. Trinket
software is called
programming. Trinket is an online tool that
Features of Python allows a user to write and run
Comments
▪ Python is interpreted codes in any browser.
Single line comments start
▪ Python has a simple syntax
with the # symbol. Procedure
▪ Python is interactive
Multiline comments are stated A procedure allows us to group
within triple single or double ▪ Python is a dynamically typed a block of code under a name.
quotes. language This block of code can be
called anywhere in the
▪ Python code is portable
program to execute the
instructions in it. teachcomputerscience.co
m