Practical:-2 Aim:-Develop Programs To Learn Different Types of Structures (List, Dictionary, Tuples) in Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

PAN:-200840131505

Practical:-2
Aim:- Develop programs to learn different types of structures (list, dictionary, tuples) in
python
PAN:-200840131505

Practical:-2.1
Aim:- To write a Python Program to find the maximum from a list of numbers.

Program:-

lis = []

count = int(input('How many numbers? '))

for n in range(count):
number = int(input('Enter number: '))
lis.append(number)

print("Largest element of the list is :", max(lis))

Output:-
PAN:-200840131505

Practical:-2.2
Aim:- Write a Python program which will return the sum of the numbers in the array,
returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count
and number that come immediately after 13 also do not count. Example : [1, 2, 3, 4] = 10 [1,
2, 3, 4, 13] = 10 [13, 1, 2, 3, 13] = 5

Program:-

def sum13(nums):
count=0
while count < len(nums):
if nums[count] == 13:
del nums[count:count+2]
continue
count +=1
return sum(nums)
print(sum13([1,2,2,1,13]))
print(sum13([13,2,2,1,13]))

Output:-
PAN:-200840131505

Practical:-2.2
Aim:- Write a Python program which takes a list and returns a list with the elements
"shifted left by one position" so [1, 2, 3] yields [2, 3, 1]. Example: [1, 2, 3] → [2, 3, 1] [11,
12, 13] → [12, 13, 11]

Program:-

values = [11,33,44]
def shift(list):
new_list=[]
for i in list:
new_list.insert(len(new_list)-1,i)
return new_list
print(shift(values))

Output:-
PAN:-200840131505

Practical:-2.3
Aim:- Write a program to convert a list of characters into a string.

Program:-

def convert(s):
new = ""
for x in s:
new += x
return new
s = ['y', 'a', 's', 'h', ' ','p', 'r', 'a', 'j', 'a', 'p', 'a', 't', 'i']
print(convert(s))

Output:-
PAN:-200840131505

Practical:-2.4
Aim:- Write a Python program

1) To generate a list except for the first 5 elements, where the values are square of numbers
between 1 and 30(both included)

Program:-

def printValues():
l = list()
for i in range(1,31):
l.append(i**2)
print(l[0:])
printValues()

Output:-
PAN:-200840131505

2) To generate a list of first and last 5 elements where the values are square of numbers btween 1
and 30.

Program:-

def printValues():

l = list()
for i in range(1,31):
l.append(i**2)
print(l[:30])
print(l[0:31])

printValues()

Output:-
PAN:-200840131505

Practical:-2.5
Aim:- Write a python program to print numbers given in the list after removing even numbers
from it.

Program:-
list = [11, 22, 33, 44, 55]
print ("Original list:")
print (list)
for i in list:
if(i%2 == 0):
list.remove(i)
print ("list after removing EVEN numbers:")
print (list)

Output:-
PAN:-200840131505

Practical:-2.6
Aim:- Write a program to count the numbers of characters in the string and store them in a
dictionary data structure.

Program:-

str=input("enter string : ")


f = {}
for i in str:
if i in f:
f[i] += 1
else:
f[i] = 1
print(f)

Output:-
PAN:-200840131505

Practical:-2.7
Aim:- Write a program to use split and join methods in the string and trace a birthday with a
dictionary data structure.

Program:-

a="hi !!!! my name is yash"


print("splite-------------")
b=a.split()
print (b)
print("join---------------")
c=" ".join(b)
print(c)

Output:-
PAN:-200840131505

Practical:-2.8
Aim:- Write a python program to sort a dictionary by value

Program:-

dt = {5:405, 1:62, 6:30}

sorted_dt = {key: value for key, value in sorted(dt.items(), key=lambda item: item[1])}

print(sorted_dt)

Output:-

You might also like