Inbuilt data structure in Python

(121 Views)


If you will ask a programmer then he will tell that python is his favorite programming language. Who would not like to code in a few lines? It is free and open-source along with easy to learn and use features. There are many applications of python including machine learning, data science, development of APIs and so on. In this article, you will learn about the inbuilt data structure in python

Inbuilt data structure in Python

Python has four built-in data structures- lists, dictionary, tuple, and sets.

  1. Lists
  2. It contains an ordered sequence of items. This data structure is mutable and non-scalar i.e you can change items by adding or removing. As lists are built-in, so, there is no need for creating them. It can be used regardless of the type of object. The elements are stored in a square bracket separated with commas. Lists in python look similar to C arrays in the python interpreter and it works like an array of pointers.

    #for displaying elements list=['asbd', 'sffs', '44'] for i in list: print (i)

    Output

    asbd sffs 44

  3. Tuples
  4. It is like lists containing an ordered sequence of elements but this data structure isn’t mutable. You can’t change elements. Elements must be in parenthesis separated with commas. It has a fixed length because of immutable nature.

    >>>#for displaying elements tuple=('efgh', 54, 56, 'rf') for i in tuple: print (i)

    Output

    efgh 54 56 rf

  5. Dictionary
  6. A dictionary consists of keys(name) and values (details). Make sure while choosing a key that it’s unique. In this data structure, keys can be immutable but we can use both immutable and mutable objects for values.

    # Program for illustrating # dictionary # Create a new dictionary d = dict() # or d = {} # Add a key - value pairs to dictionary d['abc'] = 234 d['efg'] = 567 # print the whole dictionary print (d) # print only the keys print (d.keys()) # print only values print (d.values())

    Output

    {'abc': 234, 'xyz': 567} dict_keys(['abc', 'xyz']) dict_values([234, 567])

  7. Sets
  8. It refers to the collection of simple and unique objects but in an unordered manner. Sets are used when an object in a collection matters more than its order. Elements of a set must contain an immutable data type.

    #Understanding working of set # Creating two sets set1 = set() set2 = set() # Adding elements to set1 for i in range(2, 5): set1.add(i) # Adding elements to set2 for i in range(4, 7): set2.add(i) print("Set1 = ", set1) print("Set2 = ", set2) print("\n")

    Output

    ('Set1 = ', set([2, 3, 4])) ('Set2 = ', set([4, 5, 6]))

Knowing these data structure will help you at many situations while programming. It is the basic of python programming that we want you to know.

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote

        


Comments



Search

Earn Money by Submitting Articles
Start submutting articles. Click here to get started
Play 2048 Game Online

Play Duckhunt Online
Search Tags

    All data structures used in python

    Top 10 python language syntax

    Data structures every python beginner should know