Type Inference

In Python we do not declare data types for any of the variables. Python is a dynamic language and is automatically determine the data type using type inference. When we assign a value to an identifier this become a variable. The type of the variable is determined by the literal type or by expression that is used to assign a value to the variable.

The assignment is done using equal symbol: “=”. Using this symbol into a logical expression is not allowed and is generation an error. For value comparison we use “==” in Python. A common mistake is to use “=” as a relation operator when we should use “==”. The results can be unpredictible and the compiler may not detect an error in this case.


# Examples of type inference

v = 100    #declare an integer variable
r = 100.25 #declare a float variable

# Strings can be declared with " or '
s1 = "this is a string" 
s2 = 'this is a string'


Python collections are implemented using a comprehensive mathematical notation. This makes python easy to work with and intuitive. Python define following collection types:


# Define a list of elements
my_list = [1,2,3,4,5]

# Define a tuple of elements
my_tuple = (1,2,3,4,5)

# Define a set of elements
my_set = {1,2,3,4,5}

# Define a dictionary
my_dic = {"a":1,"b":2,"c":3}


A list is a dynamic collection. You can add elements and remove elements from a list. The elements can have any data type and can be mix: strings with numbers. The element is indexed from 0 to number of elements -1. So you can use a number or subscript to refer to a particular element from the list.  A list is using notation: [1,2,3]. The empty list is [].

Following expressions are true:

my_list[0] == 1

my_list[4] == 5

my_list[-1] == 5


A tuple is like a list except it is not dynamic. Once you set a tuple the number of elements and the value of elements can’t be modified. A tuple is an immutable structure. You can only read element value from a tuple using a subscript. A tuple is using notation: (1,2,3). Empty tuple is the unit: ()

Following expressions are true:



A set is like a list except that all elements are unique. You can’t have duplicates. The elements can be any kind of data. Elements of a set can’t be addressed by a subscript. You can test if a value is in the set using the operator “in” or “not in”. A set is using literla notation: {1,2,3}. Empty set is {}.

Following expressions are true:

1 in my_set


A dictionary is a hash table. It is a pair of key and value like an index of a book. The key is unique and the value can be any kind of data type and can have duplicates. To define a dictionary we can use literal notation: {key:value, key:value}

The key can be integer, string or tuple. Python is sorting a dictionary by the key.  To get a value from dictionary you must have the key. You can use the key like a subscript index to get or set the value.

Following expressions are True:

my_dic[“a”] ==1

my_dic[“b”] ==2

Read next: Functions