0% found this document useful (0 votes)
74 views

CS Practical File 3

The document contains 17 questions related to Python programming. The questions cover topics like calculators, leap year determination, pattern printing, maximum number determination, string manipulation, sorting algorithms, and stack operations. Code snippets are provided for each question along with sample input/output.

Uploaded by

om
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

CS Practical File 3

The document contains 17 questions related to Python programming. The questions cover topics like calculators, leap year determination, pattern printing, maximum number determination, string manipulation, sorting algorithms, and stack operations. Code snippets are provided for each question along with sample input/output.

Uploaded by

om
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Q-1) program of calcultor in python

def sum(a,b):
sum=a+b
print (sum)
def sub(a,b):
sub=a-b
print (sub)
def mul(a,b):
mul=a*b
print(mul)
def div(a,b):
div=a/b
print (div)
a=int(input("enter the value of a"))
b=int(input("enter the value of b"))
while True:
choice=input(" + for add\n - for sub\n * for mul\n / for div \n enter your
choice\n")
if choice=='+':
print ("sum of two number\n")
sum (a,b)
elif choice=='-':
print ("sub of two number\n")
sub (a,b)
elif choice=='*':
print ("mul of two number\n")
mul(a,b)
elif choice=='/':
print ("div of two number\n")
div(a,b)
else:
print("invalid option")

break

output:-
enter the value of a 10
enter the value of b 5
+ for add
- for sub
* for mul
/ for div
enter your choice +
sum of two numbers 15

enter the value of a 10


enter the value of b 5
+ for add
- for sub
* for mul
/ for div
enter your choice -
sub of two numbers 5

Q-2) program to find the entered year is leap year or not.

year=int(input("enter a year"))
if(year%100)==0:
if(year%400)==0:
print("century and leap year")
else:
print("century but non leap year")

else:
if(year%4)==0:
print("not a century but leap year")
else:
print("not a century and non leap year")

output:-
enter a year 2000
century and leap year
enter a year 2100
century but non leap year
enter a year 2024
not a century but leap year
enter a year 2023
not a century and non leap year

Q-3) program to print the table of a no using for loop

num=int(input("enter a no."))
for i in range(1,11,1):
print(num*i)

output:-
enter a no. 5
5
10
15
20
25
30
35
40
45
50

Q-4) program to print the maximum number from three numbers entered by the user.

def maximum(num1,num2,num3):
if num1>num2:
if num1>num3:
return num1
else:
return num3
else:
if num2>num3:
return num2
else:
return num3

num1=int(input("enter num1"))
num2=int(input("enter num2"))
num3=int(input("enter num3"))
max=maximum(num1,num2,num3)
print("max=",max)

output:-
enter num1 5
enter num2 9
enter num3 7
max=9

Q-5) write a program in python to print the following pattern.


*
**
***
****
*****

n=int(input("enter a number"))
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print()

Q-6) print to calculate the area and perimeter of a circle in python.

def circle(r):
area=3.14*r*r
circumfrence= 2*3.14*r
return(area,circumfrence)

radius=int(input('enter radius of circle: '))


area,circumference = circle(radius)
print('area of circle is:',area)
print ('circumference of circle is:',circumfrence)

output:-
enter the radius of circle 7
area of circle is: 154
circumference of circle is:44

Q-7) program to print the string in reverse order.

def reverse(original):
stack1=list()
rev=''
for i in original:
stack1.append(i)
for i in range(len(stack1)):
rev=rev+stack1.pop()
print("reversed string=",rev)
original=input("enter a string")
reverse(original)

output:-
enter a string SCHOOL
reversed string = LOOHCS

Q-8) program to print the string in reverse order using recursion.

def reverse_string(s):
if len(s)>0:
print(s[len(s)-1],end=" ")
reverse_string(s[:len(s)-1])
s=input("enter a string")
print("reversed string=")
reverse_string(s)

