SlideShare a Scribd company logo
Python
Lecture 4Lecture 4
- Ravi Kiran Khareedi
Lists - Revisited
• Accessing Values in List
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]list2 = [1, 2, 3, 4, 5, 6, 7 ]
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
Lists - Revisited
• Updating Values in List
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "print "Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];
Lists – Revisted
• Deleting the entire list
Testlist = [‘a’, ’b’, ’c’]
print Testlistprint Testlist
del Testlist
print Testlist
More List Operations
Built in Functions
SN Function with Description
1
cmp(list1, list2)
Compares elements of both lists.
2
len(list)
Gives the total length of the list.
3
max(list)
Returns item from the list with max value.
4
min(list)
Returns item from the list with min value.
Output of cmp: cmp(list1, list2) - returns 0 if list1 = list2
returns 1 if list1 > list2
return -1 if list1 < list2
Others functions are self explanatory.
(These built in functions are supported by python for any kind of data structure not just lists)
4
Returns item from the list with min value.
5
list(seq)
Converts a tuple into list.
6
type(obj)
Returns the type of the object
More List Methods
list.append(obj) - Appends object obj to list
list.count(obj) - Returns count of how many times obj occurs in list
list.extend(seq) - Appends the contents of seq to list
list.index(obj) - Returns the lowest index in list that obj appears
list.insert(index,obj) - Inserts object obj into list at offset indexlist.insert(index,obj) - Inserts object obj into list at offset index
list.pop(obj = list[-1]) - Removes and returns last object or obj from list
list.remove(obj) - Removes object obj from list
list.reverse() - Reverses objects of list in place
list.sort() - Sorts objects of list, use compare func if given
Tuples - Revisited
• Accessing Values in Tuples
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0]print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
Tuples - Revisited
• Updating Values in Tuples
Tuples are immutable so we cannot update them or change
values of tuple elements.
But we able to take portions of an existing tuples to create a
new tuples.new tuples.
Ex:
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
tup3 = tup1 + tup2;
print tup3;
Tuples - Revisted
• Deleting Elements from tuples
– Not possible as its immutable.
– But the entire tuples can be deleted.
Ex:Ex:
tup = ('physics', 'chemistry', 1997, 2000);
print tup; del tup;
print "After deleting tup "
print tup;
More Tuple operations
Python Expression Results Description
len((1, 2, 3)) 3 Length
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation
('Hi!‘,) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition
3 in (1, 2, 3) True Membership
for x in (1, 2, 3): print x, 1 2 3 Iteration
Built in Tuple functions
SN Function with Description
1
cmp(tuple1, tuple2)
Compares elements of both tuples.
2
len(tuple)
Gives the total length of the tuple.
3
max(tuple)
Returns item from the tuple with max value.
3
Returns item from the tuple with max value.
4
min(tuple)
Returns item from the tuple with min value.
5
tuple(seq)
Converts a list into tuple.
6
type(obj)
Returns the type of the object
Dictionary – Revisted
• Accessing and Updating – Already seen
• Deleting Dictionary Elements
– Del dict(‘Key’) # remove the element with key
– dict.clear()
– del dict
Ex:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
print dict['Name'];
dict.clear();
print dict;
dict['Age'] = 25
print dict
del dict
dict['Name'] = “Muller“
TRY IT:
dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}; # Key is not unique
print "dict['Name']: ", dict['Name'];
Built in Dictionary Functions
SN Function with Description
1
cmp(dict1, dict2)
Compares elements of both dict.
2
len(dict)
Gives the total length of the dictionary. This would be equal
to the number of items in the dictionary.
str(dict)
3
str(dict)
Produces a printable string representation of a dictionary
4
type(variable)
Returns the type of the passed variable. If passed variable is
dictionary, then it would return a dictionary type.
Note: Min and Max can also be applied to a dictionary but it returns the
minimum key value based on ASCII. So its of No Practical Use
More Dictionary Methods
SN Methods with Description
1
dict.clear()
Removes all elements of dictionary dict
2
dict.copy()
Returns a shallow copy of dictionary dict
4
dict.get(key, default=None)
For key key, returns value or default if key not in dictionary
5
dict.has_key(key)
Returns true if key in dictionary dict, false otherwise
6
dict.items()
Returns a list of dict's (key, value) tuple pairs
7
dict.keys()
Returns list of dictionary dict's keys
9
dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict
10
dict.values()
Returns list of dictionary dict's values
Strings - Revisited
• String Special Operators:
a = “Hello”; b = “Python”
References
• http://www.tutorialspoint.com/python/

More Related Content

