Ad Code

Responsive Advertisement

Basic Python Program - 28

Question 28: Write a Python program to select the even items of a list.
Download hole Program / Project code, by clicking following link:
Describe various list functions in Python ?
In Python, lists are a versatile data structure, and there are several built-in functions and methods that you can use to perform various operations on lists. Here are some of the most commonly used list functions and methods:
List Functions:
  1. len(list): Returns the number of elements in the list.
    numbers = [1, 2, 3, 4, 5]
    length = len(numbers) # length will be 5

  2. max(list): Returns the maximum value in the list (only applicable to lists containing comparable elements like numbers).
    numbers = [1, 3, 5, 2, 4]
    max_value = max(numbers) # max_value will be 5
  3. min(list): Returns the minimum value in the list (only applicable to lists containing comparable elements).
    numbers = [1, 3, 5, 2, 4]
    min_value = min(numbers) # min_value will be 1

  4. sum(list): Returns the sum of all elements in the list (only applicable to numeric lists).
    numbers = [1, 2, 3, 4, 5]
    total = sum(numbers) # total will be 15
List Methods:
  1. append(item): Adds an element to the end of the list.
    fruits = ["apple", "banana"]
    fruits.append("cherry")
    # fruits will be ["apple", "banana", "cherry"]
  2. insert(index, item): Inserts an element at the specified index.
    numbers = [1, 2, 3]
    numbers.insert(1, 4)
    # numbers will be [1, 4, 2, 3]
  3. extend(iterable): Appends the elements of an iterable (e.g., another list) to the end of the list.
    numbers = [1, 2, 3]
    numbers.extend([4, 5])
    # numbers will be [1, 2, 3, 4, 5]

  4. remove(item): Removes the first occurrence of the specified item from the list.
    fruits = ["apple", "banana", "cherry"] fruits.remove("banana")
    # fruits will be ["apple", "cherry"]

  5. pop(index): Removes and returns the element at the specified index. If no index is specified, it removes and returns the last element.
    numbers = [1, 2, 3, 4, 5]
    popped_item = numbers.pop(2) # Removes the element at index 2 (3) and assigns it to popped_item
    # numbers will be [1, 2, 4, 5]

  6. index(item): Returns the index of the first occurrence of the specified item.
    fruits = ["apple", "banana", "cherry"] index = fruits.index("banana")
    # index will be 1

  7. count(item): Returns the number of times the specified item appears in the list.
    numbers = [1, 2, 2, 3, 2, 4]
    count = numbers.count(2)
    # count will be 3

  8. sort(): Sorts the elements of the list in ascending order.
    numbers = [4, 2, 1, 3]
    numbers.sort()
    # numbers will be [1, 2, 3, 4]

  9. reverse(): Reverses the order of elements in the list.
    fruits = ["apple", "banana", "cherry"]
    fruits.reverse()
    # fruits will be ["cherry", "banana", "apple"]

  10. clear(): Removes all elements from the list, making it empty.
    numbers = [1, 2, 3, 4, 5]
    numbers.clear()
    # numbers will be []
These are some of the fundamental functions and methods for working with Python lists. Depending on your needs, you can use these functions and methods to manipulate and manage lists effectively.
Programming Code:
Following code write in: BP_P28.py
# Python Program to select Even items of a list

def even_item(a):
    b = []
    # check length of list
    if len(a) > 0:
        for num in a:
            if num % 2 == 0:
                b.append(num)
    
    print("Even items of a list is: ", b)

# create a list

# a = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

# for given input list

num = int(input("Enter number of List 1 integer items: "))
a = []

for i in range(0, num):
    c = int(input(f"Enter List 1 integer item {i}: "))
    a.append(c)

print("List is: ", a)

even_item(a)

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement