Data Types: Sorting the Ledger
Not all data is created equal. You can't multiply a "Name" by a "Phone Number," and you can't capitalize a "Price." To keep your code from crashing, you must understand the four fundamental Data Types in Python.
1. Strings (str)
As we saw in Lesson 2, these are sequences of characters wrapped in quotes.
- Example:
"Hello",'12345',"Python_Ledger_v1" - Use for: Names, addresses, or any text.
2. Integers (int)
These are whole numbers with no decimal point.
- Example:
10,-5,1000000 - Use for: Counting items, ages, or quantities.
3. Floats (float)
Short for "floating-point numbers," these are numbers that contain a decimal point.
- Example:
10.5,-0.01,3.14 - Use for: Prices, percentages, or precise measurements.
4. Booleans (bool)
These represent logical values: either True or False. Note the capital letters!
- Example:
True,False - Use for: Checking if a user is logged in, if a task is finished, or if a price is too high.
The type() Inspector
If you ever get confused about what is inside a variable, Python provides a built-in magnifying glass: the type() function.
Dynamic Typing: The Python Superpower
In some languages, you have to tell the computer exactly what type a variable is. In Python, the computer figures it out automatically based on the value you provide. This is called Dynamic Typing.
🏆 The Ledger Challenge: Inventory Audit
You are auditing your supply of "Coding Fuel."
Task:
- Create a variable
item_nameand assign it a String (e.g., "Coffee"). - Create a variable
quantityand assign it an Integer (e.g., 5). - Create a variable
price_per_unitand assign it a Float (e.g., 3.50). - Create a variable
in_stockand assign it a Boolean (TrueorFalse). - Print all four variables.
Write your code below:
📚 Deep Dive
Next Steps
Now that we know the types, it's time to put them to work. In the next lesson, we’ll learn Basic Math—how to add, subtract, and manipulate these numbers in our Ledger.