output:-
enter a string SCHOOL
reversed string= LOOHCS

Q-9) program to print the sum of natural numbers using recursion.

def sum_natural(num):
if num==1:
return 1
else:
return num+sum_natural(num-1)
num=int(input("enter a natural number"))
sum=sum_natural(num)
print("sum of natural numbers=",sum)

output:-
enter a natural number 5
sum of natural numbers=15

Q-10) program to insert,delete and display the elements of stack.

def push(stack1,ele):
stack1.append(ele)
print(ele," inserted successfully")
def pop_ele(stack1):
x=stack1.pop()
print(x,"popped successfully")
def peek(stack1):
index=len(stack1)-1
print("top element=",stack1[index])
def display(stack1):
for i in range(len(stack1)):
print(stack1[i])

stack1=[]
while True:
choice=int(input("1->push\n 2->pop\n 3-peek\n 4->display\n5->exit\n enter your
choice"))
if choice==1:
ele=int(input("enter the element to insert"))
push(stack1,ele)

elif choice==2:
if len(stack1)==0:
print("stack is empty")
else:
pop_ele
(stack1)

elif choice==3:
if len(stack1)==0:
print("stack is empty")
else:
peek(stack1)
elif choice==4:
if len(stack1)==0:
print("stack is empty")
else:
display(stack1)
else:
exit

output:-
1->push
2->pop
3->peek
4->display
5->exit
enter your choice 1
enter the element to insert 5
5 inserted successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 1
enter the element to insert 10
10 inserted successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 1
enter the element to insert 15
15 inserted successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 3
top element=15

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 2
15 popped successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 4
10
5

Q-11) Write a program in python to sort elements using bubble sorting.


def bubble_sort(list1):
n=len(list1)
for i in range(n):
for j in range(0,n-i-1):
if list1[j]>list1[j+1]:
list1[j],list1[j+1]=list1[j+1],list1[j]

numlist=[5,8,3,6,7]
bubble_sort(numlist)
print("the stored list is")
for i in range(len(numlist)):
print(numlist[i],end=" ")

output:-
the sorted list is 3, 5, 6, 7, 8

Q-12) Write a program in python to sort elements using insertion sorting.


def insertion_sort(list1):
n=len(list1)
for i in range(n):
temp=list1[i]
j=i-1
while j>=0 and temp<list1[j]:
list1[j+1]=list1[j]
j=j-1
list1[j+1]=temp

numlist=[8,7,9,4,5]
insertion_sort(numlist)
print("the sorted list is:")
for i in range(len(numlist)):
print(numlist[i],end=" ")

output:-
the sorted list is:
4,5,7,8,9

Q-13) Write a program in python to sort elements using selection sorting.


def selection_sort(list1):
flag=0
n=len(list1)
for i in range(n):
min=i
for j in range(i+1,len(list1)):
if list1[j]<list1[min]:
min=j
flag=1
if flag==1:
list1[min],list1[i]=list1[i],list1[min]

numlist=[8,4,9,3,7]
selection_sort(numlist)
print("the sorted list is:")
for i in range(len(numlist)):
print(numlist[i],end=" ")

output:-
the sorted list is: 3,4,7,8,9

Q-14) write a program in python to count the no of alphabets ,upper case letters ,
lower case letters,digits
,spaces and special symbols in a string entered by the user.
def count_str(str):
alpha,upper_ctr,lower_ctr,number_ctr,space_ctr,special_ctr=0,0,0,0,0,0
length=len(str)
print("length of string=",length)
for i in str:
if(i.isalpha()):
alpha+=1
if(i.isupper()):upper_ctr+=1
if(i.islower()):lower_ctr+=1
elif(i.isdigit()):number_ctr+=1
elif(i.isspace()):space_ctr+=1
else:special_ctr+=1
return alpha,upper_ctr,lower_ctr,number_ctr,space_ctr,special_ctr
str=input("enter a string")
print("original string:",str)
alpha,upper_ctr,lower_ctr,number_ctr,space_ctr,special_ctr=count_str(str)
print("\n total number of alphabets=:",alpha)
print("\n upper case characters=:",upper_ctr)
print("\n lower case characters=:",lower_ctr)
print("\n number characters=:",number_ctr)
print("\n space characters=:",space_ctr)
print("\n special characters=:",special_ctr)

