Practical File Cs 083 BVM 2023-2024

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

BHARATH VIDYA MANDIR

SENIOR SECONDARY SCHOOL


(CBSE)
Five falls By-pass, Ilanji, Tenkasi.

(Affiliation code: 1930562)

ACADEMIC YEAR 2023 - 2024


PRACTICAL FILE
COMPUTER SCIENCE
SUBJECT CODE: 083

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 __________________________________________

This is to certify to be the bonafide work of the student in this Laboratory


and submitted for the practical examination held on ____________________ at
_______________________________________________________________.

___________________ ___________________

Principal / H. M Teacher – in - Charge

Practical Examination Marks

Internal External Total

________________________ ________________________
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

2. 30.08.2022 Menu Driven with Stack for Manipulation 5

3. 30.08.2022 Menu Driven with Stack for Data Handling 7

4. 31.08.2022 Interface Python with MySQL 10

5. 31.08.2022 SQL Queries – 1 12

6. 05.09.2022 SQL Queries – 2 15

7. 05.09.2022 SQL Queries – 3 20

8. 06.09.2022 Reading a Text File 25

9. 06.09.2022 Reading a Text File with Condition 26

10. 12.09.2022 Copying Data from One File to Another 27

11. 12.09.2022 Binary File Creation 28

12. 13.09.2022 Updating of Data in Binary File 30

13. 13.09.2022 Random Number Generator 32

14. 19.09.2022 CSV File Creation 33

15. 19.09.2022 Arithmetic Operations 35

16. 20.09.2022 Perfect Number 36

17. 20.09.2022 Armstrong Number 37

18. 26.09.2022 Factorial 38

19. 26.09.2022 Fibonacci Number 39

20. 27.09.2022 Palindrome 40

3
Ex. No.: 1
Date: 30.08.2022

Implementation of Stack Operation


Aim:
To write a program in Python to create a stack name StackVow, which takes the elements as
vowels and implement all operations (Push, POP and Traversal) on stack StackVow.
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:
StackVow = []
vowels = ['A', 'E','I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']

def push(st, elements):


for i in elements:
st.append(i)

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

Menu Driven with Stack for Manipulation


Aim:
To write a program in Python to create a stack “student” with details of student name and
their marks andwrite Operation for Push, Pop and Traversal operation using menu.
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:
stack=[]

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

Menu Driven with Stack for Data Handling


Aim:
To write a menu driven program in Python to work with stack depending on choice code
1/2/3/4 as stated below.
MENU:
1.Pushin Stack
2.Pop-out from Stack
3.DisplaytheStack
4.Exit
Display appropriate message for:1.Wrong Choice Code 2. Empty Stack
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:
stack=[]
while(True):
print(' Menu')
print(' ----\n')
print(' 1 Push in stack')
print(' 2 Popout from stack')
print(' 3 Display the stack')
print(' 4 Exit')
ch=int(input('Enter your choice code as 1/2/3/4: '))
if (ch<1 or ch>4):
print('Wrong Choice....Try Again..........')
continue
if (ch==1):
print('\nPush item to the stack ')
a=input('Enter element to push in to the stack: ')
stack.append(a)
if (ch==2):
print('\nPop items from the stack ')
if (len(stack)>0):
print("Element popped out:",stack.pop())
continue
else:
print("Can notpopout, stack is empty!")
if (ch==3):
if (len(stack)==0):

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

Push item to the stack


Enter element to push in to the stack python
Menu
----

1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 1

Push item to the stack


Enter element to push in to the stack java
Menu
----

1 Push in stack
2 Popout from stack
3 Display the stack
4 Exit
Enter your choice code as 1234 1

Push item to the stack


Enter element to push in to the stack javascript
Menu
----

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

Pop items from the stack


Element popped out: javascript
Menu
----

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

Interface Python with MySQL


Aim:
To write a program to connect Python with MySQL using database connectivity and perform
the following operations on data in database: Create a table, Insert the data, Fetch the data, update the
data, and delete the data.
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:
import mysql.connector

mydb = mysql.connector.connect(host='localhost', user="root",


passwd="bharath", database='lizzy')

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');")

myc.execute("SELECT * FROM employee;")


for i in myc:
print(i[0], i[1], i[2])