PDF
There's a Prolog in your Scala!
PDF
JS OO and Closures
PDF
The Ring programming language version 1.6 book - Part 24 of 189
PPTX
Context
PDF
The Ring programming language version 1.9 book - Part 44 of 210
PPTX
Prototypes
PDF
The Ring programming language version 1.10 book - Part 30 of 212
PPTX
Python programming -Tuple and Set Data type
There's a Prolog in your Scala!
JS OO and Closures
The Ring programming language version 1.6 book - Part 24 of 189
Context
The Ring programming language version 1.9 book - Part 44 of 210
Prototypes
The Ring programming language version 1.10 book - Part 30 of 212
Python programming -Tuple and Set Data type

What's hot (20)

PDF
The Ring programming language version 1.8 book - Part 41 of 202
PDF
Tuples All the Way Down
PPTX
Python data structures
DOCX
XTW_Import
PDF
Lambda Expressions in Java 8
DOCX
Spark_Documentation_Template1
PDF
The Ring programming language version 1.5.1 book - Part 32 of 180
PDF
The Ring programming language version 1.5.4 book - Part 22 of 185
PPT
20120518 power shell_文字處理及輕量測試
PDF
Python programming : List and tuples
PPTX
Sixth session
PDF
Python Workshop Part 2. LUG Maniapl
PDF
The Ring programming language version 1.5.2 book - Part 21 of 181
PDF
Underscore.js
PDF
AJUG April 2011 Raw hadoop example
PPTX
Aggregate functions
PPTX
Python PCEP Lists Collections of Data
PDF
Arrays in python
The Ring programming language version 1.8 book - Part 41 of 202
Tuples All the Way Down
Python data structures
XTW_Import
Lambda Expressions in Java 8
Spark_Documentation_Template1
The Ring programming language version 1.5.1 book - Part 32 of 180
The Ring programming language version 1.5.4 book - Part 22 of 185
20120518 power shell_文字處理及輕量測試
Python programming : List and tuples
Sixth session
Python Workshop Part 2. LUG Maniapl
The Ring programming language version 1.5.2 book - Part 21 of 181
Underscore.js
AJUG April 2011 Raw hadoop example
Aggregate functions
Python PCEP Lists Collections of Data
Arrays in python
Ad

Similar to Python - Lecture 4 (20)

PPT
Programming in Python Lists and its methods .ppt
DOC
Revision Tour 1 and 2 complete.doc
PPTX
UNIT-4.pptx python for engineering students
PPTX
Python list tuple dictionary .pptx
PPTX
Programming with Python_Unit-3-Notes.pptx
PDF
Python tuples and Dictionary
PPTX
Chapter 3-Data structure in python programming.pptx
PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
Tuples-and-Dictionaries.pptx
PPTX
UNIT-3 python and data structure alo.pptx
PPTX
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
PPTX
datastrubsbwbwbbwcturesinpython-3-4.pptx
PPTX
Python introduction data structures lists etc
PPTX
Python list tuple dictionary presentation
PPTX
List_tuple_dictionary.pptx
PPTX
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
PPTX
Python Collections
PPTX
python ..... _
PPTX
Python - Data Structures
PPTX
fundamental of python --- vivek singh shekawat
Programming in Python Lists and its methods .ppt
Revision Tour 1 and 2 complete.doc
UNIT-4.pptx python for engineering students
Python list tuple dictionary .pptx
Programming with Python_Unit-3-Notes.pptx
Python tuples and Dictionary
Chapter 3-Data structure in python programming.pptx
Python Variable Types, List, Tuple, Dictionary
Tuples-and-Dictionaries.pptx
UNIT-3 python and data structure alo.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
datastrubsbwbwbbwcturesinpython-3-4.pptx
Python introduction data structures lists etc
Python list tuple dictionary presentation
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
Python Collections
python ..... _
Python - Data Structures
fundamental of python --- vivek singh shekawat
Ad

More from Ravi Kiran Khareedi (11)

PDF
Python - Lecture 12
PDF
Python - Lecture 11
PDF
Python - Lecture 10
PDF
Python - Lecture 9
PDF
Python - Lecture 8
PDF
Python - Lecture 7
PDF
Python - Lecture 6
PDF
Python - Lecture 5
PDF
Python - Lecture 3
PDF
Python - Lecture 2
PDF
Python - Lecture 1
Python - Lecture 12
Python - Lecture 11
Python - Lecture 10
Python - Lecture 9
Python - Lecture 8
Python - Lecture 7
Python - Lecture 6
Python - Lecture 5
Python - Lecture 3
Python - Lecture 2
Python - Lecture 1

Recently uploaded (20)

PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Spectroscopy.pptx food analysis technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Unlocking AI with Model Context Protocol (MCP)
MIND Revenue Release Quarter 2 2025 Press Release
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Network Security Unit 5.pdf for BCA BBA.
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Empathic Computing: Creating Shared Understanding
Assigned Numbers - 2025 - Bluetooth® Document
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25-Week II
Spectroscopy.pptx food analysis technology

