Part 2: The Planet of Memory Vaults — Variables and Data Types
The Adventures of Ava the Astronaut: Learning Python One Planet at a Time
Part 2: The Planet of Memory Vaults — Variables and Data Types
The Adventures of Ava the Astronaut: Learning Python One Planet at a Time
Ava’s New Mission
After sending her first message to the universe, a new planet on Ava’s star map began to pulse with a soft, blue light. The computer screen displayed a new message:
“Welcome to Xylos, the Planet of Memory. To proceed, you must learn to store your mission data.”
Ava realized that to be a real astronaut, she couldn’t just send messages. She needed to remember things: her name, her ship’s fuel level, the number of stars she had counted. But how can a computer remember things? That is what this mission is all about.
The Spaceship’s Lockers: What Are Variables?
Imagine Ava’s spaceship has a huge wall of storage lockers. Each locker can hold one item, and each locker has a unique label on it. If she wants to store her favorite space helmet, she can place it in a locker and label it favorite_helmet. Later, when she needs it, she just looks for the locker with that label.
In Python, a variable is exactly like one of these lockers. It is a named container in the computer’s memory where you can store a piece of information (a value). You give it a name so you can find and use it again later.
Let’s help Ava store her astronaut name, age, and fuel level.
# Storing mission-critical data in our lockers!astronaut_name = "Ava"astronaut_age = 12fuel_level = 99.5
# Now, let's check what's in our lockers.print(astronaut_name)print(astronaut_age)print(fuel_level)What happens?
When you run this code, the output will be:
Ava1299.5We didn’t have to type “Ava” or 12 directly into the print() function! Instead, we just used the variable name, and Python went to the locker, fetched the value inside, and displayed it. The = sign here is called the assignment operator. It takes the value on the right side and stores it in the variable on the left side. Think of it as “put this value into this locker.”
Important naming rules for variables:
| Rule | Good Example | Bad Example |
|---|---|---|
| Must start with a letter or underscore | my_age, _score | 1st_place |
| Can contain letters, numbers, and underscores | player_1, high_score | my-age, my age |
| Cannot be a Python keyword | my_class | class, print |
Case-sensitive (age and Age are different) | fuel_Level | — |
The Different Kinds of Space Cargo: Data Types
Ava notices that her lockers can store different kinds of things. She can store a name tag (text), a count of crystals (a whole number), or a precise fuel reading (a number with a decimal). In Python, the values we store in variables also have different data types. Understanding data types is important because it determines what you can do with the data.
Here are the four most common data types you will encounter on your journey:
| Data Type | Python Name | Description | Example |
|---|---|---|---|
| String | str | Textual information. Always wrapped in quotes. | "Hello", 'Python' |
| Integer | int | Whole numbers, with no decimal point. | 10, -5, 12345 |
| Float | float | Numbers with a decimal point. | 3.14, 99.9, -0.5 |
| Boolean | bool | Represents truth. Only two possible values. | True, False |
In our earlier example, astronaut_name holds a string, astronaut_age holds an integer, and fuel_level holds a float. You can always check the type of a variable using the built-in type() function:
print(type(astronaut_name)) # Output: <class 'str'>print(type(astronaut_age)) # Output: <class 'int'>print(type(fuel_level)) # Output: <class 'float'>Talking to Mission Control: The input() Function
Ava gets a message from Mission Control: “Please state your name for the record.” She needs a way to talk back! The input() function lets the program pause and wait for the user to type something in. Whatever the user types is captured and can be stored in a variable.
Let’s build a program that asks for a name and then greets that person.
# Get the user's name from Mission Controlprint("Mission Control: Please state your name for the record.")user_name = input("Enter your name: ")
# Greet the user using an f-stringprint(f"Hello, Commander {user_name}! Welcome aboard.")When you run this, the program will print the first message and then wait, showing the prompt Enter your name: . After you type your name (say, “Alex”) and press Enter, it will store “Alex” in the user_name variable and then print: Hello, Commander Alex! Welcome aboard.
Notice the f before the quotation mark in the last print statement? That creates an f-string (formatted string literal). It is a very convenient way to insert variables directly into a piece of text. You just put the variable name inside curly braces {}, and Python replaces it with the variable’s value. It is much cleaner than trying to glue strings together with + signs!
Space Math: Arithmetic Operators
Ava’s ship computer also needs to do calculations. Python can handle math with ease using arithmetic operators.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 3 | 3.333... |
// | Floor Division (whole number result) | 10 // 3 | 3 |
** | Exponentiation (power) | 2 ** 3 | 8 |
% | Modulus (remainder) | 10 % 3 | 1 |
Here is a quick example of Ava calculating her ship’s range:
fuel = 500 # units of fuelefficiency = 2.5 # light-years per unit of fuel
max_range = fuel * efficiencyprint(f"Maximum range: {max_range} light-years")Output: Maximum range: 1250.0 light-years
Mini-Project: Build a “Space Passport”
Your mission is to create a program that generates a “Space Passport” for any traveler. It should ask the user for their personal details, store them in variables, and then print a beautifully formatted passport.
Your Task:
- Use
print()to display a welcome message for the passport application. - Use
input()to ask for the user’s name and store it in a variable. - Use
input()to ask for their age and store it. - Use
input()to ask for their home planet (e.g., “Earth”) and store it. - Finally, use
print()with f-strings to display all the collected information in a passport-style format.
Example Output:
=== INTERGALACTIC PASSPORT ===Name : Captain ZaraAge : 14Home Planet: EarthClearance : Approved==============================Bonus Challenge: Can you also ask for a “favorite color” and include it in the passport?
Mission Complete!
You have successfully navigated the Planet of Memory! You learned how to use variables to store information, discovered the different data types (strings, integers, floats, and booleans), communicated with Mission Control using the input() function, and even did some space math. The star map is glowing again, revealing a new, mysterious destination.
Next up, we travel to the “Crossroads of the Cosmos,” where we will learn how to make decisions in our code and choose which path to take! See you in Part 3!
This is Part 2 of a 10-part series: “The Adventures of Ava the Astronaut: Learning Python One Planet at a Time.”