Python for Beginners- beyond the basics | Sole Concept Lab
Python For data scienceTutorials

Python for Beginners- beyond the basics Introduction to widely used structures in python


In the last post, the utter basics of python were introduced. In this post, I aim to go beyond and explain to you the most widely used data structures in python. Let me introduce them they are.

  • Lists
  • Tuples
  • Dictionaries

Data Structures


Lists in python are widely used and really simple to understand. So a list literally means a ‘list of things‘. What can these things be in python? Can you guess?

They are numbers either float or integers and strings. So how do we go about defining a list? Can the list have only numbers or strings? Lets code to find out.

lst = ['a','2','3.4']

what you can see here is that list clearly does not require that you have only one type of data stored in it.
ok so now you understand how to create a list so how can you access an element in the list?

This is equally simple. This process is called indexing. What do you mean by indexing? Follow along

a = [1,2,3,4,5,6,7,'a','b','c']

The third element in the list is 3 to access the third element all you have to do is call the third element. Confused right. Let’s clear some air. Every element in the list has an index associated with it. This index begins at 0. Thus the third element in the list will have an index 2. So can you store the number 7 in a variable named c and print c?

c = a[6]

Excercise for you

what is the index of the element ‘c’?

Hurray! you just learned how to create a list and index it the pythonic way. But wait a minute all this is ok but can you select a range of elements from this list. Yes! of course, you can the syntax of that is the following

a[:7] #selects all the elements having index 0 to 6 let us see how that looks

Cool one more step forward. But what if I want to select only from index 1 to 7. It is easy again its as follows

a[1:7] # note that here always the first index before the: included but index after: is not


Let us turn to tuples now. Tuples are very similar to lists but there is a key difference lets practically explore them. But before that, the way to create tuples is very similar to lists the only difference is that they are enclosed in parenthesis. All other properties such as indexing and slicing remain the same. So let’s create a tuple with the elements being same as that of a and call it a_tup.

a_tup = (1,2,3,4,5,6,7,'a','b','c')

Ok fine what is the difference between a and a_tup you may ask. It’s simple. Suppose I want to change the element having an index 7 in the list to 8 I would do the following.

a[7] =8

This would work fine but try the same with tuples and see what happens?

Python code on jupyter notebook

Jupyter notebook screenshot of the code


Hmmm, error right. This is the difference I wanted to highlight. Tuples are immutable objects in python whereas lists are mutable. Let’s now move on to the other important data structure i.e dictionaries.


Dictionary is a completely different story in python. These are objects which have both a key (kind of the name of the element) and a value associated with that key (it is the element itself). It will be clear when we start coding. Let us go to the code directly.

d = {'a': 1, 'b':2} # note here a and b are keys (likes names to access elements 1 and 2)

Now given a dictionary which is way too long how can you access the keys? its simple let’s do that with the dictionary we have.

print(d.keys()) # would give the keys of the dictionary

print(d.values())  # would give the values associated with the keys.

If you want to access value associated with a particular key then the following could be done.



This would print the value associated with the key ‘a’  in the dictionary.

Time for another exercise now!

Remember how we changed the value of an element in the list. The same could be done here. Do you think the value could be changed? Give it a try? Read more in the official python documentation. Since you have the basics to data structures now feel free to explore the documentation and come up with examples of other methods for the above-mentioned data structures.


In this post, we covered other basic but very important concepts of data structures laying focus on lists, tuples, and dictionaries.

We saw how to create a list and access individual element called indexing or a group of elements called slicing. I then moved on to tuples and saw the similarities with lists. Moreover, a major difference between tuples and lists were uncovered using a practical example.

I then moved on to another very important structure called dictionary which is entirely different from lists and tuples. Here you uncovered the basic notation of dictionaries.

In the next post we I will do multiple things. I will quickly introduce you to conditional statements in python and functions in python. Then combine the concepts learned in this post to build a simple python function to do some neat stuff. Make sure to follow along.


Sharing is caring!

No Responses

  1. Pingback: Python Practice | | Sole Concept Lab March 13, 2018

Leave a Comment