Index
Combining Two Lists by Adding Elements.
Adding, Extending, and Removing Elements from a List.
List Filtering: Extracting Names with Four Characters.
Calculating Total Marks from a List of Subject Scores.
Convert odd numbers to even.
Program to Count Vowels and Reverse a String.
Program to count lowercase alphabets and check palindrome
Display Names with 'i' as the Second Last Character
Move the Last Element of a List to the Beginning
Practical Programs
1. Write a python program to add the elements of the two lists.
Code:
# Define two lists
list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]
# Initialize an empty list to store the sum
sum = []
# Using a for loop to add elements
for i in range(len(list1)):
sum.append(list1[i] + list2[i])
# Print the result
print("Sum of elements:", sum)
Output:
Sum of elements: [11, 22, 33, 44, 55]
2. Write a Python program to create a list, add an element, extend the list with multiple elements, and remove an element.
Code:
# Creating an empty list
fruits = []
# Using append() to add single elements
fruits.append("Apple")
fruits.append("Banana")
# Using extend() to add multiple elements
fruits.extend(["Mango", "Orange", "Grapes"])
# Printing the list after adding elements
print("Fruits List after append and extend:", fruits)
# Using remove() to delete an element
fruits.remove("Orange")
# Printing the final list
print("Fruits List after remove:", fruits)
Output:
Fruits List after append and extend: ['Apple', 'Banana', 'Mango', 'Orange', 'Grapes']
Fruits List after remove: ['Apple', 'Banana', 'Mango', 'Grapes']
3. Write a Python program to display all the names from a list of names whose length is of four characters.
Code:
# List of names
names = ["John", "Mike", "Sara", "Anna", "Chris", "Ella", "Tom"]
# Loop through the list to find names with length 4
for n in names:
if len(n) == 4:
print(n)
Output:
John
Mike
Sara
Anna
Ella
4. Write a Python program to compute total marks from the marks of different subjects stored in the form of a list.
Code:
# List of Marks
marks = [93, 78, 69, 71, 65]
print("Marks are:", marks)
# Initialize a value to the variable
total = 0
# Using a for loop to compute total marks
for sm in marks:
total = total + sm
# Print the result
print(" The total marks are:", total)
Output:
Marks are: [93, 78, 69, 71, 65]
The total marks are: 376
5. Write a Python program which convert all the odd numbers in the list to even by adding 1.
Code:
# List of numbers
numbers = [3, 7, 8, 12, 15, 21, 26]
# Loop through the list using index
for i in range(len(numbers)):
if numbers[i] % 2 != 0:
numbers[i] += 1
# Print the updated list
print("List after converting odd numbers to even:", numbers)
Output:
List after converting odd numbers to even: [4, 8, 8, 12, 16, 22, 26]
6. Write a Python program that performs the following tasks:
Accepts a string input from the user.
Counts and displays the number of vowels in the string.
Reverses the string and displays the reversed version.
Code:
# Read a string from the user
text = input("Enter a string: ")
# Initialize vowel count
count = 0
# Define vowels
vowels = "aeiouAEIOU"
# Count vowels
for char in text:
if char in vowels:
count += 1
# Reverse the string
reverse = text[::-1]
# Display results
print("Number of vowels:", count)
print("Reversed string:", reverse)
Output:
Enter a string: Artificial Intelligence
Number of vowels: 10
Reversed string: ecnegilletnI laicifitrA
7. Write a Python program that performs the following tasks:
Counts and displays the number of lowercase (small case) alphabets in a given string.
Checks and displays whether the input string is a palindrome or not.
Code:
# Accept input string from the user
text = input("Enter a string: ")
# Count lowercase letters
count = 0
for char in text:
if char.islower():
count += 1
# Output the results
print("Number of lowercase letters:", count)
# Convert the sting to lowercase
T = text.lower()
# Check for palindrome
if T == T[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Output:
Enter a string: Malayalam
Number of lowercase letters: 8
The string is a palindrome.
Enter a string: Program
Number of lowercase letters: 6
The string is not a palindrome.
8. Write a Python program which display only those names from the list which have 'i' as second last character.
Code:
# Sample list of names
names = ["Pritii", "Risi", "Nidhi", "Anvik", "Rani", "Ritia"]
# Display names with 'i' as the second last character
print("Names with 'i' as the second last character:")
for n in names:
if len(n) >= 2 and n[-2] == 'i':
print(n)
Output:
Names with 'i' as the second last character:
Pritii
Anvik
Ritia
9. Write a python program which removes the last element from the list and it in the beginning.
Code:
# Sample list
items = [10, 20, 30, 40, 50]
# Check if list is not empty
if items !=[ ]:
last = items.pop() # Remove last element
items.insert(0, last) # Insert at the beginning
# Display deleted item
print("Deleted Item:", last)
# Display updated list
print("Updated list:", items)
Output:
Deleted Item: 50
Updated list: [50, 10, 20, 30, 40]