All language subtitles for 10_type-conversion-functions.en

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

Welcome back. Sometimes it's necessary or convenient to

be able to convert values from one type into another in Python,

and for that, Python has type conversion functions.

So, the type conversion functions that we'll learn right now will be int,

to convert to an integer,

float to convert to a floating point number and string str to convert to a string.

Each of these type conversion functions,

expects an argument and usually,

we want to pass in an arguments that's another type.

So, for example, if we're calling int to convert to an integer,

then we'll usually pass in an argument that's something else,

either a float or a string.

So, let's look at that in code.

So, here we print out the value 3.14 and then we

print out the value of int when called with 3.14 as

an input and the value of this overall expression is going to

be 3.14 a float cast or converted into an integer.

So, the value of that expression is going to be three.

Now, when Python converts a float to a string,

it always just does that by discarding or kind of

cutting off the decimal point and everything after it.

So, 3.14 becomes three.

What that also means is that 3.99999 also becomes three.

So, that's how Python chooses to cast a float to an integer.

So, this will make sense sometimes if we do 3.0,

but other times, it might end up being a little bit strange.

So, casting negative 3.9999 from a float to an integer ends up being negative three.

So, in addition to converting floats to integers,

we can also convert strings to integers which actually comes in handy fairly often.

So, here we have a string 2345,

and let's say that we wanted to divide that string into the integer 2,345,

we'd do that by calling the int function and using the string 2345 as input.

Another note, if we take in something that's

already an integer and call the int function on it,

then we're going to get that same value back.

So, if we call int 17,

then we just get the integer 17 back.

Now, we can call int on any string.

So, for example, if we call int on this string,

but Python isn't necessarily going to be able to convert every string into an integer.

So, for example, here,

this string starts out with 23,

but it also contains bottles.

So, when Python sees that we're trying to convert this string,

which isn't quite an integer into an integer,

then Python gives us int here.

So, here we're told that on line eight,

there's an invalid literal for int.

So, it's telling us that this string can be converted into an integer.

Now, the float function works just like

the int function except rather than converting to an integer,

it converts it to a float.

So, if I call float on the string 123 period 45,

then that converts it into a float

123.45 and if we print out the type of this expression float 12345,

then we're going to get its type float.

So, you can see that the value of this expression was the float

123.45 and the value of this expression which

computed the type of this expression, is a float.

Now, in addition to int in float,

we can also convert int a string.

So, for example, we might print out what's the value of the string 17 of string 123.45,

and then if we print out what's the type of this expression,

then we're going to get that this is a string.

So, let's run our code to be sure that this is the case.

So, we can see that when we print out strings 17,

it prints out the string 17,

it just so happens that the output actually matches

up with if we printed out the integer,

but this is a string,

this is also a string because we converted it again using the str method,

and then the type of this expression is a string.

So, that's telling us that when we call str on this number,

then we get a string back.

So, one reason that we might want to use casting,

is when we want to concatenate or combined values.

So, let's suppose that we have an integer val,

which is set to 55 and we want to print out the value is val,

we get an error saying TypeError,

that must be a string not an int, not an integer.

So, we have to add a conversion to convert

val from an integer to a string by calling the str function.

So, we print out the value is,

and then if this is an integer,

we need to add the str to convert it to a string.

What value is printed when the following statement executes?

So, again, when we call the int function to convert or cast this float into an integer,

Python does that by just cutting off everything that comes after the decimal place,

and so even though this number rounds up to 54,

we're going to instead get 53 as our answer.

So, the answer here is B.

That's all for now, until next time.

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