Data Types
Discover the basic data types in Python: strings for text, integers and floats for numbers, and booleans for true/false checks.
Learning Goals
The Core Concept
In Python, every value has a data type. The most common primitive data types are Strings (text), Integers (whole numbers), Floats (decimal numbers), and Booleans (true or false values).
Understanding data types is crucial because you cannot perform the same operations on all of them. For instance, you can add two integers, but adding a string to an integer will raise a TypeError. Python provides built-in functions like type(), str(), int(), and float() to check and convert between types when necessary.
Visual guide
Python automation process
A simple original diagram to connect the lesson idea with real project flow.
Code & Implementation
# Different data types
price = 19.99 # Float
quantity = 3 # Integer
product = "Book" # String
in_stock = True # Boolean
# Checking types
print(type(price))
print(type(quantity))
# Converting types (Type Casting)
quantity_str = str(quantity)
print("Quantity as text: " + quantity_str)Expected Output
<class 'float'> <class 'int'> Quantity as text: 3
Receipt Calculator with Tax
Hands-on practice task
The Challenge
Write a program that takes a subtotal integer, converts it to float, adds 8.5% tax, and prints a message showing the final price as a string.
Helpful Hints
- •Start with subtotal = 100.
- •Calculate tax: tax = subtotal * 0.085.
- •Calculate total and convert it to string when printing next to text: print("Total: " + str(total)).
Quick Knowledge Check
Why does Python distinguish between Integers and Floats?
Can I convert any string to an integer?
Continue Learning
Next steps after this lesson
Write a program that takes a subtotal integer, converts it to float, adds 8.5% tax, and prints a message showing the final price as a string.
Supercharge your career workflows!
Discover free online utilities to format data, build job applications, and automate your productivity routine with TechIdea.