~/learn/Python · Fundamentals

Your first program: values and variables

Start from zero: show a message, name a value and follow a program one line at a time.

8 min readlesson 1 of 23printvariablesassignmentcomments

why this matters

Every script starts with naming values and following instructions. These small steps make later examples readable instead of mysterious.

A program is a set of instructions. Python normally follows them from top to bottom. You do not need to memorise everything: start with one instruction, predict what it will do, and compare that prediction with the result.

1 · Showing a result

print(...) displays a value. The parentheses hold what you want to display. Text goes inside matching quotes; the quotes mark where the text starts and ends, but are not printed.

python · editable
loading editor…

Try it: Change the first message to Hello, Ada! Keep the quotation marks.

Expected output

Hello, QueryLab!
I am learning Python.

A variable is a name for a value. In learner = "Ada", the right side is the value and the left side is its name. The equals sign means assign: give this name this value. print(learner) reads that name; print("learner") displays the literal word instead.

python · editable
loading editor…

Try it: Store your own name in learner, and change lessons_finished to 2.

Names can be reassigned. Python reads the old value on the right before storing the new value on the left. Changing a name later does not go back and change a calculation that already happened.

python · editable
loading editor…

Try it: Predict both results, then change completed = 5 to completed = 10. Does next_total change?

Readable names

Use descriptive names such as lessons_finished. Names are case-sensitive: score and Score are different. Names cannot start with a digit, contain spaces, or use reserved words such as if.

A comment starts with # and explains the code to a person; Python ignores the comment. Blank lines help separate ideas. In the next lesson, you will learn how Python distinguishes numbers from text.

key takeaways

  • Python normally follows instructions from top to bottom.
  • print displays a value; quotes mark literal text.
  • = assigns a value to a name; reassigning it does not redo earlier calculations.

check your understanding

count = 2 next_count = count + 1 count = 10 What does print(next_count) show?

free practice

Build confidence with a question you can solve without Pro.