0% found this document useful (0 votes)
57 views28 pages

Python Lab Manual

Python

Uploaded by

zoddacc02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views28 pages

Python Lab Manual

Python

Uploaded by

zoddacc02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

PYTHON PROGRAMMING CSE-II-I

UNIT-I:

1.(a).Write a program to find the largest element among three Numbers.

Program:

# Variable definition and assignment


num1 = 25
num2 = 30
num3 = 20

# Uncomment the following lines to take input from the 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 output


print("The largest number is", largest)

output: The largest number is 30

DNR ENGINEERING &TECHNOLOGYPage 1


PYTHON PROGRAMMING CSE-II-I

1.(b).Write a Program to display all prime numbers within an interval

Program:

def prime(x, y):


prime_list = []
for i in range(x, y):
if i == 0 or i == 1:
continue
else:
for j in range(2, int(i/2)+1):
if i % j == 0:
break
else:
prime_list.append(i)
return prime_list

# Driver program
starting_range = 2
ending_range = 7
lst = prime(starting_range, ending_range)
if len(lst) == 0:
print("There are no prime numbers in this range")
else:
print("The prime numbers in this range are: ", lst)

output: The prime numbers in this range are: [2, 3, 5]

DNR ENGINEERING &TECHNOLOGYPage 2


PYTHON PROGRAMMING CSE-II-I

1.(c).Write a program to swap two numbers without using a temporary variable.

Program:
x = 10
y=5

# Code to swap 'x' and 'y'

# x now becomes 15
x=x+y

# y becomes 10
y=x-y

# x becomes 5
x=x-y
print("After Swapping: x =", x, " y =", y)

output: After Swapping: x = 5 y = 10

DNR ENGINEERING &TECHNOLOGYPage 3


PYTHON PROGRAMMING CSE-II-I

1.(d).Demonstrate the following Operators in Python with suitable examples. i) Arithmetic


Operators ii) Relational Operators iii) Assignment Operatorsiv) Logical Operators v) Bit wise
Operators vi) Ternary Operator vii) Membership Operatorsviii) Identity Operators

Program:

i) Arithmetic Operators:

x=5
y=3

print(x + y)

Output: 8

ii) Relational Operators:

iii) Assignment Operators:

iv) Logical Operators:

v) Bit wise Operators

vi) Ternary Operator

vii) Membership Operators

viii) Identity Operators 5. Write a program to add and multiply complex numbers 6.
Write a program to print multiplication table of a given number.

DNR ENGINEERING &TECHNOLOGYPage 4


PYTHON PROGRAMMING CSE-II-I

1.(e).Write a program to add and multiply complex numbers

Programme:

There are three numeric types in Python:

● int
● float
● complex

1.int Example.

x=1

y = 35656222554887711

z = -3255522

print(type(x))

print(type(y))

print(type(z))

Output: <class 'int'>

2. float Example.

x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))

DNR ENGINEERING &TECHNOLOGYPage 5


PYTHON PROGRAMMING CSE-II-I

Output: <class 'float'>

3. complex Example.

x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))

Output: <class 'complex'>

DNR ENGINEERING &TECHNOLOGYPage 6


PYTHON PROGRAMMING CSE-II-I

Unit:II

Sample Experiments:

2.(a).Write a program to define a function with multiple return values.

def my_function(x):

return 5 * x

print(my_function(3))

print(my_function(5))

print(my_function(9))

Output: 15 25 45

DNR ENGINEERING &TECHNOLOGYPage 7


PYTHON PROGRAMMING CSE-II-I

2.(b).Write a program to define a function using default arguments.

Programme:

def my_function(country = "Norway"):

print("I am from " + country)

my_function("Sweden")

my_function("India")

my_function()

my_function("Brazil")

Output: I am From Sweden

I am From India

I am From Brazil

DNR ENGINEERING &TECHNOLOGYPage 8


PYTHON PROGRAMMING CSE-II-I

2.(c).Write a program to find the length of the string without using any library functions.

Programme:

