Sitemap

Python learning (Part 3)

4 min readOct 25, 2019

--

In this article we will be covering the topics like List, Tuples, Dictionary, Sets.

List:

A list is a collection which is ordered and changeable. It allows duplicate records.
In Python lists are written with square brackets.

Example:
L= [1, “Arjun”, “MSBI”, 100.50]

Output:

Press enter or click to view image in full size

We can do the operations like the slice [:] operator, concatenation (+) operator and repetition (*) operator in List as shown below

Output:

Press enter or click to view image in full size

Note: The slice [:] operator is nothing but range of list and if you gave range like [0:3] it will exclude the 3rd index value and displays the index value of 0 to 2.

Negative Indexing :

Output:

Press enter or click to view image in full size

Tuples:

Tuple is similar to list. The only difference is that once a tuple is created, that the values can’t be changed.
Tuples are unchangeable. In Python tuples are written with round brackets.

Example:
T = (1, “Movie”, “Sanju”)

Output:

Press enter or click to view image in full size

Now let’s see how it is different from List.
Here I created a tuple as T with values as shown above, then I tried to change data of Tuple[1] = “Movie” to “Film”, it should show me some error in Output.

Output:

Press enter or click to view image in full size

As Tuple and List are very similar, we can do the same actions in Tuple like the slice [:] operator, concatenation (+) operator and repetition (*) operator in Tuple as well.

Dictionary :

Dictionary is an ordered set of key-value pair of items.
It is like hash table where each key stores a specific value. It can have duplicate records.
The items in the dictionary are separated with the comma and enclosed in the curly braces {}.

Get Arjun Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:
EmpInfo = {“Name”: “Arjun”, “Age”: 23, “Company”: “QuestIT”}

Output:

Press enter or click to view image in full size

Accessing and Changing the data in Dictionary :
We can access the items/data of a dictionary by referring to its key name.

In the above example EmpInfo is a dictionary and keys are Name, Age and Company.
Note: Here %s is used for string datatype, %d is used for Integer datatype
Likewise %f is used for Float datatype.

Output:

Press enter or click to view image in full size

We can change the value/data of a specific item by referring to its key name.

Output:

Press enter or click to view image in full size

Above as you can see Age key has been changed from 23 to 24.

Sets in Python :

The sets in python are defined as the unordered collection of various items enclosed within the curly brackets. The elements of the set cannot be duplicate.
There is no index attached to the elements of the set.

Example:
Months = {“January”, “February”, “March”, “April”, “May”}

Output:

Press enter or click to view image in full size

As Sets are unordered, the ouput that we got is the same but in unordered way.

We can also perform the actions like add, remove, update, clear, delete, etc.

Output:

Press enter or click to view image in full size

Learn Python practically with below video: -

--

--