Professional Documents
Culture Documents
perfect or not.
divsum=0
for i in range(1,num):
if num%i == 0:
divsum+=i
if divsum==num:
print('Perfect Number')
else:
print('Not a perfect number')
perfect(6)
perfect(15)
Output:
Perfect Number
Not a perfect number
Program 2: Write a function to find factorial of the entered number.
f= 1
for i in range(1,n+1):
f = f*i
print("The factorial of ",num,"=",f)
Output:
Enter the number for calculating its factorial : 5
The factorial of 5 = 120
def EOSum(numbers):
ec=0
oc=0
for i in numbers:
if i%2==0:
ec=ec+i
else:
oc=oc+i
print("sum of even is ",ec)
print("sum of odd is ",oc)
l=eval(input("enter list of numbers:"))
EOSum(l)
Output:
enter list of numbers:[1,2,3,4]
sum of even is 6
sum of odd is 4
def change(arr,n):
for i in range(n):
if arr[i]%2==0:
arr[i]=arr[i]//2
else:
arr[i]=arr[i]*2
print("The resultant array is ",arr)
def display(num):
l1=[]
l2=[]
for i in num:
if i%10==0:
l1.append(i)
else:
l2.append(i)
for i in l1:
print(i,end=' ')
print()
for j in l2:
print(j,end=' ')
Output:
enter list of numbers:[1,3,4,80,50,20]
80 50 20
134
Program 6: Write a function count_words() to read data from file
“data.txt” and count occurrences of the given word in file
def count_words():
f=open("data.txt","r")
data=f.read()
words=data.split()
w=input("Enter the word to be searched:")
c=0
for i in words:
if i==w:
c=c+1
print("The word ",w,"occurs",c,"times")
f.close()
count_words()
If the content of the file data.txt is:
She started to work there
Right now, we looking forward to get you renowned
Output:
Enter the word to be searched:to
The word to occurs 2 times
def countM():
fh=open("MYSTORY.TXT","r")
lines=fh.readlines()
for i in lines:
if(i[0]=='M'):
print(i)
fh.close()
countM()
If the “MYSTORY.TXT” contents are as follows:
Output
def Words():
fh=open("ZIP.TXT","r")
data=fh.read()
w=data.split()
c=0
for i in w:
if(len(i)>4):
c=c+1
Output
def count():
fh=open("STORY.TXT","r")
data=fh.read()
w=data.split()
c=0
for i in w:
if(i=='Me' or i=='My'):
c=c+1
print("count of Me/My is ",c)
fh.close()
count()
Output:
Count of Me/My in file: 3
Program 10: Write a function AMCount() in Python, which should
read each character of a text file STORY.TXT, should count and
display the occurrences of alphabets A and M (including small cas-
es a and m too).
def AMcount():
fh=open("STORY.TXT","r")
data=fh.read()
acount=0
mcount=0
for i in data:
if(i=='M'or i=='m'):
mcount=mcount+1
if(i=='A' or i=='a'):
acount=acount+1
print("A or a :",acount)
print("M or m: ",mcount)
fh.close()
AMcount()
Output:
A or a: 4
M or m :2
Program 11:
#Main program
s=[]
top=None
while True:
print("1.PUSH")
print("2.POP")
print("3.DISPLAY")
print("4.EXIT")
ch=int(input("enter choice:"))
if ch==1:
ele=int(input("enter element to be inserted:"))
push(s,ele)
elif ch==2:
val=pop()
if val=="underflow":
print("stack is empty")
else:
print("Deleted element is:",val)
elif ch==3:
display()
else:
break
OUTPUT:
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:1
enter element to be inserted:23
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:1
enter element to be inserted:34
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:1
enter element to be inserted:67
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:3
Elements of Stack are:
23 34 67
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:2
Deleted element is: 67
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:3
Elements of Stack are:
23 34
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter choice:5
Program 12:
def POP():
while True:
if len(s)!=0:
print(s.pop())
else:
return "underflow"
PUSH(N)
POP()
OUTPUT:
stack elements are: [12, 6, 42, 39]
39
42
6
12
Program 13:
Write a program to display unique vowels present in the given word
using Stack.
vowels =['a','e','i','o','u']
word = input("Enter the word to search for vowels :")
Stack = []
for letter in word:
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)
print("The number of different vowels present in",word,"is",len(Stack))
OUTPUT:
Enter the word to search for vowels :python programming
['o', 'a', 'i']
The number of different vowels present in python programming is 3
\
14
Consider the following MOVIE table and write the SQL queries based
on it.
OUTPUT:
OUTPUT:
+---------+---------------+-----------+-------------+----------------+--------
------+
+---------+---------------+-----------+-------------+----------------+----------
----+
OUTPUT:
+---------+---------------+---------------+
+---------+---------------+---------------+
| 1 | hindi_movie | 254500 |
| 1 | Tamil_movie | 230000 |
| 3 | English_movie | 605000 |
| 4 | Bengali_movie | 172000 |
| 5 | Telugu_movie | NULL |
| 6 | Panjabi_movie | NULL |
+---------+---------------+---------------+
OUTPUT:
+-----------+
| category |
+-----------+
| musical |
| Action |
| Horror |
| Adventure |
| Comedy |
+-----------+
d) Find the net profit of each movie showing its MovieID, MovieName
and NetProfit. Net Profit is to be calculated as the difference between
Business Cost and Production Cost.
output:
+---------+---------------+-----------+
+---------+---------------+-----------+
| 1 | hindi_movie | 5500 |
| 1 | Tamil_movie | 6000 |
| 3 | English_movie | 115000 |
| 4 | Bengali_movie | 28000 |
| 5 | Telugu_movie | NULL |
| 6 | Panjabi_movie | NULL |
+---------+---------------+-----------+
15
output:
Query OK, 1 row affected (0.01 sec)
output:
Query OK, 1 row affected (0.01 sec)
output:
Query OK, 1 row affected (0.01 sec)
output:
Query OK, 1 row affected (0.00 sec)
output:
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO DESIG
VALUES(210,75000,12500,'Manager');
output:
Query OK, 1 row affected (0.01 sec)
output:
ERROR 1452 (23000): Cannot add or update a child
row: a foreign key constraint fails
(`practical`.`desig`, CONSTRAINT `desig_ibfk_1`
FOREIGN KEY (`W_ID`) REFERENCES `workers`
(`W_ID`))
OUTPUT:
OUTPUT:
Consider the tables WORKERS and DESIG. Write SQL commands for
the statements
+------+-----------+----------+--------+---------
----------+--------------+
| W_ID | FIRSTNAME | LASTNAME | GENDER | ADDRESS
| city |
+------+-----------+----------+--------+---------
----------+--------------+
| 105 | Sarah | Ackerman | F | U.S. 110
| New york |
| 255 | Mary | Jones | F | 842,
Vine Ave. | Losantiville |
| 403 | Ronny | Lee | M | 121 Har-
rison St. | New York |
| 300 | Robert | Samuel | M | 9 Fifth
Cross | Washington |
| 144 | Manila | Sengupta | F | 24
Friends Street | New Delhi |
| 210 | George | Smith | M | 83 First
Street | Howard |
| 451 | Pat | Thompson | M | 11 Red
Road | Paris |
| 102 | Sam | Tones | M | 33 Elm
St | Paris |
| 335 | Henry | Williams | M | 12 Moore
Street | Boston |
+------+-----------+----------+--------+---------
----------+--------------+
9 rows in set (0.00 sec)
iii) To display First Name, Worker ID and Address of male Workers only.
mysql> SELECT FIRSTNAME,W_ID,ADDRESS FROM WORK-
ERS WHERE GENDER=‘M';
OUTPUT:
+-----------+------+------------------+
| FIRSTNAME | W_ID | ADDRESS |
+-----------+------+------------------+
| Sam | 102 | 33 Elm St |
| George | 210 | 83 First Street |
| Robert | 300 | 9 Fifth Cross |
| Henry | 335 | 12 Moore Street |
| Ronny | 403 | 121 Harrison St. |
| Pat | 451 | 11 Red Road |
+-----------+------+------------------+
6 rows in set (0.00 sec)
iv) To display the Minimum salary among Managers and Clerks from the
table DESIG.
mysql> SELECT DESIGNATION,MIN(SALARY) FROM DESIG
GROUP BY DESIGNATION HAVING DESIGNATION IN
(‘CLERK','MANAGER');
OUTPUT:
+-------------+-------------+
| DESIGNATION | MIN(SALARY) |
+-------------+-------------+
| Manager | 70000 |
| Clerk | 40000 |
+-------------+-------------+
2 rows in set (0.00 sec)
v) To display First Name and Salary from Workers and Desig Table for
each worker.
mysql> SELECT W.FIRSTNAME,D.SALARY FROM WORKERS
W,DESIG D WHERE W.W_ID=D.W_ID;
OUTPUT:
+-----------+--------+
| FIRSTNAME | SALARY |
+-----------+--------+
| Sarah | 85000 |
| Manila | 70000 |
| George | 75000 |
| Sam | 75000 |
| Mary | 50000 |
| Robert | 45000 |
| Henry | 40000 |
| Pat | 28000 |
+-----------+--------+
8 rows in set (0.00 sec)
17
Consider the table Books given below. Write SQL Commands for the
statements
OUTPUT:
Query OK, 0 rows affected (0.02 sec)
OUTPUT:
Query OK, 1 row affected (0.00 sec)
OUTPUT:
Query OK, 1 row affected (0.00 sec)
OUTPUT:
Query OK, 1 row affected (0.01 sec)
OUTPUT:
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO BOOKS VALUES(1005,'Officer','K
Prakash','EPB',370,11);
OUTPUT:
Query OK, 1 row affected (0.00 sec)
ii) To display the average price of books which quantity is grater than 5
from the table Books
mysql> SELECT AVG(PRICE) FROM BOOKS WHERE QTY>5;
OUTPUT:
+------------+
| AVG(PRICE) |
+------------+
| 390 |
+------------+
1 row in set (0.00 sec)
iii) To display Book name, publishers of books where publisher name
starts with T
mysql> SELECT BOOK_NAME,PUBLISHERS FROM BOOKS
WHERE PUBLISHERS LIKE ’T%';
OUTPUT:
+-----------+------------+
| BOOK_NAME | PUBLISHERS |
+-----------+------------+
| The Human | TDH |
+-----------+------------+
1 row in set (0.00 sec)
iv) To display sum of price from BOOKS table
mysql> SELECT SUM(PRICE) FROM BOOKS;
OUTPUT:
+------------+
| SUM(PRICE) |
+------------+
| 2520 |
+------------+
1 row in set (0.00 sec)
18
import mysql.connector
con=mysql.connector.connect(user='root',password='jnvegh794111',host='l
ocalhost',
database='user')
cur=con.cursor()
stmt = "CREATE TABLE HRD(Ecode int primary key, Ename
varchar(25), Designation varchar(20), Remuneration float);”
cur.execute(stmt)
con.commit()
con.close()
OUTPUT:
mysql> DESC HRD;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ECODE | int | YES | | NULL | |
| ENAME | varchar(20) | YES | | NULL | |
| DESIGNATION | varchar(20) | YES | | NULL | |
| RENUMERATION | float | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
cur=con.cursor()
Stmt = "INSERT INTO HRD VALUES (10001,’Vipul’,’Programmer',
50000);"
cur.execute(stmt)
con.commit()
con.close()
OUTPUT:
mysql> SELECT * FROM HRD;
+-------+-------+-------------+--------------+
| ECODE | ENAME | DESIGNATION | REMUNERATION |
+-------+-------+-------------+--------------+
| 10001 | Vipul | Programmer | 50000 |
+-------+-------+-------------+--------------+
1 row in set (0.00 sec)
20. Write a python program to fetch records from the HRD table having remunera-
tion more than 70000
import mysql.connector
con=mysql.connector.connect(user='root',password='jnvegh794111',host='l
ocalhost',
database='user')
cur=con.cursor()
Stmt = "SELECT * FROM HRD WHERE REMUNERATION > 70000;"
try:
cur.execute(stmt)
resultset = cur.fetchall()
for row in resultset:
print (row)
except:
print ("Error: unable to fetch data")
con.close()
OUTPUT:
21. Write a python program to update remuneration of employee to 70000 from the
HRD table whose name is Vipul
import mysql.connector
con=mysql.connector.connect(user='root',password='jnvegh794111',host='l
ocalhost',
database='user')
cur=con.cursor()
Stmt = "UPDATE HRD SET REMUNERATION=70000 WHERE
ENAME=‘VIPUL';"
try:
cur.execute(stmt)
cur.commit()
except:
print(“Unable to update data”)
con.close()
OUTPUT:
mysql> SELECT * FROM HRD;
+-------+-------+-------------+--------------+
| ECODE | ENAME | DESIGNATION | RENUMERATION |
+-------+-------+-------------+--------------+
| 10001 | Vipul | Programmer | 70000 |
+-------+-------+-------------+--------------+
1 row in set (0.00 sec)