Programs and expressions 0/5

Lesson 2 of 21

Elements of programming

Build values from expressions, name them, and see how Python and C++ work out a nested expression.

Part 1 of 7 Names

Giving a value a name

Writing every number out in full works until you need the same number twice. Say you earn 15 an hour, you worked 9 hours this week, and you want to see your pay, and then your pay with a bonus of 20:

Sign up to run
print(9 * 15)
print(9 * 15 + 20)

It works, but there are two problems. 9 * 15 is written twice, and nothing in the program says that 9 is the hours and 15 is the rate. And when your rate goes up to 16, you have to find every 15 and change it. Miss one and you get a wrong answer, with no error message to warn you.

Names fix both. A name is a word you choose that stands for a value. You work the value out once, give it a name, and from then on you use the name:

Sign up to run
hours = 9
rate = 15
pay = hours * rate
print(pay)
print(pay + 20)

A line like pay = hours * rate is an assignment. Python first works out the expression on the right of =, here 9×15=1359 \times 15 = 135, and then binds the name on the left to that value. After that, pay is an expression like any other: wherever you write it, Python uses 135. And if the rate changes, you edit one line, rate = 15.

You choose the names yourself. A name can use letters, digits and underscores, but it can't contain a space or start with a digit. So week_pay and pay2 are fine, but week pay and 2nd are errors. Capitals count, so Pay and pay are two different names. Pick a name that says what the value means: hours tells the next person who reads your program much more than h.

You have to assign a name before you use it. If the program started with print(rate), Python would stop with NameError: name 'rate' is not defined. A misspelled print, such as prnt, gives the same error, and it means the same thing here: Python met a name that has no value.

Giving a name a new value

Suppose you got the hours wrong and actually worked 10. You can assign hours again, and the new value replaces the old one. Here we change hours after pay has been worked out, and then add the bonus to pay itself. Before you run it, guess what the second line of output will be:

Sign up to run
hours = 9
rate = 15
pay = hours * rate
print(pay)
hours = 10
print(pay)
pay = pay + 20
print(pay)

The second line is still 135. When pay = hours * rate ran, it worked out 135 and gave pay that number. pay doesn't remember that the number came from hours * rate, so changing hours later doesn't touch it. If you want the pay for 10 hours, you run pay = hours * rate again after the change.

The last assignment, pay = pay + 20, looks wrong if you read it as maths, because nothing equals itself plus 20. In a program = means something else: work out the right side, then make that the new value of the name on the left. So the right side uses the old pay, 135, and only then does pay become 155.

Step through the names

It helps to watch this happen. The demo below runs a program one step at a time. The box beside the code is a frame: it lists every name the program has bound so far, with its current value. Python calls this one the global frame. Step through the payroll and keep an eye on pay when hours changes.

Use the left and right arrow keys to step, Home for the first step and End for the last.

  1. hours = 9(line that just ran)
  2. rate = 15(line that runs next)
  3. pay = hours * rate
  4. print(pay)
  5. hours = 10
  6. print(pay)
  7. pay = pay + 20
  8. print(pay)

Frames

Global current
hours
9

hours is bound to 9 in the global frame.

Output

Step 1 of 16

The other two programs are worth a look too. In the second, height became 7 while width was still 3, so the last line prints 10×7=7010 \times 7 = 70, not 10×1410 \times 14. In the third, each line reads total before replacing it, so it goes 5, then 15, then 30.

Write a program that reads two numbers.

In Multiplication 1, your program reads two whole numbers and prints their product. The judge runs your submitted code with several inputs and compares its output with the expected answers.

Example input

2 5

Expected output

?
  1. Open Multiplication 1 on AtCoder. Sign in or create an AtCoder account, choose your programming language, paste your program and submit it.

  2. Accepted means your program passed the judge’s tests. For Wrong Answer, check your calculation and output format, then edit and submit again.