output:-

enter a stringi am sambhav sharma. i am in class xii. i like reading books.


original string: i am sambhav sharma. i am in class xii. i like reading books.
length of string= 61

total number of alphabets=: 46


upper case characters=: 0

lower case characters=: 46

number characters=: 0

space characters=: 12

special characters=: 3

Q-15) write a program in python to count the no of spaces in the string entered by
the user.
def check_space(string):
count=0
for i in range(0,len(string)):
if string[i]==" ":
count+=1
return count

string=input("enter the string")


print("number of spaces ",check_space(string))

output:-
enter the stringhello everyone! i am yatharth singh.
number of spaces 5

Q-16) python program to count lines starting with vowel in testfile.txt


def count_lines():
count=0
fobj=open("testfile.txt",'r')
line=fobj.readlines()
for i in line:
if i[0] in 'AEIOUaeiou':
count+=1
fobj.close()
return count
count=count_lines()
print("number of lines starting with vowel:",count)

Q-17) write a python program to perform the insertion and deletion operation on
stack.
stack=[]
while True:
choice=int(input("1-> to insert data\n 2-> to delete data\n 3-> to exit\n enter
youur choice"))
if choice==1:
ele=input("enter the name of the student")
stack.append(ele)
elif choice==2:
if stack==[]:
print("underflow")
else:
ele=stack.pop()
print(ele,"deleted successfully")
elif choice==3:
break
else:
break
print("now , our stack is",stack)
OUTPUT:-
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentram
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentshyam
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentmohan
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice2
mohan deleted successfully
1-> to insert data
2-> to delete data
3-> to exit

Q-18) write a python program to count upper case alphabets.


string=input("enter a strint")
count=0
for i in string:
if i.isupper():
count+=1
print("number of upper case alphabets=",count)

output:-
enter a strintHello Everyone!
number of upper case alphabets= 2

Q-19) write a python program to count the alphabets in a string entered by a user.
string=input("enter the string")
count=0
for i in string:
if i.isalpha():
count=count+1
print("no of alphabets=",count)

output:-
enter the stringi am sambhav sharma. i am 17 years old.
no of alphabets= 27

Q-20) Write a program to print the fibonacci series using recursion till the given
no of terms.
def fibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
n=int(input("enter the no of terms"))
print("fibonacci series:")
for i in range(n):
print(fibonacci(i),end=" ")

output:-
enter the no of terms10
fibonacci series:
0 1 1 2 3 5 8 13 21 34

Q-21) Write a python program to insert and delete names in a queue.

queue=[]
while True:
choice=int(input("1-> to insert data\n 2-> to delete data\n 3-> to exit\n enter
youur choice"))
if choice==1:
ele=input("enter the name of the student")
queue.append(ele)
elif choice==2:
if queue==[]:
print("underflow")
else:
ele=queue.pop(0)
print(ele,"deleted successfully")
elif choice==3:
break
else:
break
print("now , our queue is",queue)

output:-

1-> to insert data


2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentyatharth
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentshobhit
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentsambhav
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice2
yatharth deleted successfully
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice5
now , our queue is ['shobhit', 'sambhav']

Q-22 ) write a python program to count the no of space in a string.


def check_space(string):
count=0
for i in range(0,len(string)):
if string[i]==" ":
count+=1
return count

string=input("enter the string")


print("number of spaces ",check_space(string))

output:-
enter the string sambhav sharma
number of spaces 2

Q-23) python program to count lines starting with vowel in testfile.txt


