Python Data Types
Overview
In Python, data types are classifications that specify the kind of values a variable can hold. There are several built-in data types in Python, broadly categorized into Numeric, Sequence Types, Mappings, Sets, and Boolean.
Introduction to Data types in Python
In programming, we often work with a variety of values: sentences, numbers, collections of items, and more. For example, a student's marks, represented by a number, would be considered an integer data type in Python, while a student's name, which is a sequence of characters, would be a string data type.
In Python, every value is associated with a data type because Python is an object-oriented language where everything is treated as an object.
Before proceeding further it's important to know that data types are also classified on the basis of their mutability. Mutable data types can be modified after creation, whereas Immutable data types can't be modified after creation.
Data Types in Python
Python is an object-oriented high-level programming language that offers a vast variety of data types helping in the implementation of various applications.
Following are the data types in Python
- Numeric Data types
- Sequence Data types
- Dictionaries in Python
- Booleans in Python
- Sets in Python
1. Numeric Data types in Python
Numeric data types in Python represent numbers and are categorized into three types:
-
Integers in Python
Integers, as you know in mathematics are numbers without any fractional part. They can be 0, positive or negative. There is no limit to how long an integer can be in Python. They are represented by int class.
Syntax:
Integer numbers are of int type. It is just written as a number. Here's an example of Integer data type:
Output:
-
Floating Point numbers in Python
Floating point numbers (float) are real numbers with floating point representation, they are specified by a decimal point. They are represented by the float class.
Syntax:
Floating point numbers are of float type. It is written as a number with a decimal point. Here's an example of Float data type:
Output:
-
Complex numbers in Python
Complex numbers in python are specified as (real part) + (imaginary part)j. They are represented by the complex class.
Syntax:
Complex numbers are of complex type. They are written in the form a+bj in which a is the real value and b is the imaginary value, where j represents the imaginary part. Here's an example of a Complex data type:
Output
Note: In Python, the type() function is used to determine the data type of a value or a variable. To know more about type() function Click here.
Output:
In the above example, we are using the type() function to show the type of data. It can be seen that "10" is an int type, "10.0" is a float type, and "10+10j" is a complex type.
2. Sequence Data types in Python
Sequence data types in Python are an ordered collection of similar or different values. They are also called container data types as they usually contain more than one value. We can access elements of a sequence data type by indexing. String, list, and tuple are the different types of containers used to store the data in a sequential manner.
-
Strings in Python
A string can be defined as a sequence of characters enclosed in single, double, or triple quotation marks. While in most cases single and double quotation marks are interchangeable. Triple quotation marks are used for multi-line strings. It is an immutable data type, i.e. its values cannot be updated. String is represented by string class.
Syntax:
Strings are of string type, and are represented by enclosing in quotation marks.
Output:
In the above example, we are demonstrating Python string, using single, double, and triple quotation marks. While single and double quotation marks are used for normal strings, triple quotation marks are used for multi-line strings.
Accessing elements of a string
Any character of a string can be accessed using indexing. Python also allows us to use negative indexing to access characters even from the back of a string.
Note: Indexing of a sequence starts from 0.
Output:
In the above example, we are accessing elements of the string using indexing. We are printing the first character of the string using the 0th index and the last character of the string using the -1th index (as Python supports negative indexing).
-
Lists in Python
Lists are an ordered sequence of one or more types of values. They are just like arrays in other languages. They are mutable, i.e. their items can be modified.
Syntax:
Lists are of the type list. They are created by enclosing items (separated by commas) inside square brackets [].
Output:
In the above example, we are creating a list by enclosing the elements inside square brackets[] and then printing it. In the second line, we are printing its type using the type() function.
Accessing elements of a list
Elements of a list can be accessed by referring to their index numbers, negative indexes to access the list items from its back.
Output:
In the above example, we are accessing the elements of the list using indexing. At first, we are accessing the first element of the list by referring to its index "0" (Index number = Position of the element - 1) then we are referring to the fourth element by its index "3" and the last element of the list is accessed using negative indexing.
-
Tuples in Python
Like lists, Tuples are also an ordered sequence of one or more types of values except that they are immutable, i.e their values can't be modified. They are represented by the tuple class.
Syntax:
Tuples are of tuple type. They are created by a sequence of values separated by a comma with or without parentheses "()" for grouping of the sequence.
Output:
In the above example, we are creating a tuple by enclosing the elements inside parentheses and then printing it. In the second line, we are printing its type using the type() function.
Accessing elements of a tuple
Elements of a tuple can be accessed by referring to their index numbers, negative indexes to access the tuple items from its back.
Output:
In the above example, we are accessing the elements of a tuple using indexing. At first, we are printing the first element using the 0th index, then we are printing its third element using the 2nd index, at last, we are printing its last element using negative indexing.
Check out this article to learn more about Tuples in Python.
3. Dictionaries in Python
In Python, Dictionaries are an unordered collection of key-value pairs (key: value). This helps in retrieving the data and makes the retrieval highly optimized, especially in cases of high volume data. Keys can't be repeated in a dictionary while values can be repeated. Dictionaries are mutable, i.e. their values can be modified.
Syntax:
Dictionaries are of the type dict. The elements of a dictionary are enclosed in curly brackets {}, where each element is separated by a comma , and key-value pair by a colon :.
Output:
In the above example, we are creating a dictionary by enclosing the key-value pair in curly brackets {} then we are printing it. In the second line, we are printing its type using the type() function.
Accessing values of a dictionary
Values of a dictionary are accessed by referring to their keys.
Output:
In the above example, we are accessing the values of a dictionary by referring to their keys, in the first line we are referring to Tom using the key Name, then we are referring to 50 using the key Age, at last, we are referring to Mission Impossible using the key Movie.
4.Booleans in Python
Boolean is a data type that has one of two possible values, True or False. They are mostly used in creating the control flow of a program using conditional statements.
Syntax:
The True value in the boolean context is called "truthy" and False value is called "falsy".
Output:
In the above example, we are printing the boolean variables a (True) and b (False), then we are printing their types using the type() function.
Also to learn about bool() in Python, Click here
5. Sets in Python
Sets in Python are an unordered collection of elements. They contain only unique elements. They are mutable, i.e. their values can be modified.
Syntax:
Sets are of the type set. They are created by elements separated by commas enclosed in curly brackets {}.
Output:
In the above example, we are creating a set by enclosing the elements inside curly brackets "{}" then we are printing it. In the second line, we are printing its type using the type() function. While creating the set, we have put two duplicate values 8 but when we printed it, 8 occurred only once, that's because sets only have unique elements.
Conclusion
- Data types are classes in Python, whereas variables are objects of these classes.
- Each and every value or variable has a data type in Python.
- Numeric data types in python represent data with numeric values.
- Sequence data types in python are an ordered collection of items.
- Dictionary (also called maps, in other languages) helps in the optimized retrieval of values.
- Boolean has two types of values, True or False, True for truthy values, False for falsy values.
- Sets are an unordered collection of unique elements (like set theory in mathematics).