Lists, Dictionaries and Tuples

Lists, Dictionaries, and Tuples

Hey There, Subham here, back again with another article about Python Programming Language.
In the previous articles, I have taught you all about Python variables and I think you were been able to do your First program as I have taught you in one of the previous articles. If you haven't seen them yet I will suggest you to check those sections first then you will be able to understand today's topic.

Check out Previous Topics:

So, today's topic is all about Lists, Dictionaries, and Tuples.
Lists, Dictionaries and Tuples

Let me tell you a brief knowledge about those things with some Examples.


:List:

A List is basically a storage of same or different types of data elements. In a List, you can store any types of elements like integer, floats or doubles, strings etc. A list is denoted by "[]" symbol.

Here is an Example of Lists :

-------------------------------------------------------------------------------------------------------
list1 = [1, 2, 3.5, "subham", "chicken"]
-------------------------------------------------------------------------------------------------------
In the above example, you can see we are storing many datatypes in the same string.

Operations on Lists:


For accessing each element in a list we should take the name of the list, in the above example the list name is "list1" and to access the second element of the list we have to print - "print(list1[1])". If you are doing programs then you should know the numbering starts with "0", not "1". So, here we would see "2" will be printed in the output.

So, that was the basic things about List and it's operations.



:Dictionaries:

We all know about Dictionaries. A storage space for storing lots of words and their meanings, and whenever we are trying to search for a specific word then we have to follow the easy method to find out the word that we are looking for.
Likewise, in Python, there is a concept of Dictionary. In Python, the Dictionary is the same as our regular Dictionaries. It is denoted by "{}".

Here is an Example of Dictionaries:

------------------------------------------------------------------------------------------------------------------
sample = {"key1":25, "key2":34, "key3":45}
print(sample)
------------------------------------------------------------------------------------------------------------------

The Output will be all things in the "sample". You can check this program into your Pycharm or any other IDLE. Here, you can see "key1" stores a data that is "25" as a normal dictionary do.

Operation on Dictionaries:

If we want to access any specific data that stored in the Dictionary then simply we have to do like below-

print(sample["key1"])

and it will print a value that has been stored in that "key1". Here the output will be "25".

:Tuples:

Tuples are the same thing as List. It stores data but of the same types. Means, in List we can use multiple data types in a single list. But in the Tuples, we can not store different data types. It is denoted by "()"

Here is an Example of Tuples:

---------------------------------------------------------------------------------------------------------------------
a = ("abs", "packs", "Muscles")
print(a)
---------------------------------------------------------------------------------------------------------------------

It will print all the elements that are associated with "a".


Operation on Tuples:

If we want to access any specific data that stored in the Dictionary then simply we have to do like below-
print(a[1])

and it will print "packs" as it is in the 1st position of the Tuple "a".

:Conclusion:

So, that was all about Lists, Dictionaries, and Tuples. As you can see all those things work the same. They have been used to Store data. No matter what data type it is. So, that's it. See you in the Next Tutorial.
Previous
Next Post »