Think about different containers in your kitchen: some hold water, others hold flour, and some store spices. In Python, data types are like these containers—they define what kind of information can be stored.
Python has several data types, including integers (whole numbers), floats (decimal numbers), strings (text), and more. Understanding data types helps you store and use values correctly.💡 Analogy: Data types are like different compartments in a school bag—books go in one pocket, pens in another, and lunch in another. Each type of data has its own "pocket" in Python.
Identify different data types in Python.
Use the type() function to check the type of a variable.
Understand why different data types exist and how they are used in programming.
Data types – Categories of data that define what kind of value a variable can store (e.g., numbers, text).
Integers (int) – Whole numbers (e.g., 5, 10, -3), used for counting and calculations.
Floats (float) – Numbers with decimals (e.g., 3.14, -2.5), used for precise calculations.
Strings (str) – Text-based data, such as "hello" or "Python", stored using quotation marks.
type() function – A built-in function used to check the type of a variable, helping programmers understand the data they are working with.
Example
print(type(5)) # Output: <class 'int'>
print(type(3.14)) # Output: <class 'float'>
print(type("hello")) # Output: <class 'str'>
Forgetting to convert types: age = input("Enter your age: ") stores input as a string, so age + 5 ❌ will cause an error. Use int(age) + 5 ✅.
Mixing strings and numbers improperly: "I am " + 25 ❌ causes an error; use "I am " + str(25) ✅.
🔗 Practice on W3Schools: Data Types Tutorial – Learn about different types of data and how to check them.
🔗 Try It Online: Run Your Code Here – Test your Python code instantly.
1️⃣ Basic Challenge:
Write a program that asks the user for their age and prints the type of data stored.
2️⃣ Extended Challenge:
Modify your program to ask for a favorite number and print both the number and its type.
3️⃣ Advanced Challenge:
Write a Python program that asks for the user's name, age, and favorite decimal number. Then, print a sentence using this information, making sure to correctly format numbers and strings.
On paper, write the example from this lesson. Be sure to include:
✅ The type() function to check data types
✅ Examples of integers, floats, and strings
Read the example code before trying to write it down!