SlideShare a Scribd company logo
1 of 38
LIST, TUPLE
DICTIONARY
Lists
 Like a String, List also is, sequence data
type.
 In a string we have only characters but a
list consists of data of multiple data types
 It is an ordered set of values enclosed in
square brackets [].
 We can use index in square brackets []
 Values in the list are called elements or
items.
 A list can be modified, i.e. it is mutable.
 List index works the same way as
String index :
An integer value/expression can be
used as index
An Index Error appears, if you try and
access element that does not exist in
the list
IndexError: list index out of range
An index can have a negative value, in
that case counting happens from the
end of the list.
List Examples
i) L1 = [1,2,3,4] list of 4 integer elements.
ii) L2 = [“Delhi”, “Chennai”, “Mumbai”]
list of 3 string
elements.
iii) L3 = [ ] empty list i.e. list with no element
iv) L4 = [“abc”, 10, 20]
list with different types of
elements
Example of list:
list = [ ‘XYZ', 456 , 2.23, ‘PNB', 70.2 ]
tinylist = [123, ‘HMV']
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements 2nd & 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists
Traversing a List
 Using while loop
L=[1,2,3,4]
i = 0
while i < 4:
print (L[i])
i + = 1
Output
1 2 3 4
Traversing a List
Using for loop
L=[1,2,3,4,5]
L1=[1,2,3,4,5]
for i in L: for i in range (5):
print(i) print(L1[i])
Output: Output:
1 2 3 4 5 1 2 3 4 5
List Slices
Examples
>>> L=[10,20,30,40,50]
>>> print(L[1:4])
[20, 30, 40] #print elements 1st index to 3rd index
>>> print(L[3:])
[40, 50] #print elements from 3rd index onwards
>>> print(L[:3])
[10, 20, 30] #print elements 0th index to 2nd index
>>> print L[0:5:2]
[10, 30, 50] #print elements 0th to 4th index jump 2 steps
append() method
 to add one element at the end
Example:
>>> l=[1,2,3,4]
>>> print(l)
[1, 2, 3, 4]
>>> l.append(5)
>>> print(l)
[1, 2, 3, 4, 5]
extend() method
 To add more than one element at the
end of the list
Example
>>> l1=[10,20,30]
>>> l2=[100,200,300]
>>> l1.extend(l2)
>>> print(l1)
[10, 20, 30, 100, 200, 300]
>>> print(l2)
[100, 200, 300]
pop , del and remove functions
 For removing element from the list
 if index is known, we can use pop() or
del() method
 if the index is not known,
remove ( ) can be used.
 to remove more than one element, del
( ) with list slice can be used.
Examples
>>> l=[10,20,30,40,50]
>>> l.pop() #last index elements popped
50
>>> l.pop(1) # using list index
20
>>> l.remove(10) #using element
>>> print(l)
[30, 40]
Example-del()
>>> l=[1,2,3,4,5,6,7,8]
>>> del l[2:5] #using range
>>> print(l)
[1, 2, 6, 7, 8]
insert ()method
 used to add element(s) in between
Example
>>> l=[10,20,30,40,50]
>>> l.insert(2,25)
>>> print(l)
[10, 20, 25, 30, 40, 50]
sort() and reverse() method
sort(): Used to arrange in ascending order.
reverse(): Used to reverse the list.
Example:
>>> l=[10,8,4,7,3]
>>> l.sort()
>>> print(l)
[3, 4, 7, 8, 10]
>>> l.reverse()
>>> print(l)
[10, 8, 7, 4, 3]
Linear search program
Tuples
 We saw earlier that a list is
an ordered mutable collection.There’s
also an ordered immutable collection.
 In Python these are called tuples and
look very similar to lists, but typically
written with () instead of []:
a_list = [1, 'two', 3.0]
a_tuple = (1, 'two', 3.0)
Similar to how we used list before, you
can also create a tuple.
The difference being that tuples are
immutable.This means no assignment,
append, insert, pop, etc. Everything else
works as it did with lists: indexing, getting
the length etc.
Like lists, all of the common sequence
operations are available.
Example of Tuple:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print (tuple ) # Prints complete list
print (tuple[0]) # Prints first element of the list
print (tuple[1:3]) # Prints elements starting from 2nd till 3rd
print (tuple[2:]) # Prints elements starting from 3rd element
print (tinytuple * 2) # Prints list two times
print 9tuple + tinytuple) # Prints concatenated lists
The following code is invalid with tuple, because
we attempted to update a tuple, which is not
allowed. Similar case is possible with lists −
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 #Valid syntax with list
Lists
• A sequence of values of any type
•Values in the list are called items
and are indexed
•Are mutable
•Are enclosed in []
Example
[‘spam’ , 20, 13.5]
Tuple
• are sequence of values of any type
•Are indexed by integers
•Are immutable
•Are enclosed in ()
Example
(2,4)
Dictionary
 Python's dictionaries are kind of hash table type.
