Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.