Introduction to Python Programming
Learning Outcomes:
Upon successful completion of this lesson, you will be able to:
Identify the different components of computer systems
Describe the background and characteristics of Python
Explain the writing, compilation and execution of a Python program
Why learn to Program
Hardware Components of a Computer System
Software
What is Python?
Installing Python & IDE
A Python Program
Errors (Bugs)
Summary
REVIEW
Knowledge Check
Review Questions
Lesson 1 of 10
Why learn to Program
To most of you, this module should be your first programming module. You might be
wondering why do we need to learn programming languages. Let’s watch a short video
on “Why learn to Program”
[Link] (about 6 mins)
Most of the things that you see in the video are not possible unless humans know how to
communicate with the computers. Since computers only understand programming
languages, we need to learn to program. The first step in learning programming is to
know the hardware and software components of a computer system.
Lesson 2 of 10
Hardware Components of a Computer System
There are 5 main hardware components: Input Devices, Central Processing Unit (CPU),
Main Memory, Secondary Storage Devices and Output Devices. Each of them performs a
certain function.
Input Devices
Input Devices collects data for the processing by the computer. The input device can be a
keyboard, a mouse, a microphone or a touchscreen.
Central Processing Unit (CPU)
The CPU interprets and executes instructions. Instructions are what we write in a
program. The CPU has three components:
Control Unit: extracts instructions from memory and decodes and executes them
Arithmetic Logic Unit (ALU): handles arithmetic and logical operations
Registers are internal memory units that are used in the ALU's information processing.
Main Memory
Main Memory stores data and instruction in the computer. The computer can only
manipulate data that is in main memory.
Secondary Storages Devices
Secondary Storage stores data for a long period of time even if there is no power supply.
Examples are hard disks and thumb drives.
Output Devices
Output Devices presents data to the users. It can be a printer or monitor.
Knowledge Check
The diagram shows the interactions between the different hardware components of the
system. Do you know all of them?
Input Devices
Collect data. For example, keyboard and mouse.
Secondary Storage
Store data for a long period of time even if there is no power supply. For example,
hard disks, thumb drives
Main Memory
Store data and instructions
Output Devices
Presents data. For example, printer and monitor.
Central Processing Unit (CPU)
Interpret and execute instructions.
Lesson 3 of 10
Software
For the computers to work, hardware alone is not enough. They need software which are
programs containing instructions for the computer to follow. There are 2 main types of
software: System Software & Application Software.
System Software controls and manages the basic operation of a computer, such as
Microsoft Windows or Linux. Can you name one more?
Application Software are programs that people spend most of their time running on a
computer such as Microsoft word, emails or games. Can you name a few more?
Lesson 4 of 10
What is Python?
Story of Python
Python is a widely used high-level programming language for general-purpose
programming, created by Guido van Rossum and first released in 1991. To know more
about the creator of Python, spend a few minutes to watch this video:
The Story of Python, by Its Creator, Guido van Rossum (about 4 mins)
Features of Python
Python is a powerful, popular, quick and easy language for development. It has the
following features:
Light-weight
Lightweight programming languages have simple syntax so they
could be learnt easily and quickly. They required small memory
and is easy to implement.
Dynamically typed
A language is dynamically-typed if the type of a variable is
checked during run-time. We will learn more about this in the
later chapters. Besides Python, common examples includes
JavaScript, PHP, and Ruby.
Object-oriented
Object-oriented programming languages use the concept of
objects in programming. This is very close to how we view the
real-world. For example, to record book information (such as
isbn, author, and title), object-oriented programming creates
book objects and store the information in each object. The aim is
to bind together the data and the codes that operate on them so
that no other part of the program can change the data.
Easily integrated with C/C++
As there are many systems implemented using C/C++, this is
a important feature in Python.
REPL (Read–eval–print-loop)
Python is an interpreted language and we can run simple Python
expressions and statements in an interactive programming
environment. After you installed Python, you can open a Python
Shell by typing "Python" or "IDLE" in the Windows search box.
Lesson 5 of 10
Installing Python & IDE
32-bit or 64-bit
In computing, there are two type of processors, either 32-bit and 64-bit. These
processors tells us how much memory a processor can have access from a CPU register.
A 32-bit system can access 232 memory addresses
A 64-bit system can access 264 memory addresses
In short, any amount of memory greater than 4 GB can be easily handled by a 64-bit
system.
To check the system type of your laptop, click on the search icon, at the left bottom and
enter "about your PC". A screen will appear as shown:
Downloading & Installing Python
We can download the latest Python version for Windows from the Python Official
Website.
[Link]
For a 32-bit system, you will need the Windows x86 executable installer. If your Windows
is a 64-bit version, you need to download the Windows x86-64 executable installer.
Click on "Download" on the menu bar and scroll down.
Choose 3.x (any version above 3.6 is fine) to the next screen.
Scroll down to the Files section
Select either the Windows x86-64 executable installer or Windows x86 executable installer.
Run the Python Installer once downloaded.
Make sure you select the Install launcher for all users and Add Python 3.X to PATH checkboxes.
Select Install Now.
Once the installation is successful, click on the search icon and enter "idle" as shown:
If Python is installed successfully, the Python shell appears as shown.
Downloading & Install Intergrated Development
Environment (IDE)
Python is the interpreter software used to convert the codes you wrote into a program to
run on your computer. We need another software for us to write the python codes. This
software is know as our Integrated Development Environment (IDE). There are many
different IDE that is available to be used to write and run a python program.
Your lecturer will provide you with the details on the installation of your IDE. Please
follow the instructions given to install your IDE. Do install python first before installing
your IDE.
We will be coding our first Python program, using the IDE, in the Practical session.
Lesson 6 of 10
A Python Program
A computer program is a collection of instructions that performs a specific task when
executed by a computer.
A computer requires programs to function, and typically executes programs in a central
processing unit (CPU).
A Python program can be executed interactively or as a script file.
Interactive vs Script
For interactive, we type directly in the Python shell one statement at a time and it
interprets and respond with the result immediately. Good for experiments with 3-4 lines
of code.
The Python shell can be launched by typing "idle" in the Windows search box. Take a
moment to understand the codes. We will discuss more during Practical.
For script, we enter a sequence of statements into a file using a text editor (IDE) and tell
Python to execute the statements in the file. The script file ends with .py extension.
We can use a text editor to write Python codes, save it as [Link] file and run it in the
"Command Prompt" screen.
The Python codes saved in [Link]
Command to run the [Link] : python C:\Temp\[Link]
Compilation vs Interpretation
Computers do not understand the programs written by programmers. These programs
are known as high-level language programs. For example, C, C++, Java, Python, etc.
A compiler is used to translate the high-level language programs to a machine language
programs (e.g., an .exe file if the code is intended to be run under MS Windows). The
machine language programs can then be executed by the computer anytime, without
being re-compiled.
Another way to translate the high-level language programs is to use interpreters. The
interpreter reads each instructions, converts it to machine language instruction and
immediately executes it. This process repeats for every instruction in the program.
Interpreters typically do not create separate machine language programs.
To understand more about compilation and interpretation, watch the following video:
Compilation vs Interpretation (about 6 mins)
Lesson 7 of 10
Errors (Bugs)
When a program is not working correctly, there are errors (bugs) in the program.
Program errors are categorized into 3 different types:
Syntax Error
Syntax errors are mistakes in the way that the code is written. Common syntax errors include
spelling mistakes, incorrect use of punctuation and wrong indentation.
Example:
prin("Good Morning")
Runtime Error
Runtime errors cause the program to crash even if it appears to be nothing wrong with the program
code. Running out of memory or wrong type conversion will often cause a runtime error.
x = int( input( "Enter a number: " ) )
Output:
Enter a number : a
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: 'a'
The runtime error occurs at the line when the character ‘a’ is being converted to an integer.
Program crashes and exit immediately.
Some runtime error(s) may be detected by IDE before it is executed but there are still considered
runtime error because the interpreter is still able to understand the codes to run them. Before
execution, IDE are smart enough to flag out some straightforward code that may result in a runtime
error. The program below is one example.
b=a+5
Output:
b=a+5
^
NameError: name 'a' is not definednter a number
Semantic (Logic) Error
Semantic errors, or logic errors, happen when the program produces different results from what
we have expected. Program with semantic errors will execute without any errors being reported.
x = int( input( "Enter the first number: " ) )
y = int(input( "Enter the second number : " ))
print( "The sum is" , x * y )
Output:
Enter the first number : 3
Enter the second number : 5
The sum is 15
Semantic or logic error occurred when the output of the program is incorrect.
Syntax, Runtime and Logical Errors in Python (about 8 mins)
Knowledge Check
Determine if the Python statement is correct or incorrect.
x = input("Enter the first number :"
Incorrect. The statement has a
The above Python statement has a syntax error.
runtime error.
x=5
y = 10
print("x * y is", x + y)
The output is x * y is 15 Correct.
There is a logic error in the code
segment.
x=5
y=0
print(x/y)
Incorrect. It should be a
runtime error.
There is a syntax error in the print
statement.
Lesson 8 of 10
Summary
Key Takeaways
We have covered the following:
The different components of computer systems.
The background and characteristics of Python.
The writing, compilation and execution of a Python program.
Lesson 9 of 10
Knowledge Check
Question 1
Match the name of the hardware components to its description.
SUBMIT
Question 2
When the program can run but produces an output different from what is
expected, the program has _______ errors.
Type your answer here
SUBMIT
Question 3
For each of the following, identify the kind of error (syntax, semantic or runtime), if
applicable.
print(Hello World)
Type your answer here
SUBMIT
a = 3 + (5 + 9)
Type your answer here
SUBMIT
print(1/0)
Type your answer here
SUBMIT
Lesson 10 of 10
Review Questions
There are 4 questions in this review. A score 75% is need to pass this review.
Question
01/04
Which of the following is an input device?
Central Processing Unit
Scanner
Printer
Microphone
Monitor
Question
02/04
When a program has no syntax and runtime error, it will always produce correct
output.
True
False
Question
03/04
Which of the following statement has syntax error?
print(Good Morning)
a = (1 + 2 ) * 3
print('8' * 3)
print(10)
Question
04/04
Select all the correct statements.
An extra bracket in a Python statement will result in a runtime error.
Runtime error can only occur when the program is being executed.
A program with a syntax error will always produce the wrong results.
A spelling mistake is a logic error.
A program with syntax errors cannot be run.