Afrikaans
Akan
Albanian
Amharic
Arabic
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
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
1
In the last challenge where we calculated the BMI,
2
you saw how when we had a number that had a long list of numbers after the
3
decimal point. For example,
4
if I had divided 8 by 3 and I print this out,
5
you'll see that the value is 2.666666 and if we had just turned this 8
6
divided by 3 into an integer, right now it's a floating point number,
7
but if I convert it into an integer,
8
you'll see that all it does is it just chops off everything after the decimal
9
point. Instead of what we would traditionally do,
10
which is to round the number.
11
So if it's 2.5 it would go to 3, if it's 2.4 it would go down to 2.
12
Now in Python it's super easy to round numbers.
13
All you have to do is to use the round function like this.
14
If we write round(8 / 3) then it's going to round it into a
15
whole number and you'll see that instead of 2 we actually get 3 now
16
because 2.6 recurring becomes 3.
17
Now if you wanted to, you can actually go a step further.
18
You can specify the number of digits of precision you want to round it to.
19
So if I said I want to round it to two decimal places,
20
then I could write (8 / 3, 2) and then the number of places
21
that I want around it to.
22
So now our 2.666 recurring becomes 2.67 because I said we should round it to 2
23
decimal places. So if it makes it easier,
24
it might be easier if I write it like this.
25
So 2.666666666 and I'm going to round it to two decimal places
26
and again, I get the same result,
27
2.67. Now another way of modifying numbers is instead of dividing,
28
say 8 / 3,
29
we can also use the floor division,
30
so where you have two forward lashes instead of just one.
31
Now we know that whenever we divide any number by any other number,
32
the result always gets turned into a floating point number.
33
Now if you didn't want that and you just wanted an integer,
34
so a whole number chopping off all the numbers after the decimal in place,
35
you can just use the floor division like this.
36
And in this case, you would get 2 straight away without having to convert it
37
into an integer. And in fact,
38
if I go ahead and check the data type of the result of this calculation,
39
you'll see that it's actually an integer
40
whereas if I had just used the single, um, forward slash division,
41
then I get a floating point number with decimal places.
42
Even if this is a clean division, say 4 / 2,
43
this is still going to become a floating point number and the number is going to
44
be represented like this, 2.0, like so.
45
Now if we had saved the results of this calculation into a variable instead,
46
then one of the things that you can actually do is to continue performing
47
calculations on this variable. So for example,
48
I could do 4 / 2,
49
which is going to be equal to 2. But then if I want to divide it by the 2
50
again,
51
I could actually say result /= 2. And when I now print results,
52
I'll actually get 1 because it's 4 / 2 then divided by 2
53
again. Now very often when you're writing code,
54
say for example if you're keeping track of the user's score,
55
so you could have score = 0 to begin with and every single time in
56
your code say a user scores a point,
57
then you can get hold of this score variable again
58
and instead of saying score now equals the previous value of score plus one,
59
you can simply use this shorthand, +=,
60
so +=1. And now when we print score,
61
you'll see that it's actually equal to 1.
62
So instead of using +=, you can use -=,
63
which just takes the previous version of score and removes 1 from it.
64
*= and /=.
65
So this is really handy when you have to manipulate a value based on its
66
previous value, which you'll have to do a lot in programming.
67
Now the final thing I want to show you is something called F strings and this
68
makes it really easy to mix strings and different data types.
69
So far, up to this point,
70
if we wanted to print, uhm, something like your score is,
71
and then we wanted to print the score we have to write plus,
72
but of course because these are different data types,
73
this is a string and this is an integer, we got a type error.
74
So we've had to convert this into a string before it will actually successfully
75
print when both the datatypes match.
76
Now this is quite painful and understandably a lot of programmers will need some
77
slightly more convenient way of incorporating things that have different data
78
types. Let's say um, the score is equal to zero. Um,
79
let's say the height is equal to 1.8 and isWinning is equal to true.
80
So here we've got a integer, a float, and a boolean,
81
and we want to mix it all into a sentence that is a string and get it printed
82
out.
83
So instead of having to convert all of these and use a whole bunch of plus
84
signs and then you have to convert everything into a string,
85
it's really, really painful, right?
86
So what we can do instead is use something in Python known as an F string.
87
And when an F string allows us to do is in front of a string like this one,
88
we type the character 'f' and it's really important that it goes in front of the
89
double quotes or a single quotes if you want to write your strings like this.
90
But I like to use double quotes and a lot of other Python programmers do too.
91
So essentially you're adding just the character f in front of the string,
92
and now this is an F string and you can start adding various values into this
93
string. So for example, if I wanted to write,
94
your score is equal to this variable score,
95
then I can put that variable inside a set of curly braces like this.
96
And now when I print my string, this one right here,
97
you'll see that it says your score is zero and it does all of the converting and
98
all of the stuff behind the scenes and you don't have to worry about any of
99
this. So if I want to continue along,
100
I could say your score is this,
101
your height is adding the height and then you are winning is then let's add
102
that final boolean value
103
and get it to run.
104
You can see that our entire string now prints out your score is 0,
105
your height is 1.8 you are winning is True.
106
So all of these different data types got combined into a string by using an F in
107
front of the string and then using these curly braces to place our variables
108
into this string. By using f strings,
109
you cut down on a lot of the manual labor of inserting different data types into
110
a string.
111
And this is going to come in really handy just about on the next lesson where
112
I've got a coding challenge for you.
113
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.