You are here:

Exploring Built-in Data Types in Python

Exploring Built-in Data Types in Python

Overview

Data types in Python programming differ somewhat from other languages. Python employs dynamic typing, meaning type safety verification occurs at runtime. Python associates a data type with an object, and during specific operations, it checks if the operation is valid for that object. In this guide, I will describe the various classes of data types and teach you how to convert from one type to another.

Requirements

  • Linux system (Windows system with Python is also acceptable)
  • Python installed
  • Basic programming knowledge

Python Data Types

Numeric Types:

  1. int: Integer with 32-bit precision.
  2. long: Long integers with unlimited precision.
  3. complex: Complex numbers with real and imaginary parts, both floating-point.
  4. float: Floating-point numbers, typically implemented using double in C.

Sequence Types:

  1. str: String, a sequence of Unicode symbols in Python 3.x and a sequence of 8-bit symbols in Python 2.*
  2. list: List of elements.
  3. bytes: Python 3.x only, a sequence of integers ranging from 0 to 255.
  4. bytearray: Python 3.x, a mutable sequence of bytes.
  5. tuple: Immutable sequence of elements.

Set Types:

  1. set: Introduced since Python 2.6+, a collection of objects with no specific order.
  2. frozenset: Introduced since Python 2.6+, an immutable set.

Mapping Type:

  1. dict: Python dictionaries, unordered collections of arbitrary objects with key-based access, also known as hashmaps or associative arrays.

Boolean Type:

  1. bool: True or False, interchangeable with 0 and 1.

Creation of objects

Literal Representation of Various Data Types in Python:

  1. Decimal Numbers: Represented directly.

  2. Hexadecimal Numbers: Prefixed with 0x, e.g., 0xff for 255.

  3. Octals:

    • In Python 2.x: Added 0, e.g., 0111 represents 111.
    • In Python 3.x: Added 0o, e.g., 0o111 represents 111.
  4. Floating Points: Represented directly.

  5. Long Integers:

    • Represented directly.
    • Optionally, append L at the end, e.g., 1L for 1 long int.
  6. Complex Numbers: Comprise a real number and an imaginary part with a ‘j’, e.g., 6+7j.

  7. Strings: Enclosed in single or triple quotes, e.g., ‘word’ or ”’word”’. Double quotes (“word”) are also valid.

  8. Lists: Defined using square brackets, e.g., [‘abc’, 1, 2, 3].

  9. Tuples: Enclosed in parentheses and separated by commas, e.g., (8, ‘some words’).

  10. Dictionaries (Dicts):

    • Enclosed in curly braces.
    • Elements consist of key-value pairs separated by a colon.
    • Elements are separated by commas.
    • Example: {‘name’: ‘Alex’, ‘occupation’: ‘python developer’}.

Difference between tuple and list:

  1. Mutability:

    • Tuple: Immutable; cannot be modified after creation.
    • List: Mutable; can be modified after creation.
  2. Heterogeneity vs. Homogeneity:

    • Tuple: Heterogeneous; elements can be of different data types.
    • List: Homogeneous; elements are of the same data type.
  3. Structure vs. Order:

    • Tuple: Has structure; elements may have different meanings.
    • List: Has order; elements are arranged in a specific sequence.
Tuple = (1,2)
List = [1,2]

Size

x = tuple(range(1000))
y = list(range(1000))

x.__sizeof__() # 8024
y.__sizeof__() # 9088

Permitted operation:

x = [1,2]
y[0] = 3

y = (1,2)
a[0] = 3 // produces an error

Also, it’s important to note that you can’t delete or sort elements in a tuple. However, you can add a new element to both a tuple and a list:

x = (1,2)
y = [1,2]
x += (3,) # (1, 2, 3)
y += [3] # [1, 2, 3]

Usage: since tuples are immutable, you can use them as keys in a dictionary. On the other hand, lists are mutable and cannot be used as dictionary keys. This distinction in usage showcases the practical implications of their immutability.

x = (1,2)
y = [1,2]

z = {x: 1} // OK
c = {y: 1} // error

Data Type Conversion

Elements also allow the conversion of one type into another. This is done using some simple mechanics:

Converting to integer:

x = int(1.1)
x = int("1")
x = int (1, 11) // 1 base 11

Converting to floating:

x = float(1) // 1.0
x = float(1.1) / 1.1
x = float(1.1E-1)
x = float(False) // False is treated as 0
x = float(True) // True is treated as 1

strings:

x = str(1.1) // "1.1"
x = str([1, 2, 3]) // "[1, 2, 3]"

booleans:

x = bool(0) // False
x = bool(1) // True
x = bool([]) // empty list is False
x = bool("") // empty string is False

sets:

x = set([1, 2])

lists:

x = set([1, 2]) // we define a set
y = list(x) // we convert set to list

You can also perform indirect type conversion:

for example:

INTx = 1 // variable is integer 1
FLOATx = INTx + 1.1 // we convert integer to floating by adding 1.1 to an integer
STRx = "string is:" + str(INTx) // we convert int into a string by adding a integer to a string
INTx2 = 1 + False // boolean False is 0, so it is implicitly converted to 0 and summed with 1

In Summary

Now you know the main data types that Python supports, how to operate with them, and how to create variables. Additionally, you’ve learned how to convert from one variable type to another. This is the first step in mastering Python coding and can significantly ease your initial experience.

Was this article helpful?
Dislike 0