Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

MB Python Manual Final

Uploaded by

sreekumaran1102
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

MB Python Manual Final

Uploaded by

sreekumaran1102
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

NANDHA ENGINEERING COLLEGE

(Autonomous Institution)
Erode-638 052

LAB MANUAL
CUM
RECORD NOTE BOOK

22CSP03 – PYTHON PROGRAMMING LABORATORY


II - Semester

B.E COMPUTER SCIENCE & ENGINEERING

Department of Computer Science and Engineering


NANDHA ENGINEERING COLLEGE, ERODE-52
NANDHA ENGINEERING COLLEGE
(Autonomous Institution)
Erode-638 052

BONAFIDE CERTIFICATE

REGISTER NUMBER: 23CS122

Certified that this is the Bonafide Record of work done by THARUN A


of the II Semester B.E - COMPUTER SCIENCE AND ENGINEERING branch during
the Academic Year 2023 – 2024 in the 22CSP03 – PYTHON PROGRAMMING
LABORATORY.

……………………….. ….……………………..
Staff-in-charge Head of the Department

Submitted for the End Semester


Practical Examination held on

………………………………………..

………………………… ….……………………..
Internal Examiner External Examiner
22CSP03 - PYTHON PROGRAMMING LAB

Rubrics for Mark Split-up:

1. Code Execution (40%)


➢ Correctness: 20%
• Excellent (18-20): Code runs correctly and produces the expected output for all test cases.
• Good (14-17): Code runs correctly for most test cases.
• Satisfactory (10-13): Code runs but produces incorrect results for some test cases.
• Needs Improvement (0-9): Code does not run correctly or produces incorrect results for most test
cases.
➢ Completeness: 20%
• Excellent (18-20): All required features are implemented.
• Good (14-17): Most required features are implemented with minor omissions.
• Satisfactory (10-13): Some required features are missing.
• Needs Improvement (0-9): Many required features are missing.
2. Code Quality (30%)
➢ Readability: 15%
• Excellent (14-15): Code is very readable and well-organized with appropriate use of indentation
and naming conventions.
• Good (11-13): Code is readable but could use better organization.
• Satisfactory (8-10): Code is somewhat readable but lacks proper organization.
• Needs Improvement (0-7): Code is difficult to read and poorly organized.
➢ Modularity and Structure: 15%
• Excellent (14-15): Code is well-structured into functions or classes, facilitating reuse and
readability.
• Good (11-13): Code is mostly well-structured with some minor issues.
• Satisfactory (8-10): Code has some structure but could be better modularized.
• Needs Improvement (0-7): Code lacks structure and is not modular.
3. Reporting (20%)
➢ Report Structure (10%)
• Excellent (9-10): Well-organized and clear structure.
• Good (7-8): Generally clear with minor issues.
• Satisfactory (5-6): Basic structure with several issues.
• Needs Improvement (0-4): Poor structure or disorganized.
➢ Content Quality (10%)
• Excellent (9-10): Comprehensive and accurate.
• Good (7-8): Good coverage with minor errors.
• Satisfactory (5-6): Basic coverage with several errors.
• Needs Improvement (0-4): Poor coverage or inaccurate content.
4. Presentation and Communication (10%)
➢ Oral Presentation (5%)
• Excellent (5): Clear, concise, and engaging.
• Good (4): Clear but not engaging.
• Satisfactory (3): Basic and somewhat unclear.
• Needs Improvement (2): Poor presentation skills.
• Unsatisfactory (0-1): Unable to present the problems effectively.
➢ Questions Handling (5%)
• Excellent (5): Confident and accurate.
• Good (4): Generally confident with minor errors.
• Satisfactory (3): Somewhat confident but with several errors.
• Needs Improvement (2): Poor handling of questions.
• Unsatisfactory (0-1): Unable to address questions.
INDEX
PAGE
Ex.No. DATE TITLE OF EXPERIMENT MARK REMARKS
No.
Programs for demonstrating the use of different types of operators.
Implementation of Arithmetic
1 Operators.
Implementation of Logical Operators
Programs for demonstrating conditional statements.

Implementation of number is weird or


2 not weird.
Implementation of if and else to check the
highest mark.
Programs to implement various string operations.

Implementation of palindrome to check the


3 user win or lose.
Implementation of print substring using
slicing operator.
Programs for demonstrating the following
4
i. Lists
ii. Tuples
iii. Dictionaries
Implementation of finding second grade List
operations.
Implementation of finding the same elements
in a Tuple.
Implementation of print age using Dictionary.
Programs to demonstrate concepts using functions
5
Implementation of finding the year leap year
or non leap year using function
Implementation of calculating power for a
number using function.
Programs to implement applications using File handling
6
Implementation of file read operation.
Implementation of file read and append.
7 Programs to demonstrate modules.

8 Programs to implement applications using


regular expression.
9 Program to demonstrate GUI.
10 Perform data manipulation using NumPy.

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:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

• Red balls: 1000

• White balls: 3222

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:

To determine the availability of white balls in stock.

PROGRAM:

r = 1000
w = 3222
if r > w:
print("White balls are out of stock")
else:
print("Your order is Confirmed")

OUTPUT :
MARK ALLOCATION

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Given an integer,n , perform the following conditional actions:

If n is odd, print Weird


If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird

Input Format

A single line containing a positive integer n

Output Format

Print Weird if the number is weird. Otherwise, print Not Weird.


.
AIM:

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 :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Write a program which does the following


Take two integers b and r as input
• Print "Rob scored higher marks than Bob", if r is greater than b
• Print "Bob & Rob both scored the same", if both b and r are equal

The code above works as follows

If grade >= 90, then it will output: You got an A


If grade is between 80 and 90 - it will output: You got a B
If grade is less than 80 - there will be no output

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 :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Declare a string variable var


Assign the value String to it
Use string slicing to print string to the console.

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 :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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 :

The first line contains an integer, N, the number of students.


The 2N subsequent lines describe each student over N lines.
The first line contains a student's name.
The second line contains their grade.

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 :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

There are 3 hidden numbers A,B,C.


You somehow found out the values of min(A,B), min(B,C), and min(C,A).
Determine whether there exists any tuple (A,B,C) that satisfies the given values of min(A,B), min(B,C),
min(C,A).

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 :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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 :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

def compute_value(a, b):


c = a*a + 2*a*b + b*b
d=a+b
print(c)
print(d)

t=3
for i in range(t):
A, B = map(int, input().split())
compute_value(A, B)
OUTPUT:

MARK ALLOCATION:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

file = open(r'C:\Users\student\Downloads\mani.txt', 'a')


file.write("This will add this line")
file.close()

OUTPUT:
MARK ALLOCATION:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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"
}

#Import only the person1 dictionary from the module:

from mymodule import person1

print (person1["age"])

OUTPUT:
.MARK ALLOCATION :

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

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:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

RESULT:

Thus the above python program for GUI has been executed and verified successfully.

Signature of Faculty
EX.NO: 10
Numpy
DATE:

PROBLEM STATEMENT:

You are given a space separated list of number.


Your task is to print a reversed NumPy array with the element type float.

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))

arr = input().strip().split(' ')


result = arrays(arr)
print(result)

OUTPUT:
MARK ALLOCATION:

Code Execution (40 Marks)

Code Quality (30 Marks)

Report (20 Marks)

Presentation and Communication


(10 Marks)

Total (100 Marks)

RESULT:

Thus the above python program for Numpy module has been executed and verified
successfully.

Signature of Faculty

You might also like