bhylu.com

Advertisements

Python Variables

A variable name gives you a way to refer to a value. Assignment binds the name; it does not attach a permanent type declaration to the spelling of that name.

Understand the idea

Python names refer to objects. Reassigning a name points it at another object. For simple numeric work, think of each assignment as establishing the current value used by later expressions. Choose names that include important units so a future reader does not confuse minutes, seconds, and cents.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

minutes = 45
seconds = minutes * 60
print(seconds)
minutes = 30
print(minutes)

Read the result

The outputs are 2700 and 30. Reassigning minutes does not recalculate seconds; that earlier result was already evaluated.

Try one change

Recalculate seconds after the reassignment and compare it with the previously stored result.