Python Data Types


Python Data Types

Python can save data of different types and as the name suggests, Data Type tells the type of data that a variable can hold and the operation that can be performed on the data. Every value we declare in Python or every variable we use in python belongs to some data type. We can say that these data types are the classes and variables are the objects of the classes.

Different Classifications of Data Types

In Python, there are multiple ways to classify data types. Thought the main data types remain the same but just way to categorize changes. Let us go through each of them one by one:

1. Based on Type of Data

Types of DataclassDetails
Numericint, float, complexholds numeric values
Stringstrholds sequence of characters
Sequencelist, tuple, rangeholds collection of items
Mappingdictholds data in key-value pair form
Booleanboolholds either True or False
Setset, frozeensethold collection of unique items
Binary typesbytes, bytearray, memoryviewholds binary data

2. Primitive, Container and User Defined:

  • Basic/Primitive – int, float, complex, bool, string.
  • Container/Collections – list, tuple, set, dict.
  • User Defined – class.
Python Data Types

3. Mutable and Immutable

Mutable Data Types: Data types in which the value assigned to a variable can be changed.

Immutable Data Types: Data types in which the value assigned to a variable cannot be changed.

Mutable and Non-Mutable Data Types

Variable Type – type() function

In Python, we do not declare the type of variable that we are using. So at a later stage, if we want to know the type of variable, then we use the type() function.

We are exploring the type function in detail in the below Data Types section.

Data Types in Brief

These are the different data types python supports:

Data TypeExampletype() functionvalue returned
by type()
Integera = 5type(5)int
Floata = 5.5type(5.5)float
Complexa = 5+7jtype(5+7j)complex
Booleana = True, a = Falsetype(True)bool
Stringa = “John”, a = ‘Steve’type(“Steve”)str
Lista = [1,2,3,4,5]type([1,2,3,4,5])list
Tuplea = (1,2,3,4,5)type((1,2,3,4,5))tuple
Seta = {1,2,3,4,5}type({1,2,3,4,5})set
Dictionarya = {‘name’:’Steve’, ‘company’:’apple’}type({“name”:”Steve”,id:25})dict

Integer

An Integer is a whole number. It can be positive or negative and can be of any size. An integer is denoted by an int keyword. Some examples of Integers are -225, 350, 987654321987654321, etc.

Float

Any number that contains a decimal point is a floating-point number or a float in short. There is no size limit for floats as well. A floating-point number is denoted by a float keyword. Some Examples of float are -23.7, 5698.5, 987654321987.21.

Complex

These are the numbers that contain a real part and an imaginary part. Both are separated by the ‘+’ sign, and j is appended with the imaginary part, which comes after the + sign. Complex numbers are denoted by a complex keyword. An example of a complex number is 7+8j, here 7 is a real number and 8 is an imaginary number. Complex numbers are not commonly used in our day-to-day programming, but they are used by expert mathematicians.

Boolean

It can take any of the two values, either True or False. Note that the first letter should be capitalized. It is named after a mathematician, George Boole. Boolean is denoted by the bool keyword. Some examples are x = True, y = False, etc.

String

A collection of Unicode characters is referred to as a string. A string can be enclosed in single, double, or triple quotes. A few examples of strings are ‘Logical Python’, “Logical Python”, “””Logical Python”””, etc.

List

In simple words, we can say that a list is a collection of data items. Square brackets [] are used to define the lists, and data items are separated by a comma. An example of a list is [“Steve”, “USA”, 654789].

Tuple

The tuple is the same as a list, but it is immutable which means, values inside the tuple cannot be changed. Parentheses () are used to define the tuple, with values separate by commas. An example of a tuple is (“Steve”, “USA”, 654789). Note that, this is the same example we used for the list, but here square brackets [] are replaced by parentheses ().

Set

A set is a container that contains only unique values. A set is defined using curly brackets {} and we have comma-separated values inside curly brackets. An example of a set is {2, 4, 9, 12}.

Dictionary

It is a collection of key-value pairs. We use curly brackets {} to define the dictionary, and inside the curly brackets, we have the comma-separated key : value pairs. An example of a dictionary is {“Name” : “Steve”, “Country” : “USA”, “Id” : “654789”}.

Here is an example of each data type:

Integer

In [1]:
a = 25
print(type(a))
<class 'int'>

Float

In [2]:
a = 25.5
print(type(a))
<class 'float'>

Complex

In [3]:
a = 5+6j
print(type(a))
<class 'complex'>

Boolean

In [4]:
a = True
print(type(a))
<class 'bool'>

String

In [5]:
a = 'Python'
print(type(a))
<class 'str'>

List

In [6]:
a = [1,2,3,4]
print(type(a))
<class 'list'>

Tuple

In [7]:
a = (1,2,3,4)
print(type(a))
<class 'tuple'>

Set

In [8]:
a = {1,2,3,4}
print(type(a))
<class 'set'>

Dictionary

In [9]:
a = {'firstname':'Steve', 'lastname':'Smith'}
print(type(a))
<class 'dict'>

Manually Specify Data Type

Though, Python assigns the data type to the variable of its own, but we can also manually specify the Data Type using the following constructor functions:

Data TypeExample
Stringname = str(“Steve”)
Integerx = int(5)
Floatx = float(8.8)
Complexx = complex(2+5j)
Listnames = list((“Steve”, “Bill”, “Elon”))
Tuplenames = tuple((“Steve”, “Bill”, “Elon”))
Dictionarydetails = dict(name=”Bill”, age=66)
Setnames = set((“Steve”, “Bill”, “Elon”))
Booleanx = bool(5)

Resources

Handwritten Notes

References:

  1. Python Data Types
  2. Python type() functions
  3. Python Official – Data Types