They work like associative arrays and consist of
key-value pairs.
 Dictionaries are enclosed by curly braces ({ }) and
values can be assigned and accessed using
square braces ([])
Commonly used dict methods:
 keys() - returns an iterable of all keys in
the dictionary.
 values() - returns an iterable of all
values in the dictionary.
 items() - returns an iterable list of (key,
value) tuples.
Example of Dictionary
dict = {'name': ‘Ram','code':1234, 'dept': ‘KVS'}
print(dict) # Prints complete dictionary
print(dict.keys()) # Prints all the keys
print(dict.values()) # Prints all the values
print(dict.items())
Output:
{'dept': 'KVS', 'code': 1234, 'name': 'Ram'}
['dept', 'code', 'name']
['KVS', 1234, 'Ram']
[('dept', 'KVS'), ('code', 1234), ('name', 'Ram')]
there is a second way to
declare a dict:
sound = dict(dog='bark', cat='meow', snake='hiss')
print(sound.keys()) # Prints all the keys
print(sound.values()) # Prints all the values
Output:
['cat', 'dog', 'snake']
['meow', 'bark', 'hiss']
A few things we already saw on
list work the same for dict:
Similarly to how we can index into lists we use
d[key] to access specific elements in the dict.There
are also a number of methods available for
manipulating & using data from dict.
 len(d) gets the number of item in the dictionary.
print (len(dict))
 key in d checks if k is a key in the dictionary. print
('name' in dict) (True/False)
 d.pop(key) pops an item out of the dictionary
