Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,310 --> 00:00:03,670
In the last challenge where we calculated the BMI,
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
3
00:00:09,970 --> 00:00:11,440
decimal point. For example,
4
00:00:11,440 --> 00:00:16,440
if I had divided 8 by 3 and I print this out,
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
6
00:00:25,180 --> 00:00:30,070
divided by 3 into an integer, right now it's a floating point number,
7
00:00:30,400 --> 00:00:33,190
but if I convert it into an integer,
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
9
00:00:37,690 --> 00:00:40,450
point. Instead of what we would traditionally do,
10
00:00:40,510 --> 00:00:43,060
which is to round the number.
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.
12
00:00:49,630 --> 00:00:53,410
Now in Python it's super easy to round numbers.
13
00:00:53,800 --> 00:00:58,180
All you have to do is to use the round function like this.
14
00:00:58,870 --> 00:01:03,010
If we write round(8 / 3) then it's going to round it into a
15
00:01:03,010 --> 00:01:07,330
whole number and you'll see that instead of 2 we actually get 3 now
16
00:01:07,360 --> 00:01:10,240
because 2.6 recurring becomes 3.
17
00:01:11,170 --> 00:01:14,440
Now if you wanted to, you can actually go a step further.
18
00:01:14,470 --> 00:01:19,330
You can specify the number of digits of precision you want to round it to.
19
00:01:19,720 --> 00:01:22,990
So if I said I want to round it to two decimal places,
20
00:01:23,440 --> 00:01:28,440
then I could write (8 / 3, 2) and then the number of places
21
00:01:28,570 --> 00:01:29,680
that I want around it to.
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
23
00:01:37,570 --> 00:01:40,510
decimal places. So if it makes it easier,
24
00:01:40,540 --> 00:01:42,400
it might be easier if I write it like this.
25
00:01:42,790 --> 00:01:47,560
So 2.666666666 and I'm going to round it to two decimal places
26
00:01:47,890 --> 00:01:49,510
and again, I get the same result,
27
00:01:49,600 --> 00:01:54,600
2.67. Now another way of modifying numbers is instead of dividing,
28
00:01:55,990 --> 00:01:59,470
say 8 / 3,
29
00:01:59,980 --> 00:02:02,830
we can also use the floor division,
30
00:02:02,860 --> 00:02:05,980
so where you have two forward lashes instead of just one.
31
00:02:06,550 --> 00:02:10,210
Now we know that whenever we divide any number by any other number,
32
00:02:10,240 --> 00:02:13,780
the result always gets turned into a floating point number.
33
00:02:14,380 --> 00:02:17,230
Now if you didn't want that and you just wanted an integer,
34
00:02:17,380 --> 00:02:20,770
so a whole number chopping off all the numbers after the decimal in place,
35
00:02:21,130 --> 00:02:23,860
you can just use the floor division like this.
36
00:02:24,430 --> 00:02:28,930
And in this case, you would get 2 straight away without having to convert it
37
00:02:28,930 --> 00:02:31,420
into an integer. And in fact,
38
00:02:31,750 --> 00:02:36,750
if I go ahead and check the data type of the result of this calculation,
39
00:02:37,450 --> 00:02:39,940
you'll see that it's actually an integer
40
00:02:40,420 --> 00:02:45,340
whereas if I had just used the single, um, forward slash division,
41
00:02:45,760 --> 00:02:49,570
then I get a floating point number with decimal places.
42
00:02:50,230 --> 00:02:53,860
Even if this is a clean division, say 4 / 2,
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
44
00:03:01,000 --> 00:03:04,630
be represented like this, 2.0, like so.
45
00:03:05,890 --> 00:03:10,890
Now if we had saved the results of this calculation into a variable instead,
46
00:03:12,370 --> 00:03:17,370
then one of the things that you can actually do is to continue performing
47
00:03:17,950 --> 00:03:21,040
calculations on this variable. So for example,
48
00:03:21,220 --> 00:03:23,110
I could do 4 / 2,
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
50
00:03:28,120 --> 00:03:28,953
again,
51
00:03:29,020 --> 00:03:34,020
I could actually say result /= 2. And when I now print results,
52
00:03:38,380 --> 00:03:43,380
I'll actually get 1 because it's 4 / 2 then divided by 2
53
00:03:44,440 --> 00:03:48,490
again. Now very often when you're writing code,
54
00:03:48,730 --> 00:03:52,780
say for example if you're keeping track of the user's score,
55
00:03:53,170 --> 00:03:57,370
so you could have score = 0 to begin with and every single time in
56
00:03:57,370 --> 00:04:00,700
your code say a user scores a point,
57
00:04:01,090 --> 00:04:04,480
then you can get hold of this score variable again
58
00:04:04,750 --> 00:04:09,430
and instead of saying score now equals the previous value of score plus one,
59
00:04:09,790 --> 00:04:12,820
you can simply use this shorthand, +=,
60
00:04:13,240 --> 00:04:17,260
so +=1. And now when we print score,
61
00:04:17,560 --> 00:04:20,140
you'll see that it's actually equal to 1.
62
00:04:21,370 --> 00:04:25,120
So instead of using +=, you can use -=,
63
00:04:25,180 --> 00:04:29,920
which just takes the previous version of score and removes 1 from it.
64
00:04:30,460 --> 00:04:32,980
*= and /=.
65
00:04:33,190 --> 00:04:37,600
So this is really handy when you have to manipulate a value based on its
66
00:04:37,600 --> 00:04:40,360
previous value, which you'll have to do a lot in programming.
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
68
00:04:46,300 --> 00:04:51,300
makes it really easy to mix strings and different data types.
69
00:04:51,910 --> 00:04:53,710
So far, up to this point,
70
00:04:54,190 --> 00:04:59,190
if we wanted to print, uhm, something like your score is,
71
00:05:00,670 --> 00:05:03,760
and then we wanted to print the score we have to write plus,
72
00:05:04,030 --> 00:05:06,310
but of course because these are different data types,
73
00:05:06,310 --> 00:05:09,940
this is a string and this is an integer, we got a type error.
74
00:05:10,510 --> 00:05:15,510
So we've had to convert this into a string before it will actually successfully
75
00:05:16,480 --> 00:05:18,880
print when both the datatypes match.
76
00:05:19,600 --> 00:05:24,600
Now this is quite painful and understandably a lot of programmers will need some
77
00:05:25,270 --> 00:05:29,830
slightly more convenient way of incorporating things that have different data
78
00:05:29,830 --> 00:05:33,880
types. Let's say um, the score is equal to zero. Um,
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.
80
00:05:41,200 --> 00:05:44,800
So here we've got a integer, a float, and a boolean,
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
82
00:05:49,450 --> 00:05:50,140
out.
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
84
00:05:55,390 --> 00:05:59,600
signs and then you have to convert everything into a string,
85
00:06:00,110 --> 00:06:02,120
it's really, really painful, right?
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.
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,
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
89
00:06:20,930 --> 00:06:24,740
double quotes or a single quotes if you want to write your strings like this.
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.
91
00:06:29,600 --> 00:06:34,600
So essentially you're adding just the character f in front of the string,
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
93
00:06:40,970 --> 00:06:43,040
string. So for example, if I wanted to write,
94
00:06:43,340 --> 00:06:47,210
your score is equal to this variable score,
95
00:06:47,630 --> 00:06:52,520
then I can put that variable inside a set of curly braces like this.
96
00:06:53,270 --> 00:06:58,010
And now when I print my string, this one right here,
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
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
99
00:07:07,430 --> 00:07:11,450
this. So if I want to continue along,
100
00:07:11,480 --> 00:07:13,250
I could say your score is this,
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
102
00:07:24,830 --> 00:07:26,750
that final boolean value
103
00:07:28,850 --> 00:07:30,770
and get it to run.
104
00:07:31,130 --> 00:07:34,310
You can see that our entire string now prints out your score is 0,
105
00:07:34,550 --> 00:07:37,460
your height is 1.8 you are winning is True.
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
107
00:07:43,580 --> 00:07:48,580
front of the string and then using these curly braces to place our variables
108
00:07:49,640 --> 00:07:52,940
into this string. By using f strings,
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
110
00:07:59,030 --> 00:07:59,863
a string.
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
112
00:08:04,880 --> 00:08:06,950
I've got a coding challenge for you.
113
00:08:07,670 --> 00:08:10,100
Head over there and complete the challenge.
11045
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.