Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 In the last challenge where we calculated the BMI, 1
you saw how when we had a number that had a long list of numbers after the 2
decimal point. For example, 3
if I had divided 8 by 3 and I print this out, 4
you'll see that the value is 2.666666 and if we had just turned this 8 5
divided by 3 into an integer, right now it's a floating point number, 6
but if I convert it into an integer, 7
you'll see that all it does is it just chops off everything after the decimal 8
point. Instead of what we would traditionally do, 9
which is to round the number. 10
So if it's 2.5 it would go to 3, if it's 2.4 it would go down to 2. 11
Now in Python it's super easy to round numbers. 12
All you have to do is to use the round function like this. 13
If we write round(8 / 3) then it's going to round it into a 14
whole number and you'll see that instead of 2 we actually get 3 now 15
because 2.6 recurring becomes 3. 16
Now if you wanted to, you can actually go a step further. 17
You can specify the number of digits of precision you want to round it to. 18
So if I said I want to round it to two decimal places, 19
then I could write (8 / 3, 2) and then the number of places 20
that I want around it to. 21
So now our 2.666 recurring becomes 2.67 because I said we should round it to 2 22
decimal places. So if it makes it easier, 23
it might be easier if I write it like this. 24
So 2.666666666 and I'm going to round it to two decimal places 25
and again, I get the same result, 26
2.67. Now another way of modifying numbers is instead of dividing, 27
say 8 / 3, 28
we can also use the floor division, 29
so where you have two forward lashes instead of just one. 30
Now we know that whenever we divide any number by any other number, 31
the result always gets turned into a floating point number. 32
Now if you didn't want that and you just wanted an integer, 33
so a whole number chopping off all the numbers after the decimal in place, 34
you can just use the floor division like this. 35
And in this case, you would get 2 straight away without having to convert it 36
into an integer. And in fact, 37
if I go ahead and check the data type of the result of this calculation, 38
you'll see that it's actually an integer 39
whereas if I had just used the single, um, forward slash division, 40
then I get a floating point number with decimal places. 41
Even if this is a clean division, say 4 / 2, 42
this is still going to become a floating point number and the number is going to 43
be represented like this, 2.0, like so. 44
Now if we had saved the results of this calculation into a variable instead, 45
then one of the things that you can actually do is to continue performing 46
calculations on this variable. So for example, 47
I could do 4 / 2, 48
which is going to be equal to 2. But then if I want to divide it by the 2 49
again, 50
I could actually say result /= 2. And when I now print results, 51
I'll actually get 1 because it's 4 / 2 then divided by 2 52
again. Now very often when you're writing code, 53
say for example if you're keeping track of the user's score, 54
so you could have score = 0 to begin with and every single time in 55
your code say a user scores a point, 56
then you can get hold of this score variable again 57
and instead of saying score now equals the previous value of score plus one, 58
you can simply use this shorthand, +=, 59
so +=1. And now when we print score, 60
you'll see that it's actually equal to 1. 61
So instead of using +=, you can use -=, 62
which just takes the previous version of score and removes 1 from it. 63
*= and /=. 64
So this is really handy when you have to manipulate a value based on its 65
previous value, which you'll have to do a lot in programming. 66
Now the final thing I want to show you is something called F strings and this 67
makes it really easy to mix strings and different data types. 68
So far, up to this point, 69
if we wanted to print, uhm, something like your score is, 70
and then we wanted to print the score we have to write plus, 71
but of course because these are different data types, 72
this is a string and this is an integer, we got a type error. 73
So we've had to convert this into a string before it will actually successfully 74
print when both the datatypes match. 75
Now this is quite painful and understandably a lot of programmers will need some 76
slightly more convenient way of incorporating things that have different data 77
types. Let's say um, the score is equal to zero. Um, 78
let's say the height is equal to 1.8 and isWinning is equal to true. 79
So here we've got a integer, a float, and a boolean, 80
and we want to mix it all into a sentence that is a string and get it printed 81
out. 82
So instead of having to convert all of these and use a whole bunch of plus 83
signs and then you have to convert everything into a string, 84
it's really, really painful, right? 85
So what we can do instead is use something in Python known as an F string. 86
And when an F string allows us to do is in front of a string like this one, 87
we type the character 'f' and it's really important that it goes in front of the 88
double quotes or a single quotes if you want to write your strings like this. 89
But I like to use double quotes and a lot of other Python programmers do too. 90
So essentially you're adding just the character f in front of the string, 91
and now this is an F string and you can start adding various values into this 92
string. So for example, if I wanted to write, 93
your score is equal to this variable score, 94
then I can put that variable inside a set of curly braces like this. 95
And now when I print my string, this one right here, 96
you'll see that it says your score is zero and it does all of the converting and 97
all of the stuff behind the scenes and you don't have to worry about any of 98
this. So if I want to continue along, 99
I could say your score is this, 100
your height is adding the height and then you are winning is then let's add 101
that final boolean value 102
and get it to run. 103
You can see that our entire string now prints out your score is 0, 104
your height is 1.8 you are winning is True. 105
So all of these different data types got combined into a string by using an F in 106
front of the string and then using these curly braces to place our variables 107
into this string. By using f strings, 108
you cut down on a lot of the manual labor of inserting different data types into 109
a string. 110
And this is going to come in really handy just about on the next lesson where 111
I've got a coding challenge for you. 112
Head over there and complete the challenge.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.