Table of contents
What are the Python Data Types?
Python data types are classifications that define the nature of data stored in variables. They determine the operations that can be performed on the data and how they are stored in memory. Here is a description of some commonly used Python data types:
Commonly used Python Data Types.
Here is a description of some commonly used Python data types:
Numeric Types: Used for representing numbers. Integers (whole numbers) and floating-point numbers (decimal numbers) are examples of numeric types.
Example:
number = 42; print(number, type(number)) #Integer (int) pi = 3.14; print(pi, type(pi)) #Floating-Point Number (float)
Boolean Type: Represents logical values, either True or False. It is often used in conditional statements and logical operations.
Example:
is_true = True; print(is_true, type(is_true)) #Boolean (bool)
Sequence Types: These types are used to store collections of items. Strings store sequences of characters, and lists store sequences of arbitrary objects. They are mutable, meaning their elements can be modified.
Example:
message = "Hello"; print(message, type(message)) #String (str) numbers = [1, 2, 3]; print(numbers, type(numbers)) #List (list)
Mapping Type: The dictionary is a mapping type that stores key-value pairs. It provides efficient lookup and retrieval of values based on their corresponding keys.
Example:
person = {'name': 'John', 'age': 25}; print(person, type(person)) #Dictionary (dict)
Set Types: Sets are unordered collections of unique elements. They are useful for mathematical operations like union, intersection, and difference.
Example:
my_set = {1, 2, 3}; print(my_set, type(my_set)) #Set (set) frozen = frozenset({1, 2, 3}); print(frozen, type(frozen)) #Frozen Set (frozenset)
Sequence Types: Sequence types in Python are used to store collections of items in a specific order. They include strings, lists, tuples, and ranges. Sequence types allow indexing, slicing, and iterating over the elements, making them versatile for various operations such as data manipulation, iteration, and concatenation.
Example:
my_tuple = (1, 2, 3); print(my_tuple, type(my_tuple)) #Tuple (tuple) my_range = range(0, 10); print(my_range, type(my_range)) #Range (range)
None Type: Represents the absence of a value or a null value. It is commonly used to indicate the absence of a return value in functions or to initialize variables.
Example:
empty = None; print(empty, type(empty)) #None