0% found this document useful (0 votes)
31 views

Python Lab File Shivang

This is a python lab experiment file for beginners.

Uploaded by

Shubham Kamat
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)
31 views

Python Lab File Shivang

This is a python lab experiment file for beginners.

Uploaded by

Shubham Kamat
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/ 33

PYTHON PROGRAMMING

Course Code: BTCS2404

Lab File
For
BACHELOR OF

Engineering & Technology

SCHOOL OF COMPUTING SCIENCE AND ENGINEERING

GALGOTIAS UNIVERSITY, GREATER NOIDA

UTTAR PRADESH

Student Name: SHUBHAM KUMAR

Admission No: 20SCSE1290005

Semester : IV / WINTER 2020/21


1|Pa g e
Index
Exp. Page Remarks/Sign
Date PROGRAM NAME
No. No.

1 WAP to find area of a circle.

2. WAP to convert temperature in Celcius to


Farenheit

3. WAP to Find the maximum digit and second


maximum digit in the integer

4. WAP to generate fibonacci series upto a give


number using python

5. WAP to swap two values using function


with argument using python

6. WAP Count no of Vowels in a string

7. WAP to demonstrate slicing in strings.

8. WAP to print Local time

9. WAP to Read a List of Words and Return the


Length of the Longest One[ use List Datatype]

10. WAP to Find second max and second


minimum value in an array of Ten integers

11. WAP to Find the Largest Number in a List

WAP to Unpack a given tuple.


12.

WAP to Check if a Given Key Exists in a


13.
Dictionary or Not

14. WAP to Read the Contents of a File in Reverse


Order
WAP that Reads a Text File and Counts the
15. Number of Times a Certain Letter Appears in
the Text File

Write a program for reading csv files


16.
Write a program for numpy mathemetical
17. function
Write a program for python lambda
18.
Write a program for updating element in
19. array
Write a program to demonstrate creation of
20. Set in Python for updating element in array

Write a program to demonstrate working of


21. ways to concatenate tuples using + operator

Write a program to demonstrate working of


22. ways to concatenate tuples using + operator

Write a program to import time module and


23. compare time of loop and list

Write a program for processing Jason data in


24. to dictionary in python
Experiment no. 1

Aim: - Write a python program to print all prime numbers among the interval given by user.

Code:-

n = int(input("enter the
number:")) p = int(input("enter
the number:")) for j in
range(n, p):
if j >= 2:
for i in range(2,
j+1): if i < j:
if j % i ==
0: break

else:
print(i
) break

Output: -

2|Pa ge
Experiment no. 2

Aim: - WAP to convert temperature in Celsius to Fahrenheit.


Code: -
c=input("Enter temperature in celcius")
C=float(c)
F=(1.8*C)+32
print("The temperature in Farenheit is", F)

Output: -

3|Pa ge
Experiment no. 3

Aim: - WAP to Find the maximum digit and second maximum digit in the integer.

Code: -

n = int(input("enter a number:"))
s = str(n) # in order to convert it to
string l = list(s) # in order to convert
it to list a = int(l[0])
b = int(l[1])
c = int(l[2])

if a > b:
if a > c:
max = a
if c >
b:
max2 =
c else:
max2 = b
else:
max = c
max2 =
a
else:
if b > c:
max = b
if c >
a:
max2 =
c else:
max2 = a
else:
max = c
max2 =

4|Pa ge
Output: -

5|Pa ge
Experiment no. 4

Aim: - WAP to generate Fibonacci series up to a given number using python.

Code: -

n = int(input("enter the no. up to which you want Fibonacci


series:")) a = 0
b = 1
c = 0
print(a
)
print(b
)
for i in range(0,
n+1): c = a+b
if c > n:
brea
k
print(c)

Output: -

6|Pa ge
Experiment no. 5
Aim: - write a program to swap two values using function with argument using python.
Code: -
def swap(a,b):
a=(a^b)
b=(a^b)
a=(a^b)
print('The value of x after swapping is ', a)
print('The value of y after swapping is ', b)

x=int(input('Enter x'))
y=int(input('Enter y'))

print('The value of x before swapping is ',x)


print('The value of y before swapping is ',y)

swap(x,y)

Output: -

7|Pa ge
Experiment no. 6
Aim: - WAP Count no of Vowels in a string
Code: -
str=input('Enter a word')
count=0
for i in str.lower():
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u'):
count=count+1

print(count)

Output: -

8|Pa ge
Experiment no. 7
Aim: - WAP to demonstrate slicing in strings.
Code: -
country='Argentina'
print(country[2:7])
print(country[2:])
print(country[:7])
print(country[::-1])

Output: -

9|Pa ge
Experiment no. 8
Aim: - WAP to print Local time.
Code: -
import datetime

current_time = datetime.datetime.now()

print("the current year is : ",current_time.year)


print("the current month is : ",current_time.month)
print("today's date is : ",current_time.date())
print("the current day is :",current_time.day)
print("Hour is: ", current_time.hour)
print("Microsecond ",current_time.microsecond)

print(current_time.now())

Output: -

10 | P a g e
Experiment no. 9
Aim: - WAP to Read a List of Words and Return the Length of the Longest One[ use List Datatype]
Code: -
def longest_word(a):

longest_length=len(a[0])
longest_string=a[0]

for i in a:
if(len(i)>longest_length):
longest_length=len(i)
longest_string=i

print("The largest string is ",longest_string)


print("the largest length of string is ",longest_length)

a=["Monday","Tuesday","Saturday","Argentina","Maharashtra"]
longest_word(a)

Output: -

11 | P a g e
Experiment no. 10

Aim: -WAP to Find second max and second minimum value in an array of Ten integers.

Code: -
def second_max_min(a):
length=len(a)
a.sort()

print("second maximum element ",a[length-2])


print("second smallest element ",a[1])

a=[7,3,1,8,5,98,46,34,109,67]
second_max_min(a)

Output: -

12 | P a g e
Experiment no. 11

Aim: - WAP to Find the Largest Number in a List.

Code: -
l = eval(input("enter the list in squre
brackets:")) l.sort()
print("Largest no. in the list is:", l[-1])

Output: -

13 | P a g e
Experiment no. 12
Aim: -WAP to Unpack a given tuple.

Code: -
a=("Shivang Singh",20,"lUCKNOW",168)

(name,age,city,height)=a

print(name)
print(age)
print(city)
print(height)

Output: -

14 | P a g e
Experiment no. 13
Aim: -WAP to Check if a Given Key Exists in a Dictionary or Not.
Code: -
d = {'Rohan': 4, 'Ayush': 7, 'Aditya': 9, 'Akhilendra': 12,
'Jatin': 45, 'Umad': 2, 'Shivang':
56} print("Dictionary is:", d)
n = input("enter the key that you want to
search:-") if n in d:
print("Key", n, "is in the
dictionary.") else:
print("Key", n, "is not in the dictionary.")

Output: -

15 | P a g e
16 | P a g e
Experiment no. 14
Aim: -WAP to Read the Contents of a File in Reverse Order

Code: -
myfile = open("text/normal.txt",
'r') str = myfile.read()
l = len(str)
print("reverse order of text
file:") for i in range(-1, -l-1,
-1):

Output: -

1. normal.txt

17 | P a g e
18 | P a g e
Experiment no. 15

Aim: -WAP that Reads a Text File and Counts the Number of Times a Certain Letter Appears in
the Text File.

Code: -
myfile = open("text/poem.txt",
'r') str = myfile.read()
k = len(str)
l = input("enter the letter that you want to
search:") c = 0
for i in range(0,
k): if l ==
str[i]:
c =
c+1 else:
continue
print(c)
Output: -
1.poem.txt

19 | P a g e
Experiment no. 16

Aim: - Write a program for reading csv files.

Code: -

import csv

fields = ['Name', 'Branch', 'Year', 'CGPA']

rows = [['Shivang Singh', 'CSE', '2', '9.43'],


['Devansh', 'MBBS', '5', '9.15'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]

filename = "university_records.csv"

with open(filename, 'w') as csvfile:


csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)

Output: -

20 | P a g e
Experiment no. 17

Aim: - Write a program for numpy mathametical function.

Code: -

import csv

fields = ['Name', 'Branch', 'Year', 'CGPA']

rows = [['Shivang Singh', 'CSE', '2', '9.43'],


['Devansh', 'MBBS', '5', '9.15'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]

filename = "university_records.csv"

with open(filename, 'w') as csvfile:


csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)

Output: -

21 | P a g e
Experiment no. 18

Aim: - Write a program for python lambda.

Code: -

def power(n):
return lambda a : a ** n

base = power(2)

print("Now power is set to 2")

print("8 powerof 2 = ", base(8))

base = power(5)
print("Now power is set to 5")

print("8 powerof 5 = ", base(8))

Output: -

22 | P a g e
Experiment no. 19

Aim: - Write a program for updating element in array.

Code: -

from array import *

num_array = array('i', range(1, 10))


print("num_array before update: {}".format(num_array))

index = 0
num_array[index] = -1
print("num_array after update 1: {}".format(num_array))

num_array[2:7] = array('i', range(22, 27))


print("num_array after update 2: {}".format(num_array))

Output: -

23 | P a g e
Experiment no. 20

Aim: - Write a program to demonstrate creation of Set in Python for updating element in array.

Code: -

set1 = set()
print("Initial blank Set: ")
print(set1)

set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)

String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)

set1 = set(["Geeks", "For", "Geeks"])


print("\nSet with the use of List: ")
print(set1)

Output: -

24 | P a g e
Experiment no. 21

Aim: - Write a program to demonstrate working of ways to concatenate tuples using + operator.

Code: -

test_tup1 = (1, 3, 5)
test_tup2 = (4, 6)

print("The original tuple 1 : " + str(test_tup1))


print("The original tuple 2 : " + str(test_tup2))

res = test_tup1 + test_tup2

print("The tuple after concatenation is : " + str(res))

Output: -

25 | P a g e
Experiment no. 22

Aim: - Write a program to demonstrate working of ways to concatenate tuples using + operator.

Code: -

test_tup1 = (1, 3, 5)
test_tup2 = (4, 6)

print("The original tuple 1 : " + str(test_tup1))


print("The original tuple 2 : " + str(test_tup2))

res = test_tup1 + test_tup2

print("The tuple after concatenation is : " + str(res))

Output: -

26 | P a g e
Experiment no. 23

Aim: - Write a program to import time module and compare time of loop and list.

Code: -

import time
def for_loop(n):
result = []
for i in range(n):
result.append(i**2)
return result

def list_comprehension(n):
return [i**2 for i in range(n)]

begin = time.time()
for_loop(10**6)
end = time.time()

print('Time taken for_loop:',round(end-begin,2))

begin = time.time()
list_comprehension(10**6)
end = time.time()

print('Time taken for list_comprehension:',round(end-begin,2))

Output: -

27 | P a g e
Experiment no. 23

Aim: - Write a program for processing Jason data in to dictionary in python.

Code: -

import time
def for_loop(n):
result = []
for i in range(n):
result.append(i**2)
return result

def list_comprehension(n):
return [i**2 for i in range(n)]

begin = time.time()
for_loop(10**6)
end = time.time()

print('Time taken for_loop:',round(end-begin,2))

begin = time.time()
list_comprehension(10**6)
end = time.time()

print('Time taken for list_comprehension:',round(end-begin,2))

Output: -

28 | P a g e
Experiment no. 24

Aim: - Write a program for pyplot API.


Code: -

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

Output: -

29 | P a g e
30 | P a g e

You might also like