Now that Python is installed and VS Code can run a script, this page covers the two building blocks of every program: storing values in variables and displaying them with screen output.

Variables

A variable is a name associated with a value stored in memory. Unlike C++, Python does not require you to declare a variable’s type ahead of time - simply assign a value with = and Python figures out the type automatically:

altitude = 10000
velocity = 245.6
craft_name = "Voyager"

Each of these lines creates a variable and immediately assigns it a value. There is no separate declaration step, and a variable can later be reassigned to a value of a different type if needed. The types used above - whole numbers, decimal numbers, and text - are covered in more detail on the Input & Data Types page.

Naming Variables

Python variable names:

  1. Must start with a letter or underscore (not a digit)
  2. May contain only letters, digits, and underscores
  3. Are case-sensitive, so velocity and Velocity are different variables
  4. Cannot be one of Python’s reserved keywords, such as for, if, or class

The Python style guide (PEP 8) recommends snake_case for variable names - all lowercase, with underscores separating words, as in the examples above. Following this convention makes your code easier for others (including your future self) to read.

Displaying Output with print()

Python does not automatically display the value of a variable the way MATLAB does when a line isn’t terminated with a semicolon. To show something on screen, call the built-in print() function:

altitude = 10000
print(altitude)
print("The altitude is:", altitude)

10000
The altitude is: 10000

A few things to notice:

  • print() automatically adds a newline at the end of its output, so you do not need to add \n yourself like you would with cout in C++.
  • Passing several values, separated by commas, prints them separated by a single space. This works even when mixing text and numbers - print() converts each value to text for you.
  • Typing a bare variable name on its own line, like altitude, only echoes its value when working directly in an interactive Python session. Inside a script file, it does nothing, so you must call print().

Formatting Output

For engineering calculations, printing every digit of a floating point number is rarely useful. An f-string (formatted string) lets you embed variables directly inside a string and control how numbers are displayed:

mass = 1200.0
speed = 245.573
kinetic_energy = 0.5 * mass * speed**2

print(f"Kinetic energy: {kinetic_energy} J")
print(f"Kinetic energy: {kinetic_energy:.2f} J")

Kinetic energy: 36183658.9974 J
Kinetic energy: 36183659.00 J

An f-string is written like a normal string, but with an f immediately before the opening quote. Anything inside curly braces {} is evaluated as Python code and inserted into the string. Adding :.2f after a value formats it as a fixed-point number with 2 digits after the decimal point. The number of digits can be changed to whatever precision is appropriate.

Example: Formatted Altitude Report

Question

A weather balloon rises at 5.25 meters per second and has been ascending for 320 seconds. Write a Python script that computes the balloon’s altitude and prints it rounded to 1 decimal place, in the form Altitude: ___ m.

Solution

climb_rate = 5.25
time_elapsed = 320
altitude = climb_rate * time_elapsed

print(f"Altitude: {altitude:.1f} m")

Altitude: 1680.0 m

Reading Questions

  1. What is the main difference between declaring a variable in C++ and creating one in Python?
  2. Which of these are valid Python variable names: 2x_speed, x_2speed, for, x-speed, _altitude?
  3. What naming convention does PEP 8 recommend for Python variables?
  4. What happens if you type a variable name by itself on a line in a Python script, rather than passing it to print()?
  5. What character does print() automatically add to the end of its output?
  6. Write a line of code that prints the values of two variables, x and y, separated by a comma and space, in the form x = 3, y = 7.
  7. What does adding :.2f inside an f-string’s curly braces do?

Practice Problem: Fuel Burn Report

During a test flight, fuel remaining is logged at the start and end of the flight so the average burn rate can be calculated afterward.

Your Task

Write a script named fuel_burn_report.py that starts from the given fuel state:

fuel_start_lb = 2400
fuel_end_lb = 1150
flight_time_hr = 2.5

Compute:

  1. fuel_burned_lb - the fuel burned during the flight, in pounds (fuel_start_lb minus fuel_end_lb)
  2. burn_rate_lbhr - the average burn rate, in pounds per hour (fuel_burned_lb divided by flight_time_hr)

Print the burn rate with an f-string, rounded to 1 decimal place.

Your variable names for the two answers above must match exactly (fuel_burned_lb, burn_rate_lbhr) so that the checker below can find them.

Checking Your Work

Download check_fuel_burn_report.py and save it in the same folder as your fuel_burn_report.py script. Open a terminal in that folder and run:

python check_fuel_burn_report.py

The checker runs your script and reports whether each of the two values is correct. This is practice, not a graded assignment. If something doesn’t pass, use the feedback to find and fix the issue, then run the checker again.