Exploring Data Types in Python
This section delves deeper into Python's core data types and their characteristics.
String Data Type
Strings represent sequences of characters in Python:
Definition: A string is an immutable sequence of Unicode characters.
Example: "hello world", 'Python'
Highlight: Strings can be enclosed in single or double quotes.
Numeric Data Types
Python supports two main numeric types:
- Integers int: Whole numbers without decimal points
- Floats float: Numbers with decimal points
Example:
print(type(15)) # Output: <class 'int'>
print(type(35.6)) # Output: <class 'float'>
Boolean Data Type
Booleans represent logical values:
Definition: A boolean is a data type that can have only two values: True or False.
Vocabulary: Boolean variables are named after George Boole, who invented the mathematics of digital logic.
Example:
print(type(True)) # Output: <class 'bool'>
print(type(False)) # Output: <class 'bool'>
Type Conversion
Python allows converting between different data types:
Example:
print(type("15")) # Output: <class 'str'>
print(type(int("15"))) # Output: <class 'int'>
Highlight: Be cautious when converting between types, as not all conversions are valid or may result in data loss.