Practical File Class XI - Computer Science / IP
Table of contents
String Programs
Q1 : Write a Program to input a string and a substring and then check for the occurrence of a substring in it.
Code :
sentence = input("Enter String ")
substr = input("Enter Substring ")
c = 0
pos = sentence.find(substr)
while pos != -1:
c=c+1
pos = sentence.find(substr, pos+1)
print("Occurrence ", c)
Q2. In a school the admission number of student is a 10 digit string. First 4 digits represent the year of admission, the next 2 digits represent the class in which the student took admission, and the last four digits are the serial number. Write a program to do the following:
a. Input the admission number of student.
b. Check whether the admission number is valid or not. An admission is valid if it is 10 character long and contain digits only.
c. If the admission number is invalid then display “invalid”
d. If the admission number is valid, display its components separately.
Code:
admn = input("Enter Admission Number ")
if len(admn)==10 and admn.isdigit() == True:
year = admn[:4]
cls = admn[4:6]
serial = admn[6:]
print("Year ", year)
print("Class ", cls)
print("Serial ", serial)
else:
print("Invalid Admission Number ")
Q3. Write an equivalent code of islower() for a string. Accept a string. It should print the if the string has only lower case letters.
Code:
string = input("Enter a string ")
for i in string:
if not(ord(i)>=97 and ord(i)<=122):
print("Not in Lower Case ")
break
else:
print("Lower Case String ")
Q4. Write the Equivalent code of upper() for a string. Accept a string. It should convert all lower case letter to the corresponding upper case.
Code:
s1 = input("Enter a String ")
s2 = ""
for i in s1:
if ord(i)>=97 and ord(i) <=122:
s2 = s2 + chr(ord(i)-32)
else:
s2 = s2 + i
print("Upper Case ", s2)
Q5. Write a program to accept an identifier and check for its validity. And identifier is valid if it is starting with a letter or underscore(_) followed by underscore(_) and any alpha numeric character.
Code:
s1 = input("Enter an identifier ")
if s1[0].isalpha() == True or s1[0] == '_':
for i in s1[1: ]:
if i.isalnum()!= True and i != '_':
print("Invalid ")
break
else:
print("Valid identifier ")
else:
print("Invalid ")
Q6. Write a program that accepts a string. Encrypt the string on the basis of the given criteria and print it.
· Every letter/digit should be replaced by the next character.
· 9 should be replaced by 0
· Z/z should be replaced by A/a
· All spaces should be changed to *
· All other special characters should be remain unchanged.
Code :
s = input("Enter string ")
s2 = ""
for i in range(len(s)):
if s[i].isalnum():
if s[i] == '9':
s2 = s2 + '0'
elif s[i] == 'z':
s2 = s2 + 'a'
elif s[i] =='Z' :
s2 = s2 + 'A'
else:
s2 = s2 + s[i+1]
elif s[i] == ' ':
s2 = s2 + '*'
else:
s2 = s2 + s[i]
print(s2)
Q7. Write a program to input the full name and then display it in abbreviated form
Code:
fullname = input("Enter Full Name ")
L1 = fullname.split()
abname = fullname[0]+ "." + L1[1]
print(abname)
Q8. Consider a string — “Computer Science Students”
Write a python statement to display the first charactes in the upper case letter.
Code
string = "Computer Science Students"
string2 = string[0].upper() + string[1:].lower()
print(string2)
Tuples
Q1 . Write a program to input two tuples and create a third that contains all elements of first followed by elements of second
Code:
t1 = ()
print("Enter tuple 1 ")
for i in range(5):
elem = int(input("Enter data "))
t1 = t1 + (elem,)
print("Enter tuple 2 ")
t2 = ()
for i in range(5):
elem = int(input("Enter data "))
t2 = t2 + (elem,)
t3 = t1 + t2
print(t1)
print(t2)
print(t3)
Q2. Write a program to create the following tuple using a for loop
Tuple containing the squares of the integers 1 to 50
Code:
t1 = ()
for i in range(1, 51):
elem = i ** 2
t1 = t1 + (elem,)
print (t1)
Q3. Write python statement to create
a. A Tuple of integers each in the range 10 to 50 and name it as R.
b. A tuple of 10 floating point numbers in the range 10 to 50 and name it as Fro.
c. A Tuple y, of 10 values of the expression x2+2x-3 for integer values of x from -5 to 5
Code :
Code A :
#Code A :
R = ()
for i in range(10):
elem = int(input("Enter number "))
if (elem >=10 and elem <=50):
R = R + (elem,)
print("A ", R)
Code B:
Code C:
Output:
List
Q1. Write a program to input a list from the user and add those values in the list which are ending with 0 and display their sum.
Code:
L1 = [100,34,250,150,56,78,300]
sum = 0
for i in L1:
if i % 10 ==0:
sum = sum + i
print("Sum = ",sum)
Q2. Write a program to input a list of cities in India. Display the name of those cities in which there are more than 5 characters.
Code
cities = ["Delhi", "Mumbai", "Kolkata","Chennai","Pune","Ahmedabad"]
for i in cities :
if len(i) > 5 :
print(i)
Output
Q3. Write a program to input a list of numbers. The program should add and display sum of ten times of even values present in the list.
Code
L1 = []
n = int(input("Enter n " ))
for i in range(n):
data = int(input("Enter number "))
L1.append(data )
sum = 0
for i in L1:
if i % 2 == 0:
sum = sum + (i *10)
print("Total " , sum)
Q4. Write a program to input a list of numbers and add those values in the list which are odd. Also display the elements which have been added and their sum.
Code
L1 = []
n = int(input("Enter n " ))
for i in range(n):
data = int(input("Enter number "))
L1.append(data )
sum = 0
for i in L1:
if i % 2 != 0:
sum = sum + i
print("Total " , sum)
Q5. Write a program to input a list of name .
Input. “Name to be searched from this list and display the index where it is found.
Code:
L1 = []
n = int(input("Enter n "))
for i in range(n):
name = input("Enter name ")
L1.append(name)
name = input("Enter name to be Searched ")
for i in range(len(L1)):
if L1[i] == name:
print(name,"at",i)
break
Q6. Write a program to input two different lists and create a list that has all common elements of the two lists.
Code :
L1 = [1,2,3,4,5]
L2 = [2,6,3,4 ]
L3 = []
for i in L1:
if i in L2:
L3.append(i)
print(L3)
Q7. Write a program to input a list and their double the even elements of the list and triple the odd elements.
Code:
L1 = []
n = int(input("Enter n " ))
for i in range(n):
data = int(input("Enter number "))
L1.append(data )
for i in range(len(L1)):
if L1[i] % 2 ==0 :
L1[i] = L1[i]**2
else:
L1[i] = L1[i]**3
print(L1)
Q8. Write a program to input a list containing name of the cities and then display the name of all the cities that start with alphabet ‘A’.
Code:
cities = ["Ahemdabad", "Jaipur", "Bengaluru", "Amritsar"]
for i in cities:
if i[0] =="A" or i[0] =="a":
print(i)
Output:
Q9. Write a program to enter a list and then pus all the zeros in the list to the end of the list.
Code
L1 = []
n = int(input("Enter n "))
for i in range(n):
data = int(input("Enter number "))
L1.append(data)
count = 0
while L1.count(0)!=0:
count= count+1
L1.remove(0)
for i in range(count):
L1.append(0)
print(L1)
Q10 . Write a program to input n number of strings. Count the number of string which have more than 5 characters.
Code
L1 = []
n = int(input("Enter n "))
for i in range(n):
string = input("Enter string ")
L1.append(string)
count =0
for i in L1:
if len(i)>5:
count = count + 1
print(i)
print("Count = " , count)
Q11. Write a program to accept a string. Create a new string with all consonants deleted from the string.
Code:
s1 = input("Enter string ")
s2 = ""
for i in s1:
if i in "aeiouAEIOU":
s2 = s2+ i
print("String after consonant Deleted " , s2)
Q12. Write a python program to remove the nth index character from the string.
Code:
s1 = input("Enter a string ")
n = int(input("Enter Index number "))
s1 = s1 [ : n] + s1[n+1 : ]
print("String after deletion ", s1)
Q13. Write a program to input a string and a character and count the number of times a character appear in a string.
Code:
s1 = input("Enter a string ")
character = input("Enter a Character ")
count =0
for i in s1:
if i == character:
count = count + 1
print("Count = " , count)