Chapter 2: Your First Python Mission: The Spacecraft (Variables and Types)
Welcome back, Space Explorer! In our previous chapter, we successfully launched our Python journey by setting up our development environment and writing our first Python program. In this chapter, we’re going to embark on our first mission: designing our spacecraft. Along the way, we’ll learn about a crucial concept in programming: variables. We’ll also explore different types of data we can store in variables.
What are Variables?
In Python, and in programming in general, we often need to store and work with different types of data. For example, in the previous chapter, we calculated how many days it would take to reach Mars. We needed to store the speed of our spacecraft and the distance to Mars somewhere to make that calculation. That’s where variables come in.
Variables in Python are like containers or boxes where we store data. Once we’ve stored data in a variable, we can use it, modify it, or even combine it with other variables. Let’s see how we can create a variable:
1 # Creating a variable named 'speed' and assigning it a value of 100000
2 speed = 100000
Here, speed is the name of the variable, and 100000 is the value we’re storing in it. The = sign is known as the assignment operator because it assigns the value on its right to the variable on its left.
We can use the value stored in a variable by using the variable’s name:
1 print(speed)
This will print out the value 100000, the current value of the speed variable. Try it yourself!
Different Types of Data
In Python, variables can store different types of data. There are several data types in Python, but we’ll focus on four primary types for now:
Integer: This is a whole number, without a fraction. In Python, we call it
int.For example, the number of planets in our solar system,8,is an integer.Float: This is a number that contains a decimal or fraction. In Python, we call it
float.For example, the speed of light, approximately299792.458km/s, is a float.String: This is a sequence of characters enclosed within quotes (either single or double). In Python, we call it
str.For example,"Hello, Space Explorer!"is a string.Boolean: This is a type that can only have one of two values:
TrueorFalse.In Python, we call itbool.
Here’s an example of each of these types:
1 # An integer variable
2 planets = 8
3
4 # A float variable
5 light_speed = 299792.458
6
7 # A string variable
8 greeting = "Hello, Space Explorer!"
9
10 # A boolean variable
11 is_earth_round = True
You can check the type of a variable using the type() function:
1 # Check the type of the 'planets' variable
2 print(type(planets))
This will output <class 'int'>, confirming that planets is indeed an integer.
Naming Variables
When naming your variables, there are a few rules and best practices you should keep in mind:
Variable names can only contain letters, numbers, and underscores. They can’t contain spaces, punctuation, or other symbols.
Variable names can’t start with a number.
Python is case-sensitive, which means
speed,Speed,andSPEEDare all different variables.Don’t use Python keywords as variable names. Keywords are special words that Python reserves for its language syntax (like
print,for,if,etc.).It’s considered best practice to name your variables in a way that describes the data they’re storing. For example,
speedis a good name for a variable that stores the speed of your spacecraft.Also, it’s common to use lowercase letters and underscores for variable names (this is known as “snake case”). For example:
light_speed,number_of_planets.
Project: Designing Your Spacecraft
Now that we’ve learned about variables and data types, let’s design our spacecraft! We’ll define several variables to store key details about our spacecraft.
First, we need to name our spacecraft. Let’s call it “Voyager 3”. We’ll store this in a string variable:
1 # The name of our spacecraft
2 craft_name = "Voyager 3"
Next, let’s define the speed of our spacecraft in km/s. We’ll choose a nice round number, say 25000, and store it in a float variable:
1 # The speed of our spacecraft in km/s
2 craft_speed = 25000.0
Finally, let’s indicate whether our spacecraft is ready for launch. We’ll set this to True and store it in a boolean variable:
1 # Is our spacecraft ready for launch?
2 ready_for_launch = True
We’ve now defined three variables that store key details about our spacecraft. We can print out these details using the print() function:
1 # Print out the details of our spacecraft
2 print("Craft name:", craft_name)
3 print("Craft speed:", craft_speed, "km/s")
4 print("Ready for launch?", ready_for_launch)
Go ahead and run these cells in your Google Colab notebook. If all goes well, you should see the details of your spacecraft printed out!
Wrapping Up
Congratulations on completing your first Python mission, Space Explorer! You’ve learned about a fundamental concept in programming - variables - and have started working with different types of data. You’ve even designed your first spacecraft!
In the next chapter, we’ll pack for our space journey by learning how to organize our inventory using lists and tuples. It’s going to be another exciting mission, so make sure to come prepared!