Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Data Type Conversion.docx59 KB Data Type Conversion - Solution.docx
59 KB
Quick reference
Data Type Conversion
It is possible to convert between different data types.
When to use
Use this anytime you need to change the type of data you're working with.
Instructions
Changing between data types with Python is pretty easy.
To determine the data type you are currently using, use the type function:
print(type(variable_name))
To convert from an int to a float:
float(variable_name)
To convert from a float to an int:
int(variable_name)
To convert from an int to a string:
str(variable_name)
To convert from a list to a tuple:
tuple(list_name)
To convert from a tuple to a list:
list(tuple_name)
Hints & tips
- int(variable_name)
- str(variable_name)
- tuple(list_name)
- list(tuple_name)
- 00:04 Now, we're into the intermediate section of the course, and
- 00:07 we've looked at data types.
- 00:08 Now, I want to spend just a couple of minutes talking about converting
- 00:11 between different data types.
- 00:13 And if you think about it, there's lots of times when you're going to want to convert
- 00:16 between strings and numbers.
- 00:18 You may want to convert between lists and tuples.
- 00:20 It's very hard to convert a dictionary, just because the nature of it.
- 00:23 But those first four, we can freely convert from one to the other, and so
- 00:26 that's what we're going to look at.
- 00:27 So let's create a variable called num, and set it equal to 41.
- 00:31 Now, first of all, there's this neat little thing called type().
- 00:34 And you can pass in the parameter of the variable name,
- 00:38 and this will return what it is, what type it is.
- 00:41 So if we print this, wrap the whole thing in parentheses, and
- 00:46 run it, we can see that num is of the type integer.
- 00:50 If we change this to 41.0, it's now a float, right?
- 00:54 If we change this to John, it's now a string.
- 00:59 And the same will hold true if it's a tuple, or a list, or a dictionary, so
- 01:03 that's useful.
- 01:05 So how do we convert between things?
- 01:08 Well, each item is different in how it converts.
- 01:10 So let's start with numbers, we have an integer here.
- 01:13 Let's say we want to convert this to a float.
- 01:16 That's an easy one, from integer to float, so we just go num = float(num).
- 01:22 So we're calling the float class here, and we're passing in the parameter of num.
- 01:26 So if we save this and run it, our type now becomes float.
- 01:30 And in fact, if we print out num, we'll see that is now listed as 41.0,
- 01:35 because we've changed it from an integer to a float.
- 01:39 So we could do the same thing in reverse.
- 01:41 We can call int instead of float, we want to change this to integer.
- 01:45 So we're starting out with a float, we save it and run it.
- 01:48 It's now the type integer, because we changed it to int, and it's 41, so
- 01:51 that's pretty cool.
- 01:53 Let's take this num, let's say we want to turn it into a string,
- 01:56 bunch of reasons why we might want to do that.
- 01:58 So let's go, num = str(num),
- 02:02 save this and run it, type, string, 41.
- 02:07 So we can change this to a string, And
- 02:13 if we want to change it back to an integer, we just call int(num),
- 02:18 save this, run it, boom, now it's an integer.
- 02:22 And we can confirm that by, say, adding 10 to it, now it's 51.
- 02:28 If we didn't do this line right here, and we just kept it as a string, and
- 02:33 tried to add 10 to it, we'd get a big, fat error, so very cool.
- 02:37 So that's how to convert between numbers, super easy.
- 02:40 We could go, my_list = [1, 2, 3],
- 02:45 and if we print out the type(my_list),
- 02:50 we see that this is in fact a list.
- 02:54 If we wanted to change this into a tuple,
- 02:58 we'd call it, my_tuple = (my_list,).
- 03:03 Since we're just adding one item, we still have to put the comma.
- 03:06 And remember, we talked about that when we talked about tuples.
- 03:08 But now, if we change this to tuple,
- 03:11 and save it and run it, we see now, it's the type of tuple.
- 03:15 So we've taken my list, we've turned it into a tuple.
- 03:18 You might want to do the opposite, you could say, my_tuple = that.
- 03:25 And if we want to then change this into a list,
- 03:29 we just call list, and then pass in my_tuple.
- 03:34 And if we save this and run it, it is the type list.
- 03:38 If we print out my_list, save that and run it, boom.
- 03:45 We see, it is in fact a list, because it has these brackets.
- 03:49 And this is the better way to do it,
- 03:50 this is the way you're kind of supposed to do it.
- 03:52 The first way was sort of a hacky way.
- 03:53 Likewise, we could go, my_list, change this into a list, all right?
- 04:00 And then if we want to change this to tuple, We just call tuple,
- 04:07 call it on my_list, and,
- 04:15 Save, save, save, run, boom, now it's a tuple.
- 04:19 And you could see, the brackets confirm that it's a tuple.
- 04:21 So that is converting between different data types, super useful for
- 04:25 lots of different things.
- 04:26 In the next video, we're going to look at comparison operators.
Lesson notes are only available for subscribers.