PYTHON / CHAPTER 01

Your first Python program

Beginner

Run a few statements and follow how named values reach printed output.

By the end of this chapter

  • Read statements from top to bottom
  • Assign text and numbers to names
  • Predict what print displays

The idea

A program is a sequence of instructions. Python normally executes these statements from top to bottom. print displays a value, and text inside matching quotes is a string. The quotes tell Python where the text begins and ends; they are not included in the displayed result.

An assignment such as completed = 2 gives a name to a value. A later statement can use that name, and a later assignment can bind it to a new value. The equals sign in an assignment means store this value under this name, not ask a yes-or-no question. Arithmetic such as completed + 1 computes a new number.

Try predicting each printed line before running the example. Change one input and compare what happened with your prediction. The next chapters build strings, lists, decisions, and functions from these small statements. You do not need to understand imports, classes, or formatting codes to begin.

Python example
name = "Coder"
completed = 2
print(name)
print(completed)
completed = completed + 1
print(completed)
Result
Coder
2
3

The first print reads text. The last print sees the value after completed has been increased by one.

Try it yourself

Store your project title and a task count in two variables. Print them, add two to the count, and print the new count.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

Open Python coding challenges

CHECK YOUR UNDERSTANDING

Chapter checkpoint

What does the final line print?
count = 4
count = count + 1
print(count)