SALWAN PUBLIC
SCHOOL
RAJENDRA NAGAR
COMPUTER SCIENCE
PROGRAM FILE
Submitted to: Submitted by:
Mrs. Yojna Bablani Daksh Singh
Roll no: 08
INDEX
S.No. Topic
1. Program to accept a string and display whether it is a palindrome
2. Program that counts number of alphabets, digits, upper and lower
case letters in the string
3. Program to accept a string and return a string with first letter of
each word in capital letter
4. Program to remove all odd numbers from the given list
5. Program to display second largest element of a given list
6. Program to display frequencies of all the element of a given list
7. Program to display those strings which are starting with 'A' of given list
8. Program to accept values from user and create a tuple
9. Program to input total no. of sections and stream name in n th class
and display all info in output
10. Program to input names of 'n' countries and their capital and
currency, store it in a dictionary and display it in tabular form. Also
search and display for a particular country
11. Program to read a text file line by line and display each word separated
by a #
12. Program to remove all lines that contain character 'a' in a file and
write it to another file
13. Program to create a random number generator that generates
random numbers between 1&6
14. Program to implement a stack using a list data structure
15. Prog. to read a text file and display the no of
vowels/consonant/uppercase/lowercase characters in
it.
16. Python program to display all the record from the table of an
sql database
17. Program to delete records from employee table in previous program
of database db' that have names with 's' in them.
Program 1
#Write a program to accept a string and display whether it
is a palindrome.
def isPalindrome(str):
if (str==str[::-1]):
return "The string is a palindrome."
else:
return "The string is not a palindrome."
#_main_
str=input ("Enter string: ")
print(isPalindrome(str))
OUTPUT:
Program 2
#Program that counts number of alphabets, digits,
upper and lower case letters in the string
def counterr(str):
upper, lower, number= 0, 0,
0 for i in range(len(str)):
if str[i].isupper():
upper += 1
elif str[i].islower():
lower += 1
else :
str[i].isdigit()
number += 1
print('Upper case letters:', upper)
print('Lower case letters:', lower)
print('Number:', number)
#main
str = "HelloWOrld007"
counterr(str)
#OUTPUT:
Program 3
#Program to accept a string and return a string with
first letter of each word in capital letter
n=input('a string: ')
m=n.title()
#main
print(m)
#OUTPUT:
Program 4
#Program to remove all odd numbers from the given list
lst = [911, 2, 3, 42, 5, 66]
print("Original List : ",lst)
for i in lst:
if(i%2==1):
lst.remove(i)
print(lst)
#OUTPUT:
Program 5
#Program to display second largest element of a given list
list1 = [11460, 20235146, 999994, 4235765, 795854699]
mx = max(list1[0], list1[1])
secmx = min(list1[0], list1[1])
n = len(list1)
for i in range(2,n):
if list1[i] > mx:
secmx = mx
mx = list1[i]
elif list1[i] > secmx and mx != list1[i]:
secmx = list1[i]
elif mx == secmx and secmx != list1[i]:
secmx = list1[i]
#OUTPUT:
Program 6
#Write a program to display frequencies of all the
elements of a list
L=[3,21,5,6,3,8,21,6,21,8,0]
L1=[]
L2=[]
for i in L:
if i not in L2:
x=L.count(i)
L1.append(x)
L2.append(i)
print('Element','\t\t\t','Frequency')
for i in range (len(L1)):
print(L2[i],'\t\t\t',L1[i])
OUTPUT:
Program 7
#Write a program to display those strings which are
starting with ‘A’ of given list.
L=
['AISHIM','LEENA','AKHTAR','HIBA','NISHANT','AMAR']
count=0
for i in L:
if i[0] in ('aA'):
count+=1
print(i)
print("Appearing",count,"times")
OUTPUT:
Program 8
#Write a program to accept values from user and create
a tuple
t=tuple()
n=int(input("Enter any number: "))
for i in range(n):
a=input("Enter number: ")
t=t+(a,)
print("The output is: ")
print(t)
OUTPUT:
Program 9
#Write a program to input total no. of sections and
stream name in 11th class and display all info in output.
classxi=dict()
n=int(input("Enter total number of sections in XI class: "))
i=1
while i<=n:
a=input("Enter section: ")
b=input ("Enter stream name: ")
classxi[a]=b
i=i+1
print ("Class",'\t',"Section",'\t',"Stream name")
for k,v in classxi.items():
print ("XI\t",k,"\t\t",v)
OUTPUT:
Program 10
#Program to input names of ‘n’ countries along with
their capitals and currencies, store them in a dictionary
and
display them in a tabular form. also search for and display a
particular country.
n = int(input("Enter the number of countries to be be entered: "))
print()
cnt_dict = dict()
for i in range(n):
x = input("Enter the country name: ")
y = input("Enter the capital: ")
z = input("Enter the currency: ")
print()
cnt_dict[x] = y, z
print("This is your given dictionary: ")
print("Country", "\t\t", "Capital", "\t\t", "Currency")
for i in cnt_dict:
print(i, "\t\t", cnt_dict[i][0], "\t\t", cnt_dict[i][1])
rc = input("Enter the country whose details you'd like view: ")
if rc in cnt_dict:
print(rc, ":", cnt_dict[rc])
else:
print("No such country in the given dictionary.")
OUTPUT:
Program 11
#Program to read a text file line by line and display
each word separated by a #
fh=open(r"myfile.txt","r")
item=[]
a=""
while True:
a=fh.readline()
print(a)
words=a.split()
for j in words:
item.append(j)
if a =="":
break
print("#".join(item))
OUTPUT:
Program 12
#Program to remove all lines that contain character 'a' in
a file and write it to another file
file1=open(C:\New folder\myfile.txt)
file2=open(C:\New folder\myfile2.txt)
for line in file1:
for ‘a’ in line:
line=line.replace(‘a’,’’)
else:
file2.write(line)
file1.close()
file2.close()
OUTPUT:
Program 13
#Program to create a random number generator
that generates random numbers between 1&6
import random
def generate_random_number():
return random.randint(1, 6)
# Generate and print a random number
random_number = generate_random_number()
print("Random Number:", random_number)
Output:
Program 14
#Program to implement a stack using a list data structure
class Stack:
def init (self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
return "Stack is empty"
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
return "Stack is empty"
def size(self):
return len(self.items)
# Example usage:
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("Stack:", stack.items)
print("Stack Size:", stack.size())
print("Top Element:",
stack.peek())
popped_item = stack.pop()
print("Popped Element:", popped_item)
print("Stack after pop:", stack.items)
Output:
Program 15
#Write a program to read a text file and display the
numbers of vowels/ consonants/uppercase/lowercase
characters in it.
file1=open(r'C:\Users\welcome\Desktop\myfile.txt','r')
vowels=0
consonants=0
uppercase=0
lowercase=0
str1=file1.read()
for i in str1:
if (i>="a" and i <="z"):
lowercase+=1
elif( i >="A" and i <="Z") :
uppercase+=1
for j in str1:
j=j.lower()
if ( j =="a" or j=="e" or j=="i" or j =="o" or j ==" u"):
vowels+=1
else
: if j.isalpha():
consonants+=1
print("Lowercase count: ", lowercase)
print("Uppercase count: ",uppercase)
print("Vowels count: ", vowels)
print("Consonants count: ", consonants)
#OUTPUT:
Program 16
#write a python database connectivity script that displays all
the record from the table
import mysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='1234'
,database='db')
cur = mycon.cursor()
cur.execute('Select * from
employee') row = cur.fetchone()
while row is not None:
print(row)
row=cur.fetchone()
#OUTPUT:
Program 17
#write a python database connectivity script that deletes
records from employee table in previous program of
database db that have “s” in them
import mysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='1234'
,database='db')
cur = mycon.cursor()
sql1="Delete from employee where name like '%s%'"
cur.execute(sql1)
mycon.commit()
print("Rows afffect:",cur.rowcount)
mycon.close()
#OUTPUT: