Sei sulla pagina 1di 20

School of Computing and Information Technology

Course Delivery

B. Tech – VII Semester (BTCS15F7530)


Programming with Python
Python Collections
There are four collection data types in the Python
programming language:
• List is a collection which is ordered and changeable.
Allows duplicate members.
• Tuple is a collection which is ordered and
unchangeable. Allows duplicate members.
• Set is a collection which is unordered and
unindexed. No duplicate members.
• Dictionary is a collection which is unordered,
changeable and indexed. No duplicate members.
Lists
 A list is a collection which is ordered and
changeable.

 Allows duplicate members.

 In Python lists are written with square brackets.


Creating a List
>>> a_list = ['a', 'b', 'mpilgrim', 'z', 'example']
>>> a_list
['a', 'b', 'mpilgrim', 'z', 'example']
>>> a_list[0]
'a‘
>>> a_list[4]
'example‘
>>> a_list[-1]
'example‘
>>> a_list[-3]
'mpilgrim'
Slicing a List
>>> a_list
['a', 'b', 'mpilgrim', 'z', 'example']

>>> a_list[2]
'mpilgrim‘

>>> a_list[1:3]
['b', 'mpilgrim']

>>> a_list[1:-1]
['b', 'mpilgrim', 'z']
a_list=['a', 'b', 'mpilgrim', 'z', 'example']

>>> a_list[0:3] >>> a_list[-4:-2]


['a', 'b', 'mpilgrim'] ['b', 'mpilgrim']
>>> a_list[3:] >>> a_list[-2:-4]
['z', 'example'] []

>>> a_list[:] >>> a_list[-2:]


['a', 'b', 'mpilgrim', 'z', 'example'] ['z', 'example']

>>> a_list[3:3] >>> a_list[-2:2]


[] []
Adding item to a List
>>> a_list = ['a']
>>> a_list = a_list + [2.0, 3] ①
>>> a_list ②
['a', 2.0, 3]

>>> a_list.append(True) ③
>>> a_list
['a', 2.0, 3, True]

>>> a_list.extend(['four', 'Ω']) ④


>>> a_list
['a', 2.0, 3, True, 'four', 'Ω']

>>> a_list.insert(0, 'Ω') ⑤


>>> a_list
['Ω', 'a', 2.0, 3, True, 'four', 'Ω']
>>> a_list = ['a', 'b', 'c']
>>> a_list.extend(['d', 'e', 'f']) ①
>>> a_list
['a', 'b', 'c', 'd', 'e', 'f']

>>> len(a_list) ② >>> a_list.append(['g', 'h', 'i']) ③


6 >>> a_list
['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h', 'i']]
>>> a_list[-1]
'f' >>> len(a_list) ④
7

>>> a_list[-1]
['g', 'h', 'i']
>>> a_list = ['a', 'b', 'c']
>>> a_list.append('g', 'h', 'i')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (3 given)

>>> a_list.append(['g', 'h', 'i'])


>>> a_list
['a', 'b', 'c', ['g', 'h', 'i']]

>>> a_list[-1] >>> a_list[-1][1]


['g', 'h', 'i'] 'h'
Searching For Values In A List
 count(), in, not in, index()
>>> a_list = ['a', 'b', 'new', 'mpilgrim', 'new']
>>> a_list.count('new')
2

>>> 'new' in a_list


True

>>> 'c' in a_list


False

>>> 'c' not in a_list


False
>>> a_list.index('mpilgrim')
3
>>> a_list.index('new')
2
>>> a_list.index('c')
Traceback (innermost last):
File "<interactive input>", line 1, in ?
ValueError: list.index(x): x not in list
Removing Items From A List
 del, remove(), pop(), clear()
>>> a_list = ['a', 'b', 'new', 'mpilgrim', 'new']
>>> a_list[1]
'b'

>>> del a_list[1] ①


>>> a_list
['a', 'new', 'mpilgrim', 'new']

>>> a_list[1] ②
'new'
remove()
>>> a_list.remove('new') ①
>>> a_list
['a', 'mpilgrim', 'new']

>>> a_list.remove('new') ②
>>> a_list
['a', 'mpilgrim']

>>> a_list.remove('new')
Traceback (most recent call last):
File "<stdin>", line 1, in <module> ValueError: list.remove(x): x
not in list
pop()
>>>a_list
['a', 'b', 'new', 'mpilgrim']

>>> a_list.pop() ①
'mpilgrim'

>>> a_list
['a', 'b', 'new']

>>> a_list.pop(1) ②
'b'

>>> a_list
['a', 'new']
>>> a_list.pop()
'new'
>>> a_list.pop()
'a'
>>> a_list.pop() ③
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop from empty list
clear()
>>>a_list
['a', 'b', 'new', 'mpilgrim']

>>> a_list.clear()
>>> a_list
[]

>>> del a_list


>>> a_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a_list' is not defined
Other operations
>>> a_list = ['a', 'b', 'c']
>>> a_list.reverse()
>>> a_list
['c', 'b', 'a']

>>> a_list.reverse()
>>> a_list
['a', 'b', 'c']
sort()
>>> a_list = ['a', 'd', 'h','e','b']
>>> a_list.sort()
>>> a_list
['a', 'b', 'd', 'e', 'h']

>>> a_list = ['8', '6', '-2','3','4']


>>> a_list.sort()
>>> a_list
['-2', '3', '4', '6', '8']
List Methods
• append() Adds an element at the end of the list
• extend() Add the elements of a list (or any iterable), to the end of the current list
• insert() Adds an element at the specified position
• remove() Removes the first item with the specified value
• del() Deletes an item, or list.
• pop() Removes the element at the specified position
• reverse() Reverses the order of the list
• clear() Removes all the elements from the list
• count() Returns the number of elements with the specified value
• index() Returns the index of the first element with the specified value
• sort() Sorts the list
Thank You

Potrebbero piacerti anche