Python Data Types
Data types in Python define the type of value a variable can store. Python automatically detects the data type based on the value assigned to a variable.
Built-in Data Types
Python has several built-in data types. The most commonly used ones are:
- String (str)
- Integer (int)
- Float (float)
- Boolean (bool)
- List (list)
- Tuple (tuple)
- Dictionary (dict)
String
A string is used to store text. Strings are written inside single or double quotes.
Integer
Integers are whole numbers without decimal points.
Float
Floats are numbers with decimal points.
Boolean
Boolean data types have only two values: True or False.
List
A list is a collection of items. Lists are ordered and changeable.
Tuple
A tuple is similar to a list, but it is not changeable (immutable).
Dictionary
A dictionary stores data in key-value pairs.
The type() Function
The type() function is used to check the data type of a variable.
Why Data Types are Important
- They help organize data
- They prevent errors
- They make code easier to understand
- They are essential for operations and logic