Lesson 9/48 ยท ๐Ÿ Your First Python
๐Ÿ Your First PythonLesson 9/48
Phase 1 ยท Your First Python20 min

Variables, Storing Information

Learn to store, name, and reuse values in your programs

A variable is like a labeled box that stores a value. You give it a name, put something in it, and use the name to get that value back later.

How would you approach this? Think first, then expand.

Creating and using variablespython
name = "Alice"
age = 25
height = 5.7

print(name)    # prints: Alice
print(age)     # prints: 25
print(height)  # prints: 5.7
๐Ÿ”Code TracerStep 1 / 8
trace.py
1name = "Alice"โ†
2age = 30
3city = "London"
4greeting = f"Hello, {name}!"
Variables
No variables yet
Step 1 / 8
Data types, the kinds of values Python can store:
  • String (text): "hello", "123", "Alice"
  • Integer (whole number): 42, -7, 0
  • Float (decimal number): 3.14, -0.5
  • Boolean (true/false): True, False
  • You can combine variables with f-stringspython
    name = "Alice"
    age = 25
    print(f"My name is {name} and I am {age} years old.")
    # Output: My name is Alice and I am 25 years old.
    f-strings (formatted strings) let you embed variables directly inside text. Put an f before the quote, then use {variable_name} inside.
    Variable names rules: Start with a letter or underscore, no spaces (use _ instead), case-sensitive (Name and name are different). Bad: 2name, my name. Good: my_name, name2.
    ๐ŸŒIn the Real World...

    In CI/CD pipelines, variables store environment names, image tags, and secrets. Every Terraform variable block works this way: variable "region" { default = "us-east-1" }.

    Practice Exercises

    0/3 solved
    Exercise 1 of 3easy
    โฑ 00:00

    Name Tag

    Create two variables: first_name and last_name. Then print a greeting in this exact format:Hello, [first] [last]!
    Example: if first_name is "Ada" and last_name is "Lovelace", print: Hello, Ada Lovelace!
    solution.py
    1 / 3
    Exercise 2 of 3easy
    โฑ 00:00

    Swap Two Variables

    Given two variables a = 5 and b = 10, swap their values so that a becomes 10 and b becomes 5. Then print both.
    Expected output:`105`
    solution.py
    2 / 3
    Exercise 3 of 3medium
    โฑ 00:00

    Simple Calculator

    Write a function calculate(a, b) that:1. Computes the sum, difference, product, and quotient of a and b2. Returns a string in this format: "sum=8 diff=2 product=15 quotient=1.67"
    For quotient, round to 2 decimal places using round(value, 2).
    solution.py
    3 / 3
    Solve all 3 exercises to unlock completion