Lecture 2.6 - 2.7
Lecture 2.6 - 2.7
PROGRAMMING IN PYTHON
(21CSH-284)
3
Course Outcomes
4
Contents to be Covered
• Lists in Python.
• Operation on List
A List is a kind of
Collection
• A collection allows us to put many values in a single “variable”
• A collection is nice because we can carry all many values around in
one convenient package.
5
4
for i in [5, 4, 3, 2, 1] : 3
print i 2
print 'Blastoff!' 1
Blastoff!
Lists and definite loops - best pals
for i in range(len(friends)) :
friend = friends[i]
print 'Happy New Year:', friend Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Concatenating lists using +
• We can create a new list by
adding two exsiting lists together >>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
>>> print a
[1, 2, 3]
Lists can be sliced using :
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
Remember: Just
[41,12]
like in strings, the
>>> t[:4]
second number is
[9, 41, 12, 3]
"up to but not
>>> t[3:]
including"
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods
>>> x = list()
>>> type(x)<type 'list'>
>>> dir(x)['append', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
Source:[http://docs.python.org/tutorial/datastructures.html]
Building a list from scratch
• We can create an empty >>> stuff = list()
list and then add
elements using the >>> stuff.append('book')
append method >>> stuff.append(99)
• The list stays in order and >>> print stuff
new elements are added
at the end of the list ['book', 99]
>>> stuff.append('cookie')
>>> print stuff
['book', 99, 'cookie']
Is Something in a List?
• Python provides two
operators that let you
>>> some = [1, 9, 21, 10, 16]
check if an item is in a
>>> 9 in some
list
True
• These are logical >>> 15 in some
operators that return False
True or False >>> 20 not in some
• They do not modify the True
list >>>
A List is an Ordered Sequence
Source:[http://docs.python.org/lib/built-in-funcs.html]
Key Points
• Concept of a collection
• Lists and definite loops
• Indexing and lookup
• List mutability
• Functions: len, min, max, sum
• Slicing lists
23
Learning Material
24
Research Articles
25
Assessment Pattern
26
Thank you
e-Mail: [email protected]
27