Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,933 --> 00:00:04,966
all right everybody so we are moving on to typecasting
2
00:00:05,366 --> 00:00:08,566
typecasting is the process of converting a variable
3
00:00:08,766 --> 00:00:10,900
from one data type to another
4
00:00:11,466 --> 00:00:13,900
we have various functions to convert a value
5
00:00:13,900 --> 00:00:15,766
or variable to a string
6
00:00:15,866 --> 00:00:18,933
an integer a float or a boolean
7
00:00:19,533 --> 00:00:21,000
let's create some variables
8
00:00:21,666 --> 00:00:23,700
we will create a name variable
9
00:00:23,966 --> 00:00:25,200
type in your full name
10
00:00:26,400 --> 00:00:29,000
an age make up some age
11
00:00:30,066 --> 00:00:34,666
a GPA for grade point average let's say minus 3.2
12
00:00:35,533 --> 00:00:38,000
and a boolean of is student
13
00:00:38,766 --> 00:00:40,400
are we currently a student
14
00:00:40,466 --> 00:00:41,766
let's say that's true
15
00:00:43,133 --> 00:00:45,900
now you actually could get the data type of a variable
16
00:00:45,900 --> 00:00:48,766
or a value by using the type function
17
00:00:49,366 --> 00:00:51,700
then pass in a value or variable
18
00:00:52,466 --> 00:00:55,100
however when I run this there's no output
19
00:00:55,666 --> 00:00:57,466
so I need a print statement
20
00:00:58,266 --> 00:01:01,933
we will print what is returned by B type function
21
00:01:02,533 --> 00:01:05,800
get the type of our name variable then print it
22
00:01:07,166 --> 00:01:10,500
so our name variable is a string str
23
00:01:11,400 --> 00:01:12,733
our age variable
24
00:01:13,766 --> 00:01:15,700
is an integer and INT
25
00:01:17,133 --> 00:01:19,600
GPA is a float
26
00:01:21,966 --> 00:01:24,266
is student is a boolean
27
00:01:24,966 --> 00:01:26,666
using these typecast functions
28
00:01:26,666 --> 00:01:29,266
we can convert from one data type to another
29
00:01:29,466 --> 00:01:30,266
here's how
30
00:01:30,966 --> 00:01:32,600
let's start with something simple
31
00:01:32,733 --> 00:01:35,533
let's convert our GPA to an integer
32
00:01:35,666 --> 00:01:36,866
currently it's afloat
33
00:01:37,333 --> 00:01:39,366
I will reassign GPA
34
00:01:40,266 --> 00:01:43,933
use the INT function to typecast to an integer
35
00:01:44,466 --> 00:01:46,066
then pass in my GPA
36
00:01:46,866 --> 00:01:49,866
at the end we will print R GPA
37
00:01:51,133 --> 00:01:54,533
if we typecast 3.2 to a whole integer
38
00:01:54,666 --> 00:01:56,066
what would the result be
39
00:01:56,500 --> 00:01:58,266
a whole integer of 3
40
00:01:58,566 --> 00:02:00,600
we truncate the decimal portion
41
00:02:01,966 --> 00:02:04,600
let's convert our age to a floating point number
42
00:02:05,066 --> 00:02:07,400
we will reassign our variable of age
43
00:02:08,500 --> 00:02:10,900
use the typecast function of float
44
00:02:11,733 --> 00:02:13,466
then insert our age variable
45
00:02:14,400 --> 00:02:16,466
let's print our age variable
46
00:02:19,200 --> 00:02:22,933
and it should be a floating point number 25 point 0
47
00:02:24,600 --> 00:02:25,966
now we'll cover strings
48
00:02:26,466 --> 00:02:29,100
let's typecast our age to be a string
49
00:02:29,733 --> 00:02:34,200
age equals call the typecast function of string
50
00:02:34,300 --> 00:02:36,800
str passenger age variable
51
00:02:39,200 --> 00:02:42,066
so the result is still going to appear the same
52
00:02:43,500 --> 00:02:47,266
25 however it's a string not an integer
53
00:02:47,900 --> 00:02:49,133
and to prove that
54
00:02:49,300 --> 00:02:53,333
I will enclose my age variable with the type function
55
00:02:54,300 --> 00:02:56,766
the type of variable age is a string
56
00:02:57,766 --> 00:02:59,933
it would be the same as if we are taking this number
57
00:03:00,266 --> 00:03:02,166
and enclosing it within quotes
58
00:03:04,266 --> 00:03:05,966
so this would make a difference
59
00:03:06,066 --> 00:03:08,566
because let's say that I add one to age
60
00:03:09,066 --> 00:03:11,066
age plus equals 1
61
00:03:12,566 --> 00:03:14,100
well we would get a type air
62
00:03:14,566 --> 00:03:18,700
can only concatenate strings not integers to a string
63
00:03:19,800 --> 00:03:22,933
however if I were to add a string of 1 to the end
64
00:03:23,166 --> 00:03:25,300
we would be using string concatenation
65
00:03:25,766 --> 00:03:29,400
so let's say it's my birthday and I add 1 to 25
66
00:03:29,733 --> 00:03:31,866
while since we're working with strings now
67
00:03:32,266 --> 00:03:34,966
the result would be 251
68
00:03:35,133 --> 00:03:37,366
I am 251 years old
69
00:03:38,666 --> 00:03:41,666
so strings and numbers behave differently
70
00:03:41,933 --> 00:03:42,866
with numbers
71
00:03:42,866 --> 00:03:45,466
we can use them within arithmetic expressions
72
00:03:45,700 --> 00:03:47,266
strings not so much
73
00:03:48,166 --> 00:03:49,966
we will take our name variable
74
00:03:49,966 --> 00:03:52,000
and typecast it to a boolean
75
00:03:54,366 --> 00:03:58,366
name equals call the typecast function of Bull
76
00:03:58,866 --> 00:04:00,333
Pass in our name variable
77
00:04:01,366 --> 00:04:02,966
this has an interesting result
78
00:04:03,200 --> 00:04:04,900
so I'm going to print name
79
00:04:05,500 --> 00:04:08,266
bullions are either true or false
80
00:04:08,766 --> 00:04:12,300
if I typecast my string of text into a bullion
81
00:04:13,000 --> 00:04:14,333
that gives me true
82
00:04:15,466 --> 00:04:17,700
now it really doesn't matter what I write here
83
00:04:18,566 --> 00:04:20,933
if I were to change my name to a single character
84
00:04:20,933 --> 00:04:23,566
such as B this would still be true
85
00:04:24,366 --> 00:04:26,066
if our string variable
86
00:04:26,166 --> 00:04:28,666
was empty there were no characters within it
87
00:04:28,866 --> 00:04:30,966
that would actually give us false
88
00:04:31,733 --> 00:04:33,866
we could use this to check to see if somebody
89
00:04:33,866 --> 00:04:34,666
enters in their name
90
00:04:34,666 --> 00:04:37,266
or not if somebody types in their name
91
00:04:37,333 --> 00:04:39,300
then we typecast it to a boolean
92
00:04:39,700 --> 00:04:41,700
if somebody skips entering in their name
93
00:04:42,000 --> 00:04:43,933
that would return false
94
00:04:44,133 --> 00:04:47,266
we could reprompt the user to enter in their name again
95
00:04:47,933 --> 00:04:49,733
art everybody so that is typecasting
96
00:04:49,733 --> 00:04:52,333
it is the process of converting a variable
97
00:04:52,333 --> 00:04:54,366
from one data type to another
98
00:04:55,000 --> 00:04:58,266
this is especially useful with handling user input
99
00:04:58,300 --> 00:05:00,966
because user input is always a string
100
00:05:01,166 --> 00:05:01,733
there may be
101
00:05:01,733 --> 00:05:04,000
at times where you want to convert it to an integer
102
00:05:04,000 --> 00:05:05,800
a float or a boolean
103
00:05:06,000 --> 00:05:09,066
and well everybody that is typecasting in Python
7487
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.