Pseudo Code VS Python
Cheat Sheet by Haris Kamal Khan
Declaration
DECLARE myName : STRING myName = “” #STRING
DECLARE myAge : INTEGER myAge = 0 #INTEGER
variable DECLARE gender : CHAR gender = “” #CHAR
DECLARE price : REAL price = 0.0 #REAL
DECLARE OnSite: BOOLEAN OnSite = True #BOOLEAN
constant CONST userName “csislife” USERNAME = “csislife”
1D array DECLARE mySubjectMarks : ARRAY[1:5] OF INTEGER mySubjectMarks = [] # INTEGER
2D array DECLARE daysPoints : ARRAY[1:7, 1,5] OF INTEGER daysPoints= [ [ ] ] #INTEGER
Initialization
myName “Ali” myName = “Ali”
myAge 14 myAge = 14
variable gender ‘M’ gender = ‘M’
price 15.5 price = 15.5
OnSite TRUE OnSite = True
constant <initialized when declaring>
mySubjectMarks = [0] * 5
FOR i 1 to 5
1D array mySubjectMarks[i] = 0
for i in range(5):
NEXT
mySubjectMarks[i] = 0
FOR r 1 to 7 daysPoints = []
FOR c 1 to 5
2D array daysPoints[r, c] = 0 for _ in range(7):
NEXT row = [0] * 5
NEXT daysPoints.append(row)
Input Different Data Types
STRING INPUT “Enter your name”, myName myName = input("Enter your name: ")
INTEGER INPUT “Enter your age”, myAge myAge = int(input("Enter your age: "))
CHAR INPUT “Enter your gender”, gender gender = input("Enter your gender: ")[0]
REAL INPUT “Enter the price”, price price = float(input("Enter the price: "))
IF conditions FOR LOOP
INPUT “Enter the score”. score
score = int(input("Enter the score: "))
IF score > 60 THEN
if score > 60:
PRINT ‘Pass’
print("Pass") FOR c 1 to 5
ELSE IF score >= 40 THEN for c in range(1, 6):
elif score >= 40: PRINT “Hello”
OUTPUT ‘Near Pass’ print("Hello")
print("Near Pass") NEXT
ELSE
else:
OUTPUT ‘Fail’
print("Fail")
ENDIF
CASE REPEAT UNTIL LOOP
CASE OF OpValue if OpValue == "+":
"+" : Answer ̈ Number1 + Number2 Answer = Number1 + Number2
"-" : Answer ̈ Number1 - Number2 elif OpValue == "-":
"*" : Answer ̈ Number1 * Number2 Answer = Number1 - Number2 <python does not have
"/" : Answer ̈ Number1 / Number2 elif OpValue == "*": repeat until loop, while
OTHERWISE OUTPUT "Please enter a Answer = Number1 * Number2 loop is used instead>
valid choice" elif OpValue == "/":
ENDCASE Answer = Number1 - Number2
else: print("invalid operator")
Assignment Operator WHILE LOOP
= PRINT “Enter a positive
print("Enter a positive
Boolean Operator number”
number")
AND and INPUT num
num = int(input())
OR or WHILE num < 0 DO
while num < 0:
NOT not PRINT “Not a positive
print("Not a positive
number, enter again”
number, enter again")
INPUT num
num = int(input())
ENDWHILE
Logical Operators Mathematical Operaors
> > + +
< < - -
<= <= * *
>= >= / /
= == ^ **
<> != MOD %
Pseudo Code VS Python
Cheat Sheet by Haris Kamal Khan
SUBROUTINES
PROCEDURE sumOf2()
DECLARE x, y, z : INTEGER def sumOf2():
INPUT “Enter 1st number”, x x = input(“Enter 1st number”)
PROCEDURE INPUT “Enter 2nd number”, y y = input(“Enter 2nd number”)
z=x+y z=x+y
PRINT (z) print(z)
ENDPROCEDURE
PROCEDURE sumOf2(x: INTEGER, y:INTEGER)
DECLARE z : INTEGER def sumOf2(x, y):
z=x+y z=x+y
PROCEDURE PRINT (z) print(z)
with ENDPROCEDURE
parameters ---------------------------------------- ----------------------------
DECLARE x, y : INTEGER x = input(“Enter 1st number”)
INPUT “Enter 1st number”, x y = input(“Enter 2nd number”)
INPUT “Enter 2nd number”, y sumOf2(x, y)
sumOf2(x, y)
FUNCTION
DECLARE salary, newSalary : FLOAT
INPUT “Enter your salary”, salary
def incrementSalary10Per():
newSalary = salary * 1.1
salary = input(“Enter your salary”)
RETURN newSalary
newSalary = salary * 1.1
FUNCTION ENDFUCTION
return newSalary
------------------------------------
----------------------------------------------
DECLARE answer : FLOAT
answer = incrementSalary10per()
answer = incrementSalary10per()
print(answer)
DECLARE LineOfText : STRING
OPENFILE fileA.txt FOR READ
OPENFILE fileB.txt FOR WRITE
FILE
READFILE fileA.txt, LineOfText
WRITEFILE fileB.txt, LineOfText
CLOSEFILE fileA.txt
CLOSEFILE fileB.txt