Practice Problems ( 19 Aug 2023 )

Q.1] Write a Python program to calculate the length of a string.

Q.2] Write a Python function that takes a list and returns a new list with distinct elements from the first list.

Q.3] Write a Python program to print the even numbers from a given list.

Q.4] Write a Python program to sum all the items in a dictionary.

Q.5] Write a NumPy program to generate a random number between 0 and 1.

Q.1] Write a Python program to calculate the length of a string.

text = input('Enter a String: ')
print('Length of entered string is : ',len(text))

Q.2] Write a Python function that takes a list and returns a new list with distinct elements from the first list.

def unique_list(l):
num = []
for a in l:
if a not in num:
num.append(a)
return num
text = list(map(int, input('Enter the list numbers separated by space: ').split()))
print('entered elements are: ', text)
print('distinct elements are: ',unique_list(text))

Q.3] Write a Python program to print the even numbers from a given list.

def is_even_num(l):
even_num = []
for n in l:
if n % 2 == 0:
even_num.append(n)
return even_num
text = list(map(int, input('Enter the list numbers separated by space: ').split()))
print('entered elements are: ', text)
print('Even numbers from given list are: ', is_even_num(text))

Q.4] Write a Python program to sum all the items in a dictionary.

def get_Sum_Values(dic):
sum = 0
for item in dic.values():
sum = sum + int(item)
return sum

dic = dict()
n = int(input('enter total range for dictionary: '))
for i in range(n):
data = input(‘Enter the key & Value separated by ":", ‘)
temp = data.split(’:’)
dic[temp[0]] = temp[1]
print()
print("Dictionary are : ", dic)
print("Sum of Dictionary values are: ",get_Sum_Values(dic))

Q.5] Write a NumPy program to generate a random number between 0 and 1.

import numpy as np
rand_num = np.random.normal(0,1)
print(“Random number between 0 and 1:”)
print(rand_num)

1 Like

Hello… @prasadbhss
Amazing answers…Keep going…