Chapter 5: Plotting Your Course (Operators and Comparisons)

Welcome back, space explorer! By now, you’ve mastered quite a few Python basics. You can define variables, store and modify data in lists and tuples, print messages, and interact with your command center through user input. Now it’s time to put those skills to use as we plot the course for our space journey. For this, we need to understand Python’s operators and comparisons. So let’s dive in!

Calculating the Route (Arithmetic Operators)

In any space journey, you’ll have to do a lot of calculations. How much fuel will the spacecraft consume? What’s the shortest path to our destination? Python’s arithmetic operators can help us with these calculations. Here are the basic ones:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulo (remainder of division) (%)
  • Exponentiation (**)
  • Floor division (division that rounds down) (//)

Let’s use some of these operators to calculate the distance we’ll travel:

1 # Distance to the moon and back in km
2 distance_to_moon = 384400
3 distance = 2 * distance_to_moon
4 print("The total distance to the moon and back is", distance, "km")

In this example, we used the multiplication operator (*) to calculate the total distance.

Making Decisions (Comparison Operators)

During your journey, you’ll face decisions that depend on the current circumstances. For instance, if fuel is below a certain level, you might need to conserve it. To make these decisions, we use comparison operators, which allow us to compare two values. Here are the comparison operators in Python:

  • Equal to (==)
  • Not equal to (!=)
  • Less than (<)
  • Less than or equal to (<=)
  • Greater than (>)
  • Greater than or equal to (>=)

Comparison operators are used in conditions, which we’ll learn more about in the next chapter. For now, let’s use a comparison operator to check our fuel level:

1 # Current and minimum required fuel in liters
2 current_fuel = 5000
3 minimum_required_fuel = 2000
4 
5 # Check if we have enough fuel
6 enough_fuel = current_fuel >= minimum_required_fuel
7 print("Do we have enough fuel?", enough_fuel)

In this example, we used the >= operator to check whether current_fuel is greater than or equal to minimum_required_fuel. The result is a Boolean value (True or False), which we print out.

Plotting the Course (Assignment Operators)

As we travel through space, we’ll need to update our course based on new information. In Python, we do this using assignment operators. You’ve already used the basic assignment operator (=), which assigns a value to a variable. Python also provides several other assignment operators that perform an operation and an assignment in one step:

  • Add and assign (+=)
  • Subtract and assign (-=)
  • Multiply and assign (*=)
  • Divide and assign (/=)
  • Modulo and assign (%=)
  • Exponentiate and assign (**=)
  • Floor divide and assign (//=)

Let’s use an assignment operator to update our fuel level:

1 # Fuel consumption per day in liters
2 fuel_consumption_per_day = 100
3 
4 # Update the current fuel level
5 current_fuel -= fuel_consumption_per_day
6 print("The current fuel level is", current_fuel, "liters")

In this example, we used the -= operator to subtract fuel_consumption_per_day from current_fuel and assign the result back to current_fuel.

Project: Course Calculation System

Now that you’ve learned about Python’s operators and comparisons, it’s time for a new project! Your task is to create a course calculation system that asks the user for various details about their space journey and calculates the relevant information.

Here’s an example of what your program might do:

  1. Ask the user for the distance to their destination in kilometers.
  2. Ask the user for the speed of their spacecraft in kilometers per hour.
  3. Calculate and print the time it will take to reach the destination.
  4. Ask the user for the fuel consumption of their spacecraft in liters per hour.
  5. Calculate and print the total fuel consumption for the journey.
  6. Ask the user for the current fuel level.
  7. Check if the current fuel level is enough for the journey and print a suitable message.

Feel free to expand upon this idea and add more features! For instance, you might ask the user for more details about their journey and perform more complex calculations.

In the next chapter, we’ll learn how to make decisions in our Python code using conditional statements. This will allow our space journey to adapt to the changing circumstances. Until then, keep plotting your course, and remember: the stars are the limit!