myc.execute("UPDATE employee SET desig='Accountant' WHERE


empid=121;")
print(myc.rowcount, "RECORD UPDATED.")

myc.execute ("DELETE FROM employee WHERE empid=120;")


print(myc.rowcount, "RECORD DELETED.")

myc.execute("SELECT * FROM employee;")


for i in myc:
print(i[0], i[1], i[2])

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

book_id issuedto quantity_issued


t001 Kamal 4
c001 Aravind 5
f001 Suresh 2
Table library
book_id book_name author_name publisher price type quantity
c001 FastCook Latha Kapoor EPB 355 Cookery 5
f001 The Tears William Hopkins First Pub 650 Fiction 20
t001 My First C++ Brian and Brooke EPB 350 Text 10
t002 C++ A.W.Rossaine TDH 350 Text 15
Brainworks
f002 Thunderbolt Anna Roberts First Pub 750 Fiction 50
i) To show book_id, book_name, author_name and price of books of ‘First Pub’ publisher.
ii) To display the name and price of books in ascending order of their prices.
iii) Display the price of books which has price between 300 and 500.
iv) To increase the price of all books of EPB publisher by 50.
v) To display the book_id, book_name and quantity_issued for all books which have been
issued.
vi) To display the book name and price for all books having ‘C++’ in the book name.
vii) Delete the book whose book_id is t002.
viii) Show all books whose book name started with character ‘T’
ix) SELECT COUNT(*) FROM library;
x) SELECT MAX(price) FROM library WHERE quantity >= 15;
xi) SELECT book_name, author_name FROM library WHERE publisher=’First Pub’;
xii) SELECT COUNT(DISTINCT publisher) FROM library WHERE price >=400;
xiii) SELECT COUNT(DISTINCT(publisher)) FROM library;
SQL Queries:
i) SELECT book_id, book_name, author_name, price FROM library WHERE
publisher = 'First Pub';
ii) SELECT book_name, price FROM library ORDER BY price;
iii) SELECT price FROM library WHERE price>300 AND price<500;
iv) UPDATE library SET price=price+50 WHERE publisher='EPB';
v) SELECT library.book_id, library.book_name, issued.quantity_issued
FROM library, issued WHERE library.book_id=issued.book_id;
vi) SELECT book_name, price FROM library WHERE book_name LIKE '%C++%';
vii) DELETE FROM library WHERE book_id='t002';
viii) SELECT book_name FROM library WHERE book_name LIKE 'T%';

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

no name age department dateofadm charges sex

1 Arpit 62 Surgery 21/1/98 300 M


2 Zareena 22 Ent 12/12/97 250 F
3 Kareem 32 Orthopaedic 19/2/98 200 M
4 Arun 12 Surgery 11/1/98 300 M
5 Zubin 30 Ent 12/1/98 250 M
6 Karin 16 Ent 24/2/98 250 F
7 Ankita 29 Cardiology 22/2/98 800 F
8 Zareen 45 Gynaecology 22/2/98 300 F
9 Kush 19 Cardiology 13/1/98 800 M
10 Shilpa 23 Nuclear medicine 21/2/98 NULL F
i) Write a SQL command to create the table hospital.
ii) Write a SQL command to insert the values in the table.
iii) To select all the information of patients of all cardiology department.
iv) To list the names of female patients who are in the ENT department.
v) To list names of all patients with their date of admission in ascending order.
vi) To display patients’ name, charges, age for only female patients.
vii) To count the number of patients with age < 30.
viii) Write a SQL command to remote the records of all female patients.
ix) Write a SQL command to show the details name, age and department whose name starts
with ‘A’.
x) Write a SQL command to delete the column dateofadm from hospital table.
xi) Write a SQL command to increase the charges by 10% for all ENT doctors.
xii) Write a SQL command to display the different departments.
xiii) Write a SQL command to show the maximum charges of male and female patients.
xiv) Write a SQL command to display the details of all male patients whose age is between
19-35.
xv) Write a SQL command to count number of patients in each department.
xvi) SELECT COUNT(*) FROM hospitals 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’;
SQL Queries:
i) CREATE TABLE hospital (no INT(2), name CHAR(10), age INT(2),
department VARCHAR(20), dateofadm VARCHAR(10), charges INT(5), sex
CHAR);
ii) INSERT INTO hospital VALUES (1, 'Arpit', 62, 'Surgery', '21/1/98',
300, 'M')

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:

