(PP-2)
2A.Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.
def vowel(x):
if x in ["a","e","i","o","u"]:
return True
else:
return False
a=int(input("enter an alphabet:"))
print(vowel(a))
2B.Define a function that computes the length of a given list or string.
str="palace world"
str1=list(str)
print(str1)
count=0
for i in str:
count+=1
print("length of string is :",count)
or
def findlength(string):
count=0
for i in string:
count+=1
return count
string="hello world"
print(findlength(string))
2C.Define a procedure histogram() that takes a list of integers and prints a histogram
to the screen. For example, histogram([4, 9, 7]) should print the following:
****
*********
*******
def histogram(list):
for a in list:
print('*'*a)
histogram([4,9,7])
2D.Write a python program to copy a list into another variable.
list=[22,23,24,25,26,29]
list2=list.copy()
print(list)
print(list2)
2E.Write a python program create
a list a and print out all the elements from the list that are less than five.
A=[1,2,3,4,5,6,7,8]
for i in A:
if i<5:
print(i)
2F.Write a python program to print specified list after removing 0,2,4, & 5th element.
L1=[1,2,3,4,5,6,7]
del L1[0]
del L1[1]
del L1[2]
del L1[2]
print(L1)
2G.Write a python program that takes two list and returns true if they have atleast one common element.
L1=[1,2,3,4,5]
L2=[7,8]
flag=False
for i in L1:
if i in L2:
flag=True
break
if flag:
print("True")
else:
print("False")
or
def common(list1,list2):
for x in list1:
for y in list2:
if x==y:
result=True
return result
list1=[1,2,3,4,5]
list2=[5,6,7,8,9]
list3=[21,22,23,24,25]
list4=[12,13,14,15]
print(common(list1,list2))
print(common(list3,list4))
Comments
Post a Comment