Core Data Types
Understand Python core primitive types (str, int, float, bool), identify types with type(), and study dynamic transitions.
1. Why Data Types Matter
Computers need to know what kind of data they are working with. You can do math with numbers, but you cannot do math with letters! Python classifies data into specific **Data Types**:
- String (
str): Text values surrounded by quotes. E.g.,"Hello"or'123'. - Integer (
int): Whole numbers without decimals. E.g.,25,-5. - Float (
float): Decimal numbers containing fractional values. E.g.,8.5,4.0. - Boolean (
bool): Logical truth states. Can only beTrueorFalse.
2. Checking Data Types using type()
You can check the data type of any value or variable by passing it inside the built-in type() function. It will return the class name of the object (e.g., <class 'str'>).
💻 Python Code Examples
Example 1: Declaring Primitive Types
name = "Ann" # str
age = 12 # int
score = 95.5 # float
is_happy = True # bool
Description: Declares variables demonstrating Python's four core primitive types.
Example 2: Using type() function
x = 42
print(type(x))
y = "42"
print(type(y))
Description: Inspects types to show that x is an integer while y is a text string.
Example 3: Division Yields Float
result = 10 / 2
print(result)
print(type(result)) # Output: float
Description: Shows that division operations in Python always return a float datatype.
Example 4: Booleans are Case-Sensitive
# Correct declaration
is_valid = True
# is_valid = true -> throws NameError
print(type(is_valid))
Description: Boolean values must start with capitalized letters (True/False).
Example 5: Floats with trailing zero
val = 5.0
print(type(val)) # Output: float
Description: Even if the fraction is zero, the decimal point makes it a float datatype.
Example 6: Mixing String Quotes
msg1 = "Single 'quote' inside"
msg2 = 'Double "quote" inside'
print(msg1)
print(msg2)
Description: Shows how quotes can be nested inside strings of different quote types.
🧠 Active Retrieval Quizzes
Answer the following 10 multiple-choice questions to test your storage strength.
Question 1: Which data type represents decimal values in Python?
Question 2: What class does type('True') return?
Question 3: Which function is used to query the active data type of a variable?
Question 4: What data type does the expression x = 4 / 2 return?
Question 5: Which of these is a VALID boolean literal in Python?
Question 6: What data type is the value 100?
Question 7: What data type is the value 100.0?
Question 8: What is the data type of the value: "25.5"?
Question 9: What data type is returned by: type(5 > 2)?
Question 10: Which data type represents simple text characters?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
x = "Hello"
print(type(x))
Predict the output of the code snippet below. Type the exact output in the console.
x = 10 / 2
print(type(x))
Predict the output of the code snippet below. Type the exact output in the console.
x = 5 > 10
print(type(x))
Predict the output of the code snippet below. Type the exact output in the console.
x = 42
print(type(type(x)))
Predict the output of the code snippet below. Type the exact output in the console.
x = 5.0
print(type(x))
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.
📋 Progress Status
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.