i) CREATE TABLE hospital (no INT(2), name CHAR(10), age INT(2),


department VARCHAR(20), dateofadm VARCHAR(10), charges
INT(5), sex CHAR);
Query OK, 0 rows affected (0.010 sec)

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 (2, 'Zareena', 22, 'ENT', '12/12/97',


250, 'F');
Query OK, 1 row affected (0.002 sec)

INSERT INTO hospital VALUES (3, 'Kareem', 32, 'Orthopaedic',


'19/2/98', 200, 'M');
Query OK, 1 row affected (0.002 sec)

INSERT INTO hospital VALUES (4, 'Arun', 12, 'Surgery', '11/11/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)

INSERT INTO hospital VALUES (7, 'Ankita', 29, 'Cardiology',


'22/2/98', 800, 'F');
Query OK, 1 row affected (0.002 sec)

INSERT INTO hospital VALUES (8, 'Zareen', 45, 'Gynaecology',


'22/2/98', 300, 'F');
Query OK, 1 row affected (0.002 sec)

INSERT INTO hospital VALUES (9, 'Kush', 19, 'Cardiology',

16
'13/1/98', 800, 'M');
Query OK, 1 row affected (0.002 sec)

INSERT INTO hospital VALUES (10, 'Shilpa', 23, 'Nuclear Medicine',


'21/2/98', null, 'F');
Query OK, 1 row affected (0.002 sec)

iii) SELECT * FROM hospital WHERE department='Cardiology';


+------+--------+------+------------+-----------+---------+------+
| no | name | age | department | dateofadm | charges | sex |
+------+--------+------+------------+-----------+---------+------+
| 7 | Ankita | 29 | Cardiology | 22/2/98 | 800 | F |
| 9 | Kush | 19 | Cardiology | 13/1/98 | 800 | M |
+------+--------+------+------------+-----------+---------+------+
iv) SELECT name FROM hospital WHERE department='ENT' AND sex='F';

+---------+
| name |
+---------+
| Zareena |
| Karin |
+---------+
2 rows in set (0.000 sec)

v) SELECT name FROM hospital WHERE ORDER BY dateofadm;


+---------+
| name |
+---------+
| Arun |
| Zubin |
| Zareena |
| Kush |
| Kareem |
| Arpit |
| Shilpa |
| Ankita |
| Zareen |
| Karin |
+---------+
10 rows in set (0.000 sec)
vi) SELECT name,charges,age FROM hospital WHERE sex='F';
+---------+---------+------+
| name | charges | age |
+---------+---------+------+
| Zareena | 250 | 22 |
| Karin | 250 | 16 |
| Ankita | 800 | 29 |
| Zareen | 300 | 45 |
| Shilpa | NULL | 23 |
+---------+---------+------+
5 rows in set (0.000 sec)
vii) SELECT COUNT(*) FROM hospital WHERE age<30;
+----------+
| COUNT(*) |
+----------+

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

sno name stipend subject average division

1 Karan 400 Phy 68 1


2 Divakar 450 CS 68 1
3 Divya 300 Chem 62 2
4 Arun 350 Phy 63 1
5 Sabina 500 Math 70 1
6 John 400 Chem 55 2
7 Robert 250 Phy 64 1
8 Rubina 450 Math 68 1
9 Vikas 500 CS 62 1
10 Mohan 300 Math 57 2
i) Write a SQL command to create the table graduate.
ii) Write a SQL command to insert the values in the table.
iii) To select all the information of graduates of subject physics.
iv) To list the names of graduates who scored division first.
v) To list names of all graduates with their subjects and average in ascending order of
average.
vi) To display name of graduate whose subject is computer science and division is second.
vii) To count the number of graduates with stipend <300.
viii) Write a SQL command to remove the records of all graduates of physics.
ix) Write a SQL command to show the details name, stipend and subject whose subject ends
with ‘S’.
x) Write a SQL command to delete the column div.
xi) Write a SQL command to increase the stipend by 10% for all physics graduates.
xii) Write a SQL command to display the different subjects.
xiii) Write a SQL command to show the maximum stipend of each subject.
xiv) Write a SQL command to display the details of math graduate whose average between
50-75.
xv) Write a SQL command to count number of graduates of each subject.
xvi) SELECT COUNT(*) FROM graduate WHERE subject=’Phy’
xvii) SELECT COUNT (DISTINCT subject) FROM graduate;
xviii) SELECT MIN(stipend) FROM graduate WHERE subject=’CS’;
xix) SELECT SUM(stipend) FROM graduate WHERE subject=’Chem’;
xx) SELECT MAX(average) FROM graduate WHERE subject=’Chem’;
SQL Queries:
i) CREATE TABLE graduate(sno int(2), name char(10), stipend
int(10), subject char(10), average int(2), division int(1));
ii) INSERT INTO graduate VALUES (1,'Karan',400,'Phy',68,1);

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:

