III B.Sc(Python Manual)
III B.Sc(Python Manual)
SIMPLE CALCULATOR
# This function adds two numbers
return x + y
return x - y
return x * y
return x / y
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
try:
num1 = int(input("Enter first number: "))
except ValueError:
continue
if choice == '1':
if next_calculation == "no":
break
else:
print("Invalid Input")
OUTPUT:
2. CONTROL FLOW IF
if num=="1":
if(x==y):
else:
elif num=="2":
if x>y:
else:
elif num=="3":
if x%2:
else:
else:
else:
print (letter)
if stk==[]:
return True
else:
return False
stk.append(item)
top=len(stk)-1
def Pop(stk):
print("Underflow")
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
def Display(stk):
if isEmpty(stk):
print("Stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print (str(stk[i]))
# executable code
if __name__ == "__main__":
stk=[]
top=None
Push(stk,1)
Push(stk,2)
Push(stk,3)
Push(stk,4)
Pop(stk)
Display(stk)
OUTPUT:
4. ii) LIST AS QUEUE
def enqueue(data):
queue.insert(0,data)
def dequeue():
if len(queue)>0:
return queue.pop()
def display():
for i in range(len(queue)):
print(queue[i])
# executable code
if __name__=="__main__":
queue=[]
enqueue(5)
enqueue(6)
enqueue(9)
enqueue(5)
enqueue(3)
display()
OUTPUT:
4. iii) TUPLE AS SEQUENCE
# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
print("Tuple 1: ")
print(Tuple1)
print("\nTuple2: ")
print(Tuple2)
print(Tuple3)
print(Tuple1[1:])
print(Tuple1[::-1])
print(Tuple1[4:9])
OUTPUT:
5. NEW MODULE FOR MATHEMATICAL OPERATION
import math
def fact():
fact()
def power():
print( f"The value of {base} to the power of {exponent} is: ", result)
power()
def squareroot():
result = math.sqrt(number)
squareroot()
def Trigonometry():
angle_radians = math.radians(angle_degrees)
sin_value = math.sin(angle_radians)
cos_value = math.cos(angle_radians)
tan_value = math.tan(angle_radians)
Trigonometry()
def logrithm():
log_e = math.log(number)
print(log_e)
print(log_base)
logrithm()
OUTPUT:
6. FILE HANDLING
import os
import sys
import shutil
try:
os.makedirs(parent_path)
except:
try:
shutil.rmtree(mydir)
except:
file=open("Hello.txt",'w')
file.write(a)
file.close()
file= open("Hello.txt","r")
print()
file.close()
OUTPUT:
7. EXCEPTION HANDLING
try:
result = numerator/denominator
print(result)
except:
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
except IndexError:
try:
file1 = open("D:/Myfolder/abc.txt")
print( file1.read())
except:
def getStudentDetails(self):
def printResult(self):
print("PASS \n ")
else:
print("FAIL \n ")
print("Grade A")
elif self.percentage >= 80 and self.percentage< 90:
print("Grade B")
print("Grade C")
print("Grade D")
else:
print("Grade E")
S1=Student()
S1.getStudentDetails()
S1.printResult()
OUTPUT:
9. MY SQL AND CREATE ADDRESS BOOK
import mysql.connector
import time
db = mysql.connector.connect(
host="localhost",
user="root",
password="1234",
database="contact"
cursor = db.cursor()
cursor.execute("""
address char(100),
mobile char(15),
email char(30)
);
""")
def create_record():
cursor.execute(sql, record)
db.commit()
def search(name):
value = (name,)
cursor.execute(sql, value)
record = cursor.fetchone()
if record is None:
else:
print('Name:', record[0])
print('Address:', record[1])
print('Mobile:', record[2])
print('E-mail:', record[3])
def display_all():
def delete_record(name):
cursor.execute(sql, value)
db.commit()
if cursor.rowcount == 0:
else:
def modify_record(name):
value = (name,)
cursor.execute(sql, value)
record = cursor.fetchone()
if record is None:
else:
while True:
print("1. Name")
print("2. Address")
print("3. Mobile")
print("4. BACK")
print()
if ch == 1:
cursor.execute(sql, values)
db.commit()
elif ch == 2:
cursor.execute(sql, values)
db.commit()
elif ch == 3:
cursor.execute(sql, values)
db.commit()
elif ch == 4:
break
else:
def main():
while True:
print("\nMAIN MENU ")
print("6. EXIT")
print()
print()
if ch == 1:
create_record()
elif ch == 2:
search(name)
elif ch == 3:
display_all()
elif ch == 4:
print("DELETE RECORD")
delete_record(name)
elif ch == 5:
print("MODIFY RECORD")
modify_record(name)
elif ch == 6:
db.close()
break
else:
print("Invalid choice")
main()
OUTPUT:
10. STRING HANDLING AND REGULAR EXPRESSION
import re
string3=string1+string2
x= string1.upper()
y= string2.upper()
print(x)
print(y)
copied_string = string1[:]
rep3=string1.replace(string1,string2)
if match_object:
else:
print("Original String",line)