Python for Data Science: Laboratory Programs in Python: Basics of Python: Python Programs using different data frames like list, tuple, set and dictionary
Ex. 1
Write a program to iterate through list using enumerate() function.
Solution:
myList =[10,20,30]
for item in enumerate(myList):
print(item)
Output
(0, 10)
(1,20)
(2, 30)
Ex. 2
Write a python program to create a list of even numbers from 0 to 10.
even = [] #creating empty list
for i in range(11):
if i%2= = 0:
even.append(i)
print("Even Numbers List: ",even)
even = [] #creating empty list
for i in range(11):
if i%2= = 0:
even.append(i)
print("Even Numbers List: ",even)
Even Numbers List: [0, 2, 4, 6, 8, 10]
Program explanation: In above program,
1) We have created an empty list first.
2) Then using the range of numbers from 0 to 11 we append the empty list with even numbers. The even number is test using the if condition. i.e. if I %2==0. If so then that even number is appended in the list.
3) Finally the comprehended list will be displayed using print statement.
Write a python program to combine and print two lists using list comprehension.
print([(x,y)for x in['a','b'] for y in ['b','d'] if x!=y])
[('a', 'b'), ('a', 'd'), ('b', 'd')]
Ex. 4
To accept N numbers from user. Compute and display maximum in list, minimum in list, sum and average of numbers.
Solution :
mylist = [] # start an empty list
print("Enter the value of N: ")
N = int(input()) # read number of element in the list
for i in range(N):
print("Enter the element: ")
new_element = int(input()) # read next element
mylist.append(new_element) # add it to the list
print("The list is: ")
print(mylist)
print("The maximum element from list is: ")
max_element=mylist[0]
for i in range(N): #iterating throu list
if(mylist[i]>max_element):
max_element=mylist [i] #finding the maximum element
print(max_element)#printing the maximum element
print("The minimum element from list is: ")
min_element=mylist[0]
for i in range(N): #iterating throu list
if(mylist[i]<min_element):
min_element=mylist [i] #finding the minimum element
print(min_element)#printing the minimum element
print("The sum of all the numbers in the list is: ")
sum=0
for i in range(N): #iterating throu list
sum sum+mylist[i] # finding the sum
print(sum)
avg=sum/N # computing the average
print("The Average of all the numbers in the list is: ",avg)
Enter the value of N:
5
Enter the element:
33
Enter the element:
22
Enter the element:
55
Enter the element:
11
Enter the element:
44
The list is:
[33, 22, 55, 11, 44]
The maximum element from list is:
55
The minimum element from list is:
11
The sum of all the numbers in the list is:
165
The Average of all the numbers in the list is: 33.0
>>>
Tuple - Python Programs using data frame
The element in Tuple can be accessed using the index.
Ex. 5
For example ‒
>>> t1=(10,20,'AAA', 'BBB')
>>> print(t1[0])
10
>>> print(t1[1:3])
(20, 'AAA')
>>>
>>> t1=(10,20,30,40)
>>> print(len(t1))
4
>>>
>>> t1=(10,20,30)
>>>t2=(40,50,60)
>>> print(t1+t2)
(10, 20, 30, 40, 50, 60)
>>>
Repetition
>>> t1=(10,20,30)
>>> print(t1*3)
(10, 20, 30, 10, 20, 30, 10, 20, 30)
>>>
Membership
>>> t1=(10,20,30,40,50)
>>> print(3 in t1)
False
>>> print(30 in t1)
True
>>>
Iteration
In [16]:
t1 = (10,20,30,40,50)
for i in t1:
print(i)
10
20
30
40
50
Ex. 6
Built in Tuple Function
>>>t1=(10,20)
>>>t2=(10,20,30)
>>>print(cmp(t1, t2)
>>>‒1

In [17]:
t1=(10,20,30)
print(len(t1))
3
In [18]:
print(max(t1))
30
In [19]:
print(min(t1))
10
>>> t1=tuple()
>>> print(t1)
( )
>>>
Another example
>>> t1=tuple("hello")
>>> print(t1)
('h', 'e', '1', '1', 'o')
>>>
Set - Python Programs using data frame
Ex. 7
Accessing
Values in Set
A = {10,20,30,40}
for i in A:
print(i)
40
10
20
30
Ex. 8
Deleting
Values in Set
In [1]:
A = {10,20,30,40}
A.remove(20)
print (A)
{40, 10, 30}
In [2]:
A = {10, 20, 30, 40,50}
A.discard(30)
print (A)
{50, 20, 40, 10}
In [3]:
A = {10,20,30,40,50}
del A
print(A)
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
Traceback (most recent call last)
NameError
Cell In[3], line 3
1 A{10,20,30,40,50}
2 del A
‒‒‒‒> 3 print(A)
NameError: name 'A' is not defined
Ex. 9
Updating Values in Set
>>> A={10,20,30,40}
>>> A.add(25)
>>> print(A)
{40, 10, 20, 25, 30}
>>>
>>> A={10,20,30,40}
>>> A.update([25,35,45])
>>> print(A)
{35, 40, 10, 45, 20, 25, 30}
>>>
Ex. 10
Union
>>> a=set([1,2,3])
>>> b=set([2,3,4])
>>> c=a|b
>>> print(c) {1, 2, 3, 4}
>>>
>>> x=set(['a','b','c'])
>>> y=set(['b','c','d'])
>>> c=x.union(y)
>>> print(c)
{'a', 'd', 'b', 'c'}
>>>
Ex. 11
Intersection
>>> a=set([10,20,30])
>>> b=set([20,30,40])
>>> c=a&b
>>> print(c)
{20, 30}
>>> a=set([10,20,30])
>>> b=set([20,30,40])
>>> c=a.intersection(b)
>>> print(c)
{20, 30}
Ex. 12
Difference
>>> a=set([10,20,30,40,50])
>>> b=set([40,50,60,70,80])
>>> c‒a‒b
>>> print(c)
{10, 20, 30}
>>>a.difference(b)
{10, 20, 30}
>>>
Symmetric
difference
>>> a=set([10,20,30,40,50])
>>> b=set([40,50,60,70,80])
>>> c=a^b
>>> print(c)
{80, 20, 70, 10, 60, 30)← Note that 40 and 50 are the common elements which are not present in set c.
>>>a.symmetric_difference(b)
{80, 20, 70, 10, 60, 30}
>>>
Built in
Set Function
In [4]: A = {10,20,30,40,50)
In [5]: print(all (A))
True
In [6]:
print(any (A)).
True
In [7]: print(len(A))
5
In [8]: print(max(A))
50
In [9]:
print (min(A))
10
In [10]: print(sum(A))
150
In [11]:
B = {33,11,44,22}
print (sorted (B))
[11, 22, 33, 44]
Dictionary - Python Programs using data frame
For example
>>> my_dictionary={1:'AAA',2: 'BBB',3:'CCC') # creation of dictionary
>>> print(my_dictionary) #display
{1: 'AAA', 2: 'BBB', 3: 'CCC'}
>>> my_dictionary.clear() #using clear method
>>> print(my_dictionary) #display
{}
>>>
For example
>>> my_dictionary={1:'AAA',2:'BBB', 3:'CCC'}
>>> print(my_dictionary)
{1: 'AAA', 2: 'BBB', 3: 'CCC'}
>>> new_dictionary=my_dictionary.copy()
>>> print(new_dictionary)
{1: 'AAA', 2: 'BBB', 3: 'CCC'}
>>>
dictionary.fromkeys(sequence[, value])
>>> keys={10,20,30}
>>> values = 'Number'
>>> new_dict=dict.fromkeys(keys, values)
>>> print(new_dict)
{10: 'Number', 20: 'Number', 30: 'Number'}
>>>
dictionary.get(key, value])
>>> student={'name':'AAA', 'roll':10,'marks':98} #creation of dictionary
>>> print("Name: ",student.get('name'))
Name: AAA
>>> print("roll: ",student.get('roll'))
roll: 10
>>> print("marks: ",student.get('marks'))
marks: 98
>>> print("Address: ",student.get('address')) #this key is 'address' is not specified
#in the list
Address: None #Hence it returns none
>>>
For example
>>> my_dictionary ={'marks1':99,'marks2':96,'marks3':97}#creating dictionary
>>> print(my_dictionary.values()) #displaying values
dict_values([99, 96, 97])
>>>
Syntax
pop(key), default])
For example
my_dictionary={1:'Red', 2:'Blue', 3:'Green'} #creation of dictionary
>>> val=my_dictionary.pop(1) #removing the element with key 1
>>> print("The popped element is: ",val)
The popped element is: Red
>>> val=my_dictionary.pop(5,3)
#specifying default value. When the specified key is not present in the list, then the #default value is returned.
>>> print("The popped element using default value is: ",val)
The popped element using default value is: 3 #default value is returned
>>>
Write a python program to sort the elements of dictionary.
Solution :
Sort.py
d={'Red':10, 'Blue':42, 'Yellow':32,'Orange':21}
myList=list(d.keys())
print(myList)
myList.sort()
print("Sorted list based on Keys")
for i in myList:
print(i)
Sorted list based in Keys
Blue
Orange
Red
Yellow
Write a python program to create a tuple from given dictionary elements.
Solution: Using the method named item of dictionary, the list of tuples can be returned.
For example ‒
>>> d={'AAA':10,'BBB':20, 'CCC':30}
>>> t1=list(d.items())
>>> print(t1)
[('AAA', 10), ('BBB', 20), ('CCC', 30)]
>>>
Python for Data Science: Laboratory Programs in Python : Tag: Computer Programming, Python, Data Science : Laboratory Programs in Python - Python Programs using different data frames like list, tuple, set and dictionary
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation