Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
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
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
1
Previously we saw how the Len function gives us a type error when we give it a
2
number instead of a string.
3
So let's talk a little bit more about data types and functions.
4
And the way I think about functions is kind of like some sort of fancy machine
5
that you might see in a factory, right? So in this case,
6
we've got some sort of machine that's going to take potatoes into chips and we
7
don't really care how it does it,
8
but it's probably going to have to peel the potatoes, wash the potatoes,
9
cut it up, fry it,
10
and then finally return it to us as an output in the form of fries.
11
And if this is the first time that you're watching one of my tutorials, um,
12
this is probably a health warning. I talk about food a lot. Um,
13
so be prepared if you haven't eaten.
14
Now let's say that we took the same machine that normally processes potatoes and
15
we decided to get a rock and we just pass this through the function,
16
then we're getting an error, right? This is basically what happened here.
17
This length function doesn't like working with integers.
18
And by forcing this through,
19
we end up with an error and our code breaks and it gives us this thing, a type
20
error.
21
Now do you remember previously we had this challenge where we asked you to get
22
the um, get an input from the user, what is your name?
23
And then we use the length function to get the number of characters of their
24
name. And let's just save that to a variable. Let's call it, um,
25
num_char equals this.
26
And then we print your name has plus num_char, number of characters + characters.
27
So in this case you would expect it to prompt the user to enter their name.
28
Let's say I entered my name Angela,
29
and the length of that string would be 6
30
so this would be equal to six. And so this would get substituted in here.
31
And then with string concatenation, we end up printing
32
"Your name has six characters." But this is not what happens.
33
And I know that some of you might have tried this for the previous challenge and
34
you would have gotten a type error.
35
So it tells you that you can only concatenate strings,
36
not integers. So in this case,
37
we were concatenating a string to an integer.
38
This is what we get. We end up with an error.
39
So how can we prevent these type errors and how can we see the data type that
40
we're working with? Well,
41
we could use a function called type,
42
which basically will check whatever you put between the parentheses and give you
43
the type of data that it is. For example,
44
if I go ahead and just comment out this line of code
45
which breaks and go ahead and put num_char,
46
so this particular piece of data inside this type function,
47
and then I'm going to print the type of num_char.
48
And now if I hit run, let's enter my name.
49
And you can see it's telling you that this is an integer.
50
So adding a string to an integer doesn't make any sense,
51
which is why you got the type error.
52
Now every so often when you're writing code and you're not quite sure what the
53
type of data of something might be,
54
then you can simply just put it inside a type check function,
55
right? Like so. And then you'll get an answer.
56
Now in addition to type checking,
57
we can also do type conversion or you might hear it called type casting where we
58
change a piece of data from one particular data type to another.
59
So it, for example,
60
we know that this variable num_char has the data type of integer.
61
If we wanted to turn it into a string so that this line of code wouldn't break
62
anymore,
63
then we could convert it into a string by writing str
64
and then inside the parentheses, we put in the variable that we want to convert,
65
which is called num_char. And now we save this into a new variable,
66
we'll call it new_num_char.
67
And once we've converted this into a string and stored it under this name,
68
then we can use it inside our print statement, new_num_char,
69
and now it won't break anymore. And it tells us your name has six characters.
70
So in this case we've taken a integer data type,
71
put it inside the parentheses of a str function,
72
which takes a object in between the parentheses and converts it into a string,
73
which we then store inside this new_num_char
74
and we can now use it in our print statement because all the data types of all
75
the pieces of data that we're adding together have the same data type, string.
76
Now you're not limited to only converting numbers into strings.
77
You can convert a whole bunch of different data types.
78
So let me go ahead and comment out this code and I can show you that
79
let's say that we had a variable a, which we set two a number,
80
right? 123.
81
And if I do a type check on a and print this,
82
you'll see that as you would expect, it should give you integer, right?
83
A is an integer.
84
But now let's say I converted a into a string by wrapping it inside the string
85
function, then hit enter. You can see it's now of type string.
86
Now I can do this with other things as well. So for example,
87
I could turn my whole number here into a float and when I run my code,
88
now we see that the type of my variable is a float
89
and that's pretty straightforward, right?
90
But let me ask you two quick questions before we wrap up.
91
What will this line of code print? Alright, you guessed it,
92
this will print 170.5 but what's actually happening behind the scenes is we're
93
converting this string 100.5 into a floating-point number and then we're adding
94
70 to 100.5 and finally we're printing the results.
95
But what about this line? What will print here? Have a guess.
96
Well, this time we get 70100. So in summary,
97
you can use the type function to investigate the data type you're working with
98
and you can use functions like string, int, or float to convert to that data
99
type. In the next lesson, I've got a coding challenge for you.
100
So head over there and we'll get started.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.