and returns it, similarly to how list’s pop method
worked. dict.pop('name')
Question & Answers
Q.1Which error message would appear
when index not in list range?
Ans:
IndexError: list index out
of range
Q.2 Find the output of the following:
list = [ ‘XYZ', 456 , 2.23, ‘KVS', 70.2]
print (list[1:3])
print (list[2:])
Ans:
list[2:3] : [456 , 2.23]
list[2:] : [456 , 2.23 , ‘KVS', 70.2]
Question & Answers
Q.3 Find the output of the following:
L=[10,20,30,40,50,60,70]
print(L[0:7:2])
Ans:
L[0:7:2] : L=[10,30,50,70]
Q.4What is the difference between list
and tuple?
Ans:
List is mutable and declared by
[]
Tuple is immutable and declared
by ()
Q.5 Find the output of
dict = {'name': ‘Ram','code':1234, 'dept': ‘KVS'}
print (len(dict))
Ans: 3
THANKS

More Related Content

Similar to List_tuple_dictionary.pptx

Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 

Similar to List_tuple_dictionary.pptx (20)

Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
 
Python list
Python listPython list
Python list
 
Python lists
Python listsPython lists
Python lists
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
beginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet descriptionbeginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet description
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptx
 
The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 12 of 84The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 12 of 84
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
The Ring programming language version 1.5.1 book - Part 21 of 180
The Ring programming language version 1.5.1 book - Part 21 of 180The Ring programming language version 1.5.1 book - Part 21 of 180
The Ring programming language version 1.5.1 book - Part 21 of 180
 
List in Python
List in PythonList in Python
List in Python
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
 

Recently uploaded

Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx
pawangadkhe786
 
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
HyderabadDolls
 
Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...
Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...
Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...
HyderabadDolls
 
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Angela Justice, PhD
 
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
yynod
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
K Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CV
K VENKAT NAVEEN KUMAR
 
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 

Recently uploaded (20)

Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
 
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
 
207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx
 
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
 
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
Eden Gardens * High Profile Call Girls in Kolkata Phone No 8005736733 Elite E...
 
Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...
Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...
Howrah [ Call Girls Kolkata ₹7.5k Pick Up & Drop With Cash Payment 8005736733...
 
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime MalegaonVip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
 
Ganga Path Project (marine drive project) Patna ,Bihar .pdf
Ganga Path Project (marine drive project) Patna ,Bihar .pdfGanga Path Project (marine drive project) Patna ,Bihar .pdf
Ganga Path Project (marine drive project) Patna ,Bihar .pdf
 
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
 
B.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak KumarB.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak Kumar
 
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdfDMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
 
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
 
Call Girl Service in Ahmednagar { 9332606886 } VVIP NISHA Call Girls Near 5 S...
Call Girl Service in Ahmednagar { 9332606886 } VVIP NISHA Call Girls Near 5 S...Call Girl Service in Ahmednagar { 9332606886 } VVIP NISHA Call Girls Near 5 S...
Call Girl Service in Ahmednagar { 9332606886 } VVIP NISHA Call Girls Near 5 S...
 
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
 
Call Girls In GOA North Goa +91-8588052666 Direct Cash Escorts Service
Call Girls In GOA North Goa +91-8588052666 Direct Cash Escorts ServiceCall Girls In GOA North Goa +91-8588052666 Direct Cash Escorts Service
Call Girls In GOA North Goa +91-8588052666 Direct Cash Escorts Service
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
 
K Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CV
 
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
 
B.tech Civil Engineering Major Project by Deepak Kumar ppt.pdf
B.tech Civil Engineering Major Project by Deepak Kumar ppt.pdfB.tech Civil Engineering Major Project by Deepak Kumar ppt.pdf
B.tech Civil Engineering Major Project by Deepak Kumar ppt.pdf
 

List_tuple_dictionary.pptx

  • 2. Lists  Like a String, List also is, sequence data type.  In a string we have only characters but a list consists of data of multiple data types  It is an ordered set of values enclosed in square brackets [].  We can use index in square brackets []  Values in the list are called elements or items.  A list can be modified, i.e. it is mutable.
  • 3.  List index works the same way as String index : An integer value/expression can be used as index An Index Error appears, if you try and access element that does not exist in the list IndexError: list index out of range An index can have a negative value, in that case counting happens from the end of the list.
  • 4. List Examples i) L1 = [1,2,3,4] list of 4 integer elements. ii) L2 = [“Delhi”, “Chennai”, “Mumbai”] list of 3 string elements. iii) L3 = [ ] empty list i.e. list with no element iv) L4 = [“abc”, 10, 20] list with different types of elements
  • 5. Example of list: list = [ ‘XYZ', 456 , 2.23, ‘PNB', 70.2 ] tinylist = [123, ‘HMV'] print (list) # Prints complete list print (list[0]) # Prints first element of the list print (list[1:3]) # Prints elements 2nd & 3rd print (list[2:]) # Prints elements starting from 3rd element print (tinylist * 2) # Prints list two times print (list + tinylist) # Prints concatenated lists
  • 6. Traversing a List  Using while loop L=[1,2,3,4] i = 0 while i < 4: print (L[i]) i + = 1 Output 1 2 3 4
  • 7. Traversing a List Using for loop L=[1,2,3,4,5] L1=[1,2,3,4,5] for i in L: for i in range (5): print(i) print(L1[i]) Output: Output: 1 2 3 4 5 1 2 3 4 5
  • 8. List Slices Examples >>> L=[10,20,30,40,50] >>> print(L[1:4]) [20, 30, 40] #print elements 1st index to 3rd index >>> print(L[3:]) [40, 50] #print elements from 3rd index onwards >>> print(L[:3]) [10, 20, 30] #print elements 0th index to 2nd index >>> print L[0:5:2] [10, 30, 50] #print elements 0th to 4th index jump 2 steps
  • 9. append() method  to add one element at the end Example: >>> l=[1,2,3,4] >>> print(l) [1, 2, 3, 4] >>> l.append(5) >>> print(l) [1, 2, 3, 4, 5]
  • 10. extend() method  To add more than one element at the end of the list Example >>> l1=[10,20,30] >>> l2=[100,200,300] >>> l1.extend(l2) >>> print(l1) [10, 20, 30, 100, 200, 300] >>> print(l2) [100, 200, 300]
  • 11. pop , del and remove functions  For removing element from the list  if index is known, we can use pop() or del() method  if the index is not known, remove ( ) can be used.  to remove more than one element, del ( ) with list slice can be used.
  • 12. Examples >>> l=[10,20,30,40,50] >>> l.pop() #last index elements popped 50 >>> l.pop(1) # using list index 20 >>> l.remove(10) #using element >>> print(l) [30, 40]
  • 13. Example-del() >>> l=[1,2,3,4,5,6,7,8] >>> del l[2:5] #using range >>> print(l) [1, 2, 6, 7, 8]
  • 14. insert ()method  used to add element(s) in between Example >>> l=[10,20,30,40,50] >>> l.insert(2,25) >>> print(l) [10, 20, 25, 30, 40, 50]
  • 15. sort() and reverse() method sort(): Used to arrange in ascending order. reverse(): Used to reverse the list. Example: >>> l=[10,8,4,7,3] >>> l.sort() >>> print(l) [3, 4, 7, 8, 10] >>> l.reverse() >>> print(l) [10, 8, 7, 4, 3]
  • 17.
  • 18. Tuples  We saw earlier that a list is an ordered mutable collection.There’s also an ordered immutable collection.  In Python these are called tuples and look very similar to lists, but typically written with () instead of []: a_list = [1, 'two', 3.0] a_tuple = (1, 'two', 3.0)
  • 19. Similar to how we used list before, you can also create a tuple. The difference being that tuples are immutable.This means no assignment, append, insert, pop, etc. Everything else works as it did with lists: indexing, getting the length etc. Like lists, all of the common sequence operations are available.
  • 20. Example of Tuple: tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print (tuple ) # Prints complete list print (tuple[0]) # Prints first element of the list print (tuple[1:3]) # Prints elements starting from 2nd till 3rd print (tuple[2:]) # Prints elements starting from 3rd element print (tinytuple * 2) # Prints list two times print 9tuple + tinytuple) # Prints concatenated lists
  • 21. The following code is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists − tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 #Valid syntax with list
  • 22. Lists • A sequence of values of any type •Values in the list are called items and are indexed •Are mutable •Are enclosed in [] Example [‘spam’ , 20, 13.5] Tuple • are sequence of values of any type •Are indexed by integers •Are immutable •Are enclosed in () Example (2,4)
  • 23. Dictionary  Python's dictionaries are kind of hash table type. They work like associative arrays and consist of key-value pairs.  Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([])
  • 24. Commonly used dict methods:  keys() - returns an iterable of all keys in the dictionary.  values() - returns an iterable of all values in the dictionary.  items() - returns an iterable list of (key, value) tuples.
  • 25. Example of Dictionary dict = {'name': ‘Ram','code':1234, 'dept': ‘KVS'} print(dict) # Prints complete dictionary print(dict.keys()) # Prints all the keys print(dict.values()) # Prints all the values print(dict.items()) Output: {'dept': 'KVS', 'code': 1234, 'name': 'Ram'} ['dept', 'code', 'name'] ['KVS', 1234, 'Ram'] [('dept', 'KVS'), ('code', 1234), ('name', 'Ram')]
  • 26. there is a second way to declare a dict: sound = dict(dog='bark', cat='meow', snake='hiss') print(sound.keys()) # Prints all the keys print(sound.values()) # Prints all the values Output: ['cat', 'dog', 'snake'] ['meow', 'bark', 'hiss']
  • 27. A few things we already saw on list work the same for dict: Similarly to how we can index into lists we use d[key] to access specific elements in the dict.There are also a number of methods available for manipulating & using data from dict.  len(d) gets the number of item in the dictionary. print (len(dict))  key in d checks if k is a key in the dictionary. print ('name' in dict) (True/False)  d.pop(key) pops an item out of the dictionary and returns it, similarly to how list’s pop method worked. dict.pop('name')
  • 28. Question & Answers Q.1Which error message would appear when index not in list range?
  • 30. Q.2 Find the output of the following: list = [ ‘XYZ', 456 , 2.23, ‘KVS', 70.2] print (list[1:3]) print (list[2:])
  • 31. Ans: list[2:3] : [456 , 2.23] list[2:] : [456 , 2.23 , ‘KVS', 70.2]
  • 32. Question & Answers Q.3 Find the output of the following: L=[10,20,30,40,50,60,70] print(L[0:7:2])
  • 34. Q.4What is the difference between list and tuple?
  • 35. Ans: List is mutable and declared by [] Tuple is immutable and declared by ()
  • 36. Q.5 Find the output of dict = {'name': ‘Ram','code':1234, 'dept': ‘KVS'} print (len(dict))