Python Data Type
Python supports several built-in data types that allow you to represent and work with different kinds of data. Here are some commonly used data types in Python:
-
Numeric Types:
- int: Represents integers, such as 3, -10, or 1000.
- float: Represents floating-point numbers (decimal numbers), such as 3.14 or -2.5.
- complex: Represents complex numbers, in the form a + bj, where a and b are floats, and j represents the imaginary unit.
-
String:
- str: Represents a sequence of characters, such as "Hello" or "Python is fun!" Strings are enclosed in single quotes ('') or double quotes ("").
-
Boolean:
- bool: Represents a Boolean value, which can be either True or False. Booleans are often used for logical operations and control flow.
-
Sequence Types:
- list: Represents an ordered collection of items. Lists can contain elements of different types, and they are mutable (modifiable).
- tuple: Represents an ordered collection of items, similar to lists, but tuples are immutable (cannot be modified).
- range: Represents a sequence of numbers, commonly used in loops and iterations.
-
Mapping Type:
- dict: Represents a collection of key-value pairs. Each value is associated with a unique key, allowing efficient lookup and retrieval.
-
Set Types:
- set: Represents an unordered collection of unique elements. Sets are useful for operations like union, intersection, and difference.
- frozenset: Similar to sets, but frozensets are immutable.
-
None Type:
- None: Represents the absence of a value. It is often used to indicate a null or empty state.
These are just a few examples of Python's built-in data types. Python also allows you to create custom data types using classes and objects.
You can determine the type of a value or variable using the type() function, which returns the data type of the specified object.
Example
num = 42
print(type(num)) # Output: <class 'int'>
name = "John"
print(type(name)) # Output: <class 'str'>
is_student = True
print(type(is_student)) # Output: <class 'bool'>
Understanding data types is essential in Python programming, as it helps you choose appropriate operations, handle data correctly, and build robust applications.