Class 11 Computer Science Practical File
Programs on LIST using FOR Loop
Program 1: Display elements of a list
Aim: To display all elements of a list using for loop.
Algorithm:
• Start
• Create a list
• Use for loop to traverse the list
• Print each element
• Stop
Program:
fruits = ["Apple", "Banana", "Mango", "Orange"]
print("Fruits in the list:")
for f in fruits:
print(f)
Sample Output:
Fruits in the list:
Apple
Banana
Mango
Orange
Program 2: Print elements in reverse order
Aim: To print list elements in reverse order using for loop.
Algorithm:
• Start
• Create a list
• Use range with reverse index
• Print each element
• Stop
Program:
nums = [10, 20, 30, 40, 50]
print("List in reverse order:")
for i in range(len(nums)-1, -1, -1):
print(nums[i])
Sample Output:
List in reverse order:
50
40
30
20
10
Program 3: Calculate sum of list elements
Aim: To calculate sum of elements of a list using for loop.
Algorithm:
• Start
• Create a list
• Initialize total=0
• Use for loop to add elements
• Print total
• Stop
Program:
marks = [78, 65, 89, 90, 56]
total = 0
for m in marks:
total += m
print("Marks:", marks)
print("Total:", total)
Sample Output:
Marks: [78, 65, 89, 90, 56]
Total: 378
Program 4: Calculate average of list elements
Aim: To calculate the average of elements in a list using for loop.
Algorithm:
• Start
• Create a list
• Initialize total=0
• Add all elements
• Divide by length
• Stop
Program:
numbers = [5, 10, 15, 20, 25]
total = 0
for n in numbers:
total += n
average = total / len(numbers)
print("Numbers:", numbers)
print("Average:", average)
Sample Output:
Numbers: [5, 10, 15, 20, 25]
Average: 15.0
Program 5: Find maximum element
Aim: To find maximum element in a list using for loop.
Algorithm:
• Start
• Create a list
• Assume first element is max
• Compare each element
• Update max if needed
• Stop
Program:
values = [45, 78, 23, 90, 56]
maxi = values[0]
for v in values:
if v > maxi:
maxi = v
print("List:", values)
print("Maximum:", maxi)
Sample Output:
List: [45, 78, 23, 90, 56]
Maximum: 90
Program 6: Find minimum element
Aim: To find minimum element in a list using for loop.
Algorithm:
• Start
• Create a list
• Assume first element is min
• Compare each element
• Update min if needed
• Stop
Program:
values = [45, 78, 23, 90, 56]
mini = values[0]
for v in values:
if v < mini:
mini = v
print("List:", values)
print("Minimum:", mini)
Sample Output:
List: [45, 78, 23, 90, 56]
Minimum: 23
Program 7: Count even and odd numbers
Aim: To count the number of even and odd elements in a list.
Algorithm:
• Start
• Create a list
• Initialize counters
• Check each element
• Update counters
• Stop
Program:
nums = [11, 24, 36, 47, 59, 62]
even_count = 0
odd_count = 0
for n in nums:
if n % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Numbers:", nums)
print("Even:", even_count)
print("Odd:", odd_count)
Sample Output:
Numbers: [11, 24, 36, 47, 59, 62]
Even: 3
Odd: 3
Program 8: Separate positive and negative numbers
Aim: To separate positive and negative numbers from a list.
Algorithm:
• Start
• Create a list
• Initialize two empty lists
• Check each element
• Append to correct list
• Stop
Program:
nums = [12, -7, 9, -15, 25, -3]
positive = []
negative = []
for n in nums:
if n >= 0:
positive.append(n)
else:
negative.append(n)
print("Original List:", nums)
print("Positive Numbers:", positive)
print("Negative Numbers:", negative)
Sample Output:
Original List: [12, -7, 9, -15, 25, -3]
Positive Numbers: [12, 9, 25]
Negative Numbers: [-7, -15, -3]
Program 9: Print squares of list elements
Aim: To print squares of all elements of a list.
Algorithm:
• Start
• Create a list
• For each element compute square
• Print result
• Stop
Program:
nums = [2, 4, 6, 8, 10]
print("Squares of numbers:")
for n in nums:
print(n, "->", n**2)
Sample Output:
Squares of numbers:
2 -> 4
4 -> 16
6 -> 36
8 -> 64
10 -> 100
Program 10: Search an element in list
Aim: To search an element in a list using for loop.
Algorithm:
• Start
• Create a list
• Input element
• Check each element
• If found, stop
• Else not found
Program:
nums = [12, 45, 67, 23, 89, 34]
x = 67
found = False
for n in nums:
if n == x:
found = True
break
if found:
print(x, "is present in the list.")
else:
print(x, "is not present in the list.")
Sample Output:
67 is present in the list.
Program 11: Count frequency of an element
Aim: To count frequency of an element in a list.
Algorithm:
• Start
• Create a list
• Input element
• Initialize counter
• Traverse list and count
• Print frequency
Program:
nums = [1, 2, 3, 2, 4, 2, 5]
x = 2
count = 0
for n in nums:
if n == x:
count += 1
print("List:", nums)
print("Frequency of", x, "=", count)
Sample Output:
List: [1, 2, 3, 2, 4, 2, 5]
Frequency of 2 = 3
Program 12: Create a new list of even numbers only
Aim: To extract even numbers from a list.
Algorithm:
• Start
• Create a list
• Initialize empty even list
• Check each element
• Append if even
• Stop
Program:
nums = [3, 12, 7, 20, 15, 8]
even_list = []
for n in nums:
if n % 2 == 0:
even_list.append(n)
print("Original List:", nums)
print("Even List:", even_list)
Sample Output:
Original List: [3, 12, 7, 20, 15, 8]
Even List: [12, 20, 8]
Program 13: Multiply all elements of a list
Aim: To multiply all elements of a list using for loop.
Algorithm:
• Start
• Create a list
• Initialize product=1
• Multiply each element
• Print product
Program:
nums = [2, 3, 4, 5]
product = 1
for n in nums:
product *= n
print("Numbers:", nums)
print("Product:", product)
Sample Output:
Numbers: [2, 3, 4, 5]
Product: 120
Program 14: Find second largest number
Aim: To find the second largest element in a list.
Algorithm:
• Start
• Create a list
• Initialize largest and second
• Traverse list
• Update values
• Stop
Program:
nums = [45, 78, 23, 90, 56]
largest = second = nums[0]
for n in nums:
if n > largest:
second = largest
largest = n
elif n > second and n != largest:
second = n
print("List:", nums)
print("Second Largest:", second)
Sample Output:
List: [45, 78, 23, 90, 56]
Second Largest: 78
Program 15: Remove duplicates from list
Aim: To remove duplicate elements from a list.
Algorithm:
• Start
• Create a list
• Initialize empty list
• Append only if not already present
• Print new list
Program:
nums = [1, 2, 2, 3, 4, 4, 5]
unique = []
for n in nums:
if n not in unique:
unique.append(n)
print("Original List:", nums)
print("List without duplicates:", unique)
Sample Output:
Original List: [1, 2, 2, 3, 4, 4, 5]
List without duplicates: [1, 2, 3, 4, 5]