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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
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
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

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.