Python for AIPython 3.10+ · Pydantic 2.12
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
33 small wins to finish your pathNext lesson

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.

Example
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

Example
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.

Example
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

Example
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.

Try it yourself
  • Store your name in a variable called customer and print it.
  • Add a line waiting = waiting - 2 and 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.