Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course
3.6 Lists: Sequence and Repetition with Collections
Unit 3 – Programming Fundamentals · 75 min read · 10 MCQs to check yourself
Python for loops with lists (includes list.append())
1. What is a list?
A list stores many values in one variable, in order: marks = [78, 65, 90]. Lists are ordered, changeable (mutable) and allow duplicates and mixed types. Items are accessed by index starting at 0; negative indexes count from the end.
cities = ["Islamabad", "Karachi", "Lahore", "Quetta"]
print(cities[0], cities[-1], len(cities))
print(cities[1:3])
cities[2] = "Multan"
print(cities)
Output:
Islamabad Quetta 4
['Karachi', 'Lahore']
['Islamabad', 'Karachi', 'Multan', 'Quetta']
| Method / function | Action | Example |
|---|---|---|
append(x) |
Add x at the end | marks.append(88) |
insert(i, x) |
Insert x at index i | marks.insert(0, 50) |
remove(x) |
Remove the first x | marks.remove(65) |
pop() / pop(i) |
Remove and return last / index i | marks.pop() |
sort(), reverse() |
Sort / reverse in place | marks.sort() |
index(x), count(x) |
Position / number of x | marks.index(90) |
len, sum, max, min |
Built-in functions | sum(marks) / len(marks) |
in |
Membership test | 90 in marks |
2. Populating a list with a loop
squares = []
for n in range(1, 6):
squares.append(n * n)
print(squares)
Output:
[1, 4, 9, 16, 25]
From user input (enter 5 marks):
marks = []
for i in range(5): # input: 70 | 85 | 92 | 60 | 78
m = int(input("Enter marks: "))
marks.append(m)
print("Marks:", marks)
print("Total:", sum(marks), "Average:", sum(marks) / len(marks))
Output:
Marks: [70, 85, 92, 60, 78]
Total: 385 Average: 77.0
3. Finding an element in a list
names = ["Ali", "Sara", "Hamza", "Fatima", "Usman"]
target = "Fatima"
found = False
i = 0
while i < len(names) and not found:
if names[i] == target:
found = True
else:
i += 1
if found:
print(target, "found at index", i)
else:
print(target, "not found")
Output:
Fatima found at index 3
Find the largest value without max():
data = [34, 78, 12, 95, 47]
largest = data[0]
for x in data:
if x > largest:
largest = x
print("Largest:", largest)
Output:
Largest: 95
4. Model paper long question – count even and odd numbers using a while loop
numbers = [12, 7, 9, 20, 33, 46, 51, 8]
even = 0
odd = 0
i = 0
while i < len(numbers):
if numbers[i] % 2 == 0:
even += 1
else:
odd += 1
i += 1
print("Even numbers:", even)
print("Odd numbers:", odd)
Output:
Even numbers: 4
Odd numbers: 4
5. Strings behave like lists of characters
word = "COMPUTER"
vowels = 0
for ch in word:
if ch in "AEIOU":
vowels += 1
print(word[0], word[-1], len(word), vowels)
Output:
C R 8 3
Using the for loop and the while loop with a Python list
Short and long questions
1. Write a Python program that populates a list with the first ten even numbers using a loop.
Model answer
evens = []
for i in range(1, 11):
evens.append(2 * i)
print(evens)
Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
2. Write a Python program to find whether a given name exists in a list of names.
Model answer
names = ["Ali", "Sara", "Hamza", "Fatima"]
target = input("Name to search: ")
found = False
for n in names:
if n == target:
found = True
break
print("Found" if found else "Not found")
3. Explain any four list methods with examples.
Model answer
append(x) adds x at the end: a.append(5). insert(i, x) inserts x at index i: a.insert(0, 1). remove(x) deletes the first occurrence of x: a.remove(5). sort() arranges items in ascending order: a.sort(). (Also pop(), index(), count(), reverse().)
4. Write the output: a = [10, 20, 30, 40, 50]; print(a[1], a[-2], a[1:4], len(a))
Model answer
20 40 [20, 30, 40] 5
5. Write a Python program to count even and odd numbers in a list by using a while loop.
Model answer
numbers = [12, 7, 9, 20, 33, 46, 51, 8]
even = 0
odd = 0
i = 0
while i < len(numbers):
if numbers[i] % 2 == 0:
even = even + 1
else:
odd = odd + 1
i = i + 1
print("Even numbers:", even)
print("Odd numbers:", odd)
Output: Even numbers: 4 · Odd numbers: 4.
Explanation: i is the index starting at 0; the loop runs while i is less than the length; % 2 == 0 tests for an even number; the counter i is increased each time so the loop ends.
6. Write a Python program that inputs 10 numbers into a list and then displays the largest number, the smallest number, the sum and the average, without using max(), min() or sum().
Model answer
nums = []
for i in range(10):
nums.append(int(input("Enter a number: ")))
largest = smallest = nums[0]
total = 0
for n in nums:
if n > largest:
largest = n
if n < smallest:
smallest = n
total += n
print("Largest:", largest)
print("Smallest:", smallest)
print("Sum:", total, "Average:", total / len(nums))
The first loop populates the list (sequence + repetition); the second loop finds the required values by comparing every element.
This lesson ends with a quiz of 10 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 3.5 Python Libraries: Using Code Others Have Written · Next: 3.7 Functions and Problem Decomposition
All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course