mylist = ["apple", "orange", "cherry"]

x = len(mylist)

print(x)

Output: 3

DNR ENGINEERING &TECHNOLOGYPage 9


PYTHON PROGRAMMING CSE-II-I

2.(d).Write a program to check if the substring is present in a given string or not.

Programma:

text = "Geeks welcome to the Geek Kingdom!"

if "Geek" in text:
print("Substring found!")
else:
print("Substring not found!")

if "For" in text:
print("Substring found!")
else:
print("Substring not found!")

Output: Substring found!

Substring not found!

DNR ENGINEERING &TECHNOLOGYPage 10


PYTHON PROGRAMMING CSE-II-I

2.(e).Write a program to perform the given operations on a list:

i. addition

ii. insertion

iii. Slicing

i. addition

ii. insertion

fruits = ['apple', 'banana', 'cherry']

fruits.insert(1, "orange")

print(fruits)

Output: ['apple', 'orange', 'banana', 'cherry']

iii. Slicing

b = "Hello, World!"

DNR ENGINEERING &TECHNOLOGYPage 11


PYTHON PROGRAMMING CSE-II-I

print(b[2:5])

Output: llo

2.(f).Write a program to perform any 5 built-in functions by taking any list.

Programme:

1.Example.

x = abs(-7.25)

print(x)

Output: 7.05

2.Example.

mylist = [True, True, True]

x = all(mylist)

print(x)

Output: True

3.Example.

mylist = [False, True, False]

x = any(mylist)

print(x)

DNR ENGINEERING &TECHNOLOGYPage 12


PYTHON PROGRAMMING CSE-II-I

Output: True

4.Example.

x = ascii("My name is Ståle")

print(x)

Output: ‘My name is St\e5le’

5.Example.

x = bin(36)

print(x)

# The result will always have the prefix 0b

Output: ob100100

DNR ENGINEERING &TECHNOLOGYPage 13


PYTHON PROGRAMMING CSE-II-I

UNIT-III:

3(a). Write a program to create tuples (name, age, address, college) for at least two members and
concatenate the tuples and print the concatenated tuples.

my_tuple_1 = (11, 14, 0, 78, 33, 11)

my_tuple_2 = (10, 78, 0, 56, 8, 34)

print("The first tuple is : ")

The first tuple is :

print(my_tuple_1)

(11, 14, 0, 78, 33, 11)

print("The second tuple is : ")

The second tuple is :

print(my_tuple_2)

(10, 78, 0, 56, 8, 34)

my_result = my_tuple_1 + my_tuple_2

print("The tuple after concatenation is : " )

The tuple after concatenation is :

DNR ENGINEERING &TECHNOLOGYPage 14


PYTHON PROGRAMMING CSE-II-I

print(my_result)

Output: (11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34)

3(b).Write a program to check if a given key exists in a dictionary or not.

Programme:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")

Output: if "model" in thisdict:

Yes, 'model' is one of the keys in the thisdict dictionary

DNR ENGINEERING &TECHNOLOGYPage 15


PYTHON PROGRAMMING CSE-II-I

3(c).Write a program to add a new key-value pair to an existing dictionary.

Programme:

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

thisdict["color"] = "red"

print(thisdict)

Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}

DNR ENGINEERING &TECHNOLOGYPage 16


PYTHON PROGRAMMING CSE-II-I

3.(d).Write a program to sum all the items in a given dictionary.

Programme:

car = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

x = car.items()

car["year"] = 2018

print(x)

Output: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2018)])

DNR ENGINEERING &TECHNOLOGYPage 17


PYTHON PROGRAMMING CSE-II-I

UNIT-IV:

4(a). Write a program to sort words in a file and put them in another file. The output file should
have only lower-case words, so any upper-case words from source must be lowered.

Programme:

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]

thislist.sort()

print(thislist)

Output: ['banana', 'kiwi', 'mango', 'orange', 'pineapple']

Lower-Case Upper-Case Words:

Programme:

txt = "Hello my FRIENDS"

