Python
Python
Data Type
Date Type Description Data Type Python Example
(pseudocode) (Python)
Integer Whole number, either positive or int num = 3
negative num = int (3)
Real Positive or negative fractional values float num = 3. 12
num = float (3.12)
Character A single character or symbol ( A,$) str var = ‘A’
var = “A”
String More than one character str var = ‘Hello’
Used to hold words, names or var = “Hello”
sentences.
Boolean One of two values, either True or False bool var = False
Used to indicate the result of a
condition
NUM =0
Operator
Arithmetic Operator ( integer , float)
Operator Operation Example of use in python
+ Addition result = num1 + num2
result = 5 +5
- Subraction result = num1 - num2
* Multiplication result = num1*num2
/ Division result = num1 / num2
result = 5 /2 # result = 2.5
Casting : The process of changing the data type into another data type
by using the following function
int (value or variable)
float ( value or variable)
str ( value or variable)
Example
num = int (input ( “ Enter a number”))
Example
print ( “ Hello Python”)
result = 3 +4
print (result)
print (“The result is “ ,result)
Selection Statement
Selection used to determine a different set of steps to execute based on a
decision (condition).
Code branches and follows a different sequence based on decision made by
program.
if statement
Syntax Code :
if condition/decision : num= int (input(“Enter Number”))
code if num > 0 :
code print(“positive”)
if…else statement
Syntax Code :
if condition/decision : num= int (input(“Enter Number”))
code if num > 0 :
code print(“positive”)
else : else :
code print (“negative”)
if..elif..else statement
Syntax Code:
while loop is known as a pretest loop, which means it tests its condition before
performing an iteration
If a condition is true, do some task.
Syntax
while condition :
code # notice the indent from of this line relative to the for
Code
num = int (input(“Enter a number”))
While num !=-1
num= int (input(“Enter a number”))
Data Structure
Array
An array is a variable that can hold a set/list of data items, of the same data type,
under a single identifier (name).
When array is declared, its size is defined. In Python indexes start from zero.
There are two type of array:
1. One dimensional array
2. Multi-dimensional array
One-dimensional array
• A one-dimensional array (or single dimension array) is a type of linear array
that can be accessed individual by a index
Create blank array
studentName = [ ] or
myList = [None] * 5
Indexes 0 1 2 3 4
indexes 0 1 2 3
Element
First index = 0, Last index= 3, array length or size=4
Using Array
The index can be used to read or write values in an array.
num_arr [0]
0 1 2 3 4
num_arr
for i in range ( start value = 0, end value= the number of item in array) :
code
len(array/list name) return the number of item in array. This function is used as the
end value of for loop for array traversal
Another method
nameList = [“Aye”, “Kyaw”, “Mya”]
for a in nameList :
print( a)
nameList=[“None]*5
for i in range(0,5):
nameList[i] = int(input(“Enter a number:”))
Two-dimensional Array
Two-dimensional Array is array of array. It has row and column.
0 1 2 3 4
0 5 6 7 8 4
1 7 10 3 2 1
2 4 8 9 10 2
String
String is a sequence of characters that can be letters, numbers, symbols,
punctuation marks of spaces
String must be enclosed with quotation marks to distinguish them from variable
names
str = “Computer Science”
Each character in a string has an index number, with the first character at position
0.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
String C O M p U t e r S c i e N c E
String Traversal
A loop is used to traverse (retrieve) a string, character by character.
len(String) function find the length of a string.
str = “ computer science”
for i in range (0, len(str))
print(str[i])
Another way
str = “computer science”
for a in str :
print (a)
Other way to manipulate String
Finding a character with a particular index
str = “ computer science”
print ( str[0] ) # output the start char of string
This file can be read back into an array in the following way
- Create the blank array or variables to assign the data from the txt file
- Open the file to read data
- Read the data and assign to the array using read and split function
Code
myList = []
myFile = open(“cars.txt”, “r”)
myList = myFile.read().split(“, “)
myFile.close()
Subroutines/Subprogram (Function)
Subprogram is a block of code that preforms a specific task but does not represent
the entire program.
When a subroutines/subprogram is called, the calling program is halted and control
is transferred to the subroutine.
After a subprogram has completed execution, the control is passed back to the
calling program.
Syntax
def identifier ( parameter) : # parameter is optional
code
Code
def add ( num1, num2) : num1 and num2 parameter
result = num1 + num2
return result
# Main
a =3
b =5
add ( a , b) argument – a,b
Function
Function are similar to procedure
The difference is that the functions have one or more values passed to them
and one or more values are returned to the main program.
A parameter allows a value to be passed in to the procedure.
Syntax
def identifier ( parameter) : # parameter is optional
code
return value
Code
def add ( num1, num2) :
result = num1 + num2
return result
# Main
a =3
b =5
res = add ( a , b)
print(“The result is “ , res)
Type of Error
Type of Description Exmple
Error
Logic The program seems to run normally; however, Write a + b
there is an error in the logic of the program, instead of a-b
which means it does not produce the result you
expect.
Syntax Syntax refers to the rules of the programming Write prnt
language. instead of
A syntax error means that breaks the rules of print
the language , which stops it running Missing a
closing
bracket
Missing out
quotations
marks
Write x
instead of *
Runtime An error that occurs when the computer tries to Array out of
run code that it cannot execute index
Divided by 0
Example
The programmer accepts the number that is greater than 0 and less than
10.
Normal Test data : 1,6
Boundary: 1 and 9
Normal : 2,7
Erroneous : -1, 14
Boundary :1, 9
Evaluating program
Usability
Validation-
Code readability
Requirement
The efficiency of program- have loop and subprogram to avoid
duplicate code