Curriculum
Course: Python-100-Programmes
Login
Text lesson

F-Lists-(80-92)

F-Lists

 

  1. Create a list and print all elements.

 

Here is a simple Python program to create a list and print all its elements:

# Create a list

my_list = [10, 20, 30, 40, 50]

 

# Print all elements

for element in my_list:

    print(element)

 

Example Output

10

20

30

40

50

 

Explanation

  • my_list contains elements [10, 20, 30, 40, 50]

  • for element in my_list loops through each element

  • print(element) displays each element

 

  1. Input 10 numbers in a list and print them.

Here is a Python program to input 10 numbers into a list and print them:

# Create an empty list

numbers = []

 

# Input 10 numbers from user

for i in range(10):

    num = int(input(f”Enter number {i+1}: “))

    numbers.append(num)

 

# Print the list

print(“The numbers in the list are:”)

for num in numbers:

    print(num)

 

Example

Input:

1 2 3 4 5 6 7 8 9 10

 

Output:

The numbers in the list are:

1

2

3

4

5

6

7

8

9

10

 

  1. Find the largest number in a list.

Here is a Python program to find the largest number in a list:

# Create a list

numbers = [10, 25, 5, 70, 45]

 

# Assume the first element is the largest

largest = numbers[0]

 

# Compare each element

for num in numbers:

    if num > largest:

        largest = num

 

print(“The largest number is:”, largest)

 

Example Output

The largest number is: 70

 

Explanation

  • Start with the first element as largest

  • Loop through each number in the list
    If a number is greater than largest, update largest

  • At the end, largest contains the maximum number

 

  1. Find the smallest number in a list.

Here is a Python program to find the smallest number in a list:

# Create a list

numbers = [10, 25, 5, 70, 45]

 

# Assume the first element is the smallest

smallest = numbers[0]

 

# Compare each element

for num in numbers:

    if num < smallest:

        smallest = num

 

print(“The smallest number is:”, smallest)

 

Example Output

The smallest number is: 5

 

Explanation

  • Start with the first element as smallest

  • Loop through each number in the list

  • If a number is smaller than smallest, update smallest

  • At the end, smallest contains the minimum number

Here is a Python program to find the smallest number in a list using min():

# Create a list

numbers = [10, 25, 5, 70, 45]

 

# Find the smallest number using min()

smallest = min(numbers)

 

print(“The smallest number is:”, smallest)

 

Example Output

The smallest number is: 5

 

Explanation

  • min(numbers) returns the smallest element in the list

  • Very simple and efficient compared to looping

  1. Count even numbers in a list.

 

Here is a Python program to count even numbers in a list:

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Initialize counter

even_count = 0

 

# Loop through the list

for num in numbers:

    if num % 2 == 0:

        even_count += 1

 

print(“Number of even numbers:”, even_count)

 

Example Output

Number of even numbers: 4

 

Explanation

  • % 2 == 0 checks if a number is even

  • Increment even_count for each even number

  • At the end, even_count contains the total number of even numbers

 

  1. Count odd numbers in a list.

 

Here is a Python program to count odd numbers in a list:

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Initialize counter

odd_count = 0

 

# Loop through the list

for num in numbers:

    if num % 2 != 0:

        odd_count += 1

 

print(“Number of odd numbers:”, odd_count)

 

Example Output

Number of odd numbers: 3

 

Explanation

  • % 2 != 0 checks if a number is odd

  • Increment odd_count for each odd number

  • At the end, odd_count contains the total number of odd numbers

 

  1. Search for an element in a list.

 

Here is a simple Python program to search for an element in a list:

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Input element to search

target = int(input(“Enter the number to search: “))

 

# Initialize flag

found = False

 

# Loop through the list

for num in numbers:

    if num == target:

        found = True

        break

 

# Output result

if found:

    print(f”{target} is present in the list.”)

else:

    print(f”{target} is not present in the list.”)

 

Example

Input:

Enter the number to search: 7

 

Output:

7 is present in the list.

 

Explanation

  • Loop through each element in the list

  • If an element matches the target, set found = True and stop the loop

  • Check found to print the result

Here is a Python program to search for an element in a list and return its index:

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Input element to search

target = int(input(“Enter the number to search: “))

 

# Initialize index

index = -1

 

# Loop through the list

for i in range(len(numbers)):

    if numbers[i] == target:

        index = i

        break

 

# Output result

if index != -1:

    print(f”{target} is present at index {index}.”)

else:

    print(f”{target} is not present in the list.”)

 

Example

Input:

Enter the number to search: 7

 

Output:

7 is present at index 3.

 

