CBCS Mangalore University Python Programs
CBCS Mangalore University Python Programs
prime = []
x = array('i', [])
n = int(input("Enter the no of elements: "))
print("Enter the", n, "elements")
for i in range(n):
x.append(int(input()))
print(x)
for num in x:
if num==1:
continue
for i in range(2, n-1):
if num % i == 0:
break
else:
if num not in prime:
prime.append(num)
print("Prime numbers in the given array is: ", prime, end=" ")
Program 2: Program, using user-defined functions to find the area of rectangle,
square, circle and triangle by accepting suitable input parameters from user.
def rectangle():
l = float(input("Enter the length of the rectangle: "))
b = float(input("Enter the breadth of the rectangle: "))
print("Area of the rectangle is: ", l * b, "Sq.Units")
def square():
s = float(input("Enter the side of the square: "))
print("Area of the square is: ", s * s, "Sq.Units")
def circle():
r = float(input("Enter the radius of the Circle: "))
print("Area of the circle is: ", 3.14 * r * r, "Sq.Units")
def triangle():
a = float(input("Enter the length of first side of the triangle:
"))
b = float(input("Enter the length of the second side of the
triangle: "))
c = float(input("Enter the length of the third side of the
triangle: "))
s = (a + b + c) / 2
area = ((s - a) * (s - b) * (s - c)) ** 0.5
print("Area of the triangle is: ", area, "Sq.Units")
while True:
print("Menu: \n"
"1. Rectangle "
"2. Square "
"3. Circle "
"4. Triangle "
"5. Exit ")
x = int(input("Enter your choice from 1 to 5: "))
if x == 1:
rectangle()
elif x == 2:
square()
elif x == 3:
circle()
elif x == 4:
triangle()
elif x == 5:
break;
else:
print("Invalid entry! Try again.")
Program 3: Consider a tuple t1= (1,2,5,7,9,2,4,6,8,10). Write a program to
perform following operations:
a. Print half the values of tuple in one line and the other half in the next line.
b. Print another tuple whose values are even numbers in the given tuple.
c. Concatenate a tuple t2= (11,13,15) with t1.
d. Return maximum and minimum value from this tuple
t1 = (1, 2, 5, 7, 9, 2, 4, 6, 8, 10)
print("Given tuple is: ", t1)
tp1 = t1[:5]
tp2 = t1[5:]
print("After dividing the tuple into two equal halves: ")
print("1st half: ", tp1)
print("Second half: ", tp2)
newt = list()
for i in range(len(t1)):
if t1[i] % 2 == 0:
newt.append(t1[i])
print("Even numbers in the given tuple are: ", tuple(newt))
t2 = (11, 13, 15)
print("Tuple 1: ", t1)
print("Tuple 2:", t2)
print("Concatenated tuple is: ", t1 + t2)
print("Maximum number in ", t1, " is: ", max(t1))
print("Minimum number in ", t1, " is: ", min(t1))
Program 4: Write a function that takes a sentence as input from the user and
calculates the frequency of each letter. Use a variable of dictionary type to maintain
the count.
# Main function
s1 = input("Enter a string: ")
# printing result
print("Count of all characters in " , s1 , " is :\n " , all_freq(s1))
PART B
Program 1: Program to create a class Employee with empno, name, depname,
designation, age and salary and perform the following function
a. To accept details of N employees
b. To display details of all the employees
c. To search for an employee among all employees and display the details of that
employee.
#class definition
class Emp:
#class constructor for initialization
def __init__(self):
self.sal = None
self.age = None
self.des = None
self.d_name = None
self.e_name = None
self.eno = None
def compute_area(self):
area = self.length * self.width
return area
def compute_perimeter(self):
perimeter = 2 * (self.length + self.width)
return perimeter
class Box(Rectangle):
def compute_volume(self):
volume = self.length ** 3
return volume
class LargeValueError(Exception):
"""Raised when the input value is too large"""
pass
def text_match1(text):
patterns = '[A-Z]+[a-z]+$'
word = re.search(patterns, text)
if word:
return 'Match found \n'
else:
return 'Match not found\n'
def text_match2(text):
# text that contains 'z'
regex = re.compile("z+\w*", re.IGNORECASE)
# The findall() method returns all matching strings of the regex
pattern
match_object = regex.findall(text)
# If length of match_object is not equal to zero then it contains
matched string
if len(match_object) > 0:
# looping through the list
for word in match_object:
print("The word with the z is: ", word)
else:
print("No match\n")
def text_match3(text):
patterns = '^[a-zA-Z0-9_]*$'
if re.search(patterns, text):
return 'Found a match!\n'
else:
return 'Not matched!\n'
def text_match4(ip):
string = re.sub(r'\b0+(\d)', r'\1', ip)
return string
while True:
print("Menu-\n"
"1. Find the sequences of one upper case letter followed by
lower case letters.\n"
"2. Match a word containing 'z'\n"
"3. Match a string that contains only upper and lowercase
letters, numbers, and underscores\n"
"4. Remove leading zeros from an IP address \n"
"5. Exit \n")
x = int(input("Enter Your Choice : "))
if x == 1:
str1 = input("Enter the String:")
print(text_match1(str1))
elif x == 2:
str2 = input("Enter the String:")
text_match2(str2)
elif x == 3:
str3 = input("Enter the String:")
print(text_match3(str3))
elif x == 4:
str4 = input("Enter the IP address:")
print(text_match4(str4))
elif x == 5:
break
else:
print("Wrong Input!")
Part C
Program 1: GUI program to design a Simple Calculator
# concatenation of string
expression = expression + str(num)
# Put that code inside the try block which may generate the error
try:
global expression
equation.set(total)
# Driver code
if __name__ == "__main__":
# create a GUI window
gui = Tk()
rate = float(rate_field.get())
time = int(time_field.get())
# Driver code
if __name__ == "__main__":
# Create a GUI window
root = Tk()
cursor.close()
conn.close()
OUTPUT:
mysql> create table student1(regno int primary key, name varchar (15), m1 int, m2
int, m3 int);
Query OK, 0 rows affected (0.03 sec)
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:1
Enter how many students details?2
Enter the register number:111
Enter the name: Ram
Enter M1:90
Enter M2:89
Enter M3:99
1 row inserted...
Enter the register number:222
Enter the name: John
Enter M1:80
Enter M2:88
Enter M3:77
1 row inserted...
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:2
(111, ‘Ram’, 90, 89, 99)
(222, ‘John’, 80, 88, 77)
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:1
Enter how many students details?1
Enter the register number:111
Enter the name:Kiran
Enter M1:56
Enter M2:78
Enter M3:99
Register Number already exists
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:3
Enter the register number of the student to be deleted: 555
Record Not found
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:3
Enter the register number of the student to be deleted: 222
1 row deleted...
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:2
(111, ‘Ram’, 90, 89, 99)
Operations Used...
1. Accept student details
2. Display Student Details
3. Delete Details of Student
4. Exit
Enter your choice:4
Program 4: Create a table employee (empno, name and salary) using MySql and
perform the
followings
a. To accept the details of employees and store it in database.
b. To display the details of a specific employee
c. To display employee details whose salary lies within a certain range
d. import MySQLdb
cursor.close()
conn.close()
Output:
mysql> create table employee (empno int primary key, name varchar (15), salary
int);
Query OK, 0 rows affected (0.03 sec)
Operations Used...
1. Accept Employee details
2. Display Details of a specific Employee
3. Display employee details whose salary lies within a certain range
4. Exit
Enter your choice:1
Enter how many employeesdetails?2
Enter the employee number:111
Enter the name: Ram
Enter Salary:60000
1 row inserted...
Enter the employee number:222
Enter the name: John
Enter Salary:20000
1 row inserted...
Operations Used...
1. Accept Employee details
2. Display Details of a specific Employee
3. Display employee details whose salary lies within a certain range
4. Exit
Enter your choice:2
Enter the employee number of the employee to display the details: 222
(222, ‘John’, 20000)
Operations Used...
1. Accept Employee details
2. Display Details of a specific Employee
3. Display employee details whose salary lies within a certain range
4. Exit
Enter your choice:2
Enter the employee number of the employee to display the details: 555
No records found
Operations Used...
1. Accept Employee details
2. Display Details of a specific Employee
3. Display employee details whose salary lies within a certain range
4. Exit
Enter your choice:3
(222, ‘John’, 20000)
Operations Used...
1. Accept Employee details
2. Display Details of a specific Employee
3. Display employee details whose salary lies within a certain range
4. Exit
Enter your choice:4