2
Week 2: Variables and Data Types
Understanding data storage and manipulation
Learning Objectives
By the end of this lesson, students will:
- Create and use variables to store data
- Understand different data types (int, float, string, boolean)
- Use the input() function to get user data
- Convert between different data types
Skills Developed:
- Data management concepts
- Interactive program design
- Type system understanding
Lesson Content
1 What are Variables? (15 minutes)
Key Concepts:
- • Variables are like labeled boxes that store information
- • They have names (identifiers) and values
- • Values can change during program execution
- • Variable names should be descriptive
# Creating variables
name = "Alex"
age = 15
height = 5.6
is_student = True
# Using variables
print("My name is", name)
print("I am", age, "years old")
Instructor Notes:
Use physical boxes or containers as analogies. Show how variables make programs more flexible than hard-coded values.
2 Data Types in Python (20 minutes)
Integer (int):
Whole numbers, positive or negative
score = 100
temperature = -5
Float:
Decimal numbers
price = 19.99
pi = 3.14159
String (str):
Text, always in quotes
name = "Python"
message = 'Hello!'
Boolean (bool):
True or False values
is_ready = True
game_over = False
# Check data types
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
3 Getting User Input (15 minutes)
The input() Function:
- • Always returns a string (even if user types numbers)
- • Can include a prompt message
- • Program waits for user to press Enter
# Getting user input
name = input("What is your name? ")
print("Hello,", name)
# Input is always a string!
age_text = input("How old are you? ")
print("You typed:", age_text)
print("Type of age_text:", type(age_text))
4 Type Conversion (15 minutes)
Converting Between Types:
Common Conversions:
- • int() - converts to integer
- • float() - converts to decimal
- • str() - converts to string
- • bool() - converts to True/False
Why Convert?
- • Do math with user input
- • Format output properly
- • Avoid type errors
# Type conversion examples
age_text = input("Enter your age: ")
age_number = int(age_text) # Convert to integer
next_year = age_number + 1
print("Next year you'll be", next_year)
# Or do it in one line
age = int(input("Enter your age: "))
Hands-On Activity (25 minutes)
Project: Personal Calculator
Students will create a program that asks for personal information and performs simple calculations.
Requirements:
- • Ask for the user's name (string)
- • Ask for their current age (convert to integer)
- • Ask for their height in feet (convert to float)
- • Calculate and display their age in 10 years
- • Calculate and display their height in inches
- • Display a summary with all information
- • Include comments explaining each step
💻📱 How to Code This Project (Choose Your Platform):
📱 Pythonista 3:
- Open Pythonista 3
- Tap "+" → "Empty Script"
- Name it "calculator.py"
- Type the code below
- Tap ▶️ to run
- Interact in console
Tip: Use keyboard for easier input
🤖 Pydroid 3:
- Open Pydroid 3
- Tap "+" → "Python file"
- Name it "calculator.py"
- Type the code below
- Tap ▶️ yellow play button
- Interact in terminal
Tip: Swipe up for better keyboard
💻 PC/Mac:
- Open IDLE or editor
- Create new file
- Save as "calculator.py"
- Type the code below
- Press F5 to run
- Interact in shell
Tip: Save frequently (Ctrl+S)
# Example solution
# Personal Calculator Program
print("Welcome to the Personal Calculator!")
# Get user information
name = input("What is your name? ")
age = int(input("How old are you? "))
height_feet = float(input("How tall are you in feet? "))
# Perform calculations
future_age = age + 10
height_inches = height_feet * 12
# Display results
print("\\n--- Personal Summary ---")
print("Name:", name)
print("Current age:", age)
print("Age in 10 years:", future_age)
print("Height:", height_feet, "feet or", height_inches, "inches")
Assessment & Homework
Quick Check (In Class):
- • Can student create and use variables?
- • Do they understand different data types?
- • Can they use input() and convert types?
- • Are they choosing appropriate variable names?
Homework Assignment:
Robot Specifications Program
Create a program that asks for robot specifications (name, max speed, battery life, etc.) and calculates useful information like range or efficiency. Use at least 4 different data types.