Practical 2
Practical 2
Practical 2
PRACTICAL-2
Aim: Develop programs to learn different structures (list, dictionary, tuple)
in Python.
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]
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
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
PROGRAM
lst = ['r', 'o', 'h', 'a', 'n']
ans = ""
for i in range(len(lst)):
ans+=lst[i]
print(ans)
OUTPUT
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])
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
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"}
OUTPUT
PROGRAM
dict = {}
print(dict)
dict1 = sorted(dict.values())
for i in range(n):
dict[i] = dict1[i]
print("Sorted dictionary values", dict)
OUTPUT
6
PEN: 220843131008