[Link]
com/
CST 362: PROGRAMMING IN PYTHON
TUTORIAL QUESTIONS
MODULE-II
1. Write a program to remove all vowel characters from a string.
2. Write a program to remove characters at odd index positions from a string.
3. Write a python script for palindrome checking without reversing the string.
4. Write a program to replace all the spaces in the input string with * or if no
m
spaces found, put $ at the start and end of the string.
co
5. Write a program to slice the string into two separate strings; one with all the
characters in the odd indices and one with all characters in even indices.
s.
6. Write a program to remove all occurrence of a substring from a string.
te
7. Write a Program to converting all lowercase letters into uppercase.
no
8. Write a Program to replace all occurrence of a substring with a new substring.
9. Write a Program to reverse the first and second half of a string separately.
la
[Link] a Python program to check the validity of a password given by the user
ra
The Password should satisfy the following criteria:
1. Contains at least one letter between a and z
ke
2. Contains at least one number between 0 and 9
3. Contains at least one letter between A and Z
4. Contains at least one special character from $, #, @
Minimum length of password: 6
[Link] Python script for converting decimal number into Binary number.
[Link] Python script for converting Binary number into decimal number.
[Link] a python function to find the area of a circle.
[Link] a python program to compute nCr using a factorial function.
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] a menu driven program to implement the following
i)check even or odd
ii)check number is positive negative or zero
iii) generate factors of a number
[Link] a Python program to find the value for sin(x) up to n terms using the
series
sin(x)=1-x^3/3!+x^5/5!..... ( sin(x) = ((-1)^n/(2n+1)!)x^(2n+1) )
[Link] a Python program to print the factorial of a number using recursion.
[Link] a Python program to print n’th Fibonacci number using recursion.
m
[Link] to read list of names and sort the list in alphabetical order.( university
co
question)
s.
[Link] to find the sum of all even numbers in a group of n numbers entered
by the user. (university question)
te
[Link] to read a string and remove the given words from the string.
no
[Link] to read list of numbers and find the median
[Link] the mode of list of numbers(A number that appears most often is the
la
mode.)
ra
[Link] to remove all duplicate elements from a list
[Link] a list consisting of integers, floating point numbers and strings.
ke
Separate them into different lists depending on the data(university question)
[Link] a Python program to read list of positive integers and separate the prime
and composite numbers (university question).
[Link] a Python program to read a list of numbers and sort the list in a non-
decreasing order without using any built in functions. Separate function should
be written to sort the list wherein the name of the list is passed as the parameter.
[Link] a program to do basic set operations
[Link] duplicate elements from a list.
[Link] to completely remove duplicate elements without keeping any copy.
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] to count the number of occurrence(frequency) of each letters in a
given string( histogram)
[Link] to display the frequency of each word in a given string.(university
qstn)
[Link] a Python program to create a dictionary of roll numbers and names of
five students. Display the names in the dictionary in alphabetical
order.(university question)
[Link] to read name and phn numbers of ‘n’ customers and print the list in
sorted order of names.
m
[Link] a program that uses a dictionary to convert hexadecimal number into
co
binary.
[Link] the mode of list of numbers. s.
[Link] a Python code to create a function called list_of_frequency that takes a
te
string and prints the letters in non-increasing order of the frequency of their
no
occurrences. Use dictionaries.
la
ra
ke
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
TUTORIAL QUESTIONS
MODULE-II
1. Write a program to remove all vowel characters from a string.
Ans:
vowels="AEIOUaeiou"
m
s=input("Enter the string...")
ns=""
co
for char in s:
if char not in vowels:
s.
te
ns=ns+char
print("new string after removing vowels=",ns)
no
2. Write a program to remove characters at odd index positions from a string.
la
Ans:
ra
ke
s=input("Enter the string..:")
i=0
ns=""
while i<len(s):
if i%2==0:
ns=ns+s[i]
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
i=i+1
print("New string:",ns)
3. Write a python script for palindrome checking without reversing the string.
Ans:
s=input("Enter the string...")
m
if s==s[::-1]:
print("palindrome..")
co
else:
print("not palindrome...") s.
te
Palindrome checking using loop
no
s=input("Enter the string..")
la
beg=0
ra
end=len(s)-1
ke
while beg<end:
if s[beg]!=s[end]:
print("Not palindrome")
break
beg+=1
end-=1
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
else:
print("Palindrome")
4. Write a program to replace all the spaces in the input string with * or if no
spaces found, put $ at the start and end of the string.
Ans:
m
s=input("Enter the string:")
co
s=[Link](" ","*")
if "*" not in s: s.
te
s="$"+s+"$"
no
print(s)
la
else:
ra
print(s)
ke
5. Write a program to slice the string into two separate strings; one with all the
characters in the odd indices and one with all characters in even indices.
Ans:
s=input("enter the string:")
eps=s[0:len(s):2]
print("slice with even position characters:",eps)
ops=s[1:len(s):2]
print("slice with odd position chracters:",ops)
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
6. Write a program to remove all occurrence of a substring from a string.
Ans:
s=input("enter the string..")
ss=input("enter substring to remove..")
ls=len(s) # length of the string
lss=len(ss) # length of the substring
m
ns="" # new string
co
i=0
while i<ls:
s.
te
css=s[i:lss+i] #css is the substring to be compared extracted from main string
no
if css==ss:
la
i=i+lss
ra
else:
ke
ns=ns+s[i]
i=i+1
print("new string",ns)
7. Write a Program to converting all lowercase letters into uppercase.
Ans:
import string
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
s=input('Enter the string...')
ns=""
for c in s:
if c in string.ascii_lowercase:
c=chr(ord(c)-32)
ns=ns+c
m
print("new string=",ns)
co
8. Write a Program to replace all occurrence of a substring with a new substring.
Ans:
s.
te
s=input("enter string..")
no
ss=input("enter substring to remove..")
la
nss=input("enter the substring to replace....")
ra
ls=len(s)
ke
lss=len(ss)
ns=""
i=0
while i<ls:
css=s[i:lss+i]
if css==ss:
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
ns=ns+nss
i=i+lss
else:
ns=ns+s[i]
i=i+1
print("new string",ns)
m
9. Write a Program to reverse the first and second half of a string separately.
co
Ans:
s=input("Enter the string..:")
s.
te
l=len(s)
no
fs=s[0:l//2]
la
ss=s[l//2:]
ra
fs=fs[::-1]
ke
ss=ss[::-1]
s=fs+ss
print("New string after reversal:::",s)
[Link] a Python program to check the validity of a password given by the user
The Password should satisfy the following criteria:
1. Contains at least one letter between a and z
2. Contains at least one number between 0 and 9
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
3. Contains at least one letter between A and Z
4. Contains at least one special character from $, #, @
Minimum length of password: 6
Ans:
l, u, p, d = 0, 0, 0, 0
s = input(“Create a password )”
if (len(s) >= 6):
m
for i in s:
co
# counting lowercase alphabets
if ([Link]()):
s.
te
no
l+=1
# counting uppercase alphabets
la
ra
if ([Link]()):
ke
u+=1
# counting digits
if ([Link]()):
d+=1
# counting the mentioned special characters
if(i=='@'or i=='$' or i=='_'):
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
p+=1
if (l>=1 and u>=1 and p>=1 and d>=1 and l+p+u+d==len(s)):
print("Valid Password")
else:
print("Invalid Password")
[Link] Python script for converting decimal number into Binary number.
m
Ans:
co
decno=int(input("Enter the decimal number...."))
if decno==0: s.
print("The binary equivalent is....0000")
te
else:
no
binaryno=""
while decno!=0:
la
b=decno%2
ra
binaryno=str(b)+binaryno
ke
decno=decno//2
print("The binary equivalent is....",binaryno)
[Link] Python script for converting Binary number into decimal number.
Ans:
bitstring=input("Enter a binary number...")
decno=0
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
expnt=len(bitstring)-1
for bit in bitstring:
decno=decno+int(bit)* 2 ** expnt
expnt=expnt-1
print ("The decimal number is=", decno)
[Link] a python function to find the area of a circle.
m
Ans:
co
def circlearea (radius):
area=3.14*(radius**2)
s.
te
return area
no
#function call
la
r=int(input("Enter radius.."))
ra
area=circlearea(r)
ke
print("Area of the circle=",area)
[Link] a python program to compute nCr using a factorial function.
Ans:
def fact(n):
f=1
for i in range(1,n+1):
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
f=f*i
return f
print("Program to compute nCr...")
n=int(input("Enter n.."))
r=int(input("Enter r..."))
ncr=fact(n)/(fact(n-r)*fact(r))
m
print("nCr...",ncr)
co
[Link] a menu driven program to implement the following
i)check even or odd s.
ii)check number is positive negative or zero
te
iii) generate factors of a number
no
Ans:
la
def evenodd(n):
ra
if n%2==0:
ke
print("even")
else:
print("odd")
def postvnegtv(n):
if n>0:
print("+ve")
10
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
elif n<0:
print("-ve")
else:
print("zero")
def factors(n):
print ("factors")
m
for i in range(1,n+1):
co
if n%i==0:
print (i,end=' ')
s.
te
while True:
no
print("\n....Menu...\[Link] or odd\[Link] or negtv \[Link]\n4..exit\n")
la
ch=int(input("Enter your choice---"))
ra
if ch==4:
ke
break
n=int(input("Enter a number.."))
if ch==1:
evenodd(n)
if ch==2:
postvnegtv(n)
11
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
if ch==3:
factors(n)
[Link] a Python program to find the value for sin(x) up to n terms using the
series
sin(x)=1-x^3/3!+x^5/5!..... ( sin(x) = ((-1)^n/(2n+1)!)x^(2n+1) )
Ans:
import math
m
def sinseries(x,n):
co
sine = 0
for i in range(n):
s.
te
no
sign = (-1)**i
x=x*([Link]/180)
la
ra
sine = sine + ((x**(2.0*i+1))/[Link](2*i+1))*sign
ke
return sine
x=int(input("Enter the value of x in degrees:"))
n=int(input("Enter the number of terms:"))
print(round(sinseries(x,n),2))
output:
Enter the value of x in degrees:30
12
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
Enter the number of terms:10
0.5
[Link] a Python program to print the factorial of a number using recursion.
Ans:
def fact(n):
if n==0:
return 1
m
else:
co
return n*fact(n-1)
n=int(input('Enter n..:'))
s.
te
x=fact(n)
no
print("factorial of ..",n ," ..is.. ",x)
la
[Link] a Python program to print n‟th Fibonacci number using recursion.
ra
ke
Ans:
def fib(n):
if n <= 1:
return n
else:
return fib(n-1) + fib(n-2)
13
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
n=int(input('Enter n…'))
x=fib(n)
print (n,"th Fibonacci number is...",x)
[Link] to read list of names and sort the list in alphabetical order.( university
question)
Ans:
n=int(input("Enter the number of names...."))
m
names=[]
co
print("Enter {} names".format(n))
for i in range(n): s.
nam=input()
te
[Link](nam)
no
[Link]()
print("names in alphabetical order")
la
for nam in names:
ra
print(nam)
ke
[Link] to find the sum of all even numbers in a group of n numbers entered
by the user. (university question)
Ans:
n=int(input("Enter the number of elements..."))
print("Enter the {} elements".format(n))
l=[] # creating an empty list
for i in range(n):
14
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
x=int(input())
[Link](x)
sum=0
for i in range(n):
if l[i]%2==0:
sum=sum+l[i]
print("Sum of all even numbers",sum)
[Link] to read a string and remove the given words from the string.
m
Ans:
co
s=input("Enter the string....")
s.
wr=input("Enter the word to remove....")
te
wrds=[Link](" ")
no
ns=""
for w in wrds:
la
if w!=wr:
ra
ns=ns+" "+w
print("new string...",ns)
ke
[Link] to read list of numbers and find the median
Ans:
#We can find median by sorting the list and then take the middle element. If the
list contains even number of elements take the average of the two middle
elements.
n=int(input("Enter how many numbers...."))
print("Enter {} numbers....".format(n))
15
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
lst=[]
for i in range(n):
x=int(input())
[Link](x)
[Link]()
print(lst)
mid=n//2
if n%2==1:
print("Median",lst[mid])
m
else:
co
print("Median",(lst[mid]+lst[mid-1])/2)
s.
[Link] the mode of list of numbers(A number that appears most often is the
te
mode.)
no
Ans:
la
l=[]
n=int(input("Enter n.."))
ra
print("Enter the numbers..")
ke
for i in range(n):
x=int(input())
[Link](x)
c=[]
e=[]
for x in l:
if x not in e:
[Link]([Link](x))
16
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link](x)
mc=max(c)
ne=len(c)
i=0
print("mode..")
while i<ne:
if c[i]==mc:
print(e[i])
i+=1
m
co
[Link] to remove all duplicate elements from a list
Ans: s.
te
lst=[]
n=int(input("enter how many numbers.."))
no
print("Enter elements...")
la
for i in range(n):
ra
x=int(input())
[Link](x)
ke
nlst=[]
for x in lst:
if x not in nlst:
[Link](x)
print("new list after removing duplicates")
print(nlst)
17
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] a list consisting of integers, floating point numbers and strings. Write
a program to separate them into different lists depending on the data(university
question)
Ans:
il=[]
fl=[]
sl=[]
l=[12,23.5,'klf',34,4343,34.566,3+3j,'ddldl',35]
for i in l:
m
if type(i)==int:
co
[Link](i)
if type(i)==str: s.
[Link](i)
te
if type(i)==float:
no
[Link](i)
la
print("Integer list")
print(il)
ra
print("Float list")
ke
print(fl)
print("String List")
print(sl)
[Link] a Python program to read list of positive integers and separate the prime
and composite numbers (university question).
Ans:
def prime(n):
flag=1
18
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
for i in range(2,n//2+1):
if n%i==0:
flag=0
break
return flag
pl=[]
cl=[]
l=[]
n=int(input("Enter n.."))
m
print("Enter the numbers..")
co
for i in range(n):
x=int(input()) s.
[Link](x)
te
for i in l:
no
if prime(i):
la
[Link](i)
else:
ra
[Link](i)
ke
print("prime list")
print(pl)
print("composite list")
print(cl)
[Link] a Python program to read a list of numbers and sort the list in a non-
decreasing order without using any built in functions. Separate function should
be written to sort the list wherein the name of the list is passed as the parameter.
Ans:
19
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
def sortlist(lst,n):
for i in range(n-1):
for j in range(i+1,n):
if lst[i]>lst[j]:
lst[i],lst[j]=lst[j],lst[i]
n=int(input("Enter how many numbers...."))
print("Enter {} numbers....".format(n))
lst=[]
for i in range(n):
m
x=int(input())
co
[Link](x)
sortlist(lst,n) s.
print("sorted List is")
te
print(lst)
no
[Link] a program to do basic set operations.
la
Ans:
ra
na=int(input('Enter number of elements of the set A ..'))
A=set()
ke
print("Enter the elements of set A...")
for i in range(na):
x=int(input())
[Link](x)
nb=int(input('Enter number of elements of the set B ..'))
B=set()
print("Enter the elements of set B...")
for i in range(nb):
20
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
x=int(input())
[Link](x)
print("set operations..")
print("union")
print(A|B)
print("Intersection")
print(A&B)
print("Difference A-B and B-A")
print(A-B)
m
print(B-A)
co
print("symmetric Difference")
print(A^B) s.
te
[Link] to Remove duplicate elements from a list .
no
Ans:
la
lst=[]
ra
n=int(input("enter how many numbers.."))
print("Enter elements...")
ke
for i in range(n):
x=int(input())
[Link](x)
nlst=list(set(lst))
print("new list after removing duplicates")
print(nlst)
[Link] to completely remove duplicate elements without keeping any copy.
21
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
Ans:
n=int(input('Enter number of elements of the list ..'))
lst=[]
print("Enter the elements...")
for i in range(n):
x=int(input())
[Link](x)
nlst=list(set(lst)) # new list with only one copy
m
nlwd=[] # new list without duplicates
for i in nlst:
co
if [Link](i)==1: #non duplicate element
[Link](i) s.
te
print("New list after removing duplicates completely...")
print(nlwd)
no
[Link] to count the number of occurrence(frequency) of each letters in a
la
given string( histogram)
ra
Ans:
ke
S=input("Enter the string…..")
d=dict()
for c in S:
d[c]=[Link](c,0)+1
print("letter count")
print(d)
[Link] to display the frequency of each word in a given string.(university
qstn)
Ans:
22
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
S=input("Enter the string…..")
S=[Link](" ") #splitting it into words
d=dict()
for w in S:
d[w]=[Link](w,0)+1
print("word count")
print(d)
[Link] a Python program to create a dictionary of roll numbers and names of
m
five students. Display the names in the dictionary in alphabetical
co
order.(university question)
Ans: s.
d={}
te
for i in range(5):
no
rn=int(input("Enter roll number.."))
name=input("Enter name …")
la
d[rn]=name
ra
ke
l=list([Link]())
[Link](key=lambda v:v[1])
print("name and roll number in sorted order of name")
for i in l:
print(i[1],":",i[0])
[Link] to read name and phn numbers of „n‟ customers and print the list in
sorted order of names.
23
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
Ans:
n=int(input("Enter number of customers.."))
d={}
for i in range(n):
nm=input("Enter name..")
phn=int(input("Enter phn number…"))
d[nm]=phn
l=list([Link]()) # creating a list
m
[Link]() # sorting the list in the order of name..
co
#[Link](key=lambda v:v[1]) will sort in the order of phone number
s.
print("name and phn number in sorted order")
for i in l:
te
print(i[0],":",i[1])
no
la
[Link] a program that uses a dictionary to convert hexadecimal number into
binary.
ra
Ans:
ke
hextobin={'0':'0000','1':'0001','2':'0010','3':'0011','4':'0100','5':'0101','6':'0110','7':'
0111','8':'1000','9':'1001','A':'1010','B':'1011','C':'1100','D':'1101','E':'1110','F':'11
11'}
n=input('Enter the hexadecimal number….')
bn=''
n=[Link]()
for d in n:
h=[Link](d)
24
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
if h==None:
print('Invalid Number')
break
bn=bn+hextobin[d]
else:
print('Binary equivalent is..',bn)
[Link] the mode of list of numbers
Ans:
m
n=int(input("Enter..how many numebrs.."))
co
print("Enter {} numbers".format(n))
numbers=[] s.
for i in range(n):
te
x=int(input())
no
[Link](x)
la
ncount={}
for x in numbers:
ra
ncount[x]=[Link](x,0)+1
ke
maxcount=max([Link]())
print('Mode..')
for k in ncount:
if ncount[k]==maxcount:
print(k)
25
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] a Python code to create a function called list_of_frequency that takes a
string and prints the letters in non-increasing order of the frequency of their
occurrences. Use dictionaries.
Ans:
def list_of_frequency(s):
d=dict()
for c in s:
d[c]=[Link](c,0)+1
print("letter count in the decresing order")
m
l=list([Link]())
co
[Link](key=lambda x:x[1],reverse=True)
print(l) s.
te
s=input("Enter the string…..")
no
list_of_frequency(s)
la
ra
ke
26
For More Study Materials : [Link]