WEBVTT

0
00:00.230 --> 00:07.730
In the previous challenge, you saw that when we calculated the BMI and we printed out this large number

1
00:07.730 --> 00:14.660
divided by another number, usually there is a large number of digits after the decimal place.

2
00:14.690 --> 00:21.230
Now, this is not really an ideal way to output some number to the user.

3
00:21.230 --> 00:28.760
We'd really like to be able to round it to the nearest whole number or to a specified number of digits.

4
00:28.760 --> 00:31.160
So how do we do this?

5
00:31.190 --> 00:37.790
Well, you've already seen that we can convert any number into an integer.

6
00:37.910 --> 00:44.510
So instead of simply just printing the original BMI, we can convert it first into an integer,

7
00:44.510 --> 00:46.670
and then we try printing it.

8
00:46.670 --> 00:51.050
You can see what this does is it floors the number.

9
00:51.050 --> 00:57.820
And this is a programming term for basically just removing all of the remaining decimal places, flooring

10
00:57.820 --> 01:00.850
it to the lowest whole number.

11
01:01.030 --> 01:03.160
Now that's not quite what we want.

12
01:03.160 --> 01:10.060
We want to use the round() function which performs the rounding in the traditional mathematical sense,

13
01:10.060 --> 01:16.360
where when it's 0.5, it rounds up to the next whole number, and when it's below that, it rounds up

14
01:16.360 --> 01:19.240
to the lower whole number.

15
01:19.240 --> 01:27.130
So let's go ahead and print using the round() function and round our BMI instead.

16
01:27.130 --> 01:29.410
See what all three of them print.

17
01:29.410 --> 01:33.970
So this is our original value 30.85,

18
01:33.970 --> 01:37.150
and when we simply chop off the end,

19
01:37.150 --> 01:39.850
we have a whole number 30.

20
01:39.850 --> 01:41.700
And this is called flooring.

21
01:41.700 --> 01:49.080
Whereas rounding we'll round up or down one whole number depending on the first decimal place.

22
01:49.080 --> 01:51.510
So in this case it becomes 31.

23
01:52.350 --> 02:04.650
So this depends on, of course, whether it is 3.9 will round into 4, whereas 3.3 will round into 3.

24
02:04.650 --> 02:11.400
So it's a really, really handy function when we're working with numbers in Python.

25
02:11.430 --> 02:16.950
Now you can go even one step further, instead of just rounding the number...

26
02:16.950 --> 02:20.040
notice that it actually takes two inputs.

27
02:20.040 --> 02:27.450
So when I typed out round, and you can see the auto-suggest showing me the function that it thinks I'm

28
02:27.450 --> 02:30.750
trying to get hold of, it takes two inputs.

29
02:30.750 --> 02:37.530
One is the number you want to round, and the second one is the number of digits you want to round it

30
02:37.530 --> 02:38.040
to,

31
02:38.040 --> 02:45.360
which is kind of helpful because what if we want to take our BMI and we wanted to round it to two decimal

32
02:45.360 --> 02:46.140
places?

33
02:46.140 --> 02:52.560
Well, then we would write bmi as the number that will be inserted here, from this variable,

34
02:52.560 --> 02:55.280
and then we just simply type 2.

35
02:55.280 --> 03:00.860
And now when I hit Run, you'll see that this time, instead of simply rounding it to a whole number,

36
03:00.860 --> 03:06.530
it rounds it to a floating point number with two decimal places of accuracy.

37
03:07.160 --> 03:08.780
So that can be really helpful

38
03:08.780 --> 03:15.800
if you're working with money, for example, and you're doing Pythonic calculations, working with currencies

39
03:15.800 --> 03:17.000
or conversions.

40
03:17.000 --> 03:21.020
So Python is just generally really, really number friendly.

41
03:21.410 --> 03:30.020
Now while we're working with numbers in Python, another really handy operator is called the Assignment

42
03:30.020 --> 03:31.160
Operator.

43
03:31.160 --> 03:36.230
So we have various different ones depending on which mathematical operation you want to do.

44
03:36.230 --> 03:43.720
But essentially, what this allows us to do is to accumulate the results of our calculations.

45
03:44.200 --> 03:50.470
Now, very often when you're writing code, say, for example, if you're keeping track of the user's

46
03:50.470 --> 03:51.370
score,

47
03:51.370 --> 03:54.640
so you could have score = 0  to begin with.

48
03:54.640 --> 04:01.960
And every single time in your code, say a user scores a point, then you can get hold of this score

49
04:01.960 --> 04:02.980
variable again,

