0% found this document useful (0 votes)
6 views7 pages

Practical 2

Uploaded by

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

Practical 2

Uploaded by

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

PEN: 220843131008

PRACTICAL-2
Aim: Develop programs to learn different structures (list, dictionary, tuple)
in Python.

2.1 To write a program to find maximum from a list of numbers.

PROGRAM
nums = []
n = int(input("Enter the Size: "))
print("Enter", n, "Elements in list: ")
for i in range(n):
val = int(input())
nums.append(val)

maxi = nums[0]
for i in range(1,n):
if(maxi < nums[i]):
maxi = nums[i]

print("Maximum is:", maxi)

OUTPUT

2.2 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.

PROGRAM
nums = []
n = int(input("Enter the Size: "))
print("Enter", n, "Elements in list: ")
for i in range(n):
val = int(input())
nums.append(val)

sum = 0
1
PEN: 220843131008
while(i<n):
if(nums[i] == 13):
i+=2
continue
else:
sum+=nums[i]
i+=1

print("Sum of Elements:", sum)

OUTPUT

2.3 Write a Python program which takes a list and returns a list with the
elements “shifted left by one position” so [1, 2, 3] → [2, 3, 1], [11, 12,
13] →[12, 13, 11].

PROGRAM
nums1 = []
nums2 = []
n = int(input("Enter the Size: "))
print("Enter", n, "Elements in list: ")
for i in range(n):
val = int(input())
nums1.append(val)

for i in range(n-1):
nums2.append(nums1[i+1])

nums2.append(nums1[0])
print("Array is:", nums1)
print("After Shifting elements to left", nums2)

2
PEN: 220843131008

OUTPUT

2.4 Write a program to convert a list of characters into a string.

PROGRAM
lst = ['r', 'o', 'h', 'a', 'n']
ans = ""

for i in range(len(lst)):
ans+=lst[i]

print(ans)

OUTPUT

2.5 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
import random as r

lst1 = []
for i in range(5):
lst1.append(r.randint(1, 30)**2)

for i in range(5):
lst1.append(r.randint(1, 100))

print(lst1)

OUTPUT

3
PEN: 220843131008

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

PROGRAM
import random as r

lst1 = []
for i in range(5):
lst1.append(r.randint(1, 30)**2)

for i in range(5):
lst1.append(r.randint(1, 100))

for i in range(5):
lst1.append(r.randint(1, 30)**2)

print(lst1)

OUTPUT

2.6 Write a python program to print numbers given in the list after
removingeven numbers from it.

PROGRAM
nums1 = []
nums2 = []
n = int(input("Enter the Size: "))
print("Enter", n, "Elements in list: ")
for i in range(n):
val = int(input())
nums1.append(val)

for i in range(n):
if(nums1[i]%2 != 0):
nums2.append(nums1[i])

print("The Original list is:", nums1)


print("The list after removing even numbers:", nums2)

4
PEN: 220843131008

OUTPUT

2.7 Write a program to count the numbers of characters in the string and
storethem in a dictionary data structure.

PROGRAM
def count_characters(string):
char_count = {}
for char in string:
if char.isalnum():
char = char.lower()
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1

return char_count

input_string = input("Enter a string: ")


character_counts = count_characters(input_string)

print("Character counts:")
for char, count in character_counts.items():
print(f"{char}: {count}")

OUTPUT

5
PEN: 220843131008

2.8 Write a program to use split and join methods in the string and
trace abirthday with a dictionary data structure.

PROGRAM
dob = {"Rohan": "10/03/2004"}

name = input("Enter name:")


list1 = name.split()
for i in list1:
if i in dob.keys():
person = i
print(" ".join([person, "'s Date Of Birth is :", dob[person]]))
else:
print(str(i) + " : Sorry! No Data Found")

OUTPUT

2.9 Write a python program to sort a dictionary by value.

PROGRAM
dict = {}

n = int(input("Enter the size: "))


for i in range(n):
val = int(input("Enter the number: "))
dict[i] = val

print(dict)
dict1 = sorted(dict.values())

for i in range(n):
dict[i] = dict1[i]
print("Sorted dictionary values", dict)

OUTPUT

6
PEN: 220843131008

You might also like