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

Xii - CS - Lab Programs (2022-23)

The document outlines 20 Python programs completed as part of a practical program, including programs to check palindrome and prime numbers, generate random numbers, count characters in strings, create records using dictionaries, build menu driven programs, read and analyze text files, and interface with MySQL databases. The programs cover a range of fundamental Python concepts like functions, loops, files, and databases.

Uploaded by

Sanjay Siddharth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Xii - CS - Lab Programs (2022-23)

The document outlines 20 Python programs completed as part of a practical program, including programs to check palindrome and prime numbers, generate random numbers, count characters in strings, create records using dictionaries, build menu driven programs, read and analyze text files, and interface with MySQL databases. The programs cover a range of fundamental Python concepts like functions, loops, files, and databases.

Uploaded by

Sanjay Siddharth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

TERM 1 PRACTICAL PROGRAMS

S.NO Date Name of the Program Page Signature of the


No Teacher
1 Palindrome Number 1
2 Prime Number Using 3
Function
3 Program to generate random 5
number between 1 – 6 to
simulate the dice
4 Find the number of upper 7
case and lower case letters in
a String
5 Finding occurrence of any 9
word in a String
6 Creating records using 11
Dictionary
7 Menu Driven Program 13
8 Reading Text File 15
9 String Functions in Text File 17
10 Creating Binary File 20
11 Updating Binary File 22
12 Creating CSV File 24
13 Menu Driven Program to 26
implement Stack
14 Interface Python with 31
MySQL
15 Interface Python with 33
MySQL
16 MySQL SET 1 35
17 MySQL SET 2 37
18 MySQL SET 3 38
19 MySQL SET 4 40
20 MySQL SET 5 41
Ex.No.1 Palindrome Number

Date:

Aim:

Write a Python Program to check whether a number is a Palindrome or not

Algorithm:

Step 1: Start the Process

Step 2: Read the Number from the user and assign it into the identifier num

Step 3: Initialize num to n

Step 4: Initialize 0 to res

Step 5: Perform num modulus 10 and assign it into rem

Step 6: Perform rem+res*10 and assign it into res

Step 7: Perform num=num //10

Step 8: Repeat the steps 5, 6, and 7 until num greater than 0

Step 9: Print the Number is Palindrome, if res, n are equal

Step 10: Print the Number is not Palindrome, if res, n are not equal

Step 11: Stop the Process

1
Program Coding:

Output:

Result:

Thus the above Python Program to check whether given number is


Palindrome or not is run and verified successfully.
2
Ex.No.2 Prime Number Using Function

Date:

Aim:

Write a Python program to check if a Number is a Prime or not using


function.

Algorithm:

Step 1: Start the Process

Step 2: Read the number from the user and pass it to the function.

Step 3: Call the function to check whether the entered number is prime or not.

Step 4: If the number is prime function returns True otherwise False.

Step 5: Print the result.

Step 6: Stop the Process

3
Program Coding:

Output:

Result:

Thus the above program to check whether the given number is prime or not
using function is run and verified successfully.
4
Ex.No.3 Program to generate random number between 1 – 6
to simulate the dice

Date:

Aim:

Write a Python program to simulate the dice using random module

Algorithm:

Step 1: Start.

Step 2: Repeatedly generate random number using randint(1,6) and display the
number with equal time interval.

Step 3: Press CTRL+C to stop generating random number.

Step 4: Print the number.

Step 5: Stop.

Program Coding:

import random

import time

print("Press CTRL+C to stop the dice!!!")

play='y'

while play=='y':

try:

while True:

for i in range(10):

print()

n = random.randint(1,6)
5
print(n)

time.sleep(0.5)

except KeyboardInterrupt:

print("\nYour Number is :",n)

ans=input("Play More? (Y) :")

if ans.lower()!='y':

play='n'

break

Output:

Press CTRL+C to stop the dice!!!

Your Number is : 3

Play More? (Y) :n

Result:

Thus the above program to generate random number between 1 – 6 to


simulate the dice is run and verified successfully.

6
Ex.No.4 Find the number of upper case and lower case letters in a String

Date:

Aim:

Write a Python program to find the number of upper case and lower case
letters in a string which is passed as an argument to the function.

Algorithm:

Step 1: Start the Process

Step 2: Read string input from the user.

Step 3: Call the user defined function by passing string as an argument.

Step 4: Find the number of upper case letters and lower case letters using built
in functions

Step 5: Print the result

Step 6: Stop the Process

