Python Record
Python Record
return x+y
def subtract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
return x/y
print("\t\tSIMPLE CALCULATOR")
print("\t\t*****************")
print("select operation")
print("*************")
print("1. Add")
print("2. sub")
print("3. multi")
print("4. div")
choice= input("Enterchoice(1/2/3/4):")
if choice=='1':
elif choice=='2':
elif choice=='3':
else:
print("invalid")
Output:-
SIMPLE ARITHMETIC CALCULAATOR
************************************
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 1
Result:-
2. PROGRAM USING CONTROL FLOW TOOLS
factorial=1
if n < 0 :
elif n==0:
else:
print("Factorial of {} is :".format(n))
for i in range(1,n+1):
factorial=factorial*i
print(factorial)
Output:-
Factorial of 4 is : 24
Result:-
3. PROGRAM USING FOR LOOP
(PROGRAM TO PRINT THE MULTIPLICATION TABLE OF A GIVEN NUMBER)
for i in range(1,21):
x=n*i
print("{} * {} = {}".format(n,i,x))
Output:-
enter the number: 4
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
4 * 11 = 44
4 * 12 = 48
4 * 13 = 52
4 * 14 = 56
4 * 15 = 60
4 * 16 = 64
4 * 17 = 68
4 * 18 = 72
4 * 19 = 76
4 * 20 = 80
Result:-
4. a. PYTHON PROGRAM TO DEMONSTRATE STACK
IMPLEMENTATION USING LIST
print(stack)
stack.append("ARTS")
stack.append("SCIENCE")
print(stack)
stack.pop()
print(stack)
stack.pop()
print(stack)
Output:-
['SRI', 'VIDYA', 'KAMACHI']
Result:-
4. b. PYTHON PROGRAM TO DEMONSTRATE QUEUE
IMPLEMENTATION USING LIST
# Queue using list
queue.append("ARTS")
queue.append("SCIENCE")
print(queue)
print(queue.pop(0))
print(queue)
print(queue.pop(0))
print(queue)
Output:-
Queue elements are:
SRI
VIDYA
Result:-
4. c. PYTHON PROGRAM TO DEMONSTRATE TUPLE AND
SEQUENCE IMPLEMENTATION
print("Tuple creation in python")
print(f'tuple 1: {tuple_1}')
print(f'tuple 2: {tuple_2}')
print(f'tuple 3: {tuple_3}')
print(f'tuple 4: {tuple_4}')
for i in sequence:
print(i)
Output:-
tuple creation in python
tuple 2: ()
Result:-
5. CREATION OF NEW MODULE FOR MATHEMATICAL
OPERATIONS
Calci.py
return a + b
return a - b
return a * b
return a / b
Mathmod.py
import Calci
10 - 5 =5
10 * 5 =50
10 / 5 =2.0
Result:-
6. CREATION AND DELETION OF NEW DIRECTORIES AND FILES
import os
import shutil
def main():
# Create a directory
directory_name = "new_directory"
os.mkdir(directory_name)
open(file_name, 'a').close()
if os.path.exists(file_name):
os.remove(file_name)
else:
if os.path.exists(directory_name):
shutil.rmtree(directory_name)
else:
print(f"The directory '{directory_name}' does not exist.")
if __name__ == "__main__":
main()
Output:-
Directory 'new_directory' created.
Result:-
7. EXCEPTION HANDLING
def divide(a, b):
try:
result = a / b
except ZeroDivisionError:
except Exception as e:
else:
finally:
divide(a,b)
Output:-
enter the value a :45
Result:-
8. PROGRAM USING CLASSES
class Rectangle:
self.length = length
self.width = width
def area(self):
def perimeter(self):
rect1 = Rectangle(5, 4)
Perimeter of rectangle: 18
Result:-
9. PROGRAM TO CONNECT WITH MYSQL AND CREATING ADDRESS BOOK.
import sys
import MySQLdb
import mysql.connector
import mysql.connector
#connect
conn =
mysql.connector.connect(host='localhost',user='root',password='',database="Address_Book")
#create a cursor
cursor = conn.cursor()
#Add Records
def Add():
var = True
while var:
cursor.execute("""
values (%s,%s,%s,%s,%s,%s,%s)""",(ssn,name,address,city,state,postcode,country))
if enter == 'y':
var = True
else:
printToConsole()
var = False
#Modify Records
def Modify():
cursor.execute("""
#Delete records
def Delete():
cursor.execute("""
where ssn=%s""",ssn)
#View Records
def List():
result = cursor.fetchall()
print(record[0],"–>",record[1],"–>",record[2],"–>",record[3],"–>",record[4])
def Exit():
sys.exit()
def printToConsole():
while True:
print('1.Add Record')
print('2.Modify Record')
print('3.Delete Record')
print('4.List Record')
print('5.Exit')
if option == 1:
Add()
elif option == 2:
Modify()
elif option == 3:
Delete()
elif option == 4:
List()
elif option == 5:
Exit()
printToConsole()
Output:-
1.Add Record
2.Modify Record
3.Delete Record
4.List Record
5.Exit
Enter the Option :1
Enter SSN number:001
Enter a Name:nithi
Enter the Address:mecheri
Enter the City: salem
Enter the State:tamilnadu
Enter the Postcode:636453
Enter the country:India
Add Another Record y/n
Enter the option:y
Enter SSN number:002
Enter a Name:rathi
Enter the Address:mettur
Enter the City: salem
Enter the State:tamilnadu
Enter the Postcode:636453
Enter the country:India
Add Another Record y/n
Enter the option:n
Result:-
10. PROGRAM USING STRING HANDLING AND REGULAR EXPRESSIONS
import re
#Check if the string starts with "The" and ends with "Spain":
x = re.search("^The.*Spain$", txt)
if x:
else:
print("No match")
#Find all lower case characters alphabetically between "a" and "m":
x = re.findall("[a-m]", txt)
print(x)
x = re.findall("\AThe", txt)
print(x)
if x:
else:
print("No match")
x = re.findall("\s", txt)
print(x)
if x:
else:
print("No match")
#Split the string at every white-space character:
x = re.split("\s", txt)
print(x)
print(x)
Output:-
STRING HANDINLING USING REGULAR EXPRESSIONS
['The']
The9rain9in9Spain
Result:-