Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,880 --> 00:00:06,225
Welcome back. Sometimes it's necessary or convenient to
2
00:00:06,225 --> 00:00:10,095
be able to convert values from one type into another in Python,
3
00:00:10,095 --> 00:00:13,530
and for that, Python has type conversion functions.
4
00:00:13,530 --> 00:00:18,405
So, the type conversion functions that we'll learn right now will be int,
5
00:00:18,405 --> 00:00:20,370
to convert to an integer,
6
00:00:20,370 --> 00:00:28,725
float to convert to a floating point number and string str to convert to a string.
7
00:00:28,725 --> 00:00:31,530
Each of these type conversion functions,
8
00:00:31,530 --> 00:00:33,960
expects an argument and usually,
9
00:00:33,960 --> 00:00:36,960
we want to pass in an arguments that's another type.
10
00:00:36,960 --> 00:00:40,900
So, for example, if we're calling int to convert to an integer,
11
00:00:40,900 --> 00:00:44,545
then we'll usually pass in an argument that's something else,
12
00:00:44,545 --> 00:00:46,475
either a float or a string.
13
00:00:46,475 --> 00:00:48,530
So, let's look at that in code.
14
00:00:48,530 --> 00:00:53,060
So, here we print out the value 3.14 and then we
15
00:00:53,060 --> 00:00:57,530
print out the value of int when called with 3.14 as
16
00:00:57,530 --> 00:01:02,600
an input and the value of this overall expression is going to
17
00:01:02,600 --> 00:01:08,000
be 3.14 a float cast or converted into an integer.
18
00:01:08,000 --> 00:01:11,950
So, the value of that expression is going to be three.
19
00:01:11,950 --> 00:01:15,200
Now, when Python converts a float to a string,
20
00:01:15,200 --> 00:01:18,500
it always just does that by discarding or kind of
21
00:01:18,500 --> 00:01:21,890
cutting off the decimal point and everything after it.
22
00:01:21,890 --> 00:01:24,535
So, 3.14 becomes three.
23
00:01:24,535 --> 00:01:31,635
What that also means is that 3.99999 also becomes three.
24
00:01:31,635 --> 00:01:36,560
So, that's how Python chooses to cast a float to an integer.
25
00:01:36,560 --> 00:01:41,715
So, this will make sense sometimes if we do 3.0,
26
00:01:41,715 --> 00:01:45,800
but other times, it might end up being a little bit strange.
27
00:01:45,800 --> 00:01:54,090
So, casting negative 3.9999 from a float to an integer ends up being negative three.
28
00:01:55,850 --> 00:01:59,210
So, in addition to converting floats to integers,
29
00:01:59,210 --> 00:02:04,205
we can also convert strings to integers which actually comes in handy fairly often.
30
00:02:04,205 --> 00:02:07,675
So, here we have a string 2345,
31
00:02:07,675 --> 00:02:14,165
and let's say that we wanted to divide that string into the integer 2,345,
32
00:02:14,165 --> 00:02:20,305
we'd do that by calling the int function and using the string 2345 as input.
33
00:02:20,305 --> 00:02:23,085
Another note, if we take in something that's
34
00:02:23,085 --> 00:02:26,310
already an integer and call the int function on it,
35
00:02:26,310 --> 00:02:28,850
then we're going to get that same value back.
36
00:02:28,850 --> 00:02:31,760
So, if we call int 17,
37
00:02:31,760 --> 00:02:35,095
then we just get the integer 17 back.
38
00:02:35,095 --> 00:02:38,535
Now, we can call int on any string.
39
00:02:38,535 --> 00:02:41,590
So, for example, if we call int on this string,
40
00:02:41,590 --> 00:02:46,325
but Python isn't necessarily going to be able to convert every string into an integer.
41
00:02:46,325 --> 00:02:47,690
So, for example, here,
42
00:02:47,690 --> 00:02:49,835
this string starts out with 23,
43
00:02:49,835 --> 00:02:51,800
but it also contains bottles.
44
00:02:51,800 --> 00:02:55,820
So, when Python sees that we're trying to convert this string,
45
00:02:55,820 --> 00:02:58,775
which isn't quite an integer into an integer,
46
00:02:58,775 --> 00:03:00,950
then Python gives us int here.
47
00:03:00,950 --> 00:03:03,875
So, here we're told that on line eight,
48
00:03:03,875 --> 00:03:06,260
there's an invalid literal for int.
49
00:03:06,260 --> 00:03:12,195
So, it's telling us that this string can be converted into an integer.
50
00:03:12,195 --> 00:03:14,600
Now, the float function works just like
51
00:03:14,600 --> 00:03:18,260
the int function except rather than converting to an integer,
52
00:03:18,260 --> 00:03:20,060
it converts it to a float.
53
00:03:20,060 --> 00:03:26,290
So, if I call float on the string 123 period 45,
54
00:03:26,290 --> 00:03:29,030
then that converts it into a float
55
00:03:29,030 --> 00:03:37,610
123.45 and if we print out the type of this expression float 12345,
56
00:03:37,610 --> 00:03:39,920
then we're going to get its type float.
57
00:03:39,920 --> 00:03:45,005
So, you can see that the value of this expression was the float
58
00:03:45,005 --> 00:03:49,160
123.45 and the value of this expression which
59
00:03:49,160 --> 00:03:53,825
computed the type of this expression, is a float.
60
00:03:53,825 --> 00:03:55,955
Now, in addition to int in float,
61
00:03:55,955 --> 00:03:58,285
we can also convert int a string.
62
00:03:58,285 --> 00:04:07,895
So, for example, we might print out what's the value of the string 17 of string 123.45,
63
00:04:07,895 --> 00:04:11,464
and then if we print out what's the type of this expression,
64
00:04:11,464 --> 00:04:13,725
then we're going to get that this is a string.
65
00:04:13,725 --> 00:04:16,910
So, let's run our code to be sure that this is the case.
66
00:04:16,910 --> 00:04:20,990
So, we can see that when we print out strings 17,
67
00:04:20,990 --> 00:04:23,075
it prints out the string 17,
68
00:04:23,075 --> 00:04:25,580
it just so happens that the output actually matches
69
00:04:25,580 --> 00:04:28,350
up with if we printed out the integer,
70
00:04:28,350 --> 00:04:30,445
but this is a string,
71
00:04:30,445 --> 00:04:36,150
this is also a string because we converted it again using the str method,
72
00:04:36,250 --> 00:04:41,925
and then the type of this expression is a string.
73
00:04:41,925 --> 00:04:45,950
So, that's telling us that when we call str on this number,
74
00:04:45,950 --> 00:04:47,515
then we get a string back.
75
00:04:47,515 --> 00:04:50,570
So, one reason that we might want to use casting,
76
00:04:50,570 --> 00:04:53,600
is when we want to concatenate or combined values.
77
00:04:53,600 --> 00:04:56,840
So, let's suppose that we have an integer val,
78
00:04:56,840 --> 00:05:00,655
which is set to 55 and we want to print out the value is val,
79
00:05:00,655 --> 00:05:02,850
we get an error saying TypeError,
80
00:05:02,850 --> 00:05:06,015
that must be a string not an int, not an integer.
81
00:05:06,015 --> 00:05:09,115
So, we have to add a conversion to convert
82
00:05:09,115 --> 00:05:13,565
val from an integer to a string by calling the str function.
83
00:05:13,565 --> 00:05:15,740
So, we print out the value is,
84
00:05:15,740 --> 00:05:17,600
and then if this is an integer,
85
00:05:17,600 --> 00:05:21,290
we need to add the str to convert it to a string.
86
00:05:21,290 --> 00:05:24,950
What value is printed when the following statement executes?
87
00:05:24,950 --> 00:05:32,480
So, again, when we call the int function to convert or cast this float into an integer,
88
00:05:32,480 --> 00:05:37,160
Python does that by just cutting off everything that comes after the decimal place,
89
00:05:37,160 --> 00:05:40,670
and so even though this number rounds up to 54,
90
00:05:40,670 --> 00:05:43,580
we're going to instead get 53 as our answer.
91
00:05:43,580 --> 00:05:45,455
So, the answer here is B.
92
00:05:45,455 --> 00:05:48,320
That's all for now, until next time.7713
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.