0% found this document useful (0 votes)
122 views14 pages

Python Programming Lab Manual

The document contains 12 programs written in Python. Program 1 demonstrates list methods like insert(), remove(), append(), len(), pop(), and clear(). Program 2 finds the largest of three numbers using if/elif/else. Program 3 creates a menu to perform basic math operations using functions. Program 4 uses lambda functions to double a number and add two numbers. Program 5 transposes a matrix using nested loops. Program 6 implements the pow(x,n) function. Program 7 prints the current date and time. Program 8 creates a package, sub-package and modules. Program 9 displays a welcome message using classes and objects. Program 10 demonstrates NumPy array properties. Program 11 concatenates DataFrames from different objects. Program 12 reads from and
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
122 views14 pages

Python Programming Lab Manual

The document contains 12 programs written in Python. Program 1 demonstrates list methods like insert(), remove(), append(), len(), pop(), and clear(). Program 2 finds the largest of three numbers using if/elif/else. Program 3 creates a menu to perform basic math operations using functions. Program 4 uses lambda functions to double a number and add two numbers. Program 5 transposes a matrix using nested loops. Program 6 implements the pow(x,n) function. Program 7 prints the current date and time. Program 8 creates a package, sub-package and modules. Program 9 displays a welcome message using classes and objects. Program 10 demonstrates NumPy array properties. Program 11 concatenates DataFrames from different objects. Program 12 reads from and
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

PYTHON PROGRAMMING

LABORATORY
MANUAL & RECORD
B.TECH II YEAR – 2 SEM
PROGRAM-1 Date:

Aim:

A) Create a list and perform the following methods

1) insert() 2) remove() 3) append() 4) len() 5) pop() 6) clear()

Program: a=[1,3,5,6,7,[3,4,5],"hello"]
print(a)
a.insert(3,20)
print(a)
a.remove(7)
print(a)
a.append("hi")
print(a)
len(a)
print(a)
a.pop()
print(a)
a.pop(6)
print(a)
a.clear()
print(a)

Output:
PROGRAM-2 Date:
In the program below, the three numbers are stored in num1, num2 and num3 respectively. We've used
the if...elif...else ladder to find the largest among the three and display it.

Aim:

# Python program to find the largest number among the three input numbers

# change the values of num1, num2 and num3


# for a different result
num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three numbers from user


#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

Output:
PROGRAM-3 Date:

Aim:

A) Write a program to create a menu with the following options

1. TO PERFORM ADDITITON 2. TO PERFORM SUBTRACTION

3. TO PERFORM MULTIPICATION 4. TO PERFORM DIVISION

Accepts users input and perform the operation accordingly. Use functions with arguments.

Program:

def add(a,b):
return a+b

def sub(c,d):
return c-d

def mul(e,f):
return e*f

def div(g,h):
return g/h

print("=================")
print("1. TO PERFORM ADDITION")
print("2. TO PERFORM SUBTRACTION")
print("3. TO PERFORM MULTIPLICATION")
print("4. TO PERFORM DIVISION")
print("=================")

choice = int(input("Enter Your choice: "))

if choice == 1:
a = int(input("Enter the 1st value: "))
b = int(input("Enter the 2nd value: "))
print("Result: ", add(a,b))
elif choice == 2:
c = int(input("Enter the 1st value: "))
d = int(input("Enter the 2nd value: "))
print("Result: ", sub(c,d))

elif choice == 3:
e = int(input("Enter the 1st value: "))
f = int(input("Enter the 2nd value: "))
print("Result: ", mul(e,f))

elif choice == 4:
g = int(input("Enter the 1st value: "))
h = int(input("Enter the 2nd value: "))
print("Result: ", div(g,h))

else:
print("Wrong choice")
Output:
PROGRAM-4 Date:

Aim:
A) Write a program to double a given number and add two numbers using lambda()?

Program:

double = lambda x: 2*x

print(double(5))

add = lambda x, y: x + y

print(add(5, 4))

Output:
PROGRAM-5 Date:

In Python, we can implement a matrix as a nested list (list inside a list). We can treat each element as a
row of the matrix.
For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix. The first row can be selected
as X[0]. And, the element in the first-row first column can be selected as X[0][0].
Transpose of a matrix is the interchanging of rows and columns. It is denoted as X'. The element
at ith row and jth column in X will be placed at jth row and ith column in X'. So if X is a 3x2 matrix, X' will
be a 2x3 matrix.

Aim:
# Program to transpose a matrix using a nested loop
X = [[12, 7],
[4, 5],
[3, 8]]

# find dimensions of the matrix


num_rows = len(X)
num_cols = len(X[0])

# create a result matrix with dimensions swapped


result = [[0 for x in range(num_rows)] for y in range(num_cols)]

# iterate through rows


for i in range(num_rows):
# iterate through columns
for j in range(num_cols):
result[j][i] = X[i][j]

# print the result matrix


for row in result:
print(row)
Output:
PROGRAM-6 Date:
write a python class to implement pow(x, n)

Aim:
# Python3 program to calculate pow(x,n)

# Function to calculate x raised to the power y


def power(x, y):
if (y == 0):
return 1
elif (int(y % 2) == 0):
return (power(x, int(y / 2)) *
power(x, int(y / 2)))
else:
return (x * power(x, int(y / 2)) *
power(x, int(y / 2)))

# Driver Code
x=2
y=3
print(power(x, y))
Output:
PROGRAM-7 Date:

Aim:
A) Write a python program to print date, time for today and now.

Program:
import datetime

a = datetime.datetime.today()
b = datetime.datetime.now()

print(a)
print(b)
Output:
PROGRAM-8 Date:

Aim:

A) Write a python program to create a package (college),sub-package


(alldept),modules(it,cse) and create admin and cabin function to module?

Program:

def admin():
print("hi")

def cabin():
print("hello")

admin()
cabin()
Output:
PROGRAM-9 Date:

Aim:

A) Write a python Program to display welcome to WISTM T by using classes and objects.

Program:

class Display:
def displayMethod(self):
print("Welcome to WISTM")

# Object creation
obj = Display()
obj.displayMethod()

Output:
PROGRAM-10 Date:

Aim:
A) Using a numpy module create an array and check the following:

1. Type of array 2. Axes of array

3. Shape of array 4. Type of elements in array

Program:
import numpy as np

arr = np.array([[1, 2, 3], [4, 2, 5]])

print("Array is of type:", type(arr))


print("No. of dimensions:", arr.ndim)
print("Shape of array:", arr.shape)
print("Size of array:", arr.size)
print("Array stores elements of type:", arr.dtype)
Output:
PROGRAM-11 Date:

Aim:

A) Write a python program to concatenate the dataframes with two different objects.

Program:

import pandas as pd

one = pd.DataFrame({'Name': ['teju', 'gouri'],


'age': [19, 20]}, index=[1, 2])

two = pd.DataFrame({'Name': ['suma', 'nammu'],


'age': [20, 21]}, index=[3, 4])

concatenated = pd.concat([one, two])


print(concatenated)
Output:
PROGRAM-12 Date:

Aim:

A) Write a python code to read and write data to the file

Program:

f = open("basicpython.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

#open and read the file after the overwriting:


f = open("basicpython.txt", "r")
print(f.read())

Output:

You might also like