50
04:02.980 --> 04:09.460
and instead of saying score now equals the previous value of score + 1, you can simply use this

51
04:09.460 --> 04:11.410
shorthand +=.

52
04:11.410 --> 04:13.180
So, +=1.

53
04:13.180 --> 04:18.820
And now when we print score you'll see that it's actually equal to 1.

54
04:19.570 --> 04:26.620
So instead of using += you can use -= which just takes the previous version of score

55
04:26.620 --> 04:28.660
and removes 1 from it,

56
04:28.690 --> 04:31.450
*=, and /=.

57
04:31.450 --> 04:37.360
So this is really handy when you have to manipulate a value based on its previous value, which you'll

58
04:37.360 --> 04:38.980
have to do a lot in programming.

59
04:39.580 --> 04:44.110
Now, the final thing I want to show you is something called f-Strings.

60
04:44.170 --> 04:49.930
And this makes it really easy to mix strings and different data types.

61
04:50.050 --> 04:59.730
So far, up to this point, if we wanted to print something like, "Your score is..." and then we wanted

62
04:59.730 --> 05:02.250
it to print the score, we have to write +,

63
05:02.250 --> 05:07.500
but of course, because these are different data types; this is a string and this is an integer, we

64
05:07.500 --> 05:08.580
get a TypeError.

65
05:08.670 --> 05:13.380
So we've had to convert this into a string before

66
05:13.380 --> 05:17.580
it will actually successfully print, when both the data types match.

67
05:17.730 --> 05:25.050
Now this is quite painful, and understandably, a lot of programmers will need some slightly more convenient

68
05:25.050 --> 05:28.740
way of incorporating things that have different data types.

69
05:28.740 --> 05:32.010
Let's say,  the score  = 0.

70
05:32.130 --> 05:39.390
Let's say their height = 1.8, and is_winning = True.

71
05:39.390 --> 05:46.190
So here we've got an integer, a float, and a boolean, and we want to mix it all in to a sentence that

72
05:46.190 --> 05:48.290
is a string and get it printed out.

73
05:48.290 --> 05:55.730
So instead of having to convert all of these and use a whole bunch of plus signs, and then you have

74
05:55.730 --> 06:00.650
to convert everything into a string, it's really, really painful, right?

75
06:00.650 --> 06:08.240
So what we can do instead is use something in Python known as an f-string.

76
06:08.990 --> 06:17.090
And what an f-string allows us to do is in front of a string like this one, we type the character f,

77
06:17.090 --> 06:21.440
and it's really important that it goes in front of the double quotes or single quotes,

78
06:21.440 --> 06:23.390
if you want to write your strings like this.

79
06:23.390 --> 06:27.770
But I like to use double quotes, and a lot of other Python programmers do too.

80
06:27.800 --> 06:33.450
So essentially you're adding just the character f in front of the string.

81
06:33.450 --> 06:35.580
And now this is an f-string.

82
06:35.580 --> 06:39.870
And you can start adding various values into this string.

83
06:39.870 --> 06:47.070
So for example if I wanted to write your score is equal to this variable score, then I can put that

84
06:47.070 --> 06:51.180
variable inside a set of curly braces { } like this.

85
06:51.300 --> 07:00.160
And now when I print my string, this one right here, you'll see that it says, "Your score = 0."

86
07:00.160 --> 07:04.090
And it does all of the converting and all of the stuff behind the scenes,

87
07:04.090 --> 07:06.370
and you don't have to worry about any of this.

88
07:06.700 --> 07:17.230
So if I want to continue along, I could say, "Your score is {score}, your height is..." add in the "{height}..." and

89
07:17.230 --> 07:22.150
then, "You are winning is..."

90
07:22.270 --> 07:29.350
Then let's add that final boolean value "{is_winning}" and get it to run.

91
07:29.350 --> 07:32.830
You can see that our entire string now prints out, "Your score is = 0,

92
07:32.860 --> 07:34.240
your height is 1.8.

93
07:34.240 --> 07:36.100
You are winning is True."

94
07:36.100 --> 07:42.910
So all of these different data types got combined into a string by using an f in front of the string,

95
07:42.910 --> 07:49.470
and then using these curly braces to place our variables into the string.

96
07:49.950 --> 07:57.450
By using f-strings, you cut down on a lot of the manual labor of inserting different data types into

97
07:57.450 --> 08:04.140
a string, and this is going to come in really handy just about on the next lesson where I've got a

98
08:04.140 --> 08:08.730
coding challenge for you. Head over there and complete the challenge.