1 00:00:00,000 --> 00:00:05,000 In some situations, you will need to modify the data type of a variable. 2 00:00:05,001 --> 00:00:08,000 So for example, you want to change an integer to a float, 3 00:00:08,001 --> 00:00:12,000 or an integer to a boolean, a string to a float, etc. 4 00:00:12,001 --> 00:00:15,000 And we have actually a good example here to work on. 5 00:00:15,001 --> 00:00:20,000 The input function, as you saw before, always returns a string. 6 00:00:20,001 --> 00:00:22,000 Okay, even if you give a number. 7 00:00:22,001 --> 00:00:25,000 So then if you need to work with the number you got from the input, 8 00:00:25,001 --> 00:00:30,000 you're going to have a problem because, well, this 9 00:00:30,001 --> 00:00:31,000 number will actually be a string, not a number. 10 00:00:31,001 --> 00:00:34,000 So you need to change the type for the variable, and this is called cast. 11 00:00:34,001 --> 00:00:37,000 Okay, you are going to cast the type. 12 00:00:37,001 --> 00:00:39,000 And to cast a variable, it's very easy. 13 00:00:39,001 --> 00:00:41,000 Okay, so let's see with an example. 14 00:00:41,001 --> 00:00:44,000 Let's say you want to cast a string, okay, two. 15 00:00:44,001 --> 00:00:47,000 So we have the string here, a is a string. 16 00:00:47,001 --> 00:00:52,000 So if I do type a, I have a string. 17 00:00:52,001 --> 00:00:54,000 I want to cast a to an integer. 18 00:00:54,001 --> 00:00:57,000 In this case, I simply do int a, okay. 19 00:00:58,000 --> 00:01:02,000 You simply use the final data type you want, and then in parentheses, 20 00:01:02,001 --> 00:01:06,000 you put the variable or the value you have now. 21 00:01:06,001 --> 00:01:11,000 Okay, if I do int a, you can see now I have 67 without the quotes. 22 00:01:11,001 --> 00:01:18,000 And if I do type int a, now this is of class int. 23 00:01:18,001 --> 00:01:21,411 So this way you can change a value here from 24 00:01:21,423 --> 00:01:25,000 whatever data type it is to the data type int. 25 00:01:26,000 --> 00:01:28,000 You can also do the reverse if you want. 26 00:01:28,001 --> 00:01:30,000 You can cast something into a string. 27 00:01:30,001 --> 00:01:32,000 Let's say I want to cast 34. 28 00:01:32,001 --> 00:01:36,000 This is going to give the string 34. 29 00:01:36,001 --> 00:01:41,000 You can cast int to float, for example, float from three. 30 00:01:41,001 --> 00:01:43,000 It's going to give 3.0. 31 00:01:43,001 --> 00:01:45,000 You can do the opposite. 32 00:01:45,001 --> 00:01:48,000 Int, let's say 3.0, is going to give three. 33 00:01:48,001 --> 00:01:52,000 And let's see, for example, 3.7. 34 00:01:52,001 --> 00:01:54,000 What do we have here? 35 00:01:54,001 --> 00:01:55,001 We have just three, okay. 36 00:01:56,000 --> 00:02:01,000 This is important when you cast a float number to an 37 00:02:01,001 --> 00:02:03,000 integer number, the number is going to be truncated. 38 00:02:03,001 --> 00:02:07,000 Okay, so whatever is after the comma is going to be lost. 39 00:02:07,001 --> 00:02:09,000 Okay, so that's important to know. 40 00:02:09,001 --> 00:02:13,000 And then, so we can also cast a boolean to an integer. 41 00:02:13,001 --> 00:02:14,000 Let's see that. 42 00:02:14,001 --> 00:02:17,000 Int true is going to give one. 43 00:02:17,001 --> 00:02:21,000 And int false is going to give zero. 44 00:02:21,001 --> 00:02:25,000 Okay, so actually, if you want to see a boolean with numbers, 45 00:02:25,001 --> 00:02:28,000 well, when you have zero, it's going to be false. 46 00:02:28,001 --> 00:02:30,000 Okay, if you have one, it's going to be true. 47 00:02:30,001 --> 00:02:35,000 But in fact, if you have any other number, it's going to be true. 48 00:02:35,001 --> 00:02:41,000 If you want to do, let's say, bool from 34, it's going to be true. 49 00:02:41,001 --> 00:02:44,000 bool from minus one is going to be true. 50 00:02:44,001 --> 00:02:48,000 The only value which is going to give false is zero. 51 00:02:48,001 --> 00:02:53,000 All right, and with that, you can cast any type to any other type.