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): Integer (whole number): Float (decimal number): Boolean (true/false):
"hello", "123", "Alice"42, -7, 03.14, -0.5True, FalseYou 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 solvedExercise 1 of 3easy
โฑ 00:00Name Tag
Create two variables:
Example: if first_name is "Ada" and last_name is "Lovelace", print:
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:00Swap Two Variables
Given two variables
Expected output:
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:00Simple Calculator
Write a function
For quotient, round to 2 decimal places using
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