Thursday, December 26, 2024
ad
HomeData SciencePython Data Types: A Detailed Overview with Examples

Python Data Types: A Detailed Overview with Examples

Explore various Python data types with examples and learn how to convert from one data type to another with this guide.

In computer programming, data type specifies a particular value that you can store in a variable. Understanding data type enables you to decide the operations that can be performed on it and the information that can be extracted from such data. Integer, date/time, and boolean are some of the common examples of data types.

Python is an extensively used programming language because of its simplicity and support for feature-rich libraries. Knowing the different Python data types is crucial to understanding how data can be queried in this computational language.

In this article, you will learn about various data types in Python with examples and how to find the type of any data point. It also provides methods to convert one data type into another, which will help you use Python more effectively for data-related tasks in organizational workflows. 

Python Data Types

Image Source

The Python data types are broadly categorized into five types as follows:

  • Numeric Data Type
  • Dictionary
  • Boolean
  • Set
  • Sequence Data Type

To know the data type of any entity in Python, you can use the built-in function type(). For example, to know the data type of x = 7, you can use the type() function in the following manner:

Now, let’s look at each one of these data types in detail.

Numeric Data Type

The numeric data type represents the data that has a numeric value. It is further classified into three types as follows:

Integer Data Type 

The integer data type consists of positive and negative whole numbers without decimals or fractions. Python supports integers of unlimited length and you can perform various arithmetic operations on these integers. This includes operations such as addition, subtraction, multiplication, division, or modulus.

In the example below, you can see that when you check the data type of x = 5 and y = -11, you get output as an int type.

Float Data Type

The float data type comprises numbers with decimal points or scientific notation. Python supports float data with accuracy up to 15 decimal points. 

This example shows different float data points supported by Python. 

Complex Data Type

The complex data type contains real and imaginary parts. In Python, the imaginary part is denoted by j instead of i, as in mathematics. In the example below, 1 – 2j is a complex number where 1 is the real part, and 2 is the imaginary part.

Dictionary Data Type

A Python dictionary is an unordered collection of data stored as key-value pairs, enabling faster data retrieval. You can create a dictionary by placing data records within curly brackets {} separated by comma. The key and value together are one element and are represented as key: value.

Both key and value can be of any data type; however, values can be mutable, while keys are immutable. The syntax to write a Python dictionary is as follows:

Dict_var = {key1:value1, key2:value2, ….}

Consider the following example of a Python dictionary:

Here, “Name”, “Age”, and “City” are keys while “Katie,” 25, and “London” are corresponding values.

Boolean Data Type

Python boolean data type represents one of the two values: True or False. It is used to determine whether a given expression is valid or invalid. Consider the following examples:

The output is:

To check the data type of the boolean value, you can use the following syntax:

This gives the following output:

Set Data Type

The set data type in Python represents an unordered collection of elements that are iterable but cannot be duplicated. It is created by enclosing individual elements in curly brackets {} separated by commas. The syntax to write set is as follows:

Set1 = {element1, element2, element3,….}

The following example shows a set data type in Python:

You can add or remove elements from sets as they are mutable. 

However, you cannot directly change the individual elements in the set.

Sequence Data Type

The sequence data type allows you to store and query the collection of data points. There are three sequence data types in Python: string, lists, and tuple. Let’s look at each of these in detail.

String

It is a sequence of characters enclosed within a single, double, or triple quotation. 

This gives the following output:

To access individual characters in a string, you can use a technique called indexing. In positive or forward indexing, you can create a string containing n number of elements from 0 to (n-1). On the contrary, negative indexing is a backward indexing technique where the last element is numbered as -1 and the first as (-n).

Image Source

To get a sub-string from a string, you can opt for slicing operations as shown below:

String data types allow you to perform the following operations:

  • Concatenation: Using this process, you can join together two or more strings using the ‘+’ operator.
  • Repetition: You can multiply a string by an integer to create a specified number of copies.
  • Replace: The replace() allows you to replace a character in a string. 
  • Upper and Lower Case: You can convert a string in upper or lower case using the upper() and lower() functions.

Output:

  • Checking the Case of a String: To check whether the string is in lower or uppercase, you can use the islower() or isupper() functions. The output is a boolean data type.

Output:

  • Split: You can split a string into individual characters separated by space using split().

Output:

Lists

Python lists are like arrays containing elements in an ordered manner. You can create a list simply by placing individual elements separated by commas within square brackets [ ]. Its syntax is:

List1 = [element1, element2, element3,…..] 

Here is an example of a Python list:

Not all the elements in the list need to be of the same data type. For example, the below list contains a mix of string, integer, and float data types. 

To fetch elements in a list, you can follow the indexing method as in a string. Similarly, you can also perform concatenation and repetition by multiplying with an integer on lists. Some of the other operations that you can perform on a list are as follows:

  • Append: You can use append() to add a new element to the list.

Output:

  • Extend: extend() is used to add all elements from an iterable, such as a list, tuple, or set, to the end of a given list.

Output:

  • Pop: To remove the last element from the list, you can use pop().

Output:

Tuple

A tuple is a sequential data type similar to a list as it supports indexing, repetition of elements, and nested objects like a list. However, unlike a list data type, a tuple is immutable. You can easily create a tuple by placing elements in round brackets separated by commas, as shown below:

Tuple1 = (element1, element2, element3,….)

The operations supported by the tuple are similar to those supported by the Python list. Now, in the next section, you will learn how to convert one Python data type to another.

Python Data Type Conversion

Python allows you to convert one data type to another using the following two methods:

  • Python Implicit Type Conversion
  • Python Explicit Type Conversion

Let’s look at each one of these conversion techniques in detail:

Python Implicit Type Conversion

In implicit type conversion, the Python data type of output obtained through an operation automatically gets converted to another form. For example, you want to add x = 4 with an int data type and y = 7.1 with a float data type. The data type of output z will be float as shown below:

Python Explicit Type Conversion

You can manually change the Python data type according to your requirements using the explicit type conversion method. Some of the functions that can be used for explicit type conversion are as follows:

FunctionConversion
int()string, float -> int
float()string, int -> float
str()int, float, list, tuple, dictionary -> string
list()string, tuple, dictionary, set -> list
tuple()string, list, set -> tuple
set()string, list, tuple -> set
complex()int, float -> complex

Here is an example of explicit conversion of int data into float data type:

Final Thoughts

Python’s support for different data types makes it a rich and versatile programming language. Understanding these data types is crucial for coding and efficient problem-solving in Python. It simplifies managing large datasets and performing complex calculations. 

This article explains Python basic data types in a comprehensive way, along with data type conversion methods. You can utilize this guide to leverage Python capabilities to the maximum extent and become an effective programmer. 

Subscribe to our newsletter

Subscribe and never miss out on such trending AI-related articles.

We will never sell your data

Join our WhatsApp Channel and Discord Server to be a part of an engaging community.

Analytics Drift
Analytics Drift
Editorial team of Analytics Drift

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular