Lesson 2 of 144 min
Your first lines of Python
A variable keeps a value under a name; text and numbers act differently, and a function such as int turns text into a number.
After this lesson you can keep a value in a variable, tell text from a number, call a function, and read an error.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
Change a variable
A variable can take a new value. Python works out the right side first, with the old value, then keeps the answer under the same name. Real code counts this way all the time, and += is the short way to write it.
total = 9
total = total + 1
print(total)
total += 1
print(total)10 11
Ask Python the kind of a value
type() tells you the kind of a value: str for text, int for a whole number. A cell that ends with a name shows its value with quotes when it is text. Python uses its own single quotes there, whichever quotes your code wrote. len() counts the letters of a text.
cats = "7"
print(repr(cats))
print(type(cats))
print(type(7))
print(len("72"))'7' <class 'str'> <class 'int'> 2
Put a number into text
Joining text to a number stops with the error from the video, the other way round. str() turns the number into text first. An f-string does the same with less typing: put an f before the quotes, and a name in curly brackets is replaced by its value.
total = 9
print("Animals: " + str(total))
print(f"Animals: {total}")Animals: 9 Animals: 9
When int can't read the text
int() skips spaces and a new line around the digits, which a model's answer often has. But it reads only digits. If the answer is "7 cats", it stops with a ValueError. Then the code has to take the number out of the text first, or ask the model for the number alone.
print(int(" 7\n"))
print(int("7 cats"))7 ValueError: invalid literal for int() with base 10: '7 cats'
input() gives text too
input() asks the person at the keyboard and waits for their answer. Like a model's answer, it is always text, even when they type a number. So int() comes before any sum.
age = input("Age? ")
print(type(age))Age? 30 <class 'str'>
Run on Python 3.13.3. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
cats = "7" and dogs = 2. What happens when Python runs total = cats + dogs?
2 of 2
A chatbot answers "8", as text, and your code keeps it in reply. What does this print?
reply = "8"
print(int(reply) + 2)The ideas in this lesson
- Variables
- A variable is a name for a value: `=` works out the right side, then keeps it under the name.
- Text and numbers
- Quotes make text. `+` adds two numbers and joins two texts, and stops on text and a number.
- Calling a function
- A function's name, then brackets with its inputs, runs it: `int` gives back a number, and `print` shows its inputs.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
The bet
Here's a bet. You ask an AI about a photo of cats and dogs. It tells you how many of each. These four lines of Python add up its answers.
What will they print? The right sum, a strange number, or an error?
A variable
Seven cats, two dogs. Keep your guess.
First, what does the first line do?
Python takes the value on the right: a seven, in quotes. It keeps it in the memory, the one every cell shares. Under what name? The one on the left: cats.
This equals sign doesn't ask a question, like in maths. It's an order: keep this value, under this name.
A name for a value... is called a variable.
Line two does the same. The variable dogs holds a two.
Quotes make text
But why the quotes around the seven? Quotes make text. This seven is text, like the letters of a word... not a number to add. The memory shows it with its quotes. The AI's answers are text.
Plus joins text
So, back to the bet. Nine, seventy-two, or an error?
Line three reads cats and dogs from the memory. On two texts, plus joins them, one after the other.
Seventy-two.
Python works out the right side first. Then total keeps the answer. Line four shows it. Animals, seventy-two.
If you said nine, the plus looked like a sum. But on text, plus joins.
Plus adds numbers
Now take the quotes away. Seven and two are numbers... and plus adds them.
Animals, nine.
The same plus, and a different answer. It depends on the kind of value.
Text and a number
Now cats is text again, and dogs is a number. What will line three do?
It stops. Type error.
Read the error
These are Python's own words.
Str is Python's word for text.
Int is a whole number. And the long word at the end means join.
So Python can join text only to text. It doesn't guess what you meant.
int gives back a number
The AI sends text. So how do we get a number out of it?
Line three changes. Look: int, then brackets around cats.
Int is a function. You call a function by its name. Then come brackets, with its input inside.
It does one job... and gives back a value. Here, it reads cats, the text seven. And it gives back the number seven. Dogs is still the number two.
So what will line four print now?
Animals, nine. And cats still holds the text.
print is a function too
And print? Print is a function too. Inside its brackets are two inputs: the text "Animals", and total. Print shows them on one line.
One more thing. Print shows text without its quotes. So when you print seventy-two, it looks like a number. Only the memory shows that it's text.
A real model's answer
Is this just a toy? No. A real AI model answers in text. Ask it for a number, and the number comes back as text.
So code that adds up a model's answers calls int first.
Text and numbers
So here's what you saw. Quotes make text. Plus joins two texts, and adds two numbers.
And int turns text into a number.
The kind of value
So the kind of value decides what plus does. Now try it on this lesson's page, and take the quiz. Make Python stop with a different message.
You use this lesson in Lab 1: Score a chatbot's answers, after Lists, loops and if.
Facts last checked on .
