Manual Ver 1-0 2022
Manual Ver 1-0 2022
Training Manual
Prepared By
AN INTRODUCTION TO PYTHON
• The entire course will use python IDLE as the platform to develop python script (program).
• Python IDLE can be downloaded from the following link:
https://www.python.org/
Python
Documentation
Online interpreter
Downloadable
interpreter
Interactive shell mode allows user to write python and get the output instantly.
Editor mode allows user to write a set of statements and execute the statement as a
group of statement.
2|Page
Python Programming Training Manual Version 1.0
RESERVED WORDS
The following table lists all reserved words in Python. Each of these words has special meaning in
python and cannot be used as the name of variables or any other identifier in your program.
All reserves words in python were written using lowercase letters only.
3|Page
Python Programming Training Manual Version 1.0
VARIABLES
• In python a variable a name given by the user to point a specific value.
• For examples:
5
number = 5 number
20
score = 20 score
'Jebat'
name = 'Jebat' name
• In python there is no specific variable declaration statement like other languages such as
C++ or Java. However, it is advisable to initialise the variable before using it and maintain
specific naming convention.
Comparison between C++ and Python
C++ Python
4|Page
Python Programming Training Manual Version 1.0
DATA TYPES
• Basic data type in python
1. Integer (int)
2. Decimal (float)
3. String (str)
4. Boolean (bool)
• In python a variable is not associated with specific type of data permanently. A variable
can point to any type of data.
• The keyword type is used to check the current type of data pointed by a variable.
5
>>> number = 5 number
>>> type(number)
<class 'int'>
5
number = 3.4
number
>>> type(number)
<class 'float'> 3.4
number 3.4
number = 'five'
>>> type(number) 'five'
<class 'str'>
5|Page
Python Programming Training Manual Version 1.0
LINE CONTINUATION
• A long statement can be split across two or more lines by terminating each line
(except the last line) with a backslash character (\).
>>> lyric
'Negara Ku, Tanah Tumpahnya Darah kuRakyat Hidup, Bersatu dan
Maju'
6|Page
Python Programming Training Manual Version 1.0
Enter a number : 5
Convert '5' to an
integer value using
int() function
5
number
7|Page
Python Programming Training Manual Version 1.0
CONVERTER FUNCTIONS
• There are three function that can be used to convert string input into numeric types as
follows:
i) int()
This function is used to convert the string input into an integer values as follows:
This function will produce error message if user enters floating point value.
ii) float()
iii) eval()
This function will evaluate the expression to an integer value of floating-point value
as appropriate.
>>> num = eval(input('Enter a number : '))
Enter a number : 4
>>> type(num)
<class 'int'>
>>> num = eval(input('Enter a number : '))
Enter a number : 4.5
>>> type(num)
<class 'float'>
8|Page
Python Programming Training Manual Version 1.0
STRING
Overview and String Declaration
OR
-5 -4 -3 -2 -1
str 0 1 2 3 4
• Square bracket ([) and (]) can be used to access elements of the string.
>>> str[0]
'H'
>>> str[-1]
'o'
>>> str[5]
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
str[5]
IndexError: string index out of range
9|Page
Python Programming Training Manual Version 1.0
Syntax:
stringName[x:y]
x is beginning index and y is the index after the last index to be sliced.
For example:
The above statement slices the string start with index 0 until index 3.
>>> str[-8:-5]
'Mal'
>>> str[0:3]
>>> str[:3]
'Mal'
'Mal'
-8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7
'M' ' a' ' l' 'a' 'y' ' s' ' i' ' a'
'Malay' 'sia'
10 | P a g e
>>> str[:]
'Malaysia'
r
Python Programming
•
Training Manual
8
>>> str = "the walking dead"
>>> str.title()
Capitalize the first letter of each word
'The Walking Dead' in the string
with('M')
True
't' 'h' 'e' '' 'w' 'a' 'l' 'k' 'i' 'n' 'g' 'd' 'e' 'a' 'd'
>>> str.istitle()
False
' H' 'e' ' l' ' l' ' o' ' H' 'e' ' l' ' l' 'o' ''
>>> str.upper() >>> str.rstrip()
'HELLO' 'Hello'
11 | P a g e
Python Programming Training Manual Version 1.0
LIST
Overview and List Declaration
number = [34,56,87,25,1,689]
grade = ['A', 'B', 'A-', 'D']
countries = ['Malaysia', 1957, 'Singapore', 1965]
indices
0 1 2 3 4 5
number
34 56 87 25 1 689
-6 -5 -4 -3 -2 -1
Negative indices
>>> number[0]
34
>>> number[4]
1
A list can be referenced from the end of the list starting from -1, -2 and so on as
follows:
>>> number[-1]
689
>>> number[-3]
25
12 | P a g e
Python Programming Training Manual Version 1.0
Creating a list
• A list can be created by using the range and list function as follows:
>>> series = list(range(1,5))
>>> series
[1, 2, 3, 4]
• The following examples demonstrate the use several functions to manipulate the list.
>>> print(number)
[34, 56, 87, 25, 1, 689]
d) Extending a list using another list by adding the new list at the end of current list.
This task is achievable by using extend() function
>>> number2 = [801, 91]
>>> number.extend(number2)
>>> print(number)
[34, 56, 87, 25, 1, 689, 18, 801, 91]
13 | P a g e
Python Programming Training Manual Version 1.0
f) Inserting new elements at specific location (index) of the list using insert()
function
Syntax:
listname.insert(position, value)
If the position is greater that the of the list, the value will placed at the end the
list as follows:
>>> lst.insert(9,13)
>>> print(lst)
[34, 68, 56, 78, 13]
g) Removing and returns the element at the end of the list using pop() function.
>>> print(lst)
[34, 68, 56, 78, 13]
>>> lst.pop()
13
>>> print(lst)
[34, 68, 56, 78]
>>> lst.pop(1)
68
>>> print(lst)
[34, 56, 78]
h) A string can be split and assign the values into a list using split() function.
>>> str = "Tanah Tumpah Darah Ku"
>>> lst = str.split()
>>> lst
['Tanah', 'Tumpah', 'Darah', 'Ku']
14 | P a g e
Python Programming Training Manual Version 1.0
Nested Lists
0 1
0 'Mambau' 34000
1 'Rantau' 43000
2 'Rembau' 94000
Identifier[column][row]
>>> population[0]
['Mambau', 34000]
>>> population[0][0]
'Mambau'
>>> population[1][1]
34000
>>> population[:2]
[['Mambau', 34000], ['Rantau', 43000]]
>>> population[1:]
[['Rantau', 43000], ['Rembau', 94000]]
15 | P a g e
Python Programming Training Manual Version 1.0
Copying Lists
lst1 34 56 78
Lst2
Update any element in the list will reflect both identifiers as follows:
>>> lst1[2] = 56
>>> lst1
34 56 78 56
[34, 56, 56]
>>> lst2
[34, 56, 56]
lst1
34 56 78
Lst2 34 56 78
>>> lst1[2] = 56
>>> lst1
[34, 56, 56]
>>> lst2
[34, 56, 78]
16 | P a g e
Python Programming Training Manual Version 1.0
17 | P a g e
Python Programming Training Manual Version 1.0
Tuples
1. A tuple is a type of sequence that resembles a list however unlike l list, a tuple is
immutable.
2. A tuple is indicated by using parenthesis ().
Syntax:
identifier = (list of elements in the tuple separated by comas)
Example:
>>> vowel = ('a','e','i','o','u')
>>> vowel
('a', 'e', 'i', 'o', 'u')
18 | P a g e
Python Programming Training Manual Version 1.0
EXERCISES
1. Determine the output of the following program.
a = 2
b = 3
print((a + b,))
print((a + b))
print(())
Answer:
Amazon.com is located in Seattle, WA.
19 | P a g e
Python Programming Training Manual Version 1.0
Program Control
Statements
20 | P a g e
Python Programming Training Manual Version 1.0
Selection Statement
1. If statement
Syntax:
if condition:
indented block of true_statements
Example:
a)
if score > 50:
print('Congratulation!')
print('You have passed the examnination')
b)
var = 'Congratulation!'
if isinstance(var,str)
print('This is a string\n')
c)
var = 123
if var.isdigit()
print('This is a number\n')
21 | P a g e
Python Programming Training Manual Version 1.0
2. If..else statement
Syntax:
if condition:
indented block of true_statements
else:
indented block of false_statements
Example:
3. If..elif statement
Syntax:
22 | P a g e
Python Programming Training Manual Version 1.0
if condition:
indented block of true_statements
elif condition:
indented block of true_statements
elif condition:
indented block of true_statements
elif condition:
indented block of true_statements
else:
indented block of default_statements
Example:
score = int(input('Enter a score : '))
23 | P a g e
Python Programming Training Manual Version 1.0
Loops Statement
1. For statement
24 | P a g e
Python Programming Training Manual Version 1.0
2. While statement
25 | P a g e
Python Programming Training Manual Version 1.0
26 | P a g e
Python Programming Training Manual Version 1.0
View
Controller
Model
27 | P a g e
Python Programming Training Manual Version 1.0
28 | P a g e
Python Programming Training Manual Version 1.0
29 | P a g e
Python Programming Training Manual Version 1.0
30 | P a g e