Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PROGRAM FILE-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

SESSION 2023-2024

SALWAN PUBLIC SCHOOL

Program File
Subject code- 083
SUBMITTED TO. SUBMITTED BY.
Mrs. Yojna Bablani Kazima Fatima
XII-C
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

Program to display those strings which are starting with 'A' of given
7.
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. Program 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.

18.
MySQL codes
PROGRAM 1
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]
else:
break
print(“Second highest number:”, secmx)

OUTPUT:
PROGRAM 6
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 Frequency')

for i in range (len(L1)):


print(L2[i],'\t' ,L1[i])

OUTPUT:
PROGRAM 7
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
else:
count=count
print(i)
print("Appearing",count,"times")

OUTPUT:
PROGRAM 8
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
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', 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: "))


cnt_dict = dict()

for i in range(n):
x = input("Enter the country name: ")
y = input("Enter the capital: ")
z = input("Enter the currency: ")

cnt_dict[x] = y, z

print("This is your given dictionary: ")


print("Country \t Capital \t Currency")

for i in cnt_dict:

print(i, ‘\t’, cnt_dict[i][0], ‘\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(r‘C:\New folder\myfile.txt’,‘r’)
file2=open(r‘C:\New folder\myfile2.txt’,‘w’)
l=file1.readlines()
for i in l:
if ‘a’ in i:
i=i.replace('a', ‘’)
file2.write(i)
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)

random_number = generate_random_number()
print("Random Number:", random_number)

Output:
PROGRAM 14
Program to implement a stack using a list data structure.

def is_empty(stack):
return len(stack) == 0

def push(stack, item):


stack.append(item)

def pop(stack):
if not is_empty(stack):
return stack.pop()
else:
print("Stack is empty")

def peek(stack):
if not is_empty(stack):
return stack[-1]
else:
print("Stack is empty")

def size(stack):
return len(stack)
my_stack = []

push(my_stack, 1)
push(my_stack, 2)
push(my_stack, 3)

print("Stack:", my_stack)
print("Stack Size:", size(my_stack))
print("Top Element:", peek(my_stack))
print("Popped element:", pop(my_stack))
print("Stack after pop:", my_stack)

Output:
PROGRAM 15
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:
while j.isalpha()=True:
j=j.lower()

if ( j =="a" or j=="e" or j=="i" or j =="o" or j ==" u"):


vowels+=1
else:
consonants+=1

print("Lowercase count: ", lowercase)


print("Uppercase count: ",uppercase)
print("Vowels count: ", vowels)
print("Consonants count: ", consonants)
OUTPUT:
PROGRAM 16
Python database connectivity script that displays all the
record from a table say employee.

import mysql.connector as sqltor

mycon=sqltor.connect(host='localhost',user='root',
password=’1234',database='db')
cur = mycon.cursor()
cur.execute('Select * from employee')
row = cur.fetchone()

while row is not None:


print(row)
mycon.close()

OUTPUT:
PROGRAM 17
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,
password='1234',database='db')
cur = mycon.cursor()
sql1="Delete from employee where name like
'%s%'"
cur.execute(sql1)
mycon.commit()
print("Rows affect:",cur.rowcount)
mycon.close()

OUTPUT:
MYSQL CODES

You might also like