KEMBAR78
Python Lab 3 | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
4 views19 pages

Python Lab 3

The document contains several Python programs demonstrating various list operations, including calculating sums and averages, counting vowels, checking for palindromes, and finding the largest and smallest numbers. It also includes examples of removing duplicates, making shallow and deep copies, sorting names by length, and generating groups of consecutive numbers. Additionally, it covers frequency counting, finding common elements in two lists, and swapping elements.

Uploaded by

onkarshukla843
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Python Lab 3

The document contains several Python programs demonstrating various list operations, including calculating sums and averages, counting vowels, checking for palindromes, and finding the largest and smallest numbers. It also includes examples of removing duplicates, making shallow and deep copies, sorting names by length, and generating groups of consecutive numbers. Additionally, it covers frequency counting, finding common elements in two lists, and swapping elements.

Uploaded by

onkarshukla843
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

# Program to calculate sum and average of elements in a list

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

total = sum(numbers)

average = total / len(numbers)

print("Numbers in the list:", numbers)

print("Sum of elements:", total)

print("Average of elements:", average)

output
Numbers in the list: [10, 20, 30, 40, 50]

Sum of elements: 150

Average of elements: 30.0


# Program to count vowels in a list entered by the user

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

char_list = []

for i in range(n):

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

char_list.append(element)

vowels = ['a', 'e', 'i', 'o', 'u',

'A', 'E', 'I', 'O', 'U']

count = 0

for ch in char_list:

if ch in vowels:

count += 1

print("List entered:", char_list)

print("Number of vowels in the list:", count)

output
Enter the number of elements in the list: 6
Enter element 1: a

Enter element 2: b

Enter element 3: e

Enter element 4: i

Enter element 5: x

Enter element 6: O

List entered: ['a', 'b', 'e', 'i', 'x', 'O']

Number of vowels in the list: 4


# Program to check if a list is palindrome or not

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

lst = []

for i in range(n):

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

lst.append(element)

if lst == lst[::-1]:

print("The list is a palindrome.")

else:

print("The list is NOT a palindrome.")

output
Enter the number of elements in the list: 5

Enter element 1: 1

Enter element 2: 2

Enter element 3: 3

Enter element 4: 2

Enter element 5: 1

The list is a palindrome.


# Program to find sum of digits only if length > 5

num = input("Enter a number: ")

if len(num) > 5:

digit_sum = sum(int(d) for d in num)

print("Sum of digits:", digit_sum)

else:

print("Number length is not greater than 5, so no sum calculated.")

output
Enter a number: 987654

Sum of digits: 39

Output
Enter a number: 1234

Number length is not greater than 5, so no sum calculated.


# Program to print first 10 odd Fibonacci numbers

a, b = 0, 1

count = 0

print("First 10 odd Fibonacci numbers:")

while count < 10:

if a % 2 != 0:

print(a, end=" ")

count += 1

a, b = b, a + b

output
First 10 odd Fibonacci numbers:

1 1 3 5 13 21 55 89 233 377
# Program to find largest and smallest number in a list

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

lst = []

for i in range(n):

element = int(input(f"Enter element {i+1}: "))

lst.append(element)

largest = max(lst)

smallest = min(lst)

print("List entered:", lst)

print("Largest number:", largest)

print("Smallest number:", smallest)

output
Enter number of elements in the list: 5

Enter element 1: 12

Enter element 2: 7

Enter element 3: 45

Enter element 4: 3
Enter element 5: 28

List entered: [12, 7, 45, 3, 28]

Largest number: 45

Smallest number: 3
#Program to remove duplicate elements from a list

user_input = input("Enter elements separated by spaces: ")

elements = user_input.split()

unique_list = [] for item in elements: if item not in unique_list:

unique_list.append(item)

print("List after removing duplicates:", unique_list)

Output
Enter elements separated by spaces: 1 2 3 2 4 1 5

List after removing duplicates: ['1', '2', '3', '4', '5']


#Wap to create a shallow and deep copy of a list and observe
a difference between the two
import copy

original_list = [[1, 2, 3], [4, 5, 6]]

shallow_copy = copy.copy(original_list)

deep_copy = copy.deepcopy(original_list)

print("Original List:", original_list) print("Shallow Copy:", shallow_copy)


print("Deep Copy:", deep_copy

original_list[0][0] = 99

print("\nAfter modifying original_list[0][0] = 99")

print("Original List:", original_list)

print("Shallow Copy:", shallow_copy)

print("Deep Copy:", deep_copy)

Output

Original List: [[1, 2, 3], [4, 5, 6]

Shallow Copy: [[1, 2, 3], [4, 5, 6]]

Deep Copy: [[1, 2, 3], [4, 5, 6]]

After modifying original_list[0][0] = 99

Original List: [[99, 2, 3], [4, 5, 6]]

Shallow Copy: [[99, 2, 3], [4, 5, 6]]


Deep Copy: [[1, 2, 3], [4, 5, 6]]
#Program to sort list of names according to their length

names = input("Enter names separated by spaces: ").split()

names.sort(key=len)

print("Names sorted by length:", names)

Output
Enter names separated by spaces: John Ramesh Sita Alok

Names sorted by length: ['Sita', 'John', 'Alok', 'Ramesh']


#Program to find the name with largest and smallest length

names = input("Enter names separated by spaces: ").split()

smallest = min(names, key=len) largest = max(names, key=len)

print("Name with smallest length:", smallest) print("Name with largest


length:", largest)

Output
Enter names separated by spaces: Raju Mohan Alok Sita Shyam

Name with smallest length: Raju

Name with largest length: Mohan

#Program to demonstrate slicing on a list

elements = input("Enter list elements separated by spaces: ").split()

print("Original List:", elements)

print("First 3 elements:", elements[:3])


print("Last 3 elements:", elements[-3:])

print("Elements from index 2 to 5:", elements[2:6])

print("Every 2nd element:", elements[::2])

print("List in reverse:", elements[::-1])

Output
Enter list elements separated by spaces: 10 20 30 40 50 60 70

Original List: ['10', '20', '30', '40', '50', '60', '70']

First 3 elements: ['10', '20', '30']

Last 3 elements: ['50', '60', '70']

Elements from index 2 to 5: ['30', '40', '50', '60']

Every 2nd element: ['10', '30', '50', '70']

List in reverse: ['70', '60', '50', '40', '30', '20', '10']


#Generate groups of consecutive numbers

n = int(input("Enter how many groups: "))

k = int(input("Enter size of each group: "))

groups = []

num = 1

for i in range(n):

group = list(range(num, num + k))

groups.append(group)

num += k

print("Groups of consecutive numbers:", groups)

Output
Enter how many groups: 3

Enter size of each group: 4

Groups of consecutive numbers: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

#Count frequency of elements in a list

elements = input("Enter elements separated by spaces: ").split()

frequency = {}

for item in elements:

frequency[item] = frequency.get(item, 0) + 1

print("Frequency of elements:", frequency)


Output
Enter elements separated by spaces: apple mango apple orange mango apple

Frequency of elements: {'apple': 3, 'mango': 2, 'orange': 1}


#Common numbers in two lists

list1 = input("Enter first list (space-separated): ").split()

list2 = input("Enter second list (space-separated): ").split()

common = list(set(list1) & set(list2))

print("Common elements:", common)

Output
Enter first list (space-separated): 1 2 3 4 5

Enter second list (space-separated): 4 5 6 7 8

Common elements: ['4', '5']

#Swap every nth and (n+1)th element in a list

elements = input("Enter elements separated by spaces: ").split()

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

elements[i], elements[i+1] = elements[i+1], elements[i]

print("List after swapping:", elements)


Output
Enter elements separated by spaces: 10 20 30 40 50 60

List after swapping: ['20', '10', '40', '30', '60', '50']


# Generate random numbers in a list and calculate sum

import random

n = int(input("Enter how many random numbers: "))

numbers = [random.randint(1, 100) for _ in range(n)]

print("Random Numbers:", numbers)

print("Sum of numbers:", sum(numbers))

Output
Enter how many random numbers: 5

Random Numbers: [23, 78, 11, 56, 92]

Sum of numbers: 260

You might also like