1 SC
1 SC
Program Title:
WAP to create a list and finds its length
Input/Output Screenshots:
Source code
sizeOfDemoList = len(demoList)
Page
T&T Lab.(CS-3096), Spring 2023
print("The length of the list using the len() method is: " + str(sizeOfDemoList))
Program Title:
WAP to count even and odd numbers in the list
Input/Output Screenshots:
Source code
even_count, odd_count = 0, 0
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
Page
T&T Lab.(CS-3096), Spring 2023
Program Title:
Source code
test_list = [12, 67, 98, 34]
res = []
for ele in test_list:
sum = 0
for digit in str(ele):
sum += int(digit)
res.append(sum)
Page
T&T Lab.(CS-3096), Spring 2023
Program Title:
WAP to create a tuple and find its length.
Input/Output Screenshots:
Source code
Program Title:
WAP to create a tuple and find the index of the 3rd and 4th element
Page
T&T Lab.(CS-3096), Spring 2023
Input/Output Screenshots:
Source code
Tuple_data = (0, 1, 2, 3, 2, 3, 1, 3, 2)
# getting the index of 3
res = Tuple_data.index(3)
print('First occurrence of 1 is', res)
# getting the index of 3 after 4th
# index
res = Tuple_data.index(3, 4)
print('First occurrence of 1 after 4th index is:', res)
Input/Output Screenshots:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print(tuplex)
#Get item (4th element)of the tuple by index
item = tuplex[3]
print(item)
#Get item (4th element from last)by index negative
item1 = tuplex[-4]
print(item1)
Program Title:
WAP to get the repeated items in the tuple
Input/Output Screenshots:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
print(tuplex)
#return the number of times it appears in the tuple.
count = tuplex.count(4)
print(count)
Program Title:
WAP to find the maximum and minimum element from the set
Input/Output Screenshots:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
number_set = {5, 10, 3, 15, 2, 20}
print("Original set elements:", number_set)
print("\nMaximum value of the set:",max(number_set))
print("\nMinimum value of the set:",min(number_set))
Program Title:
WAP to find the square of each elements of the set
Input/Output Screenshots:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
num = {2,4,6,8,9,10}
res_set = set()
for x in num:
res_set.add(x*x)
print("Updated copy of set1:", res_set)
Program Title:
WAP to find the unique elements and its count in the set
Input/Output Screenshots:
Source code
def word_count(words):
Page
T&T Lab.(CS-3096), Spring 2023
word_set = set(words)
word_counts = {}
for word in word_set:
word_counts[word] = words.count(word)
return word_counts
words = ['Red', 'Orange', 'Red', 'Blue', 'Red', 'Orange', 'Green', 'Yellow', 'Green']
Program Title:
WAP script to sort (ascending and descending) a dictionary by value
Input/Output Screenshots:
Source code
import operator
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
print('Original dictionary : ',d)
sorted_d = sorted(d.items(), key=operator.itemgetter(1))
print('Dictionary in ascending order by value : ',sorted_d)
sorted_d = dict( sorted(d.items(), key=operator.itemgetter(1),reverse=True))
Page
T&T Lab.(CS-3096), Spring 2023
Program Title:
WAP to add a key to a dictionary.
Input/Output Screenshots:
Source code
d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)
Program Title:
WAP to to concatenate dictionaries to find the new one
Page
T&T Lab.(CS-3096), Spring 2023
Input/Output Screenshots:
Source code
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3): dic4.update(d)
print(dic4)
Page