lOMoARcPSD|36627316
1. Create a simple calculator to do all the arithmetic operation.
Aim:
To write a program for mathematical operation using calculator.
Procedure:
1. Start the program
2. Assign the two values.
3. Addition of two number is a+b
4. Subtraction of two number is a-b
5. Multiplication of two number is a*b
6. Division of two number is a/b
7. floor division of two number is a//b
8. modulo of two number is a%b
9. Exponent of two number is a**b
python code:
a=10; b=3
print("addition of a:",a,"&b:",b,"is:",a+b)
print("substraction of a:",a,"&b:",b,"is:",a-b)
print("multiplication of a:",a,"&b:",b,"is:",a*b)
print("division of a:",a,"&b:",b,"is:",a/b)
print("floor divison of a:",a,"&b:",b,"is:",a//b)
print("moduli of a:",a,"&b:",b,"is:",a%b)
print("exponent of a:",a,"&b:",b,"is:",a**b)
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
output:
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
2. Write a program to use control flow tools like if statement (Decision Making).
Aim:
To develop python program to accept the test score as input and print the Corresponding
Grade as output using if statement (Decision Making).
Procedure:
Step 1:Open the IDLE(python 3.11 64-bit) or Console of python 3.11 64-bit.
Step 2:Type the phyton code into the IDEL.
Step 3: Get the input from the keyboard when executing the python code, the input method is used
to get the inputs from the user screen.
Step 3: The received input values are assigned to the variable testScore.
Step 4: testScore value is test with the value 90 using the if statement if it is true then print the
grade is A, otherwise control transferred to the elif part.
Step 5: testScore value is test with the value 90 using the if statement if it is true then print the grade
is A, otherwise control transferred to the elif part.
Step 6: testScore value is test with the value 80 using the if statement if it is true then print the grade
is B, otherwise control transferred to the elif part.
Step 7: testScore value is test with the value 70 using the if statement if it is true then print the grade
is C, otherwise control transferred to the elif part.
Step 8: testScore value is test with the value 60 using the if statement if it is true then print the grade
is D, otherwise control transferred to the else part.
Step 9: print the grade is F.
Step 10: After completion of typing of python code save the code via the save option IDEL.
Step 11: After Storing the program execute the program with help of run option in IDEL give the
proper input and show the result.
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Python Code:
#accept a test score as input and print out the corresponding letter grade
testScore = float(input("Enter a test score from 0 to 100: "))
# conditional statement to determine and print the test letter grade
iftestScore>= 90:
print("your grade is A")}
eliftestScore>= 80:
print("your grade is B")
eliftestScore>= 70:
print("your grade is C")
eliftestScore>= 60:
print("your grade is D")
else:
print("your grade is F")
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output
Python 3.11.0 (main, Oct 24 2022, [Link]) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
===================== RESTART: C:\Users\ASUS\Desktop\[Link]
====================
Enter a test score from 0 to 100: 80
your grade is B
Enter a test score from 0 to 100: 90
your grade is A
Enter a test score from 0 to 100: 70
your grade is C
Enter a test score from 0 to 100: 60
your grade is D
Enter a test score from 0 to 100: 50
your grade is F
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
3. Write a program to use for loop.
Aim:
To develop python program to print the multiplication table for given number using for statement in
python.
Procedure:
1. Start the program
2. Create the table format of data.
3. Read table value.
4. Check the condition if the table value>0 and value<21
5. Print the value as in table form.
6. Stop the process.
Python Code:
#multiplication tables
try:
table = int(input("which multiplication times table do you want to
print? (choose from 1 to 12) “))
x = table
except:
print("ERROR: enter a whole number")
else:
if table > 0 and table < 21:
for y in range(1, 20):
print (x,'*', y,'=', x*y)
else:
print ("ERROR: multiplication tables can be generated from 1 to 20 only")
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output
RESTART: C:/Users/msc/[Link]
which multiplication times table do you want to print? (choose from 1 to 20)5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70
5 * 15 = 75
5 * 16 = 80
5 * 17 = 85
5 * 18 = 90
5 * 19 = 95
5 * 20 = 100
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
[Link] STRUCTURE
Aim:
To write a program for stack and queue operation.
Procedure for Stack using List
1. STACK: Stack is a linear data structure which works under the principle of last in
first out. Basic operations: push, pop, display.
2. PUSH: if (top==MAX), display Stack overflow. Otherwise reading the data and
making stack [top] =data and incrementing the top value by doing top++.
3. Pop: if (top==0), display Stack underflow. Otherwise printing the element at the
top of the stack and decrementing the top value by doing the top.
4. DISPLAY: If (top==0), display Stack is empty. Otherwise printing the elements in
the stack from stack [0] to stack [top].
Python code:
Program for implementing Stack using list
top=0
mymax=eval(input("Enter Maximum size of stack:"))
def createStack():
stack=[]
return stack
def isEmpty(stack):
return len(stack)==0
def Push(stack,item): [Link](item)
print("Pushed to stack",item)
def Pop(stack):
if isEmpty(stack):
return "stack underflow"
return [Link]()
stack=createStack()
while True:
print("[Link]")
print("[Link]")
print("[Link]")
print("[Link]")
ch=int(input("Enter your choice:"))
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
if ch==1:
if top<mymax:
item=input("Enter any elements:")
Push(stack,item)
top+=1 else:
print("Stack overflow") elif ch==2:
print(Pop(stack)) elif ch==3:
print(stack) else:
print("Exit") break
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output:
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
B) PROCEDURE FOR QUEUE USING LIST
1. QUEUE: Queue is a linear data structure which works under the principle of first
in first out. Basic operations: Insertion, deletion, display.
2. Inserion: if (rear==MAX), display Queue is full. Else reading data and inserting at
queue [rear], and doing rear++.
3. Deletion: if (front==rear), display Queue is empty .Else printing element at queue
[front] and doing front++.
4. Display: if (front==rear) ,display No elements in the queue .Else printing the
elements from queue[front] to queue[rear].
Python code:
Program for implementing Linear Queue using list
front=0 rear=0
mymax=eval(input("Enter maximum size of queue:")) def createQueue():
queue=[] return queue
def isEmpty(queue): return len(queue)==0
def enqueue(queue,item): [Link](item) print("Enqueued to queue",item)
def dequeue(queue): if isEmpty(queue):
return "Queue is empty" item=queue[0]
del queue[0] return item
queue=createQueue() while True:
print("[Link]") print("[Link]") print("[Link]") print("[Link]")
ch=int(input("Enter your choice:")) if ch==1:
if rear<mymax:
item=input("Enter any elements:") enqueue(queue,item)
rear+=1 else:
print("Queue is full") elif ch==2:
print(dequeue(queue)) elif ch==3:
print(queue) else:
print("Exit") break
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output:
C) TUPLE SEQUENCE
Aim:
To develop python program to create a different category of tuple.
Procedure:
1. Start the program
2. Create an empty tuple.
3. Read the values for the tuple value and normal.
4. Print the packing data in ordered form.
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
5. Print the unpacking data.
6. Stop the process.
Python Code:
# Different types of tuples
# Empty tuple
my_tuple = ()
print(my_tuple)
# Tuple having integers
my_tuple = (5, 10, 15)
print(my_tuple)
# tuple with mixed datatypes
my_tuple = (7, "Welcome", 5.3)
print(my_tuple)
# nested tuple
my_tuple = ("oddnumbers", ("evennumbers"), [1, 3, 5], (2, 4, 6))
print(my_tuple)
# tuple unpacking
my_tuple = 3, 4.6, "dog"
print(my_tuple)
# tuple unpacking is also possible
a, b, c = my_tuple
print(a) #3
print(b) # 4.6
print(c) # dog
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output
()
(5, 10, 15)
(7, 'Welcome', 5.3)
('oddnumbers', 'evennumbers', [1, 3, 5], (2, 4, 6))
(3, 4.6, 'dog')
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
4.6
dog
[Link] new module for mathematical operations and use in your program.
Aim:
To write a Python Program to find the square root of a number by Newton’s Method.
Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
4. Print the value of approx..
Program:
def
newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx
print('The square root is' ,newtonSqrt(100))
Sample Output:
The square root is 10
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
[Link] a program to read and write files. Create and delete directories.
Aim:
To develop python program to create a different category dictionary creation in python.
Procedure:
1. Syntax of [Link]: [Link]()
2. Code for python get current directory: #importing the os module import
os #to get the current working directory directory = [Link]() print(directory) ...
3. Syntax of chdir(): [Link](path)
4. Parameters: ...
5. Code to change current directory
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Python Code:
# Initializing an empty Dictionary
Dictionary = {}
print("The empty Dictionary: ")
print(Dictionary)
# Inserting key:value pairs one at a time
Dictionary[0] = 'R Programming'
Dictionary[2] = 'Python'
[Link]({ 3 : 'Dictionary'})
print("\nDictionary after addition of these elements: ")
print(Dictionary)
# Adding a list of values to a single key
Dictionary['list_values'] = 3, 4, 6
print("\nDictionary after addition of the list: ")
print(Dictionary)
# Updating values of an already existing Key
Dictionary[2] = 'Tutorial'
print("\nUpdated dictionary: ")
print(Dictionary)
# Adding a nested Key to our dictionary
Dictionary[5] = {'Nested_key' :{1 : 'Nested', 2 : 'Key'}}
print("\nAfteraddtion of a Nested Key: ")
print(Dictionary)
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output
The empty Dictionary:
{}
Dictionary after addition of these elements:
{0: 'R Programming', 2: 'Python', 3: 'Dictionary'}
Dictionary after addition of the list:
{0: 'R Programming', 2: 'Python', 3: 'Dictionary', 'list_values': (3, 4, 6)}
Updated dictionary:
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
{0: 'R Programming', 2: 'Tutorial', 3: 'Dictionary', 'list_values': (3, 4, 6)}
After addtion of a Nested Key:
{0: 'R Programming', 2: 'Tutorial', 3: 'Dictionary', 'list_values': (3, 4, 6), 5: {'Nested_key': {1:
'Nested', 2: 'Key'}}}
7. Write a program to Exceptions handling in Python
Aim:
To develop a python code to create a file with different options.
Procedure:
1. try block. The code which can throw any exception is kept inside(or enclosed in) a try block.
2. catch block. catch block is intended to catch the error and handle the exception condition.
3. throw statement.
4. Understanding Need of Exception Handling.
5. Using try , catch and throw Statement.
6. Using Multiple catch blocks.
7. Stop the process
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Python Code:
# create an empty text file
# In current directory
fp = open('[Link]', 'x')
[Link]()
# create a empty text file
fp = open('msc_1.txt', 'w')
[Link]('first line')
[Link]()
# Create File in a Specific Directory
# create a text file for writing
with open(r'E:\msc\python\[Link]', 'w') as fp:
[Link]('This isfirst line')
pass
importos
# list files a directory
print([Link](r'E:\mscn\python'))
# output ['[Link]', 'msc_1.txt', 'sales_2.txt' '[Link]']
# verify file exist
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
print([Link](r'E:\msc\python\[Link]'))
# output True
# create file if not exists.
importos
file_path = r'E:\msc\account\[Link]'
[Link](file_path):
print('file already exists')
else:
# create a file
with open(file_path, 'w') as fp:
# uncomment if you want empty file
[Link]('This is first line')
# File with exception
try:
file_path = r'E:\msc\account\[Link]'
# create file
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
with open(file_path, 'x') as fp:
pass
except:
print('File already exists')
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output
try:
a = int(input("Enter value of a:"))
b = int(input("Enter value of b:"))
c = a/b
print("The answer of a divide by b:", c)
exceptValueError:
print("Entered value is wrong")
exceptZeroDivisionError:
print("Can't divide by zero")
Output
Enter value of a:Ten
Entered value is wrong
Enter value of a:10
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Enter value of b:0
Can't divide by zero
[Link] a Python program to using class
Aim
To write a program to reverse a string word by word.
Procedure:
1. Start the program.
2. Create an empty list.
3. Split the list in two and append the list
4. Add the string in one by one in reverse order.
5. Stop
Python code:
fname="HELLO EVERYONE THIS IS PYTHON PROGRAMMING AND WE'RE PLAYING
WITH LISTS"
our_list=list() word=[Link]()
for element in word: our_list.append(element) #creating an empty list
#spliting up the list
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
print("tried sentence is:",our_list)
our_list.reverse() #method to reverse the elements in the list
print("list after the reverse()",our_list)
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Output:
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
[Link] WITH MYSQL AND CREATE ADDRESS BOOK.
Aim:
To write a program for create an address book with mySQL database connection
import sqlite3
# connecting to the database
connection = [Link]("[Link]")
# cursor
crsr = [Link]()
# print statement will execute if there
# are no errors
print("Connected to the database")
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
# close the connection
[Link]()
Output:
Connected to the database
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
10. Write a program using Regular Expression
Aim:
To write a program for regular expression.
Procdure:
1. The allowed characters are a-z,A-Z,0-9,#.
2. The first character should be a lower case alphabet symbol from a to k.
3. The second character should be a digit divisible by 3.
4. The length of identifier should be at least 2.
Regular Expression:
[a-k][3069][a-zA-Z0-9#]*
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
In [56]:
import re
s = input('Enter Identifier to validate :')
m = [Link]('[a-k][3069][a-zA-Z0-9#]*',s)
if m!= None:
print(s,'is valid Yava Identifier')
else:
print(s,'is not Yava Identifier')
Enter Identifier to validate
:a7 a7 is not Yava
Identifier
import re
s = input('Enter Identifier to validate :')
m = [Link]('[a-k][3069][a-zA-Z0-9#]*',s)
if m!= None:
print(s,'is valid Yava Identifier')
else:
print(s,'is not Yava Identifier')
Enter Identifier to validate
:zhd67 zhd67 is not Yava
Identifier
In [58]:
import re
s = input('Enter Identifier to validate :')
m = [Link]('[a-k][3069][a-zA-Z0-9#]*',s)
if m!= None:
print(s,'is valid Yava Identifier')
else:
print(s,'is not Yava Identifier')
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
Enter Identifier to
validate :a a is not Yava
Identifier
In [59]:
import re
s = input('Enter Identifier to validate :')
m = [Link]('[a-k][3069][a-zA-Z0-9#]*',s)
if m!= None:
print(s,'is valid Yava Identifier')
else:
print(s,'is not Yava Identifier')
Enter Identifier to validate
:a09@ a09@ is not Yava
Identifier
In [60]:
import re
s = input('Enter Identifier to validate :')
m = [Link]('[a-k][3069][a-zA-Z0-9#]*',s)
if m!= None:
print(s,'is valid Yava Identifier')
else:
print(s,'is not Yava Identifier')
Enter Identifier to validate :f3jkgjidu
f3jkgjidu is valid Yava Identifier
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])
lOMoARcPSD|36627316
import re
s = input('Enter Identifier to validate :')
m = [Link]('[a-k][3069][a-zA-Z0-9#]*',s)
if m!= None:
print(s,'is valid Yava Identifier')
else:
print(s,'is not Yava Identifier')
Enter Identifier to validate
:a6kk9z## a6kk9z## is valid
Yava Identifier
Downloaded by ELAVARASAN G (inboxelavarasan@[Link])