Learn Python Fundamental in 30 Days — Day 6 (Tuples & Sets)

devops everyday challenge
devops-challenge
Published in
3 min readMay 6, 2017

--

Tuples in python are immutable sequences of arbitrary objects,once created they cannot be changed(replaced/removed) and new elements cannot be added.

Similar to list but delimited by parentheses rather than square brackets

>>> a = (1,2,3)
>>> a
(1, 2, 3)
>>> type(a)
<class ‘tuple’>

We can access element using indexing

>>> a[0]
1

We can use len() to find number of elements

>>> len(a)
3

We can iterate it using loop

>>> for i in a:
… print(i)

1
2
3

Concatenate it using + operator

>>> a
(1, 2, 3)
>>> b = (4,5,6)
>>> a + b
(1, 2, 3, 4, 5, 6)

Repetition can be done using *

>>> a * 2
(1, 2, 3, 1, 2, 3)

As mentioned above tuples are immutable, so we try to change it, it will result an TypeError

>>> a[0] = 4
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘tuple’ object does not support item assignment

One thing to note we can’t used one object in parentheses as a single element tuple because Python is treating it as an integer

>>> b = (10)>>> type(b)<class ‘int’>

For a single element tuple include a trailing comma

>>> b = (10,)>>> type(b)<class ‘tuple’>

But then how can we specify the empty tuples,to do that just use empty parentheses

>>> c = ()>>> type(c)<class ‘tuple’>

Tuples are useful for returning multiple values

>>> def minmax(ellist):
… return min(ellist),max(ellist)

>>> a = [1,2,3,4,5,6]
>>> minmax(a)
(1, 6)

NOTE: More about iteration and function in future chapter

Tuple unpacking allow us to destructure directly into named references

>>> min,max = minmax(a)
>>> min
1
>>> max
6

We can easily swap variables using tuples

>>> a = 5>>> b = 10>>> a,b = b,a>>> a10>>> b5

Same code without tuple

>>> x = 4
>>> y = 5
>>>
>>>
>>>
>>> temp = x
>>> x = y
>>> y = temp
>>> x
5
>>> y
4

Some Tuple Method

index: We need to figure out the index of “to”

>>> a = (1,2,3,1)
>>> a.index(1)
0

count: How many times value appear in tuple

>>> a = (1,2,3,1)
>>> a.count(1)
2

Now question is where I am going to use tuple?

Let say I am writing a program where I am passing around an object and need to make sure it doesn’t get changed in those cases tuple might be our solution

Excercise

1: Write a tuple which can only print even valuesmyTup = (“Welcome”,”to”,”the”,”world”,”of”,”tuple”)Output(“to”,”world”,”tuple”)

Sets

Set are a unordered collection of unique elements. The collection is mutable in so far elements can be added/removed from the set but each element must itself be immutable very much like keys of a dictionary.

Set look similar to dictionary(as they use curly braces) but each item is a single object rather than a pair joined by a colon.

>>> c = {1,2,3,4}>>> c{1, 2, 3, 4}>>> type(c)<class 'set'>

Sets are mostly used to discard duplicates items

>>> d = {1,2,3,3,2}>>> d{1, 2, 3}

Sets are iterable

>>> for i in d:… print(i)123

To add element to the set use add method

>>> e{1, 2, 3}>>> e.add(4)>>> e{1, 2, 3, 4}

Now if we want to add the element which already exist there is no effect and neither does it produce any error.

>>> e.add(4)>>> e{1, 2, 3, 4}

To add multiple element

>>> e.update([7,8,9])>>> e{1, 2, 3, 4, 7, 8, 9}

To remove an element from the set

>>> e.remove(7)>>> e{1, 2, 3, 4, 8, 9}#To remove an element which doesn't exist result an error>>> e.remove(7)Traceback (most recent call last):File "<stdin>", line 1, in <module>KeyError: 7# Second method is discard>>> e.discard(1)>>> e{2, 3, 4}#It doesn't produce any error even the member doesn't present>>> e.discard(1)

The other most useful use of set types is group of powerful set algebra operations which are provided. These allow us to easily compute set unions, set differences and set intersection.

# Difference
>>> a = {1,2,3}
>>> b = {3,4,5}>>> a.difference(b){1, 2}# Intersection>>> a.intersection(b){3}# Subset>>> a.issubset(b)False>>> b.issubset(a)False

So this end of Day6, In case if you are facing any issue, this is the link to Python Slack channel https://devops-myworld.slack.com

Please send me your details
* First name
* Last name
* Email address
to devops.everyday.challenge@gmail.com, so that I will add you to this slack channel

--

--