Python Programming (1323203) - Summer 2024 Solution
Solution guide for Python Programming (1323203) Summer 2024 exam
Question 1(a) [3 marks]
Lists the Importance of flowchart and algorithm
Answer:
| Importance of Flowchart | Importance of Algorithm |
|---|---|
| Visual representation of program logic | Step-by-step procedure to solve a problem |
| Easier to debug and identify errors | Language-independent solution approach |
| Helps in understanding complex processes | Serves as a foundation for programming |
| Improves communication among team members | Defines logic before coding begins |
Mnemonic: "VASE Decisions" - Visualize, Analyze, Sequence, Execute
Question 1(b) [4 marks]
Draw a flowchart to find the entered number is even or odd.
Answer:
Key Steps:
- Input collection: Get number from user
- Modulo operation: Divide by 2 and check remainder
- Conditional output: Display result based on remainder
Mnemonic: "MODE" - Modulo Operation Determines Evenness
Question 1(c) [7 marks]
List out all Logical operators and explain each by giving python code example.
Answer:
| Operator | Description | Example | Output |
|---|---|---|---|
and | Returns True if both statements are true | x = 5; print(x > 3 and x < 10) | True |
or | Returns True if one of the statements is true | x = 5; print(x > 10 or x == 5) | True |
not | Reverse the result, returns False if result is true | x = 5; print(not(x > 3)) | False |
Code Example:
Python
Mnemonic: "AON Clarity" - And, Or, Not for logical clarity
Question 1(c) OR [7 marks]
Develop a Program that can calculate simple interest and compound interest on given data.
Answer:
Python
Key Formulas:
- Simple Interest (SI): Principal × Rate × Time / 100
- Compound Interest (CI): Principal × ((1 + Rate/100)^Time - 1)
Mnemonic: "PRT Money Grows" - Principal, Rate, Time make money grow
Question 2(a) [3 marks]
Create a Program to find a minimum number among the given three numbers.
Answer:
Python
Mnemonic: "MIN Finds Least" - Minimum Is Numerically Found with Least
Question 2(b) [4 marks]
Define pseudocode. Write pseudocode to find Largest of three numbers x, y and z.
Answer:
| Pseudocode Definition |
|---|
| A detailed yet readable description of what a computer program must do, expressed in a formally-styled natural language rather than in a programming language. |
Pseudocode for finding largest of three numbers:
BEGIN
INPUT x, y, z
SET largest = x
IF y > largest THEN
SET largest = y
END IF
IF z > largest THEN
SET largest = z
END IF
OUTPUT "Largest number is: ", largest
END
Mnemonic: "PIE Writing" - Program Ideas Expressed in simple writing
Question 2(c) [7 marks]
Explain While loop in python with its syntax, flowchart and with python code example.
Answer:
Syntax:
Python
Flowchart:
Code Example:
Python
Key Characteristics:
- Entry controlled: Condition checked before loop execution
- Initialization: Variables set before the loop
- Updation: Variables updated inside the loop
- Termination: Loop exits when condition becomes False
Mnemonic: "IUTE Loop" - Initialize, Update, Test for Exit
Question 2(a) OR [3 marks]
Describe continue statement in python in brief.
Answer:
| Continue Statement in Python |
|---|
| The continue statement skips the current iteration of a loop and continues with the next iteration |
| When encountered, the code inside the loop following the continue statement is skipped |
| Useful for skipping specific conditions while keeping the loop running |
Code Example:
Python
Mnemonic: "SKIP Ahead" - Skip Keeping Iteration Process
Question 2(b) OR [4 marks]
What is the output of the following code:
Python
Answer:
| Operation | Result | Explanation |
|---|---|---|
x*y | 16 | Multiplication: 8 × 2 = 16 |
x**y | 64 | Exponentiation: 8² = 64 |
x%y | 0 | Modulo (remainder): 8 ÷ 2 = 4 with remainder 0 |
x>y | True | Comparison: 8 > 2 is True |
Mnemonic: "MEMO" - Multiply, Exponent, Modulo, Operator comparison
Question 2(c) OR [7 marks]
Explain if-elif-else Ladder in python with its syntax, flowchart and with python code example.
Answer:
Syntax:
Python
Flowchart:
Code Example:
Python
Key Characteristics:
- Sequential evaluation: Conditions checked from top to bottom
- Exclusive execution: Only one block executes
- Default action: Else block executes if no conditions are True
Mnemonic: "SEEP Logic" - Sequential Evaluation with Exclusive Path
Question 3(a) [3 marks]
Write a Python program to print odd numbers between 1 to 20 using loops.
Answer:
Python
Alternate approach:
Python
Mnemonic: "STEO" - Skip Two, Extract Odds
Question 3(b) [4 marks]
Explain Nested if statement in brief.
Answer:
| Nested if Statement |
|---|
| An if statement inside another if statement |
| Allows for more complex conditional logic |
| Inner if only evaluated when outer if is True |
| Can have multiple levels of nesting |
Code Example:
Python
Mnemonic: "LION" - Layered If-statements Operating Nested
Question 3(c) [7 marks]
Using a user-defined function write a Program to check entered number is an 'Armstrong number' or a palindrome in which number is passed as argument in calling function.
Answer:
Python
Armstrong Examples:
- 153: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✓
- 370: 3³ + 7³ + 0³ = 27 + 343 + 0 = 370 ✓
Palindrome Examples:
- 121: Same forward and backward ✓
- 123: Not same backward (321) ✗
Mnemonic: "APTEST" - Armstrong Palindrome Test Equal Sum Test
Question 3(a) OR [3 marks]
Write a python program to find sum of 1 to 100.
Answer:
Python
Mnemonic: "SUM Formula" - Sum Using Mathematical Formula
Question 3(b) OR [4 marks]
Write a python program to print the following pattern.
1
2 3
4 5 6
7 8 9 10
Answer:
Python
Pattern Logic:
- Row 1: 1 number (1)
- Row 2: 2 numbers (2, 3)
- Row 3: 3 numbers (4, 5, 6)
- Row 4: 4 numbers (7, 8, 9, 10)
Mnemonic: "CNIR" - Counter Number Increases with Rows
Question 3(c) OR [7 marks]
Write a Program using the function that reverses the entered value.
Answer:
Python
Alternate Method for Number Reversal:
Python
Mnemonic: "FLIP Digits" - Function Logic Inverts Position of Digits
Question 4(a) [3 marks]
Describe python math module with proper python code example.
Answer:
| Python Math Module Features |
|---|
| Provides mathematical functions and constants |
| Includes trigonometric, logarithmic, and other functions |
| Contains mathematical constants like pi and e |
| Requires import before use |
Code Example:
Python
Mnemonic: "CALM Operations" - Constants And Logarithmic Mathematical Operations
Question 4(b) [4 marks]
Write a python program that explains scope of variable.
Answer:
Python
Output:
Inside function - Global variable: I am global
Inside function - Local variable: I am local
Inside function - Shadowed global: I am local with global name
Outside function - Global variable: I am global
Mnemonic: "GLOVES" - Global Local Variable Encapsulation System
Question 4(c) [7 marks]
Explain List Methods and its built-in Functions
Answer:
| Method/Function | Description | Example | Output |
|---|---|---|---|
append() | Adds an element at the end | fruits = ['apple']; fruits.append('banana'); print(fruits) | ['apple', 'banana'] |
insert() | Adds element at specified position | nums = [1, 3]; nums.insert(1, 2); print(nums) | [1, 2, 3] |
remove() | Removes specified item | colors = ['red', 'blue']; colors.remove('red'); print(colors) | ['blue'] |
pop() | Removes item at specified index | letters = ['a', 'b', 'c']; x = letters.pop(1); print(x, letters) | b ['a', 'c'] |
clear() | Removes all elements | items = [1, 2]; items.clear(); print(items) | [] |
len() | Returns number of elements | print(len([1, 2, 3])) | 3 |
sorted() | Returns sorted list | print(sorted([3, 1, 2])) | [1, 2, 3] |
max()/min() | Returns max/min value | print(max([5, 10, 3]), min([5, 10, 3])) | 10 3 |
Code Example:
Python
Mnemonic: "LISP Operations" - List Insert Sort Pop Operations
Question 4(a) OR [3 marks]
List out Python standard library mathematical functions.
Answer:
| Mathematical Function | Description | Example |
|---|---|---|
abs() | Returns absolute value | abs(-5) → 5 |
round() | Rounds to nearest integer | round(3.7) → 4 |
max() | Returns largest item | max(1, 5, 3) → 5 |
min() | Returns smallest item | min(1, 5, 3) → 1 |
sum() | Adds items of iterable | sum([1, 2, 3]) → 6 |
pow() | Returns x to power y | pow(2, 3) → 8 |
divmod() | Returns quotient and remainder | divmod(7, 2) → (3, 1) |
Additional from math module:
math.sqrt(): Square rootmath.floor(): Rounds downmath.ceil(): Rounds upmath.factorial(): Factorial of a numbermath.gcd(): Greatest common divisor
Mnemonic: "SMART Calculations" - Standard Mathematical Arithmetic Routines and Tools
Question 4(b) OR [4 marks]
Explain built in function in python.
Answer:
| Built-in Functions in Python |
|---|
| Pre-defined functions available in Python without importing any module |
| Called directly without any prefix |
| Designed to perform common operations |
| Examples include print(), len(), type(), input(), range() |
Categories with Examples:
Python
Mnemonic: "EPIC Functions" - Embedded Python Integrated Core Functions
Question 4(c) OR [7 marks]
Write a Python Program to count and display the number of vowels, consonants, uppercase, lowercase characters in a string.
Answer:
Python
Example:
- Input: "Hello World!"
- Output:
- Vowels: 3 (e, o, o)
- Consonants: 7 (H, l, l, W, r, l, d)
- Uppercase: 2 (H, W)
- Lowercase: 8 (e, l, l, o, o, r, l, d)
Mnemonic: "VOCAL Analysis" - Vowels Or Consonants And Letter case
Question 5(a) [3 marks]
Write a python code to swap given two elements in a list.
Answer:
Python
Mnemonic: "STEP Logic" - Swap Two Elements with Python Logic
Question 5(b) [4 marks]
Write a python Program to check if a substring is present in a given string.
Answer:
Python
Alternate method using find():
Python
Mnemonic: "FIND Method" - Find IN Directly with Methods
Question 5(c) [7 marks]
Explain tuple Operations, Functions and Methods
Answer:
| Operation/Function/Method | Description | Example | Output |
|---|---|---|---|
| Creation | Create tuples with parentheses | t = (1, 2, 3) | (1, 2, 3) |
| Indexing | Access tuple elements | t[1] | 2 |
| Slicing | Get subset of tuple | t[1:3] | (2, 3) |
| Concatenation | Join two tuples | (1, 2) + (3, 4) | (1, 2, 3, 4) |
| Repetition | Repeat tuple elements | (1, 2) * 2 | (1, 2, 1, 2) |
| Membership | Check if element exists | 3 in (1, 2, 3) | True |
| len() | Get number of items | len((1, 2, 3)) | 3 |
| min()/max() | Find min/max value | min((3, 1, 2)) | 1 |
| count() | Count occurrences of value | (1, 2, 1).count(1) | 2 |
| index() | Find position of value | (1, 2, 3).index(2) | 1 |
| sorted() | Return sorted list from tuple | sorted((3, 1, 2)) | [1, 2, 3] |
Code Example:
Python
Mnemonic: "ICONS" - Immutable Collection Operations, Numbering, and Searching
Question 5(a) OR [3 marks]
Write a python program find the sum of elements in a list.
Answer:
Python
Mnemonic: "SALT" - Sum All List Together
Question 5(b) OR [4 marks]
Write a Program to demonstrate the set functions and operations.
Answer:
Python
Mnemonic: "COSI Methods" - Create, Operate, Search, Investigate with Set Methods
Question 5(c) OR [7 marks]
Write a Program to demonstrate the dictionaries functions and operations.
Answer:
Python
Key Operations:
- Access: Using key or get() method
- Modify: Assign new value to existing key
- Add: Assign value to new key
- Remove: Using pop(), popitem(), or del statement
- Iterate: Through keys, values, or items
Mnemonic: "ACME Dictionary" - Access, Create, Modify, Extract from Dictionary