Program Coding:

7
Output:

Enter any String: Achyuta Public School

Original String : Achyuta Public School

No. of Upper case characters : 3

No. of Lower case characters : 16

Result:

Thus the above Python Program to find the number of uppercase, lowercase
characters in a String is run and verified successfully.

8
Ex.No.5 Finding occurrence of any word in a String

Date:

Aim:

Write a Python program to find the occurrence of any word in a string which
is passed as an argument to function.

Algorithm:

Step 1: Start the Process

Step 2: Read input string from the user.

Step 3: Read the word to be searched in the above string.

Step 4: Call user defined function by passing string and the word as an
argument.

Step 5: Split the passed string and count number of words.

Step 6: Return the count value.

Step 7: Print the result.

Step 8: Stop the Process

9
Program Coding:

Output:

Result:

Thus the above Python Program to find the occurrence of any word in a
String is run and verified successfully.

10
Ex.No.6 Creating records using Dictionary

Date:

Aim:

Write a Python program to create n records using Dictionary

Algorithm:

Step 1: Start the Process


Step 2: Read how many records from the user and assign it into n
Step 3: Read Admission no, Roll no, Name and Percentage
Step 4: Repeat the Step 3 n times
Step 5: Print the records using for loop
Step 6: Stop the Process
Program Coding:

11
Output:

Result:

Thus the above Python Program to create records using Dictionary is run
and verified successfully.
12
Ex.No.7 Menu Driven Program

Date:

Aim:

Write a Python Program to create a Menu Driven Program using Functions

Algorithm:

Step 1: Start the Process

Step 2: Display the Menu to the user and read the choice

Step 3: Call the user defined function Area by passing the choice as Parameter

Step 4: Read the side from the user and compute area of square if the choice is 1

Step 5: Print the area of square

Step 6: Read length, breadth from the user and compute area of rectangle if the

choice is 2

Step 7: Print the area of rectangle

Step 8: Read base, height from the user and compute the area of triangle if the

choice is 3

Step 9: Print the area of triangle

Step 10: Exit if the choice is 4

Step 11: Stop the Process

13
Program Coding:

Output:

Result:

Thus the above Python Program to create a menu driven using functions is
run and verified successfully.

14
Ex.No.8 Reading Text File

Date:

Aim:

Write a Python Program to read a text file line by line and display each word
separated by #

Algorithm:

Step 1: Start the Process

Step 2: Open the file in read mode

Step 3: Use for loop to traverse the file object

Step 4: Use the string function split() to split the words based on space

Step 5: Append # at the end of each word and print it

Step 6: Close the file object

Step 7: Stop the Process

Program Coding:

15
Output:

Result:

Thus the above Python program to read a text files line by line and display
each word separated by # is run and verified successfully
16
Ex.No.9 String Functions in Text File

Date:

Aim:

Write a Python Program to read a text file line by line and display the count
of vowels, consonants, uppercase, lowercase and other characters.

Algorithm:

Step 1: Start the Process

Step 2: Open the file in read mode

Step 3: Use for loop to traverse the file object

Step 4: Use the string function isupper() to find and print the uppercase count

Step 5: Use the string function islower() to find and print the lowercase count

Step 6: Check each character is in vowels list, increment and print the vowels

count if the condition is True

Step 7: Use the string function isdigit() to find and print the digit count

Step 8: Print the consonants count after performing alphabets_count –

vowels_count

Step 9: Print the characters count apart from letters and digits

Step 10: Stop the Process

17
Program Coding:

18
Output:

Result:

Thus the Python Program to read a text file line by line and display the count
of vowels, consonants, and uppercase, lowercase and other characters is run and
verified successfully.
19
Ex.No.10 Creating Binary File

Date:

Aim:

Write a Python program to create binary file and store the details of a
student.

Algorithm:

1. Start.

2. Import pickle module and open binary file in write mode.

3. Get student details from the user and store it in dictionary variable.

4. Write the record into the file using dump().

5. Open the same file in read mode and read its content using load()

6. Display the content of file.

7. Stop.

20
Program Coding:

Output:

Result:

Thus the above program is run and verified successfully.


21
Ex.No.11 Updating Binary File

Date:

Aim:

Write a Python program to create binary file and update the details of a
student.

Algorithm:

1. Start.

2. Import pickle module and open binary file in read/write mode.

3. Get the roll number of a student to identify the record.

4. Read the file using load() and identify the record to be updated.

5. Update the mark of a student and write the updated record back into the file
using dump().

