Chapter 1: Launching Your Python Journey
Welcome to the first chapter of your Python journey, space explorer! In this chapter, we will prepare you for your mission by discussing why we’re learning Python and how to set up your programming environment. We will also guide you on how to write and run your first Python program.
Why Learn Python?
Python is a fantastic language to learn for several reasons:
Easy to read and write: Python’s syntax is designed to be readable and straightforward, which makes it an excellent language for beginners.
Powerful and versatile: From web development to data analysis to artificial intelligence, Python is used in a wide array of domains.
Strong community: Python has a vibrant community that contributes to a massive ecosystem of libraries and frameworks.
High demand in job market: Python is one of the top programming languages sought by employers.
Setting up your programming environment with Google Colab
We’ll be using Google Colab as our development environment. Google Colab is a free, online platform that allows you to write and execute Python code. It requires no setup, and you can access it through your browser, which makes it a perfect tool for our journey.
To start using Google Colab, follow these steps:
Open your favorite web browser and navigate to the Google Colab website.
Sign in with your Google account. If you don’t have one, you will need to create it.
Once signed in, you’ll be taken to a page with a “NEW NOTEBOOK” option in the lower right. Click on this to start a new notebook.
You’re now in a Google Colab notebook! Notebooks are made up of cells, which can contain either code or text. To run a cell, click on it and then click the play button that appears on the left, or simply press Shift + Enter.
Let’s start by running some code. In the first cell of your new notebook, type the following:
1 print("Hello, Space Explorer!")
Then press Shift + Enter to run the cell. You should see the message “Hello, Space Explorer!” appear beneath the cell. Congratulations, you’ve just written and run your first Python program!
Throughout this book, you’ll be writing and running a lot of Python code in this way. You can always create new cells by clicking the “+ Code” or “+ Text” buttons in the upper left.
Writing your first Python program
Now, let’s write a Python program that does a bit more. How about a program that calculates the number of days it takes to travel to Mars?
Copy the following Python code into a new cell in your Google Colab notebook:
1 # Speed of the spacecraft in km/h
2 speed = 100000
3
4 # Distance from Earth to Mars in km
5 distance = 225000000
6
7 # Calculate the time to Mars in hours
8 time_to_mars = distance / speed
9
10 # Convert that time to days
11 days_to_mars = time_to_mars / 24
12
13 # Print out the result
14 print("It will take about", days_to_mars, "days to reach Mars.")
Once again, run the cell with Shift + Enter. The program calculates the time it takes to travel to Mars and prints out the result!
In the next chapters, we’ll dive deeper into what each line of this program does. For now, it’s enough to know that you’ve written a program that does a useful calculation, and you’re well on your way to becoming a Python programmer.
In the next chapter, we’ll be designing our spacecraft for our space journey, learning about Python variables and different data types along the way. So buckle up and get ready to launch!