0% found this document useful (0 votes)
112 views3 pages

Assignment 1 Data Type

The document provides instructions for 3 Python assignments: 1) Create lists of data types and perform operations like concatenation and frequency counting. 2) Create sets of integers, find common/uncommon elements, and remove an element. 3) Create a dictionary with state names and COVID cases, print keys, and update a value.

Uploaded by

Sukhwinder Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
112 views3 pages

Assignment 1 Data Type

The document provides instructions for 3 Python assignments: 1) Create lists of data types and perform operations like concatenation and frequency counting. 2) Create sets of integers, find common/uncommon elements, and remove an element. 3) Create a dictionary with state names and COVID cases, print keys, and update a value.

Uploaded by

Sukhwinder Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

ASSIGNMENT-1 Data Types

Please implement by using Python.

1. Construct 2 lists containing all the available data types (integer, float,
string, complex and Boolean) and do the following. a. Create another list by
concatenating above 2 lists b. Find the frequency of each element in the
concatenated list. c. Print the list in reverse order.

Answer –#Construct 2 lists containing all the available data types (integer,
float, string, complex and Boolean)list1 =
[1,1.24,"list1",complex(5,2),True]print("list1 :", list1)

list2 = [2,3.4,"list2",complex(7,3),False]print("list2 :", list2)

#Create another list by concatenating above 2 listslistc=list1+list2print("listc


:", listc)

#Find the frequency of each element in the concatenated list.freq={}for


item in listc: if item in freq: freq[item] += 1 else: freq[item] = 1print("freq :",
freq)

#Print the list in reverse orderlistc.reverse()print("listc in reverse order:" ,


listc)

Output – 2. Create 2 Sets containing integers (numbers from 1 to 10 in one


set and 5 to 15 in other set) a. Find the common elements in above 2 Sets. b.
Find the elements that are not common. c. Remove element 7 from both the
Sets.

Answer –

#Create 2 Sets containing integers (numbers from 1 to 10 in one set and 5 to


15 in other
set)a={'1','2','3','4','5','6','7','8','9','10'}b={'5','6','7','8','9','10','11','12','13','
14','15'}print("Set a :", a)print("Set b :", b)

#Find the common elements in above 2 Sets.def common_elem(a, b): x =


set(a) y = set(b)

if (x & y): print(x & y) #(AND operator) else: print("No common


elements")common_elem(a, b)

#Find the elements that are not common.def uncommon_items(a,b): x =


set(a) y = set(b)

This study source was downloaded by 100000789086474 from CourseHero.com on 01-09-2023 00:15:51 GMT -06:00

https://www.coursehero.com/file/181063602/assignment-1-data-typedocx/
if(x ^ y): print (str(x ^ y)) #(XOR operator) else: print ("No Uncommon
elements")uncommon_items(a, b)

#Remove element 7 from both the Sets.a.remove('7')b.remove('7')print("Set


a after removing element 7 :", a)print("Set b after removing element 7 :", b)
Output –

3. Create a data dictionary of 5 states having state name as key and number
of covid-19 cases as values. a. Print only state names from the dictionary. b.
Update another country and it’s covid-19 cases in the dictionary.

Answer –

a = {'Karnataka': '6578','Mumbai': '7894','Hyderabad':'7412','Andhra


Pradesh':'5698','Tamilnadu':'5241'} print(a.keys()) a.update({'Bihar':'8732'})
print(a)

Output –

This study source was downloaded by 100000789086474 from CourseHero.com on 01-09-2023 00:15:51 GMT -06:00

https://www.coursehero.com/file/181063602/assignment-1-data-typedocx/
This study source was downloaded by 100000789086474 from CourseHero.com on 01-09-2023 00:15:51 GMT -06:00

https://www.coursehero.com/file/181063602/assignment-1-data-typedocx/
Powered by TCPDF (www.tcpdf.org)

You might also like