Python Data Types (Complete List, Types, and Examples)

By Abhishek Jain Last updated : December 25, 2023

Python Data Types

In Python, the data types are used to define the type of a variable which tells the compiler/interpreter about the data to be stored in a variable.

Complete List of Python Data Types

Below is the complete list of Python data types:

  1. Python Numeric Types
    1. Integer (int)
    2. Float (float)
    3. Complex (complex)
  2. Python None Type (None)
  3. Python Sequence Types
    1. String (str)
    2. List (list)
    3. Tuple (tup)
  4. Python Set Types
    1. Set (set)
    2. Frozenset (frozenset)
  5. Python Mapping Type (Dictionary) (dict)
  6. Python Boolean Type (bool)
  7. Python Binary Types
    1. Bytes (bytes)
    2. Byte Array (bytearray)
    3. Memory View (memoryview)

Types of Python Data Types

The following are the different types of data types used in Python programming language:

1. Python Numeric Types (Number Types)

Number data type stores Numerical Values (See: Python numeric types). This data type is immutable i.e. value of its object cannot be changed (we will talk about this aspect later). These are of three different types:

  1. Integer
  2. Float/floating point
  3. Complex

1.1. Python Integer Type

Integers are the whole numbers consisting of + or - sign with decimal digits like 100000, -99, 0, 17.

Example:

This example demonstrates the use of numeric type (integer).

# Creating variables
a = 108
b = 10000008
c = 99999999999100088999999

# Printing values and types
print("Value of a:", a)
print("Value of b:", b)
print("Value of c:", c)
print("Type of a:", type(a))
print("Type of b:", type(b))
print("Type of c:", type(c))

Output:

Value of a: 108
Value of b: 10000008
Value of c: 99999999999100088999999
Type of a: <class 'int'>
Type of b: <class 'int'>
Type of c: <class 'int'>

1.2. Python Floating Point Type

Numbers with fractions or decimal point are called floating point numbers.

A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963.

Example:

This example demonstrates the use of numeric type (float).

# Creating variables
a = 108.108
b = 10000008.108
c = 1.23456789e-05

# Printing values and types
print("Value of a:", a)
print("Value of b:", b)
print("Value of c:", c)
print("Type of a:", type(a))
print("Type of b:", type(b))
print("Type of c:", type(c))

Output:

Value of a: 108.108
Value of b: 10000008.108
Value of c: 1.23456789e-05
Type of a: <class 'float'>
Type of b: <class 'float'>
Type of c: <class 'float'>

1.3. Python Complex Type

Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by j instead of i, so 1+0j denotes zero imaginary part.

Example:

This example demonstrates the use of numeric type (complex).

# Creating variable
a = 108 + 0j

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

# Printing Real and Imaginary parts of
# complex number
print("Real part:", a.real)
print("Imaginary part:", a.imag)

Output:

Value of a: (108+0j)
Type of a: <class 'complex'>
Real part: 108.0
Imaginary part: 0.0

2. Python None Type

This is special data type with single value. It is used to signify the absence of value/false in a situation. It is represented by None.

Example:

This example demonstrates the use of None type.

# Creating variable
a = None

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: None
Type of a: <class 'NoneType'>

3. Python Sequence Types

A sequence is an ordered collection of items, indexed by positive integers. It is combination of mutable and non-mutable data types. Three types of sequence data type available in Python are Strings, Lists & Tuples.

3.1. Python String

String is an ordered sequence of letters/characters. They are enclosed in single quotes (' ') or double (" "). The quotes are not part of string. They only tell the computer where the string constant begins and ends.

If we are not sure, what is the data type of a value, Python interpreter can tell us:

Example:

This example demonstrates the use of sequence type (string).

# Creating variables
a = "Hello!"
b = 'Hello, world!'

# Printing values and types
print("Value of a:", a)
print("Value of b:", b)

print("Type of b:", type(b))
print("Type of b:", type(b))

Output:

Value of a: Hello!
Value of b: Hello, world!
Type of b: <class 'str'>
Type of b: <class 'str'>

3.2. Python List

Python list is also a sequence of values of any type. Values in the list are called elements / items. These are mutable and indexed/ordered. List is enclosed in square brackets.

Example:

This example demonstrates the use of sequence type (list).

# Creating variable
a = ["India", "UK", "USA"]

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: ['India', 'UK', 'USA']
Type of a: <class 'list'>

3.3. Python Tuples

Python tuples are a sequence of values of any type, and are indexed by integers. They are immutable. Tuples are enclosed in (). We have already seen a tuple, in Example 2 (4, 2).

Example:

This example demonstrates the use of sequence type (tuple).

# Creating variable
a = ("India", "UK", "USA")

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: ('India', 'UK', 'USA')
Type of a: <class 'tuple'>

4. Python Set Types

The set and frozenset are the set types in Python.

4.1. Python Sets

Python set is an unordered collection of values, of any type, with no duplicate entry. Sets are immutable.

Example:

This example demonstrates the use of Python set.

# Creating variable
a = {"India", "UK", "USA"}

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: {'UK', 'India', 'USA'}
Type of a: <class 'set'>

4.2. Python Frozen Sets

Python frozen set is just an immutable version of a set object. Which freezes a set to make it unchangeable.

Example:

This example demonstrates the use of Python frozen set.

# Creating variable
a = frozenset({"India", "UK", "USA"})

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: frozenset({'India', 'USA', 'UK'})
Type of a: <class 'frozenset'>

5. Python Mapping: Dictionaries

This data type is unordered and mutable. Dictionaries fall under Mappings.

Python dictionaries can store any number of python objects. What they store is a key – value pairs, which are accessed using key. Dictionary is enclosed in curly brackets.

Example:

This example demonstrates the use of Python mapping type (dictionary).

# Creating variable
a = {"Name": "Bharat", "Age": 21, "Height": 5.7}

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: {'Name': 'Bharat', 'Age': 21, 'Height': 5.7}
Type of a: <class 'dict'>

6. Python Boolean Type

Python Boolean data type accepts two values either True or False. Generally, it is used for truth values.

Example:

This example demonstrates the use of Python Boolean type.

# Creating variables
a = True
b = False

# Printing values and types
print("Value of a:", a)
print("Value of b:", b)

print("Type of a:", type(a))
print("Type of b:", type(b))

Output:

Value of a: True
Value of b: False
Type of a: <class 'bool'>
Type of b: <class 'bool'>

7. Python Binary Types

The bytes, bytearray, and memoryview are the Binary data types.

7.1. Python Bytes

The bytes type is used to create bytes type variable, the value should start by literal b.

Example:

This example demonstrates the use of Python binary type (bytes).

# Creating variable
a = b"Hello"

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: b'Hello'
Type of a: <class 'bytes'>

7.2. Python Byte Array

The bytearray is a mutable sequence of integers (between 0 to 255). Generally, it is used to work with binary data.

Example:

This example demonstrates the use of Python binary type (bytearray).

# Creating variable
a = bytearray(5)

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: bytearray(b'\x00\x00\x00\x00\x00')
Type of a: <class 'bytearray'>

7.3. Python Memory View

The memoryview is used to get a memoryview object based on the bytes or bytearray parameter.

Example:

This example demonstrates the use of Python binary type (memoryview).

# Creating variable
a = memoryview(bytes(5))

# Printing value and type
print("Value of a:", a)
print("Type of a:", type(a))

Output:

Value of a: <memory at 0x7f7b22799d80>
Type of a: <class 'memoryview'>

Web References

Python Tutorial

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.