MB Python Manual Final
MB Python Manual Final
(Autonomous Institution)
Erode-638 052
LAB MANUAL
CUM
RECORD NOTE BOOK
BONAFIDE CERTIFICATE
……………………….. ….……………………..
Staff-in-charge Head of the Department
………………………………………..
………………………… ….……………………..
Internal Examiner External Examiner
22CSP03 - PYTHON PROGRAMMING LAB
TOTAL
EX.NO: 1(a)
Arithmetic Operators
DATE:
PROBLEM STATEMENT
The provided code stub reads two integers from STDIN, a and b. Add code to print three lines where:
1. The first line contains the sum of the two numbers.
2. The second line contains the difference of the two numbers (first - second).
3. The third line contains the product of the two numbers.
Input Format
The first line contains the first integer, a.
The second line contains the second integer, b
Output Format
Print the three lines as explained above.
Sample Input 0
3
2
Sample Output 0
5
-1
6
AIM:
To create a program that takes two integers as input and prints their sum, difference, and product on
separate lines.
PROGRAM:
a = int(input())
b = int(input())
print(a+b)
print(a-b)
print(a*b)
OUTPUT:
MARK ALLOCATION:
RESULT:
Thus the python Arithematic operator program has been executed and verified successfully.
Signature of Faculty
EX.NO: 1(b)
Logical Operators
DATE:
PROBLEM STATEMENT:
In a ball manufacturing company, there are two types of balls produced: Red balls (r) and White balls
(w). The production counts are as follows:
Write a Python code to check the availability of white balls in stock. If the number of red balls (r) is
greater than the number of white balls (w), print "White balls are out of stock". Otherwise, print "Your
order is confirmed". Ensure correct indentation for proper execution.
AIM:
PROGRAM:
r = 1000
w = 3222
if r > w:
print("White balls are out of stock")
else:
print("Your order is Confirmed")
OUTPUT :
MARK ALLOCATION
RESULT:
Thus the above python program for Logical operators has been executed and verified
successfully.
Signature of Faculty
EX.NO: 2(a)
Conditional Statement
DATE:
PROBLEM STATEMENT:
Input Format
Output Format
To determine whether a given integer is weird or not based on specific conditions and print the result
accordingly.
PROGRAM:
n=int(input())
if(n>=2 and n<=5 and n%2==0):
print("Not Weird")
elif(n>=6 and n<=20 and n%2==0):
print("Weird")
elif(n>20):
if(n%2==0):
print("Not Weird")
else:
print("Weird")
else:
print("Weird")
OUTPUT:
MARK ALLOCATION :
RESULT:
Thus the above python program for Conditional Statement has been executed and verified
successfully.
Signature of Faculty
EX.NO: 2(b)
Conditional Statement
DATE:
PROBLEM STATEMENT:
AIM:
To compares two integers and provides the corresponding message, followed by evaluating one the
integers to determine the grade message.
PROGRAM :
b = int(input())
r = int(input())
if(b<r):
print("Rob scored higher marks than Bob")
elif(b==r):
print("Bob & Rob both scored the same")
else:
print("")
OUTPUT:
MARK ALLOCATION :
RESULT:
Thus the above python program for Conditional Statement has been executed and verified
successfully.
Signature of Faculty
EX.NO: 3(a)
String Operation
DATE:
PROBLEM STATEMENT:
The citizens of Byte land regularly play a game. They have blocks each denoting some integer from 0 to
9. These are arranged together in a random manner without seeing to form different numbers keeping in
mind that the first block is never a 0. Once they form a number they read in the reverse order to check if
the number and its reverse is the same. If both are same then the player wins. We call such
numbers palindrome.
Ash happens to see this game and wants to simulate the same in the computer. As the first step he wants
to take an input from the user and check if the number is a palindrome and declare if the user wins or
not.
Input Format
The first line of the input contains T, the number of test cases. This is followed by T lines containing an
integer N.
Output Format
For each input output "wins" if the number is a palindrome and "loses" if not, in a new line.
AIM:
To create a concise program that checks if a given number is a palindrome and outputs "wins" or "loses"
accordingly for each test case.
PROBLEM:
def is_palindrome(n):
num_str = str(n)
reversed_str = num_str[::-1]
if num_str == reversed_str:
return True
else:
return False
a = int(input())
for _ in range(a):
n = int(input())
if is_palindrome(n):
print("wins")
else:
print("loses")
OUTPUT :
MARK ALLOCATION:
RESULT:
Thus the above python program for String Operation has been executed and verified
successfully.
Signature of Faculty
EX.NO: 3(b)
String Operation
DATE:
PROBLEM STATEMENT:
Sample Input :
str = 'Interesting'
substring = str[0:4]
print(substring)
Sample Output :
Inte
AIM:
To perform string slicing on a given string and print a specific substring.
PROGRAM:
var = "String"
substring = var[2::]
print(substring)
OUTPUT:
MARK ALLOCATION :
RESULT:
Thus the above python program for String Operation has been executed and verified successfully.
Signature of Faculty
EX.NO: 4(i)
Lists
DATE:
PROBLEM STATEMENT:
Given the names and grades for each student in a class of students, store them in a nested list
and print the name(s) of any student(s) having the second lowest grade.
(Note: If there are multiple students with the second lowest grade, order their names
alphabetically and print each name on a new line.)
Input Format :
Output Format :
Print the name(s) of any student(s) having the second lowest grade in. If there are multiple students, order
their names alphabetically and print each one on a new line.
AIM:
To create a Python program that takes input of student names and grades, stores them in a nested
list, and then prints the name(s) of any student(s) having the second lowest grade.
PROGRAM:
students = [ ]
for _ in range(int(input())):
name = input()
score = float(input())
students.append([name, score])
students = sorted(students, key = lambda x: x[1])
second_lowest_score = sorted(list(set([x[1] for x in students])))[1]
desired_students = []
for stu in students:
if stu[1] == second_lowest_score:
desired_students.append(stu[0])
print("\n".join(sorted(desired_students))
OUTPUT :
MARK ALLOCATION :
RESULT:
Thus the above python program for List has been executed and verified successfully.
Signature of Faculty
EX.NO: 4(ii)
Tuples
DATE:
PROBLEM STATEMENT:
Input Format:
The first line of input will contain a single integer T, denoting the number of test cases.
The first and only line of each test case contains 33 space-separated integers denoting the values of
min(A,B),min(B,C), and min(C,A)
Output Format
For each test case Output Format
For each test case, output YES if there exists any valid tuple (A,B,C), and NO otherwise.
You can print each letter of the output in any case. For example YES, yes, yEs will all be considered
equivalent.
AIM:
Develop a program that, given the minimum values of min(A,B) , min(B,C), and min(C,A) for each
test case, determines whether there exists any tuple (A,B,C) that satisfies the given conditions.
PROGRAM:
n=int(input())
for i in range(n):
a,b,c=map(int,input().split())
x=min(a,b)
y=min(b,c)
z=min(a,c)
if(x==y and y==z and x==z):
print('YES')
else:
print('NO')
OUTPUT:
MARK ALLOCATION :
RESULT:
Thus the above python program for Tuple has been executed and verified
successfully.
Signature of Faculty
EX.NO: 4(iii)
Dictionaries
DATE:
PROBLEM STAEMENT:
Write a program To create a dictionary with names and roll numbers, then print the roll numbers of
Rahul and Karan.
Input Fomat :
Create a dictionary x which contains the following names and roll Numbers ("Ram":24, "Rahul":12,
"Karan":18)
Store the roll number of Rahul in the variable y. Store the roll number of Karan in the variable f.
Output Format :
Output y and f to the console on separate lines
AIM:
To create a dictionary with names and roll numbers, then print the roll numbers of Rahul and
Karan.
PROGRAM:
x = {"Ram":24,"Rahul":12,"Karan":18}
y = x.get("Rahul")
f = x.get("Karan")
print(y)
print(f)
OUTPUT:
MARK ALLOCATION :
RESULT:
Thus the above python program for Tuple has been executed and verified
successfully.
Signature of Faculty
EX.NO: 5(a)
Functions
DATE:
PROBLEM STAEMENT:
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True,
otherwise return False.
(Note that the code stub provided reads from STDIN and passes arguments to the is_leap
function. It is only necessary to complete the is_leap function.)
Input Format :
Read , the year to test
.
Output Format:
The function must return a Boolean value (True/False). Output is handled by the provided
code stub.
AIM:
To create a Python function, `is_leap`, which takes a year as input and returns `True` if it's a leap
year and `False` otherwise.
PROGRAM:
def is_leap(year):
if(year>2000):
if(year%400==0):
return True
else:
return False
elif(year%4==0):
return True
else:
return False
year = int(input())
print(is_leap(year))
OUTPUT:
MARK ALLOCATION:
RESULT:
Thus the above python program for Function has been executed and verified
successfully.
Signature of Faculty
EX.NO: 5(b)
Functions
DATE:
PROBLEM STAEMENT:
Update the function given in the IDE to output the following to the IDE by printing from inside
the function.
A2+2*A+A*B+B2( * means multiplication)
A+B
Sample input:
35
27
41
Sample output:
64
8
81
9
25
5
AIM:
To create function in the IDE to compute and print the expressions \(A2 + 2 *A + A * B + B*2)
and (A + B).
PROGRAM:
t=3
for i in range(t):
A, B = map(int, input().split())
compute_value(A, B)
OUTPUT:
MARK ALLOCATION:
RESULT:
Thus the above python program for Function has been executed and verified
successfully.
Signature of Faculty
EX.NO: 6(a)
File Handling
DATE:
PROBLEM STATEMENT:
In the given Python code snippet, what is the purpose of the `open()` function with the specified
file path and mode, followed by the `read()` function to read the contents of the file, and finally,
the `close()` function to close the file after reading?
AIM:
To file handling in Python through opening a file with open(), reading its content with read(), and
closing it with close() in a single line of code.
PROGRAM:
fp = open('C:\Users\student\Downloads\mani.txt', 'r')
print(fp.read())
fp.close()
OUTPUT:
MARK ALLOCATION:
RESULT:
Thus the above python program for File handling has been executed and verified
successfully.
Signature of Faculty
EX.NO: 6(b)
File Handling
DATE:
PROBLEM STATEMENT:
What function in Python is used to open a file, append content to it, and then close it, as
demonstrated in the provided code?
AIM:
to open a file, append the string "This will add this line”, to it, and then close the file.
PROGRAM:
OUTPUT:
MARK ALLOCATION:
RESULT :
Thus the above python program for File handling has been executed and verified successfully.
Signature of Faculty
EX.NO: 7
Modules
DATE:
PROBLEM STATEMENT:
What is the purpose of importing a module in Python, and how does it enhance code Organization and
reusability?
AIM:
The aim of this code is to demonstrate how to import a specific variable (`person1`) from the
`mymodule` module and then access one of its attributes (`age`) for further processing or display.
PROGRAM:
#The module named mymodule has one function and one dictionary:
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
print (person1["age"])
OUTPUT:
.MARK ALLOCATION :
RESULT:
Thus the above python program for Module has been executed and verified
successfully.
Signature of Faculty
EX.NO: 8
Application using Regular Expression
DATE:
PROBLEM STATEMENT:
Write a Python script to validate email addresses using regex.
AIM:
The aim to write a Python script to validate email addresses using regex.
PROBLEM:
import re
mailid = input()
r = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
if(re.match(r,mailid)):
print("Valid email")
else:
print("Invalid email")
OUTPUT:
Mani123@gmail.com
Valid email
MARK ALLOCATION:
RESULT:
Thus the above python program for Module has been executed and verified successfully.
Signature of Faculty
EX.NO: 9
Graphical User Interface
DATE:
PROBLEM STATEMENT:
Create a tkinter GUI application that displays a button labeled "Click Me". When the button is
clicked, a message box should pop up displaying the text "Button clicked!".
This exercise should help you practice creating a basic GUI with tkinter and handling events
like button clicks. Let me know if you need help with the code!
AIM:
To create a tkinter GUI application that displays a button labeled "Click Me" and shows a message
box with the text "Button clicked!" when the button is clicked.
PROGRAM:
import tkinter as tk
def button_click():
label.config(text="Button clicked!")
root = tk.Tk()
root.geometry("400x400")
root.title("Button Click Example")
label = tk.Label(root, text="")
label.pack(pady=20)
button = tk.Button(root, text="Click Me", command=button_click)
button.pack()
root.mainloop()
OUTPUT:
MARK ALLOCATION:
RESULT:
Thus the above python program for GUI has been executed and verified successfully.
Signature of Faculty
EX.NO: 10
Numpy
DATE:
PROBLEM STATEMENT:
Input Format:
A single line of input containing space separated numbers.
Output Format:
Print the reverse Numpy array with type float.
Sample Input
1 2 3 4 -8 -10
Sample Output
[-10. -8. 4. 3. 2. 1.]
AIM:
To reverse a space-separated list of numbers and print a NumPy array with the element type float.
PROGRAM:
import numpy
import numpy as np
def arrays(arr):
return (np.array(arr[::-1],float))
OUTPUT:
MARK ALLOCATION:
RESULT:
Thus the above python program for Numpy module has been executed and verified
successfully.
Signature of Faculty