i) CREATE TABLE graduate(sno int(2), name char(10), stipend


int(10), subject char(10), average int(2), division int(1));

Query OK, 0 rows affected (0.010 sec)

ii) INSERT INTO graduate VALUES (1,'Karan',400,'Phy',68,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (2,'Divakar',450,'CS',68,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (3,'Divya',300,'Chem',62,2);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (4,'Arun',350,'Phy',63,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (5,'Sabina',500,'Math',70,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (6,'John',400,'Chem',55,2);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (7,'Robert',250,'Phy',64,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (8,'Rubina',450,'Math',68,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (9,'Vikas',500,'CS',62,1);


Query OK, 1 row affected (0.002 sec)

INSERT INTO graduate VALUES (10,'Mohan',300,'Math',57,2);


Query OK, 1 row affected (0.002 sec)

iii) SELECT * FROM graduate WHERE subject='Phy';


+------+--------+---------+---------+---------+----------+

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

Reading a Text File


Aim:
To write a program in Python by reading a text file line by line and displaying each word
separated by #.
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:
myfile = open(r"samplepratical8.txt", "r")

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

Reading a Text File with Condition


Aim:
To write a program in Python by reading a text file and displaying the number of
vowels/consonants/uppercase/lowercase characters in the file.
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:
myfile = open(r"samplepractical9.txt", "r")
file_text = myfile.read()

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)

Source File (samplepractical9.txt):


Welcome to Python file handling concept
now we interact with file
Output:
Number of Vowels: 19
Number of Consonants: 36
Number of Upper case: 2
Number of Lower case: 53

Result:
The above program is executed successfully and the output is verified.

26
Ex. No.: 10
Date: 12.09.2022

Copying Data from One File to Another


Aim:
To write a Python program to remove all the lines that contain the character ‘a’ in a file and
write it to another file.
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:
source_file = open(r"pratical_10_source_file.txt", "r+")
output_file = open(r"pratical_10_output_file.txt", "w")

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()

Source File (pratical_10_source_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.

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

Binary File Creation


Aim:
To write a Python program that creates a binary file with name and roll number, searches for
a given roll number and displays the name oran appropriate message if not found.
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:
import pickle

my_file = open(r"sample.dat", "wb+")

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

Updating of Data in Binary File


Aim:
To write a Python program to create a binary file with roll number, name and marks, input a
roll number and update the marks.
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 (part1.py):
import pickle

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]
]

my_file = open(r"student.dat", "wb")


pickle.dump(marks_db, my_file)
print("Write Successful.")
my_file.close()

Source Code (part2.py):


import pickle

myfile = open(r"student.dat", "rb+")


marks_db = pickle.load(myfile)

find_roll = int(input("Enter roll no you want to update:"))


new_mark = int(input("New mark:"))

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

Random Number Generator


Aim:
To write a random number generator Python program to generate random numbers between 1
and 6 (simulatesa dice).
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:
import random
choice = "y"
while choice == "y":
print("Random number:", random.randint(1, 6))
choice = input('Would you like to continue y/n?')

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

CSV File Creation


Aim:
To write a Python program to create a CSV file by entering user-id and password, read and
search the password for given user-id.
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:
import csv

heading = ["id", "password"]


user_list = [["userid1", "pass1"],
["userid2", "pass2"],
["userid3", "pass3"],
["userid4", "pass4"],
["userid5", "pass5"],
]

my_csv_file = open(r"sample.csv", "w+", newline="\r\n")


my_writer = csv.writer(my_csv_file)
my_writer.writerow(heading)
my_writer.writerows(user_list)

# Search the password for given user id:

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

for i in range(1, num_to_check):


if num_to_check % i == 0:
print(i)
sum_of_divisors += i

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

You might also like