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

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,310 --> 00:00:03,670 In the last challenge where we calculated the BMI, 1 2 00:00:04,090 --> 00:00:09,090 you saw how when we had a number that had a long list of numbers after the 2 3 00:00:09,970 --> 00:00:11,440 decimal point. For example, 3 4 00:00:11,440 --> 00:00:16,440 if I had divided 8 by 3 and I print this out, 4 5 00:00:17,650 --> 00:00:22,650 you'll see that the value is 2.666666 and if we had just turned this 8 5 6 00:00:25,180 --> 00:00:30,070 divided by 3 into an integer, right now it's a floating point number, 6 7 00:00:30,400 --> 00:00:33,190 but if I convert it into an integer, 7 8 00:00:33,580 --> 00:00:37,690 you'll see that all it does is it just chops off everything after the decimal 8 9 00:00:37,690 --> 00:00:40,450 point. Instead of what we would traditionally do, 9 10 00:00:40,510 --> 00:00:43,060 which is to round the number. 10 11 00:00:43,510 --> 00:00:48,510 So if it's 2.5 it would go to 3, if it's 2.4 it would go down to 2. 11 12 00:00:49,630 --> 00:00:53,410 Now in Python it's super easy to round numbers. 12 13 00:00:53,800 --> 00:00:58,180 All you have to do is to use the round function like this. 13 14 00:00:58,870 --> 00:01:03,010 If we write round(8 / 3) then it's going to round it into a 14 15 00:01:03,010 --> 00:01:07,330 whole number and you'll see that instead of 2 we actually get 3 now 15 16 00:01:07,360 --> 00:01:10,240 because 2.6 recurring becomes 3. 16 17 00:01:11,170 --> 00:01:14,440 Now if you wanted to, you can actually go a step further. 17 18 00:01:14,470 --> 00:01:19,330 You can specify the number of digits of precision you want to round it to. 18 19 00:01:19,720 --> 00:01:22,990 So if I said I want to round it to two decimal places, 19 20 00:01:23,440 --> 00:01:28,440 then I could write (8 / 3, 2) and then the number of places 20 21 00:01:28,570 --> 00:01:29,680 that I want around it to. 21 22 00:01:30,280 --> 00:01:35,280 So now our 2.666 recurring becomes 2.67 because I said we should round it to 2 22 23 00:01:37,570 --> 00:01:40,510 decimal places. So if it makes it easier, 23 24 00:01:40,540 --> 00:01:42,400 it might be easier if I write it like this. 24 25 00:01:42,790 --> 00:01:47,560 So 2.666666666 and I'm going to round it to two decimal places 25 26 00:01:47,890 --> 00:01:49,510 and again, I get the same result, 26 27 00:01:49,600 --> 00:01:54,600 2.67. Now another way of modifying numbers is instead of dividing, 27 28 00:01:55,990 --> 00:01:59,470 say 8 / 3, 28 29 00:01:59,980 --> 00:02:02,830 we can also use the floor division, 29 30 00:02:02,860 --> 00:02:05,980 so where you have two forward lashes instead of just one. 30 31 00:02:06,550 --> 00:02:10,210 Now we know that whenever we divide any number by any other number, 31 32 00:02:10,240 --> 00:02:13,780 the result always gets turned into a floating point number. 32 33 00:02:14,380 --> 00:02:17,230 Now if you didn't want that and you just wanted an integer, 33 34 00:02:17,380 --> 00:02:20,770 so a whole number chopping off all the numbers after the decimal in place, 34 35 00:02:21,130 --> 00:02:23,860 you can just use the floor division like this. 35 36 00:02:24,430 --> 00:02:28,930 And in this case, you would get 2 straight away without having to convert it 36 37 00:02:28,930 --> 00:02:31,420 into an integer. And in fact, 37 38 00:02:31,750 --> 00:02:36,750 if I go ahead and check the data type of the result of this calculation, 38 39 00:02:37,450 --> 00:02:39,940 you'll see that it's actually an integer 39 40 00:02:40,420 --> 00:02:45,340 whereas if I had just used the single, um, forward slash division, 40 41 00:02:45,760 --> 00:02:49,570 then I get a floating point number with decimal places. 41 42 00:02:50,230 --> 00:02:53,860 Even if this is a clean division, say 4 / 2, 42 43 00:02:54,580 --> 00:02:59,580 this is still going to become a floating point number and the number is going to 43 44 00:03:01,000 --> 00:03:04,630 be represented like this, 2.0, like so. 44 45 00:03:05,890 --> 00:03:10,890 Now if we had saved the results of this calculation into a variable instead, 45 46 00:03:12,370 --> 00:03:17,370 then one of the things that you can actually do is to continue performing 46 47 00:03:17,950 --> 00:03:21,040 calculations on this variable. So for example, 47 48 00:03:21,220 --> 00:03:23,110 I could do 4 / 2, 48 49 00:03:23,440 --> 00:03:28,120 which is going to be equal to 2. But then if I want to divide it by the 2 49 50 00:03:28,120 --> 00:03:28,953 again, 50 51 00:03:29,020 --> 00:03:34,020 I could actually say result /= 2. And when I now print results, 51 52 00:03:38,380 --> 00:03:43,380 I'll actually get 1 because it's 4 / 2 then divided by 2 52 53 00:03:44,440 --> 00:03:48,490 again. Now very often when you're writing code, 53 54 00:03:48,730 --> 00:03:52,780 say for example if you're keeping track of the user's score, 54 55 00:03:53,170 --> 00:03:57,370 so you could have score = 0 to begin with and every single time in 55 56 00:03:57,370 --> 00:04:00,700 your code say a user scores a point, 56 57 00:04:01,090 --> 00:04:04,480 then you can get hold of this score variable again 57 58 00:04:04,750 --> 00:04:09,430 and instead of saying score now equals the previous value of score plus one, 58 59 00:04:09,790 --> 00:04:12,820 you can simply use this shorthand, +=, 59 60 00:04:13,240 --> 00:04:17,260 so +=1. And now when we print score, 60 61 00:04:17,560 --> 00:04:20,140 you'll see that it's actually equal to 1. 61 62 00:04:21,370 --> 00:04:25,120 So instead of using +=, you can use -=, 62 63 00:04:25,180 --> 00:04:29,920 which just takes the previous version of score and removes 1 from it. 63 64 00:04:30,460 --> 00:04:32,980 *= and /=. 64 65 00:04:33,190 --> 00:04:37,600 So this is really handy when you have to manipulate a value based on its 65 66 00:04:37,600 --> 00:04:40,360 previous value, which you'll have to do a lot in programming. 66 67 00:04:41,440 --> 00:04:46,300 Now the final thing I want to show you is something called F strings and this 67 68 00:04:46,300 --> 00:04:51,300 makes it really easy to mix strings and different data types. 68 69 00:04:51,910 --> 00:04:53,710 So far, up to this point, 69 70 00:04:54,190 --> 00:04:59,190 if we wanted to print, uhm, something like your score is, 70 71 00:05:00,670 --> 00:05:03,760 and then we wanted to print the score we have to write plus, 71 72 00:05:04,030 --> 00:05:06,310 but of course because these are different data types, 72 73 00:05:06,310 --> 00:05:09,940 this is a string and this is an integer, we got a type error. 73 74 00:05:10,510 --> 00:05:15,510 So we've had to convert this into a string before it will actually successfully 74 75 00:05:16,480 --> 00:05:18,880 print when both the datatypes match. 75 76 00:05:19,600 --> 00:05:24,600 Now this is quite painful and understandably a lot of programmers will need some 76 77 00:05:25,270 --> 00:05:29,830 slightly more convenient way of incorporating things that have different data 77 78 00:05:29,830 --> 00:05:33,880 types. Let's say um, the score is equal to zero. Um, 78 79 00:05:33,910 --> 00:05:38,910 let's say the height is equal to 1.8 and isWinning is equal to true. 79 80 00:05:41,200 --> 00:05:44,800 So here we've got a integer, a float, and a boolean, 80 81 00:05:44,860 --> 00:05:49,450 and we want to mix it all into a sentence that is a string and get it printed 81 82 00:05:49,450 --> 00:05:50,140 out. 82 83 00:05:50,140 --> 00:05:55,140 So instead of having to convert all of these and use a whole bunch of plus 83 84 00:05:55,390 --> 00:05:59,600 signs and then you have to convert everything into a string, 84 85 00:06:00,110 --> 00:06:02,120 it's really, really painful, right? 85 86 00:06:02,420 --> 00:06:07,420 So what we can do instead is use something in Python known as an F string. 86 87 00:06:10,850 --> 00:06:15,850 And when an F string allows us to do is in front of a string like this one, 87 88 00:06:17,120 --> 00:06:20,930 we type the character 'f' and it's really important that it goes in front of the 88 89 00:06:20,930 --> 00:06:24,740 double quotes or a single quotes if you want to write your strings like this. 89 90 00:06:25,220 --> 00:06:29,240 But I like to use double quotes and a lot of other Python programmers do too. 90 91 00:06:29,600 --> 00:06:34,600 So essentially you're adding just the character f in front of the string, 91 92 00:06:35,420 --> 00:06:40,420 and now this is an F string and you can start adding various values into this 92 93 00:06:40,970 --> 00:06:43,040 string. So for example, if I wanted to write, 93 94 00:06:43,340 --> 00:06:47,210 your score is equal to this variable score, 94 95 00:06:47,630 --> 00:06:52,520 then I can put that variable inside a set of curly braces like this. 95 96 00:06:53,270 --> 00:06:58,010 And now when I print my string, this one right here, 96 97 00:06:58,400 --> 00:07:03,400 you'll see that it says your score is zero and it does all of the converting and 97 98 00:07:03,980 --> 00:07:07,430 all of the stuff behind the scenes and you don't have to worry about any of 98 99 00:07:07,430 --> 00:07:11,450 this. So if I want to continue along, 99 100 00:07:11,480 --> 00:07:13,250 I could say your score is this, 100 101 00:07:13,790 --> 00:07:18,790 your height is adding the height and then you are winning is then let's add 101 102 00:07:24,830 --> 00:07:26,750 that final boolean value 102 103 00:07:28,850 --> 00:07:30,770 and get it to run. 103 104 00:07:31,130 --> 00:07:34,310 You can see that our entire string now prints out your score is 0, 104 105 00:07:34,550 --> 00:07:37,460 your height is 1.8 you are winning is True. 105 106 00:07:37,910 --> 00:07:42,910 So all of these different data types got combined into a string by using an F in 106 107 00:07:43,580 --> 00:07:48,580 front of the string and then using these curly braces to place our variables 107 108 00:07:49,640 --> 00:07:52,940 into this string. By using f strings, 108 109 00:07:52,940 --> 00:07:57,940 you cut down on a lot of the manual labor of inserting different data types into 109 110 00:07:59,030 --> 00:07:59,863 a string. 110 111 00:07:59,930 --> 00:08:04,880 And this is going to come in really handy just about on the next lesson where 111 112 00:08:04,880 --> 00:08:06,950 I've got a coding challenge for you. 112 113 00:08:07,670 --> 00:08:10,100 Head over there and complete the challenge. 11049

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.