Variables: giving a value a name
Printing the same ticket text in three places means typing it three times. A variable stores a value once under a name you can use anywhere after it.
ticket = "My parcel has not arrived"
print(ticket)= means store, not equals. The line says: put this text somewhere and call it ticket. After that, writing ticket gives you the text back.
Changing a variable
waiting = 3
print("Waiting:", waiting)
waiting = waiting + 1
print("Waiting:", waiting)print accepts several values separated by commas and puts a space between them.
waiting = waiting + 1 looks odd as maths but reads fine as instructions. Python works out the right side first, 3 + 1, then stores the result under the name on the left.
Kinds of value
Every value has a type, which decides what you can do with it. type tells you which one you have.
print(type("My parcel has not arrived"))
print(type(3))
print(type(4.5))
print(type(True))str is text, short for string. int is a whole number. float is a number with a decimal point. bool is True or False, and nothing else.
A name that does not exist
ticket = "My parcel has not arrived"
print(tickt)A typo in a name is a NameError. Python only knows the names you have stored something under, and here it even suggests the one you probably meant.
Names use lowercase letters, digits and underscores, like ticket_count, and cannot start with a digit. A name that says what the value is makes the code easier to read than x.
- Store your name in a variable called
customerand print it. - Add a line
waiting = waiting - 2and print the result. - Print
type("3"). The quotes decide the type, not what is inside them.
You understood something today that you didn't yesterday.