Day 14: Python – Tuple

Tuple (‘a’, ‘b’, ‘c’, ‘d’)

Similar to Lists and Dictionary, Tuple is another type of collection in Python. However, tuple has some differences as compared to lists.

– It is immutable. The value cannot be reassigned.
– It uses parenthesis ().
– General syntax: (1,2,3)
– It can use different data type within a tuple.
– It can use indexing and slicing.
– It has two methods associated with tuple, count and index.
– It can be unpacked into several variables.

Why do we want to use tuple when list has more flexibility?

According to the online tutor from the online learning website I am learning right now, tuple is not being used most of the time as compared to list. It is only when we did not want to allow the flexibility of changing the values of the objects, tuple is the choice. It depends on the application’s requirements whether immutability is required. It may provide a convenient source of data integrity.

How to declare a tuple using the parenthesis ()? 
It looks quite similar with lists except it is not using square bracket, but it is using (). It allows duplicated values, just like my example below:

How use index and slicing to read the elements in the object?
It is same as lists.

Tuples have two methods associated with,
.count(‘a’) = returns count of number of ‘a’ in the tuple.
.index(‘a’) = returns the position/index of the given element. It returns error if it cannot be found. 

Edited on 2019/01/27.
More interesting about tuple with list and for statement. Examples below show how to use for statement to iterate the tuple’s values and how to use list to store a number of tuples and print out the list’s values.

Tuple can do unpacking It is something I learned recently and I updated this blog.

Edited: 2019-05-04
How to do unpacking and packing in tuple?

There is a powerful tuple assignment feature that assigns right hand side of values into left hand side. In other way it is called unpacking of a tuple of values into a variable. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable.

See the below code for the example of pack and unpack tuple:

#Pack tuple
#Tuple declaration
a = ("MNNIT Allahabad", 5000, "Engineering") 

#Unpack tuple
#This line of code unpack the values of variable, a 
(college, student, type_ofcollege) = a 
print(college)
print(student)
print(type_ofcollege)

Besides that, we can use the *args argument in tuple unpacking. I think I have not covered what is *args meant in my last 13 days’ entries.
This means that there can be many number of arguments in place of (*args).

All values will be assigned to every variable on left hand side and all remaining values will be assigned to *args. Let look at the sample code below to understand the line above.

# first and last will be assigned to x and z 
# remaining will be assigned to y 
x, *y, z = (10, "Geeks ", " for ", "Geeks ", 50) 
  
# print details 
print(x) 
print(y) 
print(z) 
#output of y is ['Geeks ', ' for ', 'Geeks ']
  
# first and second will be assigned to x and y 
# remaining will be assigned to z 
x, y, *z = (10, "Geeks ", " for ", "Geeks ", 50) 
print(x) 
print(y) 
print(z)
#output of z is [' for ', 'Geeks ', 50]

The value is assigned based on the variable on the left side and the remaining values are assigned to the placeholder *args, in the above example, they are y and z respectively.

Summary of the day:

  • Characteristic of a tuple.
  • Declare and use of index and slicing to read the elements in the object.
  • Two methods associated with tuples, .count() and .index().
  • Tuple allows index and slicing
  • Tuple unpacking.

Advertisements

Day 14: Python – Set

Set {1,2,3}
Finally, I come to the last type of collection in Python, Sets.

The unique about sets is sets are unordered collections with unique elements. It means no duplication of the same object. If I try to insert a same value into the set, it will not able to be added.

A simple way to declare an empty set by,
myset = set()

To add value into a set using .add(),
myset.add(1)

and set will return {1}. Then, if you want to test whether set really does not take duplicate value, can run the codes again multiple times, it will show only 1 value added.

Converting a list which contains duplicated values into a set will remove the duplicate records from the list. See below output of set(my_list).

Set is immutable object, can only add new elements and cannot change the values.

How to access a set?
For loop
for x in my_list
print x

The output from the above codes look,
1
2
3


To check if a value is existed using print()
print(1 in my_list)

The output from the above codes look,
True

Summary of the day:

  • Python Set’s characteristic.
  • Add value into a set.
  • Check if a value exists in a set and returns Boolean result.
  • Convert a list into a set to remove duplication.