def count_lines():
count=0
fobj=open("testfile.txt",'r')
line=fobj.readlines()
for i in line:
if i[0] in 'AEIOUaeiou':
count+=1
fobj.close()
return count
count=count_lines()
print("number of lines starting with vowel:",count)

Q-24) write a python program to count upper case alphabets.


string=input("enter a string")
count=0
for i in string:
if i.isupper():
count+=1
print("number of upper case alphabets=",count)

output:-
enter a strintINDIA is a democratic nation.
number of upper case alphabets= 5

Q-25) program of linear searching.


def linearSearch(list1,key):
for i in range(0,n):
if list1[i]==key:
return i+1
return None
list1=[]
n=int(input("Enter the no. of elements in the list:"))
print("enter each element and press enter:")
for i in range(0,n):
ele=int(input())
list1.append(ele)
print("the list contents are:",list1)
key=int(input("enter the element to be searched"))
pos=linearSearch(list1,key)
if pos is None:
print(key,"is not present in the list")
else:
print(key,"is present at position",pos)

output:-
Enter the no. of elements in the list:5
enter each element and press enter:
2
7
4
8
9
the list contents are: [2, 7, 4, 8, 9]
enter the element to be searched4
4 is present at position 3

Q-25 ) program of binary search


def binary_search(list,key):
first=0
last=len(list)-1
while (first<=last):
mid=(first+last)//2
if list[mid]==key:
return mid
elif key>list[mid]:
first=mid+1
elif key<list[mid]:
last=mid-1
return -1
list=list()
print("create a list by entering elements in ascending order")
print("press enter after each element ,press -99 to stop")
num=int(input())
while num!=-99:
list.append(num)
num=int(input())
key=int(input("enter the element to be searched"))
pos=binary_search(list,key)
if pos!=-1:
print(key,"is found at position",pos+1)
else:
print(key,"is not found in the list")

output:-

create a list by entering elements in ascending order


press enter after each element ,press -99 to stop
3
4
5
6
7
-99
enter the element to be searched5
5 is found at position 3

26.Write a menu driven python program to implement a stack having names of student.
stack = [ ]
while True :
print()
print("Enter your choice as per given -")
print("1 = For insert data Enter insert ")
print("2 = For delete data enter delete ")
print("3 = For Exit enter exit ")
print()
user = input("Enter your choice :- ")
if user == "insert" :
data = input("Enter the name of student :- ")
stack.append(data)
elif user == "delete" :
if stack == [ ]:
print("UnderFlow")
else :
stack.pop()
else :
break
print("Now our stack = ",stack)
output:-

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- insert


Enter the name of student :- ram
Now our stack = ['ram']

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- insert


Enter the name of student :- shyam
Now our stack = ['ram', 'shyam']

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- insert


Enter the name of student :- mohan
Now our stack = ['ram', 'shyam', 'mohan']

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- delete


Now our stack = ['ram', 'shyam']

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- exit

select* from employee;


+-------+------------+-----------+----------------+------------+
| empid | first_name | last_name | address | city |
+-------+------------+-----------+----------------+------------+
| 10 | harish | sharma | ibm colony | udaipur |
| 106 | vijay | gaur | shastri nagar | ajmer |
| 163 | ravindra | dadhich | vaishali nagar | jaipur |
| 215 | gurpreet | singh | naisarak | ganganagar |
| 244 | arvind | sharma | namastey chowk | jodhpur |
| 440 | peter | same | near charch | kota |
| 460 | pawan | winy | lalchand marg | alwar |
| 555 | vandna | thompson | cnt.road | delhi |
| 670 | ahemad | khan | check point | mumbai |
+-------+------------+-----------+----------------+------------+

select* from empsalary;


