Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Day 3: Data Types in Detail & The type() Function 🔍

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.

Why Data Types Matter

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.

Common Python Data Types (Recap & Deep Dive)

  1. Integers (int):
    • Used for whole numbers (positive, negative, or zero).
    • Examples: 5, -100, 0, 123456789.
  2. Floats (float):
    • Used for numbers with decimal points.
    • Examples: 3.14, 0.001, -9.99, 2.0 (even 2.0 is a float).
  3. 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!).
  4. Booleans (bool):
    • Represents logical truth values: True or False. (Note the capital 'T' and 'F').
    • Crucial for decision-making and control flow in programs.

The type() Function

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'>

📝 Practice Exercises

  1. 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.
  2. Type Mismatch: Try to add a string and an integer together directly (e.g., "Result: " + 10). Observe the TypeError. Then, fix it by converting the integer to a string using str().
  3. String to Number: Take a string that represents a number (e.g., "500") and convert it to an integer using int(). Do a simple calculation with it (e.g., add 100) and print the result.
  4. Boolean Logic: Create two boolean variables. Print the result of True and False, True or False, and not True. (We'll cover these operators more later, but you can experiment now!)

✨ Best Practices & Professional Notes

  • Type Coercion vs. Type Casting:
    • Coercion (Implicit Conversion): Python sometimes automatically converts types for you in certain operations (e.g., 5 + 3.0 results in 8.0 - the integer 5 is 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.
  • 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 using int() or float() 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.

🏃 How to Run This Code

  1. Open your terminal or command prompt.
  2. Navigate to the Day_03_DataTypes directory.
    cd path/to/your/fluffy-python/Day_03_DataTypes
  3. Run the script using:
    python data_types_in_detail.py
    # Or if you installed python3:
    # python3 data_types_in_detail.py

➡️ What's Next?

Tomorrow, we'll shift gears to Basic Operators – how to perform arithmetic and other comparisons with our variables!