x = txt.lower()

print(x)

Output: hello my friends

DNR ENGINEERING &TECHNOLOGYPage 18


PYTHON PROGRAMMING CSE-II-I

4(b). Python program to print each line of a file in reverse order.

Programme:1

fruits = ['apple', 'banana', 'cherry']

fruits.reverse()

print(fruits)

Output: ['cherry', 'banana', 'apple']

Programme:2

alph = ["a", "b", "c", "d"]

ralph = reversed(alph)

for x in ralph:

print(x)

Output:

DNR ENGINEERING &TECHNOLOGYPage 19


PYTHON PROGRAMMING CSE-II-I

4(c). Python program to compute the number of characters, words and lines in a file.

Programme:

txt = "I love apples, apple are my favorite fruit"

x = txt.count("apple", 10, 24)

print(x)

Output: 1

DNR ENGINEERING &TECHNOLOGYPage 20


PYTHON PROGRAMMING CSE-II-I

4(d).Write a program to create, display, append, insert and reverse the order of the items
in the array.

Programme:

fruits = ['apple', 'banana', 'cherry']

fruits.reverse()

print(fruits)

Output: ['cherry', 'banana', 'apple']

Insert the value "orange" as the second element of the fruit list:

Programme:

fruits = ['apple', 'banana', 'cherry']

fruits.insert(1, "orange")

print(fruits)

Output: ['apple', 'orange', 'banana', 'cherry']

DNR ENGINEERING &TECHNOLOGYPage 21


PYTHON PROGRAMMING CSE-II-I

4(e). Write a program to add, transpose and multiply two matrices.

Programme:

# Program to add two matrices using nested loop

X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[9,8,7],
[6,5,4],
[3,2,1]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]

for r in result:
print(r)

Output:

[10, 10, 10]


[10, 10, 10]
[10, 10, 10]

DNR ENGINEERING &TECHNOLOGYPage 22


PYTHON PROGRAMMING CSE-II-I

4(f).Write a Python program to create a class that represents a shape. Include methods to

Programme:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

Output:

John
36

DNR ENGINEERING &TECHNOLOGYPage 23


PYTHON PROGRAMMING CSE-II-I

UNIT-V:

5(a).Python program to check whether a JSON string contains complex object or not.

Programme:

import json

# a Python object (dict):


x={
"name": "John",
"age": 30,
"city": "New York"
}

# convert into JSON:


y = json.dumps(x)

# the result is a JSON string:


print(y)

Output:

{"name": "John", "age": 30, "city": "New York"}

DNR ENGINEERING &TECHNOLOGYPage 24


PYTHON PROGRAMMING CSE-II-I

5(b).Python Program to demonstrate NumPy arrays creation using array () function.

Programme:

import numpy as np

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

print(arr)

print(type(arr))

Output: [1 2 3 4 5]

<class ‘numpy .ndarry’>

DNR ENGINEERING &TECHNOLOGYPage 25


PYTHON PROGRAMMING CSE-II-I

5(c).Python program to find min, max, sum, cumulative sum of array.

Programme: 1.Min Number

x = min(5, 10)

print(x)

Output: 5

Programme: 2.Min Number

x = min("Mike", "John", "Vicky")

print(x)

Output: John

Programme: 1.Max Number

DNR ENGINEERING &TECHNOLOGYPage 26


PYTHON PROGRAMMING CSE-II-I

x = max(5, 10)

print(x)

Output: 10

Programme: 2.Max Number

x = max("Mike", "John", "Vicky")

print(x)

Output: Vicky

Programme:1 cumulative sum of array

a = (1, 2, 3, 4, 5)

x = sum(a)

print(x)

Output: 15

Programme:2

a = (1, 2, 3, 4, 5)

x = sum(a, 7)

DNR ENGINEERING &TECHNOLOGYPage 27


PYTHON PROGRAMMING CSE-II-I

print(x)

Output: 22

DNR ENGINEERING &TECHNOLOGYPage 28

You might also like