6. Now display the content of updated file.

7. Stop.

22
Program Coding:

Output:

Result:

Thus the above Python Program to create binary file and update the details
of a student is run and verified successfully.
23
Ex.No.12 Creating CSV File

Date:

Aim:

Write a Python program to create csv file to store information about


commercial products.

Algorithm:

1. Start.

2. Import csv module and open file in write mode.

3. Create writer object and write the product details into the file using
writerow().

4. Open the above created file in read mode using with statement.

5. Read the file using reader object.

6. Display the contents of file.

7. Stop.

24
Program Coding:

Output:

Result:

Thus the above program to create CSV File is run and verified successfully.
25
Ex.No.13 Menu Driven Program to implement Stack

Date:

Aim:

Write a Python Program to develop a Menu Driven Program to implement


Stack Operations.

Algorithm:

Step 1: Start the Process

Step 2: Define a function to push the elements into the Stack

Step 3: Define a function to pop element from the Stack

Step 4: Define a function to display the Stack

Step 5: Define a function to check whether Stack is Empty

Step 6: Define a function to display the peek element

Step 7: Display the menu to the user

Step 8: Call the push function from main to add elements into Stack, if the
user choice is 1

Step 9: Call the pop function to remove an element from Stack, if the user
choice is 2

Step 10: Call the peek function to display the peek element, if the user
choice is 3

Step 11: Call the display function to display the Stack elements, if the user
choice is 4

Step 12: Repeat the Steps from 9 to 11 until the user exits

Step 13: Stop the Process

26
Program Coding:

27
28
Output:

29
Output:

Result:

Thus the above Python Program to create a Menu Driven Program to


implement Stack Operations is run and verified successfully.
30
Ex.No.14 Interface Python with MySQL

Date:

Aim:

Write a Python Program to establish a connection between Python and


MySQL, display the results from the table.

Algorithm:

Step 1: Start the Process

Step 2: Import the mysql.connector library

Step 3: Setup the connection using the connect() method by passing the
required arguments

Step 4: Check whether the connection is established properly using the


method is_connected()

Step 5: Create the cursor using cursor() method

Step 6: Execute the query to select all the records from the employee

Step 7: Use fetchall() method to read all the records from the cursor

Step 8: Print the records

Step 9: Stop the Process

31
Program Coding:

Output:

Result:

Thus the above Python Program to establish a connection between Python


and MySQL is run and verified successfully.
32
Ex.No.15 Database Operations using Python

Date:

Aim:

Write a Python Program to search a record in the table and update it

Algorithm:

Step 1: Start the Process

Step 2: Import the mysql.connector library

Step 3: Setup the connection using the connect() method by passing the
required arguments

Step 4: Check whether the connection is established properly using the


method is_connected()

Step 5: Create the cursor using cursor() method

Step 6: Execute the query to select all the records from the employee

Step 7: Use fetchall() method to read all the records from the cursor

Step 8: Read the employee ID from the user

Step 9: Check whether the ID exists in the table

Step 10: Increment the salary by 10000, if the ID Exists

Step 11: Print “Record Not Found”, if the ID is not there in the table

Step 12: Print the Records

Step 13: Stop the Process

33
Program Coding:

Output:

Result:

Thus the above Program to perform Database Operations is run and verified
successfully.
34
MYSQL – SET 1

Date:

1. Write a MySQL Statement to create a STUDENT table

2. View the Description of the STUDENT Table

3. Add a new attribute NATIVE into the STUDENT Table

35
4. Add a new attribute HOBBIES into the STUDENT Table

5. Drop the attribute HOBBIES from the STUDENT Table

Adding a New attribute TOTAL_MARKS into the STUDENT Table

36
MYSQL – SET 2

Date:

1. Write MYSQL Command to INSERT Values into the STUDENT Table

2. Write MYSQL Command to UPDATE Values into the STUDENT


Table

37
MYSQL – SET 3

Date:

1. Write MYSQL Command to sort the records in ascending order

2. Write MYSQL Command to sort the records in descending order

38
3. Write MYSQL Command to count the records native

wise
4. Display the Average Marks Section wise

5. Display the Maximum Secured from either Salem or Erode

39
MYSQL – SET 4

Date:

Write MYSQL Commands to demonstrate aggregate functions

40
MYSQL – SET 5

Date:

1. Display the Cartesian Product of STUDENT, STUDENT_HOBBIES

41
2. Equi Join

3. Natural Join

42

You might also like