To write a Python program that calculates the sum of all elements in a list.
- Define a list of numbers.
- Use Python’s built-in
sum()function to calculate the total. - Print the result.
items=[153,147,124,102]
print(sum(items))
Thus the program executed successfully.
To write a Python program that filters out and returns all elements from a list that do not contain the letter 'e', using regular expressions (regex).
- Import the
remodule. - Initialize an empty list
l1to store results. - Define a list of words:
items = ['goal', 'new', 'user', 'sit', 'eat', 'dinner'] - Iterate through each word in the list:
- Use
re.search(r"e", i)to check if the word contains'e'. - If not, append the word to
l1.
- Use
- Print the final filtered list.
import re
l1 = []
items = ['goal', 'new', 'user', 'sit', 'eat', 'dinner']
for i in items:
if not re.search(r"e", i):
l1.append(i)
print("Words without 'e':", l1)
Thus the program executed successfully.
To write a Python program that accepts a string and removes the character at a specified index.
- Define a function named
removethat takes the input string as an argument. - Read the index
nfrom the user input. - Initialize an empty string
ato store the new string. - Iterate over each index of the string using a
forloop. - Check if the current index
iis not equal ton. - If
i != n, append the character at indexito stringa. - After the loop, return the modified string
a. - Print the final result.
n=int(input())
def remove(a):
for i in range(0,len(a)):
if(i!=n):
print(a[i],end='')
Thus the program executed successfully.
To write a Python program to check whether the string "google" is a palindrome or not, without using built-in palindrome checking functions.
- Assign the string
"google"to a variable. - Reverse the string manually using slicing (
[::-1]). - Compare the original string with the reversed string.
- If they are equal, print that the string is a palindrome.
- Otherwise, print that it is not a palindrome.
- Execute the program.
a=input()
s=a[::-1]
if a==s:
print("The entered string is palindrome")
else:
print("The entered string is not palindrome")
Thus the program executed successfully.
To write a Python program that checks if the element 'n' and the element 8 exist within a given tuple.
- Define a tuple
xwith some letters and numbers. - Use the
inoperator to check if the string'n'exists within the tuple. - Use the
inoperator to check if the integer8exists within the tuple. - Print the results.
tuplex = input()
print("n" in tuplex)
print("8" in tuplex)
Thus the program executed successfully