Python - Lecture 4

  • 1. Python Lecture 4Lecture 4 - Ravi Kiran Khareedi
  • 2. Lists - Revisited • Accessing Values in List list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ]list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
  • 3. Lists - Revisited • Updating Values in List list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : "print "Value available at index 2 : " print list[2]; list[2] = 2001; print "New value available at index 2 : " print list[2];
  • 4. Lists – Revisted • Deleting the entire list Testlist = [‘a’, ’b’, ’c’] print Testlistprint Testlist del Testlist print Testlist
  • 6. Built in Functions SN Function with Description 1 cmp(list1, list2) Compares elements of both lists. 2 len(list) Gives the total length of the list. 3 max(list) Returns item from the list with max value. 4 min(list) Returns item from the list with min value. Output of cmp: cmp(list1, list2) - returns 0 if list1 = list2 returns 1 if list1 > list2 return -1 if list1 < list2 Others functions are self explanatory. (These built in functions are supported by python for any kind of data structure not just lists) 4 Returns item from the list with min value. 5 list(seq) Converts a tuple into list. 6 type(obj) Returns the type of the object
  • 7. More List Methods list.append(obj) - Appends object obj to list list.count(obj) - Returns count of how many times obj occurs in list list.extend(seq) - Appends the contents of seq to list list.index(obj) - Returns the lowest index in list that obj appears list.insert(index,obj) - Inserts object obj into list at offset indexlist.insert(index,obj) - Inserts object obj into list at offset index list.pop(obj = list[-1]) - Removes and returns last object or obj from list list.remove(obj) - Removes object obj from list list.reverse() - Reverses objects of list in place list.sort() - Sorts objects of list, use compare func if given
  • 8. Tuples - Revisited • Accessing Values in Tuples tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0]print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5]
  • 9. Tuples - Revisited • Updating Values in Tuples Tuples are immutable so we cannot update them or change values of tuple elements. But we able to take portions of an existing tuples to create a new tuples.new tuples. Ex: tup1 = (12, 34.56); tup2 = ('abc', 'xyz'); tup3 = tup1 + tup2; print tup3;
  • 10. Tuples - Revisted • Deleting Elements from tuples – Not possible as its immutable. – But the entire tuples can be deleted. Ex:Ex: tup = ('physics', 'chemistry', 1997, 2000); print tup; del tup; print "After deleting tup " print tup;
  • 11. More Tuple operations Python Expression Results Description len((1, 2, 3)) 3 Length (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation ('Hi!‘,) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition 3 in (1, 2, 3) True Membership for x in (1, 2, 3): print x, 1 2 3 Iteration
  • 12. Built in Tuple functions SN Function with Description 1 cmp(tuple1, tuple2) Compares elements of both tuples. 2 len(tuple) Gives the total length of the tuple. 3 max(tuple) Returns item from the tuple with max value. 3 Returns item from the tuple with max value. 4 min(tuple) Returns item from the tuple with min value. 5 tuple(seq) Converts a list into tuple. 6 type(obj) Returns the type of the object
  • 13. Dictionary – Revisted • Accessing and Updating – Already seen • Deleting Dictionary Elements – Del dict(‘Key’) # remove the element with key – dict.clear() – del dict Ex: dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; print dict['Name']; dict.clear(); print dict; dict['Age'] = 25 print dict del dict dict['Name'] = “Muller“ TRY IT: dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}; # Key is not unique print "dict['Name']: ", dict['Name'];
  • 14. Built in Dictionary Functions SN Function with Description 1 cmp(dict1, dict2) Compares elements of both dict. 2 len(dict) Gives the total length of the dictionary. This would be equal to the number of items in the dictionary. str(dict) 3 str(dict) Produces a printable string representation of a dictionary 4 type(variable) Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type. Note: Min and Max can also be applied to a dictionary but it returns the minimum key value based on ASCII. So its of No Practical Use
  • 15. More Dictionary Methods SN Methods with Description 1 dict.clear() Removes all elements of dictionary dict 2 dict.copy() Returns a shallow copy of dictionary dict 4 dict.get(key, default=None) For key key, returns value or default if key not in dictionary 5 dict.has_key(key) Returns true if key in dictionary dict, false otherwise 6 dict.items() Returns a list of dict's (key, value) tuple pairs 7 dict.keys() Returns list of dictionary dict's keys 9 dict.update(dict2) Adds dictionary dict2's key-values pairs to dict 10 dict.values() Returns list of dictionary dict's values
  • 16. Strings - Revisited • String Special Operators: a = “Hello”; b = “Python”