Python Sequence and Collections
Python Sequence and Collections
1. Python Strings
A string is a group of characters. Since Python has no provision for arrays, we
simply use strings. This is how we declare a string:
We can use a pair of single or double quotes. And like we’ve always said,
Python is dynamically-typed. Every string object is of the type ‘str’.
>>> type(name)
Output
<class ‘str’>
>>> name=str()
>>> name
Output
”
>>> name=str('Ayushi')
>>> name
Output
‘Ayushi’
>>> name[3]
Output
‘s’
2. Python Lists
Since Python does not have arrays, it has lists. A list is an ordered group of
items. To declare it, we use square brackets.
>>> groceries=['milk','bread','eggs']
>>> groceries[1]
Output
‘bread’
>>> groceries[:2]
Output
[‘milk’, ‘bread’]
A Python list can hold all kinds of items; this is what makes it heterogenous.
>>> mylist=[1,'2',3.0,False]
>>> groceries[0]='cheese'
>>> groceries
Output
[‘cheese’, ‘bread’, ‘eggs’]
>>> groceries[0]='cheese'
>>> groceries
Output
print(“Hi”)
>>> newlist=[sayhi,sayhi]
>>> newlist[0]
Output
<function sayhi at 0x05907300>
>>> newlist[0]()
Output
Hi
3. Python Tuples
A tuple, in effect, is an immutable group of items. When we say immutable, we
mean we cannot change a single value once we declare it.
>>> name=('Ayushi','Sharma')
>>> type(name)
Output
<class ‘tuple’>
>>> name=tuple(['Ayushi','Sharma'])
>>> name
Output
(‘Ayushi’, ‘Sharma’)
>>> name[0]='Avery'
Output
Traceback (most recent call last):File “<pyshell#594>”, line 1, in <module>
name[0]=’Avery’
4. Bytes Sequences
The function bytes() returns an immutable bytes object. We dealt with this
when we talked Built-in Functions in Python.
>>> bytes(5)
Output
b’\x00\x00\x00\x00\x00′
>>> bytes([1,2,3,4,5])
Output
b’\x01\x02\x03\x04\x05′
>>> bytes('hello','utf-8')
Output
b ‘hello’
>>> a=bytes([1,2,3,4,5])
>>> a
Output
b’\x01\x02\x03\x04\x05′
>>> a[4]=3
Output
Traceback (most recent call last):File “<pyshell#46>”, line 1, in <module>
a[4]=3
5. Bytes Arrays
In that article, we discussed this one too. A bytesarray object is like a bytes
object, but it is mutable.
>>> a=bytearray(4)
>>> a
Output
bytearray(b’\x00\x00\x00\x00′)
>>> a=bytearray(4)
>>> a
Output
bytearray(b’\x00\x00\x00\x00\x01′)
>>> a[0]=1
>>> a
Output
bytearray(b’\x01\x00\x00\x00\x01′)
>>> a[0]
Output
1
>>> bytearray([1,2,3,4])
Output
bytearray(b’\x01\x02\x03\x04′)
>>> a=bytearray([1,2,3,4,5])
>>> a
Output
bytearray(b’\x01\x02\x03\x04\x05′)
>>> a[4]=3
>>> a
Output
bytearray(b’\x01\x02\x03\x04\x03′)
See? It is mutable.
6. range() objects
A range() object lends us a range to iterate on; it gives us a list of numbers.
>>> a=range(4)
>>> type(a)
Output
<class ‘range’>
>>> for i in range(7,0,-1):
print(i)
Output
7
6
5
4
3
2
1
1. Concatenation
Concatenation adds the second operand after the first one.
>>> 'Ayu'+'shi'
Output
‘Ayushi’
2. Integer Multiplication
We can make a string print twice by multiplying it by 2.
>>> 'ba'+'na'*2
Output
‘banana’
3. Membership
To check if a value is a member of a sequence, we use the ‘in’ operator.
4. Python Slice
Sometimes, we only want a part of a sequence, and not all of it. We do it with
the slicing operator.
>>> 'Ayushi'[1:4]
Output
‘yus’
>>> len('Ayushi')
Output
6
>>> min('cat')
Output
‘a’
>>> max('cat')
Output
‘t’
1. Python index()
This method returns the index of the first occurrence of a value.
>>> 'banana'.index('n')
Output
2
2. Python count()
count() returns the number of occurrences of a value in a Python sequence.
>>> 'banana'.count('na')
Output
2
>>> 'banana'.count('a')
Output
3
Python Collections
Python collection, unlike a sequence, does not have a deterministic ordering.
Examples include sets and dictionaries.
Every time we visit a set, we get its items in the same order. However, if we
add or remove an item, it may affect the order.
1. Python Set
A set, in Python, is like a mathematical set in Python. It does not hold
duplicates. We can declare a set in two ways:
>>> nums={2,1,3,2}
>>> nums
Output
{1, 2, 3}
>>> nums=set([1,3,2])
>>> nums
Output
{1, 2, 3}
A set is mutable.
>>> nums.discard(2)
>>> nums
Output
{1, 3}
But it may not contain mutable items like lists, dictionaries, or other sets.
2. Python Dictionaries
Think of a dictionary as a real-life dictionary. It holds key-value pairs, and this
is how we declare it:
>>> a={'name':1,'dob':2}
>>> a=dict()
>>> a['name']=1
>>> a['dob']=2
>>> a
Output
{‘name’: 1, ‘dob’: 2}
Output
{0: 1, 1: 2, 2: 4, 3: 8}
However, a key cannot be of an unhashable type.
>>> a={[1,2,3]:1,1:[1,2,3]}
Output
Traceback (most recent call last):File “<pyshell#629>”, line 1, in <module>
a={[1,2,3]:1,1:[1,2,3]}
So, this was all about the Python sequence and collections tutorial. Hope you
like our explanation.
Conclusion
To conclude this Python Sequence and collection tutorial, we will say that a
sequence has a deterministic ordering, but a collection does not.