PROGRAM FILE-2
PROGRAM FILE-2
PROGRAM FILE-2
Program File
Subject code- 083
SUBMITTED TO. SUBMITTED BY.
Mrs. Yojna Bablani Kazima Fatima
XII-C
INDEX
S.No. Topic
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
Program to display those strings which are starting with 'A' of given
7.
list
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
16. Python program to display all the record from the table of an sql
database
18.
MySQL codes
PROGRAM 1
Program to accept a string and display whether it is
a palindrome.
def isPalindrome(str):
if (str==str[::-1]):
else:
#_main_
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.
OUTPUT:
PROGRAM 4
Program to remove all odd numbers from the given list.
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]
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')
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):
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:
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.
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
for i in cnt_dict:
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 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:
mycon=sqltor.connect(host='localhost',user='root',
password=’1234',database='db')
cur = mycon.cursor()
cur.execute('Select * from employee')
row = cur.fetchone()
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