Explanation

  • Loop through the list using range(len(numbers)) to get indexes

  • If element matches, store its index and break the loop

  • If index is -1, element is not found

Here is a Python program to find the index of an element using list.index():

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Input element to search

target = int(input(“Enter the number to search: “))

 

# Using list.index() with try-except

try:

    index = numbers.index(target)

    print(f”{target} is present at index {index}.”)

except ValueError:

    print(f”{target} is not present in the list.”)

 

Example

Input:

Enter the number to search: 7

 

Output:

7 is present at index 3.

 

Explanation

  • numbers.index(target) returns the index of the first occurrence

  • If the element is not found, it raises a ValueError, which we catch using try-except

 

  1. Reverse a list without using reverse().

Here is a Python program to reverse a list without using reverse():

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Initialize an empty list for reversed elements

reversed_list = []

 

# Loop through the original list and prepend each element

for num in numbers:

    reversed_list = [num] + reversed_list

 

print(“Reversed list:”, reversed_list)

 

Example Output

Reversed list: [20, 9, 12, 7, 4, 25, 10]

 

Explanation

  • Start with an empty list reversed_list

  • For each element in the original list, add it at the beginning of reversed_list

  • This way, the list is reversed without using reverse()

 

  1. Swap even-index elements with odd-index elements.

Here is a Python program to swap even-index elements with odd-index elements in a list:

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Loop through the list with step 2

for i in range(0, len(numbers)-1, 2):

    numbers[i], numbers[i+1] = numbers[i+1], numbers[i]

 

print(“List after swapping:”, numbers)

 

Example Output

List after swapping: [25, 10, 7, 4, 9, 12, 20]

 

Explanation

  • range(0, len(numbers)-1, 2) goes through even indexes

  • Swap element at index i with element at i+1

  • If the list has odd length, the last element remains unchanged

 

  1. Remove duplicates from a list.

 

Here is a Python program to remove duplicates from a list:

# Create a list

numbers = [10, 25, 4, 7, 10, 25, 20]

 

# Using a new list to store unique elements

unique_numbers = []

 

for num in numbers:

    if num not in unique_numbers:

        unique_numbers.append(num)

 

print(“List after removing duplicates:”, unique_numbers)

 

Example Output

List after removing duplicates: [10, 25, 4, 7, 20]

 

Explanation

  • Start with an empty list unique_numbers

  • Loop through each element in the original list

  • If the element is not already in unique_numbers, append it

  • At the end, unique_numbers contains only unique elements

 

  1. Find the largest/smallest number in a list.

Here is a Python program to find both the largest and smallest numbers in a list:

# Create a list

numbers = [10, 25, 4, 7, 12, 9, 20]

 

# Assume first element as largest and smallest

largest = numbers[0]

smallest = numbers[0]

 

# Loop through the list

for num in numbers:

    if num > largest:

        largest = num

    if num < smallest:

        smallest = num

 

print(“Largest number:”, largest)

print(“Smallest number:”, smallest)

 

Example Output

Largest number: 25

Smallest number: 4

 

Explanation

  • Start with the first element as both largest and smallest

  • Loop through each element:

    • If element > largest, update largest

    • If element < smallest, update smallest

  • Finally, print both values

 

  1. Input a list of elements, search for a given element in the list.

Here is a Python program to input a list of elements from the user and search for a given element:

# Input number of elements

n = int(input(“Enter number of elements in the list: “))

 

# Input elements into the list

my_list = []

for i in range(n):

    element = input(f”Enter element {i+1}: “)

    my_list.append(element)

 

# Input the element to search

target = input(“Enter the element to search: “)

 

# Search for the element

if target in my_list:

    print(f”{target} is present in the list.”)

else:

    print(f”{target} is not present in the list.”)

 

Example

Input:

Enter number of elements in the list: 5

Enter element 1: 10

Enter element 2: 25

Enter element 3: 4

Enter element 4: 7

Enter element 5: 12

Enter the element to search: 7

 

Output:

7 is present in the list.

 

Explanation

  • User inputs number of elements and then the elements themselves

  • if target in my_list: checks if the element exists in the list

  • Prints the result

  1. Write a random number generator that generates random numbers between 1 and 6.

 

Here is a Python program to generate random numbers between 1 and 6 (like a dice roll):

 

import random

 # Generate a random number between 1 and 6

number = random.randint(1, 6)

 print(“Random number:”, number)

 

Example Output

Random number: 4

 

Explanation

  • import random imports Python’s random module

  • random.randint(1, 6) generates a random integer between 1 and 6 (inclusive)

  • Each run of the program can give a different number

 

Scroll to Top