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)
 
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
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
 

Recently uploaded

Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
amitlee9823
 
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
amitlee9823
 
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
Call Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night StandCall Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...
amitlee9823
 
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
amitlee9823
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
Ken Fuller
 
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
amitlee9823
 

Recently uploaded (20)

Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Bommanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 
Joshua Minker Brand Exploration Sports Broadcaster .pptx
Joshua Minker Brand Exploration Sports Broadcaster .pptxJoshua Minker Brand Exploration Sports Broadcaster .pptx
Joshua Minker Brand Exploration Sports Broadcaster .pptx
 
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls DubaiDark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
 
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
 
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
 
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Call Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night StandCall Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Kengeri Satellite Town ☎ 7737669865 🥵 Book Your One night Stand
 
Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.
 
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
 
Resumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineResumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying Online
 
Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...
Vip Mumbai Call Girls Ghatkopar Call On 9920725232 With Body to body massage ...
 
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big BoodyDubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
 
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night StandHot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negron
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
 
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Devanahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Miletti Gabriela_Vision Plan for artist Jahzel.pdf
Miletti Gabriela_Vision Plan for artist Jahzel.pdfMiletti Gabriela_Vision Plan for artist Jahzel.pdf
Miletti Gabriela_Vision Plan for artist Jahzel.pdf
 
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 

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))