Python Data Types

Python has four in-built data types, which can be used to hold a collection of objects.

Remember, everything is an object in python.

  1. List
  2. Tuple
  3. Dictionary
  4. Set

1.List:

If you are aware of arrays (in other programming language), then you can think of arrays and lists (in Python) are similar (although it’s not the exact same). Below are the properties of lists:

A list holds collection of objects. Lists can be heterogeneous. This means the objects can be of same types or different types.

Lists are Ordered. This means the objects in the list are stored by the interpreter the order in which you list them. They are indexed like arrays. The index starts from 0 onwards.

    For example, Fruits = [‘Apple’, ‘Orange’, ‘mango’]

    In this example, Fruits is a list, and the first object ‘Apple’ has an index of  0, the second object ‘Orange’ has an index of 1, and so on.

  • Lists are mutable. This means you can change/add/delete the objects in a list at any time.
  • Lists are represented by square brackets [].

       Syntax: List Name= [ object1, object 2, ………]

2.Tuple:

Tuples are same as Lists, expect that Tuple is immutable. This means that once you create a tuple, the objects in the tuple cannot be changed afterwards. Think of tuple as a constant list.

All other properties of lists like ordered, indexed, heterogeneous are also applicable to tuple as well.

Tuples are represented by open brackets ().

          Syntax: Tuple Name = (object1, object 2, ………)

3.Dictionary:

It stores key/Value pairs. The key must be unique. The values associated with the keys can be heterogeneous (any object).

Dictionary in python is mutable like lists.

However, Dictionary is unordered. This means the key/value pairs in a dictionary are stored by the interpreter in a different order than you mention them.

In layman term, a dictionary is a two-column and multi row table.

Dictionary is represented by curly brackets {}.

               Syntax: Dictionary Name = {‘key1’: ‘Value1’, ‘Key2’:’Value2’, ……..}

4. Set:

A set is a collection of unique objects. It does not allow duplicate data to present.

Its unordered and mutable like dictionary.

It is represented by curly brackets {}.

Syntax: Set Name = {object1, object 2, ………}

Leave a Reply

Your email address will not be published.