S02 · Place Your First Prop

What we learned in Python

The Python from session S02, one idea at a time. The same notes are in your course folder as lessons/sessions/s02/student/python-notes.md.

Back to the S02 slides

1 · Python concept

The idea

Today's Python
variableA name that stores a value, so you can use the value later.
assignmentUses = to store a value under a name. In Python, = means “store this”, not “is equal to”.
stringText in quotation marks, like "Moon Compass".
integerA whole number with no quotation marks, like 240.
coordinatesNumbers that describe a position. On the Trail screen, x is left/right and y is up/down.

2 · Code we wrote

The code

object_name = "Moon Compass"
x = 240
y = 180
color = "purple"

print(object_name)
print(x, y)
print(color)

Run it from the course folder:

python lessons/sessions/s02/student/starter.py

Output:

Moon Compass
240 180
purple

3 · What the code means

Line by line

Each important line
object_name = "Moon Compass"Store the string "Moon Compass" under the variable name object_name.
x = 240Store the integer 240 under the variable name x.
y = 180Store the integer 180 under the variable name y.
color = "purple"Store the string "purple" under the variable name color.
print(object_name)Show the value stored in object_name. No quotes, so Python uses the variable, not the word.
print(x, y)Show both values on one line, separated by a space: 240 180.

4 · Why programmers use this

Beyond this lesson

Variables let a program remember information and reuse it by name. Change the value in one place and everything that uses the name follows.

Integers can be compared and calculated with; text stored as a string cannot.

Outside Explore Studio: a map app stores your location as numeric coordinates, a spreadsheet stores values under column names, and a science experiment stores measurements in variables.

5 · What we debugged

A bug worth understanding

x = "240"
print(x + 100)
TypeError: can only concatenate str (not "int") to str

"240" is text, not a number, so Python cannot add 100 to it. The object file has the same rule: x: "240" fails validation because the package needs an integer.

Fixed:

x = 240
print(x + 100)  # 340

6 · Key Python words

Words to know

Vocabulary
variableA name that stores a value.
valueThe data stored, like 240 or "purple".
assignment, =Storing a value under a variable name.
stringText inside matching quotation marks.
integerA whole number, written without quotes.
coordinatesNumbers that describe a position, like x and y.
TypeErrorPython was asked to combine values of types that do not fit together.

7 · Try it yourself

Your turn

  1. Predict, then test: what moves when you change x = 240 to x = 340?
  2. Add print(type(x)) and print(type(color)) to the starter. What does Python say each type is?
  3. In my-explore-world/explorer.py and companion.py, replace every TODO with concrete choices for your own Explorer and Companion (or check your existing values, if you set these up before), then run both files and confirm your own values appear in the output.

What we discovered

Coordinates, maps, and navigation

In Nova’s world · fiction

The Moon Compass is a fictional exploration tool: the expedition's first instrument in the story.

In the program, it is a world object you placed with two integers, x and y.

In our world · fact

  • Coordinates are numbers that describe a position.
  • Maps use coordinate systems too. Latitude says how far north or south of the equator a place is; longitude says how far east or west of the prime meridian. The Statue of Liberty is near 40.7° N, 74.0° W.
  • On most maps north is up, so latitude grows as you go up. On a computer screen, y grows as you go down.
  • Before GPS, navigators used maps, magnetic compasses, the positions of the Sun and stars, and careful records of speed, direction, and time.
  • A magnetic compass needle lines up with Earth's magnetic field, so it points roughly north. Magnetic north is not exactly the same place as the geographic North Pole.

Think about it: What do coordinates describe? How is Nova's Moon Compass different from a real magnetic compass?