Variables and Data Types
In Python, variables can refer to different types of data, enabling us to manipulate that data as needed. To facilitate this, Python offers a range of standard data types that are specifically designed for storing and manipulating various kinds of data.
Variables
One of the most notable and distinct features of variables in Python is their dynamic typing
. This means that a single variable can change its type as required during the execution of the program
.
Example
Data Types
Python categorizes data types into the following categories: Text
, Numeric
, Sequence
, Mapping
, Set
, Boolean
, and Binary types
.
Text Type
str
Represents a sequence of characters, such as "hello" or "Python". Strings are immutable
.
Numeric Type
int
Represents integers (whole numbers) like 5, -3, 100.
float
Represents floating-point numbers with decimal places like 3.14, -0.5, 2.0.
Example
Sequence Type
Tip
For more information, checkout Built-In Sequences.
List
Represents an ordered collection of elements enclosed in square brackets []. Lists can contain elements of different data types
and are mutable
.
Tuple
Tuples represent an ordered collection of elements enclosed in parentheses ()
. However, tuples are immutable
.
>>> my_tuple = (1, 2, 3, 4, 5)
>>> print(my_tuple)
>>> print(type(my_tuple))
>>> my_tuple = tuple((1, 2, 3, 4, 5)) # cannot tuple(1, 2, 3, 4, 5)
>>> print(my_tuple)
>>> print(type(my_tuple))
>>> print("Tuple with only one element")
>>> my_tuple = (1,) # or tuple((1,))
>>> print(my_tuple)
>>> print(type(my_tuple))
(1, 2, 3, 4, 5)
<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'tuple'>
Tuple with only one element
(1,)
<class 'tuple'>
Range
Python provides a built-in range()
function that generates a sequence of numbers
within a specified range. The range()
function is commonly used in loops and iterations.
Set
Represents an unordered collection of unique elements
enclosed in curly braces {}
or created using the set()
constructor. Sets are useful to perform operations like union, intersection, and difference
.
FrozenSet
Similar to set, frozensets are immutable
and represent an unordered collection of unique elements. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation
.
Mapping Type
Dictionary
Represents a collection of key-value pairs
enclosed in curly braces {}
or created using the dict()
constructor. Dictionaries provide a way to store and retrieve data using unique keys
.
Boolean Type
Bool
Represents a logical value indicating either True
or False
. Boolean values are often the result of comparisons or logical operations.
Binary Types
Bytes
Bytes are immutable sequences
of individual bytes. They represent a fixed sequence of bytes that cannot be modified
once created. Bytes can be created using literals or by calling the built-in bytes()
constructor. They are commonly used to store and process raw binary data
.
bytearray
Bytearray, unlike bytes, is a mutable sequence
of bytes. It allows modifications after creation
, such as appending, replacing, or deleting elements. Bytearray objects can be created using literals or by calling the bytearray()
constructor. They provide a flexible way to modify binary data
.
Memoryview
Memoryview is a Python object that provides a way to access the internal data of an object
(such as bytes or bytearray) without making a copy
. It acts as a window into the data, allowing efficient access and manipulation of large amounts of binary data
. Memoryview objects can be created by calling the built-in memoryview()
constructor.
None Type
None
Represents a special value indicating the absence of a value. It is commonly used to denote a variable that has not been assigned a value.