Python Data Types

This page is an overview and basic introduction of commonly used Python data types, such as int, str, list, or dict. Follow the links for details about individual types.

Most commonly used data types

bool

Python bool data type represents two boolean values: True or False.

It is mainly used for program logic and controlling execution flow. A typical use case of bool data type is in conditional statements, such as: if a variable is True, do something, else do something different.

The variables that are True in Python include any non-zero number, non-empty string, or non-empty sequence. Conversely, variables that are False include zero, empty string, empty sequence, or the None constant. [more details...]

int

Python int data type represents integers (whole numbers without decimal point). It can be positive, negative, or zero.

An int can be created by assigning a whole number to a variable or by converting other data types using the int() function.

Python supports all common arithmetic and comparison operations between ints. Besides representing discrete numeric values, int has a wide range of uses in Python, such as indexing and slicing, as counter in loops, coordinates of pixels, or error codes. [more details...]

float

Python float data type (short for floating-point number) represents decimal numbers. It can be positive, negative, or zero.

A float can be created by assigning a decimal number (such as 1.5, -110.99, or 5.0) to a variable or by converting another data type using the float() function.

While highly efficient in terms of performance and memory, the float data type may suffer from floating point imprecision – small rounding errors caused by computers representing real numbers with a fixed number of binary digits. This imprecision is very small and not material in many use cases, but in some (such as financial or scientific applications) it is better to use other data types, such as Decimal or Fraction. [more details...]

str

Python str data type represents strings (sequences of characters such as words, sentences, or other text).

A string can be created by assigning a piece of text, enclosed in single or double quotes (interchangeable), to a variable (for example, s = "Hello world" or domain = 'pytut.com'). Strings can contain uppercase and lowercase letters, digits, and special characters, including spaces.

Python supports a wide range of operations with strings, including concatenation with the + operator, repeating a string using the * operator, slicing to extract substrings, converting case with upper() and lower(), replacing substrings with replace(), and many more. [more details...]

tuple

Python tuple is a collection data type that can store multiple values of any data type, separated by commas and enclosed in parentheses. Tuples are immutable – they cannot be modified once created (this is the main difference from list).

Tuples are often used to group related pieces of data together, such as coordinates, RGB color values, or database record fields. They can also be used to return multiple values from a function. Tuples can be unpacked to extract their values. Like other Python sequences, tuples can also be compared and sorted. [more details...]

list

Python list represents a collection of ordered items.

Lists are created using square brackets [], with items separated by commas. Lists can contain any combination of data types, including other lists. For example: a = [123, 'PyTut', True, 13.8, [1, 2, 3]].

Main difference from tuple is that list is mutable (can be changed). Python provides a variety of built-in functions for manipulating lists, such as append(), extend(), insert(), remove(), and sort().

Lists are versatile data types that can be used for a wide range of purposes, such as storing data, representing complex structures, and implementing algorithms. [more details...]

set

Python set is a mutable unordered collection of unique elements. It can't contain duplicates, which is the main difference from list and tuple.

A set can be created using comma separated sequence of items enclosed in curly braces, e.g. s = {123, 'PyTut', 13.8, True, (1, 2), True}. Items can be of any hashable type, such as int, float, str, tuple, but not list or dict.

A list or tuple can be converted to set using the built-in set() function.

Common set operations include intersection, union, difference, symmetric difference, testing for membership, adding and removing elements, and checking length (number of items). Sets are useful for tasks such as finding unique items, comparing collections, and eliminating duplicates. [more details...]

dict

Python dict (short for dictionary) represents a collection of key-value pairs.

A dict can be created by enclosing comma-separated key-value pairs within curly braces. For example: site = {"domain" : "pytut.com", "title" : "PyTut", "font_size" : 12, "https" : True}. Keys are unique and can be any immutable data type (such as strings, numbers, or tuples). Values can be any data type.

Python provides various methods to add, modify, or remove items in a dictionary. Common operations include accessing values by keys, checking if a key is in a dictionary, getting a list of keys or values, or iterating over the key-value pairs. Dictionaries are useful for mapping between related pieces of information or for counting occurrences of items. [more details...]

List of built-in data types

The type() built-in function

The data type of any Python variable can be checked using the built-in type() function.

>>> type(123)
<class 'int'>
>>> 
>>> type(123.456)
<class 'float'>
>>> 
>>> type('AAPL')
<class 'str'>
>>> 
>>> type([1, 2, 3])
<class 'list'>

Official documentation

Built-in data types:
https://docs.python.org/3/library/stdtypes.html

Built-in type() function:
https://docs.python.org/3/library/functions.html#type

By remaining on this website or using its content, you confirm that you have read and agree with the Terms of Use Agreement.

We are not liable for any damages resulting from using this website. Any information may be inaccurate or incomplete. See full Limitation of Liability.

Content may include affiliate links, which means we may earn commission if you buy on the linked website. See full Affiliate and Referral Disclosure.

We use cookies and similar technology to improve user experience and analyze traffic. See full Cookie Policy.

See also Privacy Policy on how we collect and handle user data.

© 2024 PyTut