Pseudo code to Python Translation Guide
Pseudo code to Python Translation Guide
Pseudocode:
Python:
Pseudocode:
Python:
Pseudocode:
FOR i ← 1 TO 5
OUTPUT i
NEXT i
Python:
Pseudocode:
counter ← 1
WHILE counter <= 5 DO
OUTPUT counter
counter ← counter + 1
ENDWHILE
Python:
counter = 1
while counter <= 5:
print(counter)
counter = counter + 1 # or counter += 1
Pseudocode:
counter ← 1
REPEAT
OUTPUT counter
counter ← counter + 1
UNTIL counter > 5
Python:
counter = 1
while True:
print(counter)
counter = counter + 1
if counter > 5:
break
OR
counter = 1
while not (counter > 5):
print(counter)
counter = counter + 1
Arrays/Lists
Pseudocode:
Python:
Pseudocode (Procedure):
PROCEDURE greet(name)
OUTPUT "Hello, " + name
ENDPROCEDURE
greet("Alex")
Python:
def greet(name):
print("Hello, " + name)
greet("Alex")
Pseudocode (Function):
FUNCTION square(number)
RETURN number * number
ENDFUNCTION
result ← square(4)
OUTPUT result
Python:
def square(number):
return number * number
result = square(4)
print(result)
Essential Programming Constructs
1. Variables and Constants
Python:
# Variables
name = "Alex" # String
age = 16 # Integer
height = 1.75 # Float
is_student = True # Boolean
Key points:
2. String Operations
Python:
# Length
length = len(name) # 16
# Slicing
substr = name[0:8] # 'Computer'
# Concatenation
greeting = "Hello, " + name # 'Hello, Computer Science'
# String methods
lowercase = name.lower() # 'computer science'
uppercase = name.upper() # 'COMPUTER SCIENCE'
3. Mathematical Operations
Python:
a = 10
b = 3
addition = a + b # 13
subtraction = a - b # 7
multiplication = a * b # 30
division = a / b # 3.3333... (float)
integer_division = a // b # 3 (integer)
remainder = a % b # 1
power = a ** b # 1000 (10^3)
4. Logical Operators
Python:
x = 5
y = 10
# Comparison operators
is_equal = x == y # False
not_equal = x != y # True
greater_than = x > y # False
less_than = x < y # True
greater_equal = x >= y # False
less_equal = x <= y # True
# Logical operators
and_result = (x > 0) and (y > 0) # True
or_result = (x > 10) or (y > 0) # True
not_result = not (x > 0) # False
Common Algorithm Patterns
1. Input Validation
Python:
Python:
print(max_value) # 67
3. Counting Occurrences
Python:
print(count) # 3
4. Linear Search
Python:
Python:
Python:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
# Swap elements
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
Python:
Python:
# Creating lists
empty_list = []
numbers = [10, 20, 30, 40, 50]
mixed = [1, "hello", True, 3.14]
# Modifying elements
numbers[1] = 25 # [10, 25, 30, 40, 50]
# List operations
length = len(numbers) # 5
numbers.append(60) # Add to end: [10, 25, 30, 40, 50, 60]
numbers.insert(1, 15) # Insert at position: [10, 15, 25, 30, 40, 50, 60]
numbers.remove(30) # Remove by value: [10, 15, 25, 40, 50, 60]
popped = numbers.pop() # Remove & return last: 60, list becomes [10, 15, 25,
40, 50]
numbers.sort() # Sort in place: [10, 15, 25, 40, 50]
Python:
Python:
# Creating a dictionary
student = {
"name": "Alex",
"age": 16,
"grades": [85, 90, 78]
}
# Accessing values
name = student["name"] # "Alex"
# Alternative safer method
age = student.get("age", 0) # 16 (returns 0 if key doesn't exist)
# Adding/modifying entries
student["school"] = "High School"
student["age"] = 17