Homework Hacks: Variables & Data Types


🏠 Homework Hack: Simple Grade Calculator

Program Description: A program that calculates a student’s grade based on three test scores.

# Simple Grade Calculator

# Store student information (final variables using uppercase naming convention)
STUDENT_NAME = "Alex Johnson"
CLASS_NAME = "Computer Science 101"

# Store three test scores (whole numbers 0-100)
test_score_1 = 85
test_score_2 = 92
test_score_3 = 88

# Calculate the average of the three test scores
average_score = (test_score_1 + test_score_2 + test_score_3) / 3

# Determine the letter grade based on average
if average_score >= 90:
    letter_grade = 'A'
elif average_score >= 80:
    letter_grade = 'B'
elif average_score >= 70:
    letter_grade = 'C'
elif average_score >= 60:
    letter_grade = 'D'
else:
    letter_grade = 'F'

# Display the results
print("=" * 50)
print("GRADE REPORT")
print("=" * 50)
print(f"Student Name: {STUDENT_NAME}")
print(f"Class: {CLASS_NAME}")
print(f"\nTest Score 1: {test_score_1}")
print(f"Test Score 2: {test_score_2}")
print(f"Test Score 3: {test_score_3}")
print(f"\nAverage Score: {average_score:.2f}")
print(f"Letter Grade: {letter_grade}")
print("=" * 50)

Expected Output:

==================================================
GRADE REPORT
==================================================
Student Name: Alex Johnson
Class: Computer Science 101

Test Score 1: 85
Test Score 2: 92
Test Score 3: 88

Average Score: 88.33
Letter Grade: B
==================================================

Key Programming Concepts:

  • Final Variables: STUDENT_NAME and CLASS_NAME are written in UPPERCASE to indicate they should not be changed (Python convention for constants)
  • Integer Variables: Test scores are stored as whole numbers (int type)
  • Float Calculation: The average is automatically calculated as a decimal (float type)
  • Conditional Logic: Uses if-elif-else statements to determine letter grade
  • String Formatting: Uses f-strings to display formatted output

🍿 Popcorn Hack #1: Your Favorite Things

Create variables for your favorite things with appropriate data types:

# Popcorn Hack #1: My Favorite Things

# 1. Your favorite food (text) - String type
favorite_food = "Tacos"

# 2. Your age (whole number) - Integer type
age = 16

# 3. Your height in feet (decimal) - Float type
height_in_feet = 5.7

# 4. Do you like pizza? (True/False) - Boolean type
likes_pizza = True

# 5. First letter of your favorite color - Character (stored as String in Python)
favorite_color_letter = 'B'

# 6. Your birth year (never changes) - Final variable (Integer, uppercase naming)
BIRTH_YEAR = 2008

# Display all favorite things
print("My Favorite Things:")
print("-" * 40)
print(f"Favorite Food: {favorite_food}")
print(f"Age: {age} years old")
print(f"Height: {height_in_feet} feet")
print(f"Likes Pizza: {likes_pizza}")
print(f"Favorite Color starts with: {favorite_color_letter}")
print(f"Birth Year: {BIRTH_YEAR}")
print("-" * 40)

# Show the data types
print("\nData Types:")
print(f"favorite_food is type: {type(favorite_food).__name__}")
print(f"age is type: {type(age).__name__}")
print(f"height_in_feet is type: {type(height_in_feet).__name__}")
print(f"likes_pizza is type: {type(likes_pizza).__name__}")
print(f"favorite_color_letter is type: {type(favorite_color_letter).__name__}")
print(f"BIRTH_YEAR is type: {type(BIRTH_YEAR).__name__}")

Expected Output:

My Favorite Things:
----------------------------------------
Favorite Food: Tacos
Age: 16 years old
Height: 5.7 feet
Likes Pizza: True
Favorite Color starts with: B
Birth Year: 2008
----------------------------------------

Data Types:
favorite_food is type: str
age is type: int
height_in_feet is type: float
likes_pizza is type: bool
favorite_color_letter is type: str
BIRTH_YEAR is type: int

🍿 Popcorn Hack #2: Pick the Best Type

Identify the best data type for each piece of information:

# Information Best Data Type Explanation
1 Number of siblings int (integer) Siblings are counted in whole numbers (0, 1, 2, 3, etc.). You can’t have 2.5 siblings!
2 Your first name str (string) Names are text/characters, so they must be stored as strings. Example: “Sarah”
3 Are you hungry? bool (boolean) This is a yes/no question with only two possible values: True or False
4 Your favorite letter str (string) Even though it’s a single character, Python stores it as a string. Example: ‘A’ or “A”
5 Your height in inches float (decimal) Height can include decimal values like 65.5 inches for precision
6 Number of days in a year (never changes) int (integer, final/constant) Always 365 (or 366), a whole number that doesn’t change. Should be written as DAYS_IN_YEAR = 365

Code Example:

# Popcorn Hack #2: Variable Type Examples

# 1. Number of siblings - Integer
number_of_siblings = 2

# 2. Your first name - String
first_name = "Emma"

# 3. Are you hungry? - Boolean
are_you_hungry = True

# 4. Your favorite letter - String (character)
favorite_letter = 'M'

# 5. Your height in inches - Float
height_in_inches = 65.5

# 6. Number of days in a year (constant) - Final Integer
DAYS_IN_YEAR = 365

# Display all variables
print("Variable Type Examples:")
print("-" * 50)
print(f"Number of siblings: {number_of_siblings} (type: {type(number_of_siblings).__name__})")
print(f"First name: {first_name} (type: {type(first_name).__name__})")
print(f"Are you hungry?: {are_you_hungry} (type: {type(are_you_hungry).__name__})")
print(f"Favorite letter: {favorite_letter} (type: {type(favorite_letter).__name__})")
print(f"Height in inches: {height_in_inches} (type: {type(height_in_inches).__name__})")
print(f"Days in a year: {DAYS_IN_YEAR} (type: {type(DAYS_IN_YEAR).__name__})")

Expected Output:

Variable Type Examples:
--------------------------------------------------
Number of siblings: 2 (type: int)
First name: Emma (type: str)
Are you hungry?: True (type: bool)
Favorite letter: M (type: str)
Height in inches: 65.5 (type: float)
Days in a year: 365 (type: int)

📝 Summary of Data Types

Python’s Basic Data Types:

  1. int (Integer) - Whole numbers without decimals
    • Examples: 5, 100, -3, 0
    • Use for: counting, ages, years
  2. float (Floating-point) - Numbers with decimals
    • Examples: 3.14, 5.7, -2.5, 0.0
    • Use for: measurements, percentages, prices
  3. str (String) - Text and characters
    • Examples: "Hello", 'Python', "A"
    • Use for: names, words, sentences
  4. bool (Boolean) - True or False values
    • Examples: True, False
    • Use for: yes/no questions, conditions
  5. Constants (Final Variables) - Values that never change
    • Written in UPPERCASE: PI = 3.14159, MAX_SCORE = 100
    • Use for: fixed values that should not be modified