Block 2: Basic Types & Variables
Practice int, float, str, bool and variable assignment.
Concepts
- Variable assignment: x = 5
- Basic types: int, float, str, bool
- type() to inspect a variable's type
- Simple arithmetic and string concatenation
Code Examples
Variable assignment and type()
x = 5
y = 3.14
name = 'Python'
flag = True
print(type(x), type(y), type(name), type(flag))
Arithmetic and string concatenation
a, b = 10, 3
print(a + b, a - b, a * b, a / b)
greeting = 'Hello' + ' ' + 'World'
print(greeting)
Exercise
Create variables for your age, height, name, and a boolean. Print them in a friendly sentence. Compute and print (100 + 50) * 2 / 3
Solution
age = 25
height = 1.75
name = 'Alex'
is_student = True
print(f'Hi, I\'m {name}, {age} years old, {height}m tall. Student: {is_student}')
result = (100 + 50) * 2 / 3
print(f'Computation result: {result}')
print('Type of result:', type(result))Practice Problems
Problem 1: Create variables for a product: name, price, and in_stock. Print a formatted string.
Hint: Use f-strings: f'Product: {name}'
Problem 2: What does type(7/2) return? What about type(7//2)?
Hint: // is floor division
Application
In data science, you constantly work with numeric types (int/float) for calculations and strings for labels, categories, and text data.
Case Study
A fitness app stores user data: age (int), weight (float), name (str), and premium_member (bool). Each variable type is chosen for the right kind of data.
Homework
Describe the difference between int and float in your own words with one example each.