Lessons ·
Variables and types
Name a value, recognize its type, calculate and convert, with Awa's sales.
Transcript
Let's come back to Awa, who runs her shop in Yaoundé. On each item, she sticks a label with a price. That way, when a customer asks how much the soap costs, she doesn't have to keep everything in her head: she simply reads the label.
A program has exactly the same need, because it must remember certain values while it works. For that, it uses a variable. So a variable is a bit like this label? Yes, that is a very good image. A variable is a name placed on a value.
Let's see how Awa writes it. On the left, she puts the name, here price. In the middle, she puts the equal sign. On the right, she puts the value, here 1500. And there you go, the label is on.
But this equal sign, is it the same as in mathematics? No, and it is very important to understand this well. In mathematics, it says that two things are equal. Here, it gives an order: put the value 1500 on the name price. So we can read this line like this: price receives the value 1500.
Now, Awa wants to see this price on the screen. For that she uses the print function, which displays things. Between the parentheses, she writes the name price, without quotation marks. So Python reads the label and displays its value, 1500.
And if the price changes tomorrow, does she have to rewrite her whole program? No, she does what she does in her shop: she replaces the label. She writes price again, the equal sign, then the new value, 1800. The old value is then lost for this name.
Python always reads code from top to bottom, one line after another. When it reaches this second print, the last value given to price is 1800, so that is the value it displays.
In her shop, Awa doesn't only have prices. She also has quantities, city names and customers who are registered or not. Does Python store all of this in the same way? No, and that makes sense. We can add two quantities, but adding two city names would make no sense at all. So Python must know what kind each value is, and that is what we call its type.
Let's start with the quantity, 25. It is a number without a decimal part, which we call an integer. In Python, this type is called int. Look, it goes on the shelf for whole numbers.
Here now is a price of 1500 point 5. It has a part after the decimal point, so it is another type, called float. In Python, as in English, we write a point and never a comma.
Let's move on to the name of the city, Douala. This is text, and this type is called str. We always recognize text by its quotation marks, which tell Python: this is not the name of a variable, it is text.
So if I write 25 in quotation marks, it is no longer a number? Exactly, and look where it goes: on the shelf for text. To us, it looks like a number, but to Python, it is just two characters, the 2 and the 5. Remember this detail, because it causes a lot of errors, and we will meet it again in a moment.
Finally, to say whether a customer is registered or not, there are only two possible answers: true or false. This type is called bool, and its two values are written True and False, with a capital letter.
Now that she has her numbers, how does she do her calculations? Addition, subtraction and multiplication are written almost as at school, with the plus sign, the minus sign and the star.
Division, however, deserves that we take our time, because it comes in several forms. Let's imagine that Awa has 17 items to store in batches of 5.
With a single slash, we do the usual division: 17 divided by 5 gives 3 point 4. But Awa cannot make 3 batches and 4 tenths of a batch.
So she wants to know how many full batches she can make. For that, she writes two slashes, which keep only the whole part. The result is 3, and you can see her three full batches.
And the items that don't fit in any batch? That is the remainder of the division, and we get it with the modulo operator, which is written with the percent sign. So here, it has nothing to do with a percentage. The result is 2, since 3 batches of 5 make 15 items, and 2 more are left to reach 17.
Remember one small trap as well. Even when a division comes out exactly, the single slash always gives a float. So 10 divided by 2 does not give 5, but 5 point 0.
Let's go back to our digits in quotation marks. When a customer fills in a form on the internet and types 42 in the quantity box, the answer almost always arrives as text, even if the customer typed a number.
So if Awa adds 8 to this answer, she doesn't get 50? No, she even gets an error, and Python stops. This error is called TypeError, which means a type error. Python refuses to add text and a number, because it doesn't know what you really want. An error is not a disaster: it is Python explaining to you what is wrong.
And how do we fix this error? We must first turn the text into a number, and that is what we call a conversion. The int function takes the text 42 and gives the whole number 42. Look closely: the quotation marks have disappeared.
Now, Python has two numbers in front of it, 42 and 8, and the addition does give 50. So remember this rule: what comes from a form is text, and we convert it before calculating.
One last thing remains to be seen, because we often want to display a result inside a sentence. Let's take a school report card, with a name and a grade of 15 point 75.
We then write an f-string, which looks like text with holes in it. We put the letter f just before the quotation marks. Then, wherever we want a value, we open curly braces and write the name of the variable.
And the format, colon, point 1 f, what is it for? It asks Python to round the number to a single digit after the decimal point. That is why 15 point 75 is displayed as 15 point 8. So Python fills in the holes, and the sentence appears.
Very good, and now, what should we do to check that we have understood? The best thing is to try without watching the video again. It may seem difficult, but it is precisely by searching for the answer from memory that we retain it. In the lesson, you will write these calculations yourself, then spot the mistake in code written by an artificial intelligence.
Voices: Google Chirp 3 HD (Despina, Enceladus).
Photos : One Idea LLC, StockSnap, CC0

