All language subtitles for 8. Number Manipulation and F Strings in Python

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.