These are the solved end-of-unit exercises for Unit 2, Python Programming, from the PECTAA Computer Science and Entrepreneurship-11 textbook used by BISE Rawalpindi, Lahore and the other Punjab boards. Every answer is written from the text of the unit, for the 1st Year (HSSC Part I) Annual Examination 2027. The textbook calls its chapters “units”.
This unit teaches Python from the start: variables, data types, input and output, operators and precedence, if statements and loops, functions, modules, lists, tuples and modular programming.
| Unit | 2: Python Programming |
|---|---|
| Class | 1st Year (HSSC Part I), Punjab boards |
| MCQs answered | 9 |
| Questions solved | 14 |
Exercise questions deleted in the ALP Smart Syllabus 2027
Under PECTAA’s ALP Smart Syllabus 2027 for Computer Science and Entrepreneurship-11, the following content of Unit 2 is not part of the 2027 annual exam: Topics 2.8 Object-Oriented Programming in Python, 2.9 Advanced Python Concepts (exception and file handling) and 2.10 Testing and Debugging in Python · MCQ 6 · Short Questions 5 and 8 · Long Question 1 parts (a) and (c). These questions are marked below and still solved, so you can practise them if you want.
Multiple Choice Questions
| Q. | Correct option | Reason from the textbook |
|---|---|---|
| 1 | (c) Check “Add Python to PATH” | Ticking this box lets you run Python from the command line easily. |
| 2 | (a) variable1 | A name must start with a letter or underscore. It cannot start with a digit or contain a hyphen or a space. |
| 3 | (a) Age: 25 | print() shows the text followed by the value stored in age. |
| 4 | (b) ** | ** is the exponentiation operator, for example 10**3 = 1000. |
| 5 | (b) for | A for loop iterates over a sequence such as a list, tuple or string. |
| 6 (ALP deleted) | (b) Creates a sequence of numbers | range() produces a sequence of numbers for a loop to use. Note: the answer key printed at the back of the book gives (d). Option (b) matches the chapter text, so check with your teacher. |
| 7 | (c) def | Functions are defined with the def keyword. |
| 8 | (b) Warm | temperature is 25 and wind_speed (15) > 10, so the second condition is True and “Warm and breezy” is printed. |
| 9 | (c) + | The + operator concatenates two lists, for example [2, 3] + [6, 7]. |
Short Questions
Q.1 Explain the purpose of using comments in Python code.
- Comments are lines that the Python interpreter does not execute.
- They are written to explain the code or leave notes, so the program is easier to read and maintain for you and for others.
- A single-line comment starts with
#. A multi-line comment is written between triple quotes"""…""".
# This line is a comment and is not executed
print("K2 is the second-highest mountain in the world")
Q.2 Describe the difference between integer and float data types in Python. Provide an example of each.
| Integer (int) | Float (float) |
|---|---|
| Stores whole numbers without a decimal point. | Stores numbers with a decimal point. |
Example: age = 17 |
Example: price = 19.99 |
Input is converted with int(input(...)) |
Input is converted with float(input(...)) |
Q.3 Define operator precedence and give an example of an expression where operator precedence affects the result.
Operator precedence is the order in which Python performs the operations in an expression. The order, from highest to lowest, is: parentheses (), then exponentiation **, then *, /, %, then + and -.
Example:
3 + 2 * 4= 3 + 8 = 11, because multiplication is done before addition.(3 + 2) * 4= 5 * 4 = 20, because the parentheses are done first.
The same numbers give different answers, so precedence changes the result.
Q.4 How does the short-hand if-else statement differ from the regular if-else statement?
| Regular if-else | Short-hand if-else |
|---|---|
Written over several lines, with indented blocks under if and else. |
Written on a single line. |
| Each block can hold many statements. | Picks one of two values or expressions. |
Syntax: if condition: … else: … |
Syntax: value_if_true if condition else value_if_false |
temperature = 15
# regular
if temperature > 30:
print("It's a hot day")
else:
print("It's not a hot day")
# short-hand
m = "It's a hot day" if temperature > 30 else "It's not a hot day"
print(m)
Q.5 Explain the use of the range() function in a for loop. (Deleted in ALP Smart Syllabus 2027)
range()creates a sequence of numbers, and the for loop takes the numbers one at a time.- Its form is
range(start, stop, step). The loop starts at start, stops before stop, and moves by step. range(5)gives 0, 1, 2, 3, 4.range(2, 11, 2)gives 2, 4, 6, 8, 10.
for i in range(2, 11, 2): # even numbers from 2 to 10
print(i)
Q.6 Explain how default parameters work in Python functions.
- A parameter can be given a default value when the function is defined.
- If the function is called without that argument, the default value is used. If an argument is passed, it replaces the default.
def greet(name="Student"):
return "Hello " + name + "!"
print(greet()) # Output: Hello Student!
print(greet("Umer")) # Output: Hello Umer!
Q.7 Explain why modular programming is useful in Python.
Modular programming divides a program into smaller, manageable and reusable pieces called modules. It is useful because:
- Different people can work on different modules independently.
- Code written once in a module can be reused in other programs.
- Large and complex programs are easier to manage, read and maintain.
- Related modules can be grouped into packages, for example an
ecommercepackage withproducts.py,customers.pyandorders.py.
Q.8 Explain the difference between a class and an object in Python. (Deleted in ALP Smart Syllabus 2027)
| Class | Object |
|---|---|
| A template or blueprint that describes what an object should be like. | An actual thing (instance) created from the class. |
Defined once with the class keyword. |
Many objects can be created from one class. |
Example: the class ToyCar lists colour, size and wheels. |
Example: car1 = ToyCar("red", "small", 4) |
Long Questions
Q.1 Evaluate the following Python expressions.
Rule used: brackets first, then **, then * and / (left to right), then + and -. In Python / always gives a float.
(a) (18 / 3 + 4 ** 2) - (2 * (7 - 3)) / (9 7, 4) (Deleted in ALP Smart Syllabus 2027)
- First bracket: 18 / 3 = 6.0, and 4 ** 2 = 16, so 6.0 + 16 = 22.0
- Second bracket: 7 – 3 = 4, and 2 * 4 = 8
- The last part
(9 7, 4)is a printing error in the textbook. Python cannot evaluate it and gives a SyntaxError. Ask your teacher for the intended expression. This part is not in the 2027 exam anyway.
(b) (25 + 3 * 4 ** 2 - 6) / (2 ** 3 + 1) - 7
- 4 ** 2 = 16, so 3 * 16 = 48
- Numerator: 25 + 48 – 6 = 67
- Denominator: 2 ** 3 = 8, so 8 + 1 = 9
- 67 / 9 = 7.444…
- 7.444… – 7 = 0.444 (Python prints 0.44444444444444464)
(c) (12 + 6 * (5 - 2)) ** 2 / ((4 ** 2 - 7) + 10) (Deleted in ALP Smart Syllabus 2027)
- 5 – 2 = 3, then 6 * 3 = 18, then 12 + 18 = 30
- 30 ** 2 = 900
- 4 ** 2 = 16, then 16 – 7 = 9, then 9 + 10 = 19
- 900 / 19 = 47.368… (Python prints 47.36842105263158)
(d) 45 / (2 ** 2 + 3 * 4) + 8 * (7 - 3)
- 2 ** 2 = 4, and 3 * 4 = 12, so the bracket is 4 + 12 = 16
- 45 / 16 = 2.8125
- 7 – 3 = 4, so 8 * 4 = 32
- 2.8125 + 32 = 34.8125
Q.2 & 3 Translate the following mathematical expressions to Python syntax: (a) 5 × (3 + 2²) ÷ (6 − 2 × 3) (b) 7 + 2²
The textbook prints these as Q.2 and Q.3. In Python, × becomes *, a power becomes **, and a fraction becomes / with brackets around the top and the bottom.
| Expression | Python syntax | Result |
|---|---|---|
| (a) 5 × (3 + 2²) over (6 − 2 × 3) | 5 * (3 + 2 ** 2) / (6 - 2 * 3) |
The bottom is 6 − 6 = 0, so Python stops with ZeroDivisionError. The translation itself is correct. |
| (b) 7 + 2² | 7 + 2 ** 2 |
11 |
Q.4 Explain the concept of variables in Python.
- A variable is a named storage place in the computer’s memory. Data can be stored in it, read from it and changed.
- Its value can change while the program runs:
age = 71
print("Ahmad lived for", age, "years")
age = 60 # the same variable now holds a new value
print("Iqbal lived for", age, "years")
Naming rules:
- The name must begin with a letter (a-z, A-Z) or an underscore (_).
- After that it may contain letters, digits (0-9) or underscores.
- Names are case-sensitive:
ageandAgeare two different variables. - Reserved keywords such as
for,whileandifcannot be used. - Good practice: use meaningful lowercase names with underscores, such as
student_name.
Common types of variables:
| Type | Stores | Example |
|---|---|---|
| int | whole numbers | age = 17 |
| float | decimal numbers | price = 19.99 |
| str | text | name = "Ali" |
| bool | True or False | is_student = True |
Q.5 Write a Python program that takes a number as input and checks whether it is positive, negative, or zero using an if-elif-else statement.
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")
Sample run: input -7 prints The number is negative. Input 0 prints The number is zero.
Working: the first true condition decides the output. If neither number > 0 nor number < 0 is true, the number must be zero, so the else block runs.
Q.6 Write a Python program using a while loop that prints all the odd numbers between 1 and 100. Also, count and print the total number of odd numbers.
number = 1
count = 0
while number <= 100:
if number % 2 != 0: # remainder 1 means odd
print(number)
count += 1
number += 1
print("Total odd numbers:", count)
Output: 1, 3, 5 … 99 are printed one per line, followed by Total odd numbers: 50.
Working: the loop runs while number is 100 or less. number % 2 gives the remainder after division by 2, and an odd number leaves remainder 1. Each odd number is printed and added to count, and number += 1 moves to the next number so the loop eventually stops.
More for 1st Year Computer Science
- All units: 1st Year Computer Science exercise solutions 2027
- 11th Class pairing scheme, model papers and ALP deleted topics
The exercise questions are from the PECTAA Computer Science and Entrepreneurship-11 textbook. The deleted list follows the PECTAA ALP Smart Syllabus 2027. Please confirm all dates and figures on the official website: pectaa.edu.pk
Need help with 1st Year Computer Science? Join our live online classes for Intermediate (Punjab and Federal Board). Book a free demo class.

Ask a question about this topic