Data Types in Python
Python Data Types
In programming, a data type defines the kind of value a variable can hold. Every value in Python has a specific data type, and these types help the interpreter understand what operations can be performed on a value.
Python has several built-in data types, and they can be grouped into different categories.
Built-in Data Types
Here are the main data types in Python:
- Text Type:
str
: Represents a sequence of characters (strings).
- Numeric Types:
int
: Represents whole numbers.float
: Represents decimal (floating point) numbers.complex
: Represents complex numbers (numbers with real and imaginary parts).
- Sequence Types:
list
: Ordered and changeable collection of items.tuple
: Ordered but unchangeable (immutable) collection.range
: Represents a sequence of numbers.
- Mapping Type:
dict
: Represents key-value pairs (like a dictionary).
- Set Types:
set
: Unordered collection of unique items.frozenset
: Unordered and immutable set.
- Boolean Type:
bool
: Represents two values,True
orFalse
.
- Binary Types:
bytes
: Immutable sequence of bytes.bytearray
: Mutable sequence of bytes.memoryview
: Provides a view of the memory of an existing object.
- None Type:
NoneType
: Represents the absence of a value (similar tonull
in other languages).
Getting the Data Type
To check the type of a value or variable, you can use the type()
function.
Example:
python
1x = 5
2y = "Hello"
3z = [1, 2, 3]
4
5print(type(x)) # Output: <class 'int'>
6print(type(y)) # Output: <class 'str'>
7print(type(z)) # Output: <class 'list'>