8. Programming
8.1 Variables and Constants (Python)
Variable Constant
What is it? A named data store to contain a value
that can change that should stay fixed
When to use? hen the data may change when the
W hen the data should NOT change
W
program runs throughout the program
How to use? Assign a value to it using the=sign
Naming: lowercase or snake_case Naming: ALL_CAPITAL_LETTERS
Example core= 0
s I= 3.14159
P
score =score+ 5 area =PI* 5 * 5
print(score) print(area)
utput:
O utput:
O
5 78.53975
Variables and Constants (Pseudocode)
Variable Constant
How to use? Declare a variable with data type using:sign
Assign a value to it using the←sign
Note: Constants do not need to be declared
Naming: PascalCase
Example ECLAREStudentScore: INTEGER
D CONSTANTInterestRate← 0.06
StudentScore← 90
Prepared by: Claudia Heng
,IGCSE Tuition - 0478 CS
8.2 Data Types
Pseudocode Python
INTEGER num_of_students = 1
REAL average_height = 165.5 (In Python, it is calledfloat)
CHAR gender = “M” (In Python, there is no char data type, it is juststring)
STRING student_name = “John Doe” (Either “” or ‘’)
BOOLEAN game_over = True / game_over = False
8.3 Input and Output
Example of input Pseudocode Python
Input without prompt UTPUT“This is a Prompt”
O surprise =input()
INPUTVariableName
Input for string first_name =input(“Enter your first name: ”)
Input for number/integer age =int(input(“Enter your age: “))
Input for decimal/float weight =float(input(“Enter your weight: “))
Note: You always assign a variable to the input tostore the result of your input, e.g, when a user types
something to answer to your prompt, you need a storage, i.e., a variable.
Note: casting is when you convert 1 data type to another,using these functions: int(), float(), str(). input() by
default takes in the input as a string data type.
Prepared by: Claudia Heng
, IGCSE Tuition - 0478 CS
Example of output Pseudocode Python
Output a string ECLAREName : STRING
D ame = "John Doe"
n
Name ← “John Doe” print(name)
OUTPUTName or
or print("John Doe")
OUTPUT“John Doe”
Output a number/integer ECLAREAge : INTEGER
D ge = 21
a
Age ← 21 print(age)
OUTPUTAge
Output a decimal/float ECLAREHeight : REAL
D eight = 1.70
h
Height ← 1.70 print(height)
OUTPUTHeight
Output a boolean ECLAREGameOver : BOOLEAN
D ame_over = True
g
GameOver ← TRUE print(game_over)
OUTPUTGameOver
utput a combination of
O ECLAREName : STRING
D ame = "John Doe"
n
stringand a variable ofstring Name ← “John Doe” print(“Welcome!”, name)
data type OUTPUT“Welcome! ”, Name or
(String concatenation)
name = "John Doe"
print(“Welcome! ” + name)
utput a combination of
O ECLAREAge : INTEGER
D ge = 21
a
stringand a variable of Age ← 21 print(“Your age is”, age)
integerdata type OUTPUT“Your age is ”, Age or
age = 21
print(“Your age is ” +str(age))
utput a combination of
O ECLAREHeight : REAL
D eight = 1.70
h
string and a variable of float Height ← 1.70 print(“Your height is”, height)
data type OUTPUT“Your height is ”, Height or
height = 1.70
print(“Your height is ” +str(height))
Note: You can use comma (,) or plus (+) sign to combinestring with a data stored in variable, differences:
(i) comma adds a space automatically and plus does not, (ii) comma does not throw error when you combine
different data types whereas plus does, and you need to do casting each time)
Prepared by: Claudia Heng