Python Lab Exercises for BCA 4th SEM
Python Lab Exercises for BCA 4th SEM
The Quadratic Equation ax2+bx+c=0 Where a, b, c are real numbers and a not equal to 0.
The Solution of quadratic equation is given by
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
Source Code:
number = int(input ("Enter the number of which to print the multiplication table: "))
print ("The Multiplication Table of: ", number)
for count in range(1, 11):
print (number, 'x', count, '=', number * count)
Output 1: Output 2:
Enter the number of which the user wants Enter the number of which the user wants
to print the multiplication table: 5 to print the multiplication table: 12
The Multiplication Table of: 5 The Multiplication Table of: 12
5 x 1 =5 12 x 1 = 12
5 x 2 = 10 12 x 2 = 24
5 x 3 = 15 12 x 3 = 36
5 x 4 = 20 12 x 4 = 48
5 x 5 = 25 12 x 5 = 60
5 x 6 = 30 12 x 6 = 72
5 x 7 = 35 12 x 7 = 84
5 x 8 = 40 12 x 8 = 96
5 x 9 = 45 12 x 9 = 108
5 x 10 = 50 12 x 10 = 120
Output:
Select an operation.
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice(1/2/3/4): 1
Enter first number: 10
Enter second number: 5
10.0 + 5.0 = 15.0
Let's do next calculation? (yes/no): y
Enter choice(1/2/3/4): 2
Enter first number: 10
Enter second number: 5
10.0 - 5.0 = 5.0
Let's do next calculation? (yes/no): y
Enter choice(1/2/3/4): 3
Enter first number: 10
Enter second number: 2
10.0 * 2.0 = 20.0
Let's do next calculation? (yes/no): y
Enter choice(1/2/3/4): 4
Enter first number: 10
Enter second number: 2
10.0 / 2.0 = 5.0
Let's do next calculation? (yes/no): y
Enter choice(1/2/3/4): 5
Invalid Input
Enter choice(1/2/3/4):
nums = []
print("Enter the size of list: ", end="")
tot = int(input())
print("Enter", tot, "numbers for the list: ", end="")
for i in range(tot):
[Link](int(input())) Output 1:
Enter the size of list: 5
Enter 5 numbers for the list: 6
for i in range(tot-1):
5
chk = 0
4
small = nums[i] 3
for j in range(i+1, tot): 2
if small > nums[j]: Sorted List is: 2 3 4 5 6
small = nums[j]
chk = chk + 1 Output 2:
index = j Enter the size of list: 10
if chk != 0: Enter 10 numbers for the list: 3
temp = nums[i] 5
nums[i] = small 10
66
nums[index] = temp
22
33
print("\nSorted List is: ", end="")
11
for i in range(tot): 09
print(nums[i], end=" ") 06
1
STACK: A stack is a linear data structure where data is arranged objects on over another. It stores
the data in LIFO (Last in First Out) manner.
We can perform the two operations in the stack - PUSH and POP. The PUSH operation is when we
add an element and the POP operation is when we remove an element from the stack.
Problem Solution
1. Create a class Stack with instance variable items initialized to an empty list.
2. Define methods push, pop and is_empty inside the class Stack.
3. The method push appends data to items.
4. The method pop pops the first element in items.
5. The method is_empty returns True only if items is empty.
6. Create an instance of Stack and present a menu to the user to perform operations on the
stack.
Program/Source Code
Here is the source code of a Python program to implement a stack. The program output is
shown below.
class Stack:
def init (self):
[Link] = []
def is_empty(self):
return [Link] == []
def pop(self):
return [Link]()
s = Stack()
while True:
print('push <value>')
print('pop')
print('quit')
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
[Link](int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', [Link]())
elif operation == 'quit':
break
Output:
Output 1:
push <value>
pop
quit
What would you like to do? push 10
push <value>
pop
quit
What would you like to do? push 20
push <value>
pop
quit
What would you like to do? push 30
push <value>
pop
quit
What would you like to do? pop
Popped value: 30
push <value>
pop
quit
What would you like to do? pop
Popped value: 20
push <value>
pop
quit
What would you like to do? pop
Popped value: 10
push <value>
pop
quit
What would you like to do? pop
Stack is empty.
push <value>
pop
quit
What would you like to do? quit
Part- B
1. Demonstrate usage of basic regular expression.
import re
string = input('Enter a String\n')
pattern = input('Enter a pattern to search\n')
match = [Link](pattern, string)
if match:
print("Match found!")
else:
print("Match not found.")
Output 1: Output 2:
Enter a String Enter a String
Hello World Hello World
Enter a pattern to search Enter a pattern to search
World Hi
Match found! Match not found.
import re
print("Please enter password")
# Password should be 1) One Capital Letter 2) One Special Charactern 3) One Number 4)
Length Should be 8-18
pswd = input()
reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{8,18}$"
# compiling regex
match_re = [Link](reg)
# searching regex
res = [Link](match_re, pswd)
# validating conditions
if res:
print("Valid Password")
else:
print("Invalid Password")
Output 1: Output 2:
Please enter password Please enter password
welcome Welcome@123
Invalid Password Valid Password
Dictionary = {}
print("The empty Dictionary: ")
print(Dictionary)
Dictionary[0] = 'Javatpoint'
Dictionary[2] = 'Python'
[Link]({ 3 : 'Dictionary'})
print("\nDictionary after addition of these elements: ")
print(Dictionary)
Dictionary['list_values'] = 3, 4, 6
print("\nDictionary after addition of the list: ")
print(Dictionary)
Dictionary[2] = 'Tutorial'
print("\nUpdated dictionary: ")
print(Dictionary)
Output 1:
Updated dictionary:
{0: 'Javatpoint', 2: 'Tutorial', 3: 'Dictionary', 'list_values': (3, 4, 6)}
Connect To Database
import sqlite3
conn = [Link]('[Link]')
[Link]()
# INSERT Operation
# SELECT Operation
Output:
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
# UPDATE Operation
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0
4th SEM BCA Page16
Python Lab
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
# Delete Operation
Output:
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
Source Code
from tkinter import *
import calendar
def showCalender():
gui = Tk()
[Link](background='white')
[Link]("Calender for the year")
[Link]("525x550")
year = int(year_field.get())
gui_content= [Link](year)
calYear = Label(gui,text= gui_content,font=("times", 10,))
[Link](row=5, column=1,padx=10)
[Link]()
if name ==' main ':
new = Tk()
[Link](background='white')
[Link]("Calender For Any Year")
[Link]("410x620")
cal = Label(new, text="Calender For Any Year",font=("times", 30, "bold",))
year = Label(new, text="Enter year", bg='orange',font=("times", 30, "bold",))
year_field=Entry(new,font=("times", 20, "bold",))
button = Button(new, text='Show
Calender',fg='white',bg='Orange',command=showCalender,font=("times", 20, "bold",))
Exit = Button(new, text="Exit", command=[Link],font=("times", 20, "bold",))
[Link](row=1, column=1)
[Link](row=2, column=1)
year_field.grid(row=3, column=1)
[Link](row=4, column=1)
[Link](row=5, column=1)
[Link]()
Output:
Output 1:
try block
Enter a number: 10
Enter another number: 2
else block
Division = 5.0
finally block
Out of try, except, else and finally blocks.
Output 2:
try block
Enter a number: 1
Enter another number: 0
except ZeroDivisionError block
Division by 0 not accepted
finally block
Out of try, except, else and finally blocks.
Output:
import numpy as np
import [Link] as plt
# creating the dataset
data = {'C':20, 'C++':15, 'Java':30,
'Python':35}
courses = list([Link]())
values = list([Link]())
fig = [Link](figsize = (10, 5))
# creating the bar plot
[Link](courses, values, color ='maroon',width = 0.4)
[Link]("Courses offered")
[Link]("No. of students enrolled")
[Link]("Students enrolled in different courses")
[Link]()
Output:
# Creating dataset
cars = ['AUDI', 'BMW', 'FORD', 'TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]
fig = [Link](figsize =(8, 5))
[Link](data, labels = cars)
[Link]()
# plotting Histogram
fig, ax = [Link](figsize =(8, 5))
[Link](cars, bins = [0, 25, 50, 75, 100])
[Link]()
Output:
Piechart
Histogram
import numpy as np
print('Second array:')
array2 = [Link](11,20, dtype = np.float_).reshape(3, 3)
print(array2)
# Reciprocal of array
# power of array
Output:
First Array:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
Second array:
[[11. 12. 13.]
[14. 15. 16.]
[17. 18. 19.]]