About 5 minutes
Variables: name tags
Programs need to remember things: a name, a price, today's special. You'll learn to store a value under a name, and use that name later.
1. Sticking a name on a value
This line stores the text Alex and sticks the name name on it:
name = "Alex"
nameThe name you choose. Like a tag.=Means: stick this name on this value. Not "equals" like in maths."Alex"The value: text, in quotes.Nothing appears on the screen. It's only stored. The name goes on the left, the value on the right.
2. Quotes or no quotes
This is the key idea. No quotes means: go and find the name, and use its value. Quotes mean: this exact text.
No quotes
name = "Alex" print(name)
OUTPUT
Python finds the name and shows its value.Alex
Quotes
name = "Alex"
print("name")OUTPUT
It's just the text "name". Python doesn't look anything up.name
TRY IT: STORE YOUR NAME, THEN PRINT IT
OUTPUT
Press Run
name = "Ravi". What does print("name") show?
3. Moving a name
A name can be moved to a new value. The last one wins.
name = "Alex" name = "Nora" print(name)
OUTPUT
Nora
4. Naming rules
Use letters, numbers and underscores. No spaces, and don't start with a number. Capitals matter: name and Name are different names.
customer_name workscustomer name doesn't (space)2nd_name doesn't (starts with a number)5. One common mistake
Using a name that doesn't exist
print(city)
NameError: name 'city' is not defined
There's no name called city yet. Check the spelling, or store it first: city = "Oslo".One more short lesson before your missionF1.3 Comments
Next lessonWas this page clear?