+-------+--------+----------+----------+
| empid | salary | benefits | desg |
+-------+--------+----------+----------+
| 10 | 75000 | 12000 | manager |
| 106 | 60000 | 10000 | manager |
| 163 | 37000 | 25000 | director |
| 215 | 50000 | 12300 | manager |
| 244 | 55000 | 11000 | clerk |
| 440 | 28000 | 12800 | salesman |
| 460 | 32000 | 7500 | salesman |
| 555 | 20000 | 10000 | clerk |
| 670 | 40000 | 3000 | clerk |
+-------+--------+----------+----------+

1. To display first name,last name , address and city of all employee living in
kota from the table employee.

select first_name, last_name,address,city from employee where city='kota';


+------------+-----------+-------------+------+
| first_name | last_name | address | city |
+------------+-----------+-------------+------+
| peter | same | near charch | kota |
+------------+-----------+-------------+------+

2. To display the content of employee table in descending order of first name.

select* from employee order by first_name desc;


+-------+------------+-----------+----------------+------------+
| empid | first_name | last_name | address | city |
+-------+------------+-----------+----------------+------------+
| 106 | vijay | gaur | shastri nagar | ajmer |
| 555 | vandna | thompson | cnt.road | delhi |
| 163 | ravindra | dadhich | vaishali nagar | jaipur |
| 440 | peter | same | near charch | kota |
| 460 | pawan | winy | lalchand marg | alwar |
| 10 | harish | sharma | ibm colony | udaipur |
| 215 | gurpreet | singh | naisarak | ganganagar |
| 244 | arvind | sharma | namastey chowk | jodhpur |
| 670 | ahemad | khan | check point | mumbai |
+-------+------------+-----------+----------------+------------+

3. To display the first name,last name and total salary of all managers from the
table employee and empsalary where total salary is calculated as salary+ benefits.

select first_name,last_name,salary+benefits as "total_sal" from employee,empsalary


where employee.empid=empsalary.empid and desg='manager';
+------------+-----------+-----------+
| first_name | last_name | total_sal |
+------------+-----------+-----------+
| harish | sharma | 87000 |
| vijay | gaur | 70000 |
| gurpreet | singh | 62300 |
+------------+-----------+-----------+

4. To display the maximum salary among managers and clerks from the table
empsalary.

select desg, max(salary) from empsalary group by desg having desg


in('manager','clerk');
+---------+-------------+
| desg | max(salary) |
+---------+-------------+
| manager | 75000 |
| clerk | 55000 |
+---------+-------------+

5. select first_name,salary from employee,empsalary where desg='salesman' and


employee.empid=empsalary.empid;
+------------+--------+
| first_name | salary |
+------------+--------+
| peter | 28000 |
| pawan | 32000 |
+------------+--------+

6. select count(distinct desg) from empsalary;


+----------------------+
| count(distinct desg) |
+----------------------+
| 4 |
+----------------------+

7. select desg,sum(salary) from empsalary group by desg having count(*)>2;


+---------+-------------+
| desg | sum(salary) |
+---------+-------------+
| manager | 185000 |
| clerk | 115000 |
+---------+-------------+
select desg,sum(salary) from empsalary group by desg having count(desg)>2;
+---------+-------------+
| desg | sum(salary) |
+---------+-------------+
| manager | 185000 |
| clerk | 115000 |
+---------+-------------+

select* from emp;


+-------+----------+----------+------------+-------+--------+
| empno | ename | job | hiredate | sal | deptno |
+-------+----------+----------+------------+-------+--------+
| 7369 | arvind | manager | 2000-12-17 | 12000 | 5 |
| 7499 | atul | clerk | 2001-02-20 | 6000 | 10 |
| 7521 | sunil | salesman | 2001-02-20 | 8000 | 5 |
| 7566 | rajesh | clerk | 2001-04-02 | 6500 | 15 |
| 7654 | gurpreet | manager | 2001-09-28 | 11500 | 10 |
| 7698 | nand lal | salesman | 2001-05-01 | 8900 | 15 |
| 7782 | praful | salesman | 2002-12-09 | 9200 | 10 |
| 7839 | ravindra | manager | 2001-11-17 | 13000 | 15 |
| 7876 | mithlesh | clerk | 2001-12-03 | 7000 | 15 |
| 7900 | kapil | salesman | 2002-01-23 | 9500 | 5 |
+-------+----------+----------+------------+-------+--------+

