Practical File Cs 083 BVM 2023-2024
Practical File Cs 083 BVM 2023-2024
Practical File Cs 083 BVM 2023-2024
NAME :
CLASS :
REG. NO :
1
BHARATH VIDYA MANDIR
SENIOR SECONDARY SCHOOL (CBSE)
Courtallam Five Falls By -Pass, Ilanji -627805. Tirunelveli District.
Phone: 04633 223101,223203
Managed by AppasamyKarayalar Educational & Charitable Trust
RECORD NOTEBOOK
Name __________________________________________
Register No.________________ Year_________________
School __________________________________________
___________________ ___________________
________________________ ________________________
Internal Examiner External Examiner
2
INDEX
XII CS – Practical Assignments
Sl. No Date Description of Assignment Pg. No. Sign
1. 30.08.2022 Implementation of Stack Operation 4
3
Ex. No.: 1
Date: 30.08.2022
def pop(st):
if st == []:
return None
else:
return st.pop()
print("# Push operation:")
push(StackVow, vowels)
print(StackVow)
print("# Pop operation :", pop(StackVow))
print("# Traversal Operation:")
for i in range(len(StackVow)):
print(pop(StackVow), end=" ")
Output:
# Push operation:
['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
# Pop operation : u
# Traversal Operation:
o i e a U O I E A
Result:
The above program is executed successfully and the output is verified.
4
Ex. No.: 2
Date: 30.08.2022
def push(stack):
hn=(input('Enter name:'))
ts=int(input('Enter marks:'))
lt=[hn,ts]
stack.append(lt)
def pop(stack):
if (stack==[]):
print('No record!')
else:
print('Deleted record is :',stack.pop())
def records(stack):
for i in stack:
print(i)
while(True):
print('1. Add record')
print('2. Delete records')
print('3. Display records')
print('4. Exit ')
op=int(input('Enter your choice: '))
if (op==1):
push(stack)
elif (op==2):
pop (stack)
elif (op==3):
records(stack)
elif (op==4):
break
5
Output:
1. Add record
2. Delete records
3. Display records
4. Exit
Enter your choice: 1
Enter name:Marvel
Enter marks:100
1. Add record
2. Delete records
3. Display records
4. Exit
Enter your choice: 1
Enter name:Thor
Enter marks:99
1. Add record
2. Delete records
3. Display records
4. Exit
Enter your choice: 3
['Marvel', 100]
['Thor', 99]
1. Add record
2. Delete records
3. Display records
4. Exit
Enter your choice: 2
Deleted record is : ['Thor', 99]
1. Add record
2. Delete records
3. Display records
4. Exit
Enter your choice: 4
Result:
The above program is executed successfully and the output is verified.
6
Ex. No.: 3
Date: 30.08.2022
7
print("Stack is empty.....")
continue
print ('Current stack:',stack,' \tStack
size :',len(stack))
if (ch==4):
break
Output:
Menu
----
1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 1
1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 1
1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 1
1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 3
Current stack ['python', 'java', 'javascript'] Stack size 3
Menu
----
8
1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 2
1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 4
Result:
The above program is executed successfully and the output is verified.
9
Ex. No.: 4
Date: 31.08.2022
if mydb.is_connected():
print("Connection OK.")
else:
print("Error connecting to mysql database.")
myc = mydb.cursor()
myc.execute("CREATE TABLE employee (empid INT(5), name CHAR(10),
desig VARCHAR(20));")
myc.execute("INSERT INTO employee VALUES (120, 'Alisha',
'Manager');")
myc.execute("INSERT INTO employee VALUES (121, 'Mohit',
'Manager');")
myc.execute("INSERT INTO employee VALUES (122, 'Suja', 'AO');")
10
mydb.commit()
mydb.close()
Output:
Connection OK.
120 Alisha Manager
121 Mohit Manager
122 Suja AO
1 RECORD UPDATED.
1 RECORD DELETED.
121 Mohit Accountant
122 Suja AO
Result:
The above program is executed successfully and the output is verified.
11
Ex. No.: 5
Date: 31.08.2022
SQL Queries – 1
Aim:
To write SQL commands for the queries (i) to (viii) and the output for (xi) to (xiii) based on
the following tables.
Table issued
Output:
12
i) SELECT book_id, book_name, author_name, price FROM library
WHERE publisher = 'First Pub';
+---------+-------------+-----------------+-------+
| book_id | book_name | author_name | price |
+---------+-------------+-----------------+-------+
| f001 | The Tears | Willaim Hopkins | 650 |
| f002 | Thunderbolt | Anna Roberts | 750 |
+---------+-------------+-----------------+-------+
2 rows in set (0.000 sec)
ii) SELECT book_name, price FROM library ORDER BY price;
+----------------+-------+
| book_name | price |
+----------------+-------+
| My First C++ | 350 |
| C++ Brianworks | 350 |
| Fast Cook | 355 |
| The Tears | 650 |
| Thunderbolt | 750 |
+----------------+-------+
5 rows in set (0.000 sec)
iii) SELECT price FROM library WHERE price>300 AND price<500;
+-------+
| price |
+-------+
| 355 |
| 350 |
| 350 |
+-------+
3 rows in set (0.000 sec)
iv) UPDATE library SET price=price+50 WHERE publisher='EPB';
Query OK, 2 rows affected (0.004 sec)
Rows matched: 2 Changed: 2 Warnings: 0
v) SELECT library.book_id, library.book_name,
issued.quantity_issued FROM library, issued WHERE
library.book_id=issued.book_id;
+---------+--------------+-----------------+
| book_id | book_name | quantity_issued |
+---------+--------------+-----------------+
| c001 | Fast Cook | 5 |
| f001 | The Tears | 2 |
| t001 | My First C++ | 4 |
+---------+--------------+-----------------+
3 rows in set (0.001 sec)
vi) SELECT book_name, price FROM library WHERE book_name LIKE
'%C++%';
+----------------+-------+
| book_name | price |
+----------------+-------+
| My First C++ | 450 |
| C++ Brianworks | 350 |
+----------------+-------+
2 rows in set (0.000 sec)
vii) DELETE FROM library WHERE book_id= 't002 ';
Query OK, 1 row affected (0.004 sec)
13
viii) SELECT book_name FROM library WHERE book_name LIKE 'T%';
+-------------+
| book_name |
+-------------+
| The Tears |
| Thunderbolt |
+-------------+
2 rows in set (0.000 sec)
ix) SELECT COUNT(*) FROM library;
+----------+
| COUNT(*) |
+----------+
| 4 |
+----------+
1 row in set (0.002 sec)
x) SELECT MAX(price) FROM library WHERE quantity >= 15;
+------------+
| MAX(price) |
+------------+
| 750 |
+------------+
1 row in set (0.000 sec)
xi) SELECT book_name, author_name FROM library WHERE
publisher='First Pub';
+-------------+-----------------+
| book_name | author_name |
+-------------+-----------------+
| The Tears | Willaim Hopkins |
| Thunderbolt | Anna Roberts |
+-------------+-----------------+
2 rows in set (0.000 sec)
xii) SELECT COUNT(DISTINCT publisher) FROM library WHERE price
>=400;
+---------------------------+
| COUNT(DISTINCT publisher) |
+---------------------------+
| 2 |
+---------------------------+
1 row in set (0.000 sec)
xiii) SELECT COUNT(DISTINCT(publisher)) FROM library;
+----------------------------+
| COUNT(DISTINCT(publisher)) |
+----------------------------+
| 2 |
+----------------------------+
1 row in set (0.000 sec)
Result:
The above SQL Queries are executed successfully and the output is verified.
14
Ex. No.: 6
Date: 05.09.2022
SQL Queries – 2
Aim:
To consider the following table hospital and answer the following questions.
Table hospital
15
iii) SELECT * FROM hospital WHERE department='Cardiology';
iv) SELECT name FROM hospital WHERE department='ENT' AND sex='F';
v) SELECT name FROM hospital ORDER BY dateofadm;
vi) SELECT name,charges,age FROM hospital WHERE sex='F';
vii) SELECTCOUNT(*) FROM hospital WHERE age<30;
viii) DELETE FROM hospital WHERE sex='F';
ix) SELECT name, age, department FROM hospital WHERE name LIKE 'a%';
x) ALTER TABLE hospital DROP dateofadm;
xi) UPDATE hospital SET charges=charges*1.10 WHERE department='ENT';
xii) SELECT DISTINCT department FROM hospital;
xiii) SELECT sex,MAX(charges) FROM hospital GROUP BY sex;
xiv) SELECT * FROM hospital WHERE age>=19 AND age<=35;
xv) SELECT COUNT(*) FROM hospital GROUP BY department;
xvi) SELECT COUNT(*) FROM hospital WHERE age<30;
xvii) SELECT COUNT(DISTINCT charges) FROM hospital;
xviii) SELECT MIN(age) FROM hospital WHERE sex='F';
xix) SELECT SUM(charges) FROM hospital WHERE department='ENT';
xx) SELECT MAX(charges) FROM hospital WHERE sex='M';
Output:
ii) INSERT INTO hospital VALUES (1, 'Arpit', 62, 'Surgery', '21/1/98',
300, 'M')
Query OK, 1 row affected (0.002 sec)
INSERT INTO hospital VALUES (5, 'Zubin', 30, 'ENT', '12/1/98', 250,
'M');
Query OK, 1 row affected (0.002 sec)
INSERT INTO hospital VALUES (6, 'Karin', 16, 'ENT', '24/2/98', 250,
'F');
Query OK, 1 row affected (0.002 sec)
16
'13/1/98', 800, 'M');
Query OK, 1 row affected (0.002 sec)
+---------+
| name |
+---------+
| Zareena |
| Karin |
+---------+
2 rows in set (0.000 sec)
17
| 6 |
+----------+
1 row in set (0.000 sec)
viii) DELETE FROM hospital WHERE sex='F';
Query OK, 5 rows affected (0.005 sec)
ix) SELECT name, age, department FROM hospital WHERE name LIKE 'a
%';
+-------+------+------------+
| name | age | department |
+-------+------+------------+
| Arpit | 62 | Surgery |
| Arun | 12 | Surgery |
+-------+------+------------+
2 rows in set (0.000 sec)
x) ALTER TABLE hospital DROP dateofadm;
Query OK, 0 rows affected (0.039 sec)
Records: 0 Duplicates: 0 Warnings: 0
xi) UPDATE hospital SET charges=charges*1.10 WHERE department='ENT';
Query OK, 3 rows affected (0.004 sec)
Rows matched: 3 Changed: 3 Warnings: 0
xii) SELECT DISTINCT department FROM hospital;
+------------------+
| department |
+------------------+
| Surgery |
| ENT |
| Orthopedic |
| Cardiology |
+------------------+
4 rows in set (0.000 sec)
xiii) SELECT sex, MAX(charges) FROM hospital GROUP BY sex;
+------+--------------+
| sex | MAX(charges) |
+------+--------------+
| M | 800 |
+------+--------------+
2 rows in set (0.000 sec)
xiv) SELECT * FROM hospital WHERE age>=19 AND age<=35 AND sex='M';
+------+--------+------+------------+-----------+---------+------+
| no | name | age | department | dateofadm | charges | sex |
+------+--------+------+------------+-----------+---------+------+
| 3 | Kareem | 32 | Orthopedic | 19/2/98 | 200 | M |
| 5 | Zubin | 30 | ENT | 12/1/98 | 25 | M |
| 9 | Kush | 19 | Cardiology | 13/1/98 | 800 | M |
+------+--------+------+------------+-----------+---------+------+
3 rows in set (0.001 sec)
xv) SELECT COUNT(*) FROM hospital GROUP BY department;
+----------+
| COUNT(*) |
+----------+
| 1 |
| 1 |
| 1 |
| 2 |
+----------+
4 rows in set (0.001 sec)
xvi) SELECT COUNT(*) FROM hospital WHERE age<30;
18
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
1 row in set (0.000 sec)
xvii) SELECT COUNT(DISTINCT charges) FROM hospital;
+-------------------------+
| COUNT(DISTINCT charges) |
+-------------------------+
| 4 |
+-------------------------+
1 row in set (0.000 sec)
xviii) SELECT MIN(age) FROM hospital WHERE sex='F';
+----------+
| MIN(age) |
+----------+
| NULL |
+----------+
1 row in set (0.000 sec)
xix) SELECT SUM(charges) FROM hospital WHERE department='ENT';
+--------------+
| SUM(charges) |
+--------------+
| 825 |
+--------------+
1 row in set (0.000 sec)
xx) SELECT MAX(charges) FROM hospital WHERE sex='M';
+--------------+
| MAX(charges) |
+--------------+
| 800|
+--------------+
1 row in set (0.000 sec)
Result:
The above SQL Queries are executed successfully and the output is verified.
19
Ex. No.: 7
Date: 05.09.2022
SQL Queries – 3
Aim:
To consider the following table graduate and answer the following questions:
Table graduate
20
iii) SELECT * FROM graduate WHERE subject='Phy';
iv) SELECT * FROM graduate WHERE division='1';
v) SELECT name, subject, average FROM graduate ORDER BY average;
vi) SELECT name FROM graduate WHERE subject='CS' and
division='2';
vii) SELECT COUNT(*) FROM graduate WHERE stipend<300;
viii) DELETE FROM graduate WHERE subject='Phy';
ix) SELECT name, stipend, subject FROM graduate WHERE subject
LIKE '%s';
x) ALTER TABLE DROP division;
xi) UPDATE graduate SET stipend=stipend*1.10 WHERE subject='Phy';
xii) SELECT DISTINCT subject FROM graduate;
xiii) SELECT MAX(stipend) FROM graduate GROUP BY subject;
xiv) SELECT * FROM graduate WHERE subject='Math' AND average>50
AND average<75;
xv) SELECT COUNT(*) FROM graduate GROUP BY subject;
Output:
21
| sno | name | stipend | subject | average | division |
+------+--------+---------+---------+---------+----------+
| 1 | Karan | 400 | Phy | 68 | 1 |
| 4 | Arun | 350 | Phy | 63 | 1 |
| 7 | Robert | 250 | Phy | 64 | 1 |
+------+--------+---------+---------+---------+----------+
3 rows in set (0.000 sec)
iv) SELECT * FROM graduate WHERE division='1';
+------+---------+---------+---------+---------+----------+
| sno | name | stipend | subject | average | division |
+------+---------+---------+---------+---------+----------+
| 1 | Karan | 400 | Phy | 68 | 1 |
| 2 | Divakar | 450 | CS | 68 | 1 |
| 4 | Arun | 350 | Phy | 63 | 1 |
| 5 | Sabina | 500 | Math | 70 | 1 |
| 7 | Robert | 250 | Phy | 64 | 1 |
| 8 | Rubina | 450 | Math | 68 | 1 |
| 9 | Vikas | 500 | CS | 62 | 1 |
+------+---------+---------+---------+---------+----------+
7 rows in set (0.000 sec)
v) SELECT name, subject, average FROM graduate ORDER BY average;
+---------+---------+---------+
| name | subject | average |
+---------+---------+---------+
| John | Chem | 55 |
| Mohan | Math | 57 |
| Divya | Chem | 62 |
| Vikas | CS | 62 |
| Arun | Phy | 63 |
| Robert |Phy | 64 |
| Divakar | CS | 68 |
| Rubina | Math | 68 |
| Karan | Phy | 68 |
| Sabina | Math | 70 |
+---------+---------+---------+
10 rows in set (0.000 sec)
vi) SELECT name FROM graduate WHERE subject='CS' and division='2';
Empty set (0.000 sec)
vii) SELECT COUNT(*) FROM graduate WHERE stipend<300;
+----------+
| COUNT(*) |
+----------+
| 1 |
+----------+
1 row in set (0.000 sec)
viii) DELETE FROM graduate WHERE subject='Phy';
Query OK, 3 rows affected (0.004 sec)
ix) SELECT name, stipend, subject FROM graduate WHERE subject LIKE
'%s';
+---------+---------+---------+
| name | stipend | subject |
+---------+---------+---------+
| Divakar | 450 | CS |
| Vikas | 500 | CS |
+---------+---------+---------+
2 rows in set (0.000 sec)
22
x) ALTER TABLE graduate DROP division;
Query OK, 0 rows affected (0.042 sec)
Records: 0 Duplicates: 0 Warnings: 0
xi) UPDATE graduate SET stipend=stipend*1.10 WHERE subject='Phy';
Query OK, 0 rows affected (0.000 sec)
Rows matched: 0 Changed: 0 Warnings: 0
xii) SELECT DISTINCT subject FROM graduate;
+---------+
| subject |
+---------+
| CS |
| Chem |
| Math |
+---------+
3 rows in set (0.001 sec)
xiii) SELECT subject, MAX(stipend) FROM graduate GROUP BY subject;
+---------+--------------+
| subject | MAX(stipend) |
+---------+--------------+
| Chem | 400 |
| CS | 500 |
| Math | 500 |
+---------+--------------+
3 rows in set (0.000 sec)
xiv) SELECT * FROM graduate WHERE subject='Math' AND average>50 AND
average<75;
+------+--------+---------+---------+---------+
| sno | name | stipend | subject | average |
+------+--------+---------+---------+---------+
| 5 | Sabina | 500 | Math | 70 |
| 8 | Rubina | 450 | Math | 68 |
| 10 | Mohan | 300 | Math | 57 |
+------+--------+---------+---------+---------+
3 rows in set (0.000 sec)
xv) SELECT subject, COUNT(*) FROM graduate GROUP BY subject;
+---------+----------+
| subject | COUNT(*) |
+---------+----------+
| Chem | 2 |
| CS | 2 |
| Math | 3 |
+---------+----------+
3 rows in set (0.001 sec)
xvi) SELECT COUNT(*) FROM graduate WHERE subject='Phy';
+----------+
| COUNT(*) |
+----------+
| 0 |
+----------+
1 row in set (0.000 sec)
xvii) SELECT COUNT(distinct subject) FROM graduate;
+-------------------------+
| COUNT(distinct subject) |
+-------------------------+
| 3 |
+-------------------------+
1 row in set (0.000 sec)
xviii) SELECT MIN(stipend) FROM graduate WHERE subject='CS';
23
+--------------+
| MIN(stipend) |
+--------------+
| 450 |
+--------------+
1 row in set (0.000 sec)
xix) SELECT SUM(stipend) FROM graduate WHERE subject='Chem';
+--------------+
| SUM(stipend) |
+--------------+
| 700 |
+--------------+
1 row in set (0.000 sec)
xx) SELECT MAX(average) FROM graduate WHERE subject='Chem';
+--------------+
| MAX(average) |
+--------------+
| 62 |
+--------------+
1 row in set (0.000 sec)
Result:
The above SQL Queries are executed successfully and the output is verified.
24
Ex. No.: 8
Date: 06.09.2022
lines = myfile.readlines()
for i in range(len(lines)):
words = lines[i].split()
print("#".join(words))
myfile.close()
Source File:
welcome to python file handling concept
now we interact with file
Output:
welcome#to#python#file#handling#concept
now#we#interact#with#file
Result:
The above program is executed successfully and the output is verified.
25
Ex. No.: 9
Date: 06.09.2022
vow = con = up = lo = 0
for i in file_text:
if i.isalpha():
if i in ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]:
vow += 1
else:
con += 1
if i.isupper():
up += 1
elifi.islower():
lo += 1
print("Number of Vowels:", vow)
print("Number of Consonants:", con)
print("Number of Upper case:", up)
print("Number of Lower case:", lo)
Result:
The above program is executed successfully and the output is verified.
26
Ex. No.: 10
Date: 12.09.2022
source_lines = source_file.readlines()
output_lines = []
for i in range(len(source_lines)):
if "a" not in source_lines[i]:
output_lines.append(source_lines[i])
output_file.writelines(output_lines)
source_file.close()
output_file.close()
OutputFile (pratical_10_output_file.txt):
Welcome to Python file handling concept.
Now we interact with text file.
This line does not use the wrong letter.
Forth line got through just almost...
You don't find it on this one.
Result:
The above program is executed successfully and the output is verified.
27
Ex. No.: 11
Date: 12.09.2022
stud_record = {}
for i in range(5):
roll = int(input("Enter Roll number:"))
name = input("Enter Name:")
stud_record["roll"] = roll
stud_record["name"] = name
pickle.dump(stud_record, my_file)
my_file.seek(0)
search = int(input("Enter Roll no to search:"))
while True:
try:
record = pickle.load(my_file)
if record["roll"] == search:
print("Roll No:", record["roll"],
"Name:", record["name"])
break
except EOFError:
print("Student details not found.")
break
my_file.close()
Output:
Enter Roll number:101
Enter Name:Hulk
Enter Roll number:102
Enter Name:Jen
28
Enter Roll number:103
Enter Name:Strange
Enter Roll number:104
Enter Name:Stark
Enter Roll number:105
Enter Name:Olaf
Enter Roll no to search:105
Roll No: 105 Name: Olaf
Result:
The above program is executed successfully and the output is verified.
29
Ex. No.: 12
Date: 13.08.2022
marks_db = [
[101, "Student 1", 96],
[102, "Student 2", 56],
[103, "Student 3", 97],
[104, "Student 4", 19],
[105, "Student 5", 92],
[106, "Student 6", 57],
[107, "Student 7", 90],
[108, "Student 8", 81]
]
for i in marks_db:
if i[0] == find_roll:
print("Original mark:", i[2])
i[2] = new_mark
print("Updated mark:", i[2])
break
30
myfile.seek(0)
pickle.dump(marks_db, myfile)
myfile.close()
Output (part1.py):
Write Successful.
Output (part2.py):
Enter roll no you want to update:105
New mark:100
Original mark: 92
Updated mark: 100
Result:
The above program is executed successfully and the output is verified.
31
Ex. No.: 13
Date: 13.09.2022
Output:
Random number: 1
Would you like to continue y/n?y
Random number: 1
Would you like to continue y/n?y
Random number: 2
Would you like to continue y/n?y
Random number: 2
Would you like to continue y/n?n
Result:
The above program is executed successfully and the output is verified.
32
Ex. No.: 14
Date: 19.09.2022
my_csv_file.seek(0)
my_reader = csv.reader(my_csv_file)
find_user = input("Enter username:")
found = False
for i in my_reader:
if i[0] == find_user:
print("The password is ", i[1])
found = True
break
if not found:
print("Username not found.")
my_csv_file.close()
33
Output:
Enter username:userid5
The password is pass5
Result:
The above program is executed successfully and the output is verified.
34
Ex. No.: 15
Date: 19.09.2022
Arithmetic Operations
Aim:
To write a Python program to enter two numbers and print the arithmetic operations like +,-,*,
/, // and %.
Procedure:
1. Start Python IDLE by clicking on its icon on the desktop or select Start Menu > Python 3.8 >
IDLE (Python 3.8 64-bit).
2. Click File > New File in the interactive mode.
3. Enter the source code in script mode.
4. Click File > Save to save the file with .py extension.
5. Click File > Run or press F5 to run the program.
Source Code:
num1 = int(input('Enter first number:'))
num2 = int(input('Enter second number:'))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = num1 % num2
print('Sum of ', num1, 'and', num2, 'is:', add)
print('Difference of ', num1, 'and', num2, 'is:', dif)
print('Product of ', num1, 'and', num2, 'is:', mul)
print('Division of ', num1, 'and', num2, 'is:', div)
print('Floor Division of ', num1, 'and', num2, 'is:', floor_div)
print('Exponent of ', num1, 'and', num2, 'is:', power)
print('Modulus of ', num1, 'and', num2, 'is:', modulus)
Output:
Enter first number:50
Enter second number:5
Sum of 50 and 5 is: 55
Difference of 50 and 5 is: 45
Product of 50 and 5 is: 250
Division of 50 and 5 is: 10.0
Floor Division of 50 and 5 is: 10
Exponent of 50 and 5 is: 312500000
Modulus of 50 and 5 is: 0
Result:
The above program is executed successfully and the output is verified.
35
Ex. No.: 16
Date: 20.09.2022
Perfect Number
Aim:
To write a Python program to find whether an inputted number is perfect or not.
Procedure:
1. Start Python IDLE by clicking on its icon on the desktop or select Start Menu > Python 3.8 >
IDLE (Python 3.8 64-bit).
2. Click File > New File in the interactive mode.
3. Enter the source code in script mode.
4. Click File > Save to save the file with .py extension.
5. Click File > Run or press F5 to run the program.
Source Code:
num_to_check = int(input("Enter any number:"))
sum_of_divisors = 0
print("---\n", sum_of_divisors)
if sum_of_divisors == num_to_check:
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")
Output:
Enter any number:28
1
2
4
7
14
---
28
The number is a Perfect number!
Result:
The above program is executed successfully and the output is verified.
36
Ex. No.: 17
Date: 20.09.2022
Armstrong Number
Aim:
To write a Python program to check if the entered number is Armstrong or not.
Procedure:
1. Start Python IDLE by clicking on its icon on the desktop or select Start Menu > Python 3.8 >
IDLE (Python 3.8 64-bit).
2. Click File > New File in the interactive mode.
3. Enter the source code in script mode.
4. Click File > Save to save the file with .py extension.
5. Click File > Run or press F5 to run the program.
Source Code:
check_no = int(input("Enter any number to check : "))
sum_of_cubes = 0
num = check_no
while num> 0:
digit = num % 10
digit_cube = digit ** 3
sum_of_cubes += digit_cube
num = int(num / 10)
print(digit,"^ 3 =", digit_cube)
print(sum_of_cubes)
if sum_of_cubes == check_no:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
Output:
Enter any number to check : 153
3 ^3 = 27
5 ^3 = 125
1 ^3 = 1
153
Armstrong Number
Result:
The above program is executed successfully and the output is verified.
37
Ex. No.: 18
Date: 26.09.2022
Factorial
Aim:
To write a Python program to find factorial of the entered number.
Procedure:
1. Start Python IDLE by clicking on its icon on the desktop or select Start Menu > Python 3.8 >
IDLE (Python 3.8 64-bit).
2. Click File > New File in the interactive mode.
3. Enter the source code in script mode.
4. Click File > Save to save the file with .py extension.
5. Click File > Run or press F5 to run the program.
Source Code:
num = int(input("Enter number for calculating factorial: "))
fact = 1
i = 1
while i<= num:
fact = fact * i
i = i + 1
print("The factorial of ", num, "=", fact)
Output:
Enter the number for calculating its factorial: 4
The factorial of 4 = 24
Result:
The above program is executed successfully and the output is verified.
38
Ex. No.: 19
Date: 26.09.2022
Fibonacci Number
Aim:
To write a Python program to enter the number of terms and to print the Fibonacci Series.
Procedure:
6. Start Python IDLE by clicking on its icon on the desktop or select Start Menu > Python 3.8 >
IDLE (Python 3.8 64-bit).
7. Click File > New File in the interactive mode.
8. Enter the source code in script mode.
9. Click File > Save to save the file with .py extension.
10. Click File > Run or press F5 to run the program.
Source Code:
i = int(input("Enter the limit:"))
x = 0
y = 1
z = 1
print("Fibonacci series")
print(x, y, end=" ")
while (z <= i):
print(z, end=" ")
x = y
y = z
z = x + y
Output:
Enter the limit:14
Fibonacci series
0 1 1 2 3 5 8 13
Result:
The above program is executed successfully and the output is verified.
39
Ex. No.: 20
Date: 27.09.2022
Palindrome
Aim:
To write a Python program to enter the string and to check if it’s palindrome or not using
loop.
Procedure:
1. Start Python IDLE by clicking on its icon on the desktop or select Start Menu > Python 3.8 >
IDLE (Python 3.8 64-bit).
2. Click File > New File in the interactive mode.
3. Enter the source code in script mode.
4. Click File > Save to save the file with .py extension.
5. Click File > Run or press F5 to run the program.
Source Code:
str_to_test = input("Enter any string: ")
full_length = len(str_to_test)
half_length = int(full_length/2)
is_palin = True
for i in range(half_length):
left_char = str_to_test[i]
right_char = str_to_test[-i-1]
if left_char != right_char:
is_palin = False
break
if is_palin:
print("String is Palindrome!")
else:
print("String is NOT Palindrome!")
Output:
Enter any string: asdffdsa
String is Palindrome!
Enter any string: asdfdsa
String is Palindrome!
Enter any string: asdfgf
String is NOT Palindrome!
Result:
The above program is executed successfully and the output is verified.
40