Welcome to Day 3 of Fluffy Python. Yesterday, we learned about variables as containers. Today, we're focusing on what's inside those containers: Data Types! Understanding the different types of data Python handles is fundamental, as it dictates what operations you can perform.
Every piece of information in Python has a type. Python is a "dynamically typed" language, meaning you don't have to explicitly declare the type of a variable when you create it – Python figures it out. However, knowing the type helps you:
- Perform Correct Operations: You can add numbers, but you can't "add" a number and a word directly.
- Prevent Errors: Mismatched types are a common source of bugs.
- Optimize Memory/Performance: Though Python handles a lot, understanding types helps in more advanced scenarios.
- Integers (
int):- Used for whole numbers (positive, negative, or zero).
- Examples:
5,-100,0,123456789.
- Floats (
float):- Used for numbers with decimal points.
- Examples:
3.14,0.001,-9.99,2.0(even2.0is a float).
- Strings (
str):- Used for sequences of characters (text).
- Enclosed in single quotes (
'...') or double quotes ("..."). - Examples:
"Hello World",'Python',"123"(even numbers in quotes are strings!).
- Booleans (
bool):- Represents logical truth values:
TrueorFalse. (Note the capital 'T' and 'F'). - Crucial for decision-making and control flow in programs.
- Represents logical truth values:
Python provides a built-in function called type() that allows you to check the data type of any variable or value.
my_number = 42
print(type(my_number)) # Output: <class 'int'>
my_text = "Coding"
print(type(my_text)) # Output: <class 'str'>- Identify Types: Create variables for your age (as an integer), your exact height (as a float), your full name (as a string), and whether you like coding (as a boolean). Use
print(type(...))for each to verify their types. - Type Mismatch: Try to add a string and an integer together directly (e.g.,
"Result: " + 10). Observe theTypeError. Then, fix it by converting the integer to a string usingstr(). - String to Number: Take a string that represents a number (e.g.,
"500") and convert it to an integer usingint(). Do a simple calculation with it (e.g., add 100) and print the result. - Boolean Logic: Create two boolean variables. Print the result of
True and False,True or False, andnot True. (We'll cover these operators more later, but you can experiment now!)
- Type Coercion vs. Type Casting:
- Coercion (Implicit Conversion): Python sometimes automatically converts types for you in certain operations (e.g.,
5 + 3.0results in8.0- the integer5is coerced to a float). - Casting (Explicit Conversion): When you use functions like
int(),str(),float(),bool()to explicitly convert a value from one type to another. This is often necessary and good practice when you need control over the conversion.
- Coercion (Implicit Conversion): Python sometimes automatically converts types for you in certain operations (e.g.,
- Input is Always a String: Remember from Day 1, the
input()function always returns a string. If you ask for a number (like age or quantity), you must convert it usingint()orfloat()before you can perform mathematical operations on it. - Data Integrity: Be mindful of your data types, especially when dealing with user input or data from external sources. Incorrect types can lead to unexpected behavior or program crashes.
- Open your terminal or command prompt.
- Navigate to the
Day_03_DataTypesdirectory.cd path/to/your/fluffy-python/Day_03_DataTypes - Run the script using:
python data_types_in_detail.py # Or if you installed python3: # python3 data_types_in_detail.py
Tomorrow, we'll shift gears to Basic Operators – how to perform arithmetic and other comparisons with our variables!