select* from dept;


+--------+------------+---------+
| deptno | dname | loc |
+--------+------------+---------+
| 5 | accounting | delhi |
| 10 | research | jodhpur |
| 15 | sales | jaipur |
+--------+------------+---------+

1.List the name of employee whose empno are 7369,7521,7698,7782.

select ename from emp where empno in(7369,7521,7698,7782);


+----------+
| ename |
+----------+
| arvind |
| sunil |
| nand lal |
| praful |
+----------+
2. List the employee names whose name is having 'T' as second character.
select ename from emp where ename like'_t%';
+-------+
| ename |
+-------+
| atul |
+-------+
3.List theemployee sal,pf,hra,da and gross salary where pf is 10% of basic ,hra
is 50% of basic,da is 30% of basic.
select ename,sal,10/100*sal as "pf",50/100*sal as "hra",30/100*sal as
"da",sal+50/100*sal+30/100*sal-10/100*sal as "gross_sal" from emp;
+----------+-------+-----------+-----------+-----------+------------+
| ename | sal | pf | hra | da | gross_sal |
+----------+-------+-----------+-----------+-----------+------------+
| arvind | 12000 | 1200.0000 | 6000.0000 | 3600.0000 | 20400.0000 |
| atul | 6000 | 600.0000 | 3000.0000 | 1800.0000 | 10200.0000 |
| sunil | 8000 | 800.0000 | 4000.0000 | 2400.0000 | 13600.0000 |
| rajesh | 6500 | 650.0000 | 3250.0000 | 1950.0000 | 11050.0000 |
| gurpreet | 1150 | 115.0000 | 575.0000 | 345.0000 | 1955.0000 |
| nand lal | 8900 | 890.0000 | 4450.0000 | 2670.0000 | 15130.0000 |
| praful | 9200 | 920.0000 | 4600.0000 | 2760.0000 | 15640.0000 |
| ravindra | 13000 | 1300.0000 | 6500.0000 | 3900.0000 | 22100.0000 |
| mithlesh | 7000 | 700.0000 | 3500.0000 | 2100.0000 | 11900.0000 |
| kapil | 9500 | 950.0000 | 4750.0000 | 2850.0000 | 16150.0000 |
+----------+-------+-----------+-----------+-----------+------------+
4.List the total salary of all the employees.
select sum(sal) as "total_sal" from emp;
+-----------+
| total_sal |
+-----------+
| 81250 |
+-----------+
5.List the maximum salary and minimum salary of the employee.
select max(sal),min(sal) from emp;
+----------+----------+
| max(sal) | min(sal) |
+----------+----------+
| 13000 | 1150 |
+----------+----------+
6.List the department wise average salary of the employees.
select deptno,avg(sal) from emp group by deptno;
+--------+-----------+
| deptno | avg(sal) |
+--------+-----------+
| 5 | 9833.3333 |
| 10 | 5450.0000 |
| 15 | 8850.0000 |
+--------+-----------+
7.find the name of the manager whose department number is 15.
select ename from emp where deptno=15 and job='manager';
+----------+
| ename |
+----------+
| ravindra |
+----------+
8.List all employee names whose department location is "Delhi".
select ename from emp where deptno=(select deptno from dept where loc="delhi");
+--------+
| ename |
+--------+
| arvind |
| sunil |
| kapil |
+--------+
9.List all employee names whose salary is greater than 8500.
select ename from emp where sal>8500;
+----------+
| ename |
+----------+
| arvind |
| nand lal |
| praful |
| ravindra |
| kapil |
+----------+

You might also like