0% found this document useful (0 votes)
17 views

Lecture - 4 (Python) E-Notes

Uploaded by

rebelrishi07
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture - 4 (Python) E-Notes

Uploaded by

rebelrishi07
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Training On Python

Lecture – 4
Collections In Python
List In Python
 List is used to represent group of elements into a single entity (or) object.

 Insertion order is preserved.

 Duplicate elements are allowed.

 Heterogeneous elements are allowed.

 Every element in the list object is represented with unique index.

 List supports both positive and negative indexes.

 We can perform the operations on the elements of the list object by using ‘indexes’.

 We can create the list object by using “list()” function and by using square brackets “[ ]”.

 List objects are mutable objects.


Example Application - 1
# Example Application using List
a=[]#Blank List
print(a) #Print the Blank List
print(type(a)) #Print the type of a
b=[10,20,30] #Create a list b
print(b) #Print the list b
print(type(b)) #Print the type of b
x=list() # Create an empty list
print(x) # Print the empty list
print(type(x)) # Print the type of x
y=list('softpro') # Create a list y
print(y) # Print the list y
print(type(y))# Print the type of y
Example Application - 2
"""
Develop a program in python to take a list of your friend and sort it in alphabetical order
"""
friends=["Rohit","Ajay","Yashi","Brijesh"]
print("Names before sorting")
print(friends)
friends.sort()
print("Names after sorting")
print(friends)
Example Application - 3
# Create a user defined list to take 10 nos. as input now find max and min no.
mylist=[]
print("Enter ten numbers to the list")
for i in range(0,10):
n=int(input())
mylist.append(n)
print(mylist)
print("Max No=",max(mylist))
print("Min No=",min(mylist))
Example Application - 4
# List Example with different built-in methods
x=[10,20,30,40,50]
print(x)
print(len(x)) #len() method find the length of list
x.append(60) #append() method add new element at the end of list
print(x)
x.insert(2,70) #insert() method add new element at given position
print(x)
y=['abc','def','xyz',30]
print(y)
x.extend(y) #extend() method is used to add a list in another list
print(x)
Example Application – 4 (cont..)
print(x.index('abc')) #index() method is used to display index of given element
x.remove(30) #remove() method remove the given element from list
print(x)
x.reverse() #reverse() method reverse the list elements
print(x)
x.pop() #pop() method remove the last element of list
print(x)
x.pop(3)
print(x)
Tuple In Python

• Tuple is used to represent group of elements into a single entity.

• Tuple objects are immutable objects i.e., once if you create tuple object with same element, later we can’t
modify the elements of that object.

• Insertion order is preserved.

• Duplicate elements are allowed.

• Every element in the tuple object is representing with index.

• Tuple supports both forward and backward indexes.


Example Application - 5

#This example will demonstrate the concept of tuple

tuple1 = (10, 20, 30, 40, 50, 60)

print(tuple1)

count = 0

for i in tuple1:

print("tuple1[%d] = %d"%(count, i))


Example Application – 5 (cont..)

Output:-

(10, 20, 30, 40, 50, 60)

tuple1[0] = 10

tuple1[0] = 20

tuple1[0] = 30

tuple1[0] = 40

tuple1[0] = 50

tuple1[0] = 60
Example Application – 6

tuple1 = tuple(input("Enter the tuple elements ..."))

print(tuple1)

count = 0

for i in tuple1:

print("tuple1[%d] = %s"%(count, i));


Example Application – 6 (cont..)
Output:-
Enter the tuple elements ...123456
('1', '2', '3', '4', '5', '6')
tuple1[0] = 1
tuple1[0] = 2
tuple1[0] = 3
tuple1[0] = 4
tuple1[0] = 5
tuple1[0] = 6
Difference Between List And Tuple

List Tuple
List objects are mutable objects. Tuple objects are immutable objects.
Iterating the list is slower. Iterating tuple is faster.
List objects are not used as a key for The tuple objects which contain
the dictionary immutable objects can be used as a key
for the dictionary.
If the data changes frequently then it is If the data doesn’t change then it is
recommended to represent the data by recommended to represent the data by
using list. using tuple.
Dictionary In Python

• Dictionary is used to represent group of key, value pairs into a single entity.

• Insertion order is not preserved.

• Duplicate keys are not allowed but values can be duplicate.

• Heterogeneous keys and values are allowed.

• Immutable objects only allowed for keys.


Example Application - 7
#This python application demonstrates dictionary
x={}
print(x)
print(type(x))
y=dict([(1,'apple'),(2,'ball')])
print(y)
print(type(y))
z={'java':90,'python':99,'hadoop':95}
print(z)
print(type(z))
print(len(z))

• .
Example Application - 8
#Dictionary Demo..
dict1={
'empid':'SPI1003',
'empname':'Brijesh Mishra',
'department':'Development',
'salary':50000
}
for k,v in dict1.items():
print(k,":",v).

You might also like