Python Data Types

Table of contents

Python Data Types

Python data types are the types of values assigned to variables. Python is a dynamically typed language. That means you don’t need to declare data type while declaring variables. The interpreter automatically assigns data type based on the value stored to a variable. Let us discuss all the data types supported by python briefly here. We cover each data type in more detail including methods, functions, and operations supported by them later in the tutorial. The standard python data types are as follows:

Python Number

The number data type in python supports numerical values only such as 10, 99.99, 5 + 6j, etc. There are three types of python numbers.

  • Integers: Integers in python are any numbers without a decimal point. They can be of any length. Integers fall under int python class. For example, 1, 100, 9999, etc.
  • Floating point: The floating point numbers contain decimal points and integer part and floating points are separated by the dot (.). Python floating point numbers support up to 15 decimal places. For example, 1.192, 20.0, 99.9999, etc.
  • Complex: Complex numbers in python are in the form of x + yj. Here, x is the real part and y is the imaginary part. For example: 4 + 5j, 1 + 2j, etc.

For example:

a = 100 # assigning integer 100 to a variable a
b = 99.99 # assigning floating point number 99.99 to a variable b
c = 3 + 2j # assigning complex number 3 + 2j to a variable c

Python String

The python strings are the sequences of characters within single or double quotes (”’ or “””). Triple quotes (”’, “””) can be used for multi-line strings. A single character in the python is also represented as a string, unlike C or C++ which have separate data types to store single characters.

For example:

x = 'This is a string example'
y = "This is also a string example"
z = '''This is a multi-line string

Python List

Python list is one of the most used data types in python and it is an ordered sequence of items. The items in a single list can be of different data types. The items in the list are separated by commas (,) and enclosed within square brackets [ ]. The individual item can be accessed using the indexes and the indexing in the python lists start from 0. Lists are mutable, that is the items in the list can be modified through the course of the program.

For example:

li = [100, 200, 99.99, "yonep"] # a list with four items
print(li[0]) # prints 100, that is the first item in the list
print(li[2]) # prints 99.99, that is the third item in the list

Python Tuple

Python tuples are also similar to lists except that tuples are immutable. That is, the items cannot be modified once the tuple is created. The items in the tuples are also separated by commas (,) and enclosed within parentheses. Tuples are ordered and also allow to have items with different data types.

For example:

tu = (100, 200, 99.99, "yonep") # a tuple with four items

Python Set

Python sets are unordered sequences which can contain unique items only. The items in tuples are inside braces { } and are separated by commas (,). If you include duplicate values in the set while defining it, the python interpreter automatically deletes duplicate values and keeps single instances of the items in the set. The set data type in python does not support indexing as it is unordered, unlike a list or tuple.

For example:

se = {100, 200, 44.44, "Python"} # a set with four items

Python Dictionary

Python supports a database-like data type called dictionary which allows you to store key-value pairs. The keys in the dictionaries must be unique as they are used to access the values associated with them. Similar to lists, tuples or sets, the key-value pairs in the dictionaries are separated by commas (,) and both keys and values can be of any data type supported by python. The items are enclosed within braces { }. The key and value in each key-value pair are separated by a colon (:) with the key at the left side and value at the right side of the colon.

For example:

di = {'name' : 'yonep', 'age' : 1, 20 : 'value'}
""" a dictionary with three key-value pairs where 'name', 'age' 
and 20 are keys and ‘yonep’, 1 and ‘value’ are values 
associated with the keys respectively """
print(di['name']) # prints value associated with key 'name, that is'yonep'

Python Boolean

The boolean data type in python is used to represent truth values (True or False). Unlike strings, you don’t need to enclose truth values within ” or “”.

For example:

a = True # a boolean variable storing True
b = False # a Boolean variable storing False