Python Data Types
Every value in Python has a data type, which determines what kind of data it represents and what operations can be performed on it. Some common data types in Python are:
Table of Contents
- Numeric Data Types
- 1.1 Integer (
int
) - 1.2 Floating-Point (
float
) - 1.3 Complex (
complex
)
- 1.1 Integer (
- Textual Data Types
- 2.1 String (
str
) - 2.2 String Operations
- 2.1 String (
- Sequence Data Types
- 3.1 List (
list
) - 3.2 Tuple (
tuple
) - 3.3 Range (
range
)
- 3.1 List (
Mapping Data Type- 4.1 Dictionary (
dict
)
- 4.1 Dictionary (
Set Data Type- 5.1 Set (
set
) - 5.2 Frozenset (
frozenset
)
- 5.1 Set (
Boolean Data Type- 6.1 bool: Boolean Values
Binary Types- 7.1 bytes: Bytes
- 7.2 bytearray: Byte Arrays
NoneType- 8.1 None: The None Object
Type Conversion (Casting)- User-Defined Classes
- Checking Data Types
1. Numeric Data Types
1.1 Integer (int
)
Integers represent whole numbers without decimal points. They can be positive or negative.
1.2 Floating-Point (float
)
Floating-point numbers are numbers with decimal points. They are used to represent real numbers.
1.3 Complex (complex
)
Complex numbers consist of a real part and an imaginary part.
2. Textual Data Types
2.1 String (str
)
Strings are sequences of characters enclosed in single or double quotes.
2.2 String Operations
Strings support various operations like concatenation, slicing, and formatting.
3. Sequence Data Types
3.1 List (list
)
Lists are ordered collections that can contain elements of different types.
3.2 Tuple (tuple
)
Tuples are similar to lists but are immutable
3.3 Range (range
)
Ranges represent sequences of numbers.
4. Mapping Data Type
4.1 Dictionary (dict
)
Dictionaries store key-value pairs and allow fast lookup.
5. Set Data Type
5.1 Set (set
)
Sets are unordered collections of unique elements.
5.2 Frozenset (frozenset
)
Frozensets are like sets but are immutable.
6. Boolean Data Type
6.1 bool: Boolean Values
Boolean values represent truth (True) or falsehood (False).
7. Binary Types
7.1 bytes: Bytes
Bytes represent sequences of bytes (integers from 0 to 255).
7.2 bytearray: Byte Arrays
Byte arrays are mutable sequences of bytes.
8. None Type
8.1 None: The None Object
The None object represents the absence of a value.
9. User-Defined Classes
You can define your own data types using classes.
10. Type Conversion (Casting)
Python allows you to convert between different data types using type casting functions.
11. Checking Data Types
Python provides the type()
function to determine the data type of a value. This function returns the class type of the argument. Here's how you can use it: