Python Data Types and Data Structures

Python Data Types and Data Structures

·

3 min read

Python Data Types ⌨️

  • Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

  • Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

  • Python has the following data types built-in by default: Numeric(Integer, complex, float), Sequential(string,lists, tuples), Boolean, Set, Dictionaries, etc

  • To check what is the data type of the variable you used , wrtie : type(variable)

Data Structures 🏗️

  • Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

  • Lists : Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.

      # we use [] to write list
      List = [1, 2, 3, 4]
      # to access list using index
      print(List[0]) # index starts from 0 [0 1 2 3 4 5 6]
      # to sort the list 
      List.sort() #sorts them in alphabetic / numeric format
    
    • Accessing elements is done through zero-based indexing, allowing for easy retrieval of specific elements.

    • Python provides a wide range of built-in methods for list manipulation, including append(), pop(), extend(), and more

    • You can learn more about lists here .

  • Tuple: Python Tuple is a collection of Python objects much like a list but Tuples are immutable in nature i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.

    • Unlike lists, they are immutable, Once created, tuples cannot be changed. Elements cannot be added, removed, or modified

        tuple1 = (0, 1, 2, 3)
        tuple2 = ('python', 'tuple')
      
        # Concatenating above two
        print(tuple1 + tuple2)
      
    • You can learn more about tuple here .

  • Dictionary: Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized .

    • Dictionaries are not ordered collections, meaning elements are not indexed or sorted.

    • Access element using key :

        # to access element using key
        print(dict['key'])
      
    • You Can learn more about dictionary here .

  • Set: Sets are unordered collections of unique elements. They are useful when dealing with data that requires distinct values and set operations .

    • Like dictionaries, sets are unordered collections and do not support indexing.

  • You can learn more about set here .

Difference between List , tuple , Dictionary and set❗