1 00:00:12,290 --> 00:00:13,760 Hi, everybody, and welcome. 2 00:00:14,270 --> 00:00:20,210 This is a bonus lecture to the last lectures that we've dealt with variables and that this lecture we 3 00:00:20,210 --> 00:00:21,410 will talk about casting. 4 00:00:21,950 --> 00:00:27,800 So we basically learned what casting is without knowing it in the last lecture. 5 00:00:27,800 --> 00:00:31,420 We have, for example, this command right here. 6 00:00:31,550 --> 00:00:38,270 And we converted this number, which is a float because it has two numbers after that, similar as we 7 00:00:38,270 --> 00:00:39,000 talked in the last. 8 00:00:39,590 --> 00:00:41,620 So we converted it into a string. 9 00:00:41,630 --> 00:00:43,460 So this action is casting. 10 00:00:43,460 --> 00:00:51,940 When you put a variable inside brackets and outside, you type in the you type in the type of the variable. 11 00:00:51,950 --> 00:00:55,140 So basically you cast it because it is one type. 12 00:00:55,190 --> 00:00:56,540 This is the first type. 13 00:00:56,810 --> 00:00:59,480 And then you cast it into a different type, which is a string. 14 00:01:01,070 --> 00:01:02,630 So, all right. 15 00:01:02,640 --> 00:01:05,630 So Python is an object oriented language, as you all know. 16 00:01:05,630 --> 00:01:11,420 And as such, it uses classes to define it, to define data types, including its primitive types, 17 00:01:11,420 --> 00:01:14,690 which are string, integer and variable. 18 00:01:14,960 --> 00:01:20,970 So in this lecture, we'll talk about casting, by the way, the new class Biton file. 19 00:01:22,040 --> 00:01:25,850 Call it casting like variable casting. 20 00:01:27,110 --> 00:01:28,010 And 21 00:01:30,800 --> 00:01:40,610 and first of all, let's just enter into action and say here, casting from integer to string, which 22 00:01:40,610 --> 00:01:49,490 we already know how to do, and let's declare a integer variable. 23 00:01:52,540 --> 00:02:02,680 And that's said that is five and then let's do the following thing, let's do our string variable, 24 00:02:03,970 --> 00:02:05,860 you know what, integer? 25 00:02:07,530 --> 00:02:20,040 In Dejour, variable casted to string and what we want to do here, as Samia's we've seen a few seconds 26 00:02:20,040 --> 00:02:27,470 ago right here, let's put a SDR in brackets and put the variable string variable inside. 27 00:02:28,110 --> 00:02:33,420 So SDR recants and put the integer variable. 28 00:02:37,580 --> 00:02:38,220 Right here. 29 00:02:39,380 --> 00:02:47,750 So basically, if we have the integer variable here and we have the SDR casting action above it, so 30 00:02:47,840 --> 00:02:52,340 this variable should be good, you guess what type is this one? 31 00:02:52,850 --> 00:02:53,780 Let's try it. 32 00:02:53,870 --> 00:02:54,920 Let's just print it. 33 00:02:56,180 --> 00:03:06,530 But if we just copy this variable and place it right here, we are obviously going to see the simple 34 00:03:06,590 --> 00:03:07,250 value of it. 35 00:03:07,280 --> 00:03:08,870 So we're going to see five, right? 36 00:03:09,050 --> 00:03:09,950 So 37 00:03:12,410 --> 00:03:13,130 it's just five. 38 00:03:13,130 --> 00:03:14,350 We can see the type of it. 39 00:03:14,810 --> 00:03:27,200 But if you put the key word type outside of it and cover it with brackets like this, so everything 40 00:03:27,200 --> 00:03:30,470 that I'm marked right here is inside of the type, right. 41 00:03:30,980 --> 00:03:32,130 Sorry, inside the print. 42 00:03:32,720 --> 00:03:37,660 So all of this, we want to print it and now let's see what we want to print. 43 00:03:37,670 --> 00:03:42,530 We have the variable right here, which is an integer type variable with a value of five. 44 00:03:43,670 --> 00:03:47,730 And we have set the keywords a type outside of it. 45 00:03:48,020 --> 00:03:52,880 So basically what we want to print is as we want to print and the type of the variable. 46 00:03:52,880 --> 00:03:53,160 All right. 47 00:03:53,160 --> 00:03:56,300 That special word in Python and let's run it 48 00:03:59,270 --> 00:03:59,590 right. 49 00:03:59,600 --> 00:04:00,020 Amazing. 50 00:04:00,020 --> 00:04:02,380 So we can see the type of the variable here. 51 00:04:02,390 --> 00:04:07,940 So the integer variable cast it to string the casting was successful. 52 00:04:07,940 --> 00:04:10,730 And we can see it right here because we got the string type. 53 00:04:11,510 --> 00:04:11,870 All right. 54 00:04:11,870 --> 00:04:13,670 So let's do another example. 55 00:04:16,280 --> 00:04:19,130 We can do another print here with. 56 00:04:23,670 --> 00:04:31,460 Like, just for us to know, you know what, let's live it like this, so when you won't get confused. 57 00:04:31,500 --> 00:04:31,690 Right. 58 00:04:32,250 --> 00:04:39,900 So that's another point here and with quotation marks and put some stars just to differentiate what 59 00:04:39,900 --> 00:04:41,820 we get in the blogosphere. 60 00:04:41,880 --> 00:04:42,240 All right. 61 00:04:42,600 --> 00:04:45,300 So let's do another print. 62 00:04:45,540 --> 00:04:46,010 All right. 63 00:04:46,260 --> 00:04:52,050 And right here, casting from an integer. 64 00:04:54,420 --> 00:05:03,210 To float, so again, let's, uh, let's we have actually we have this integer variable, so let's just 65 00:05:03,210 --> 00:05:04,740 use it and. 66 00:05:04,740 --> 00:05:06,800 All right, so we're focusing on this one. 67 00:05:06,810 --> 00:05:09,570 This is our base variable that we going to cast now. 68 00:05:10,560 --> 00:05:14,030 So let's do the following thing, same as we did here. 69 00:05:15,090 --> 00:05:19,110 Let's create an integer variable. 70 00:05:21,600 --> 00:05:26,220 All right, let's button on the score here, integer variable casted. 71 00:05:28,040 --> 00:05:42,980 To float and now let's let's dive the key word, float the brackets and let's copied and pasted here. 72 00:05:45,420 --> 00:05:50,130 And now what we want to do is to print the type. 73 00:05:52,530 --> 00:05:55,770 That's the type of this variable, right? 74 00:05:55,800 --> 00:06:01,020 This is the new one we've done the casting right here and here is the new variable. 75 00:06:01,230 --> 00:06:06,330 So let's copy it and place it inside that the type brackets. 76 00:06:09,700 --> 00:06:18,580 And right, click on the file and run, you guys are so casting from integer to flow, so this variable 77 00:06:18,580 --> 00:06:23,370 is a float as we want it and let's do another one. 78 00:06:23,680 --> 00:06:25,300 Just copy the stars. 79 00:06:30,040 --> 00:06:37,060 Place sits right here and now we want to do casting from. 80 00:06:39,480 --> 00:06:45,690 From float to integer. 81 00:06:47,190 --> 00:06:51,090 All right, so we have here. 82 00:06:51,600 --> 00:06:54,870 All right, this one, this guy right here. 83 00:06:56,140 --> 00:07:04,410 We know it's a float, right, we have it's a float, so let's do casting on top of casting. 84 00:07:04,420 --> 00:07:10,870 All right, so let's bring this guy right here, which is a float back to being integer. 85 00:07:11,440 --> 00:07:13,660 So I'll just copy this one. 86 00:07:15,800 --> 00:07:19,040 And create a variable called float. 87 00:07:21,190 --> 00:07:24,190 Very able cast to. 88 00:07:25,310 --> 00:07:38,240 Integer and displays it and then put it outside like that. 89 00:07:39,170 --> 00:07:46,280 And so this one is a casting for the float variable. 90 00:07:46,670 --> 00:07:50,750 Copy this one copied and then again print. 91 00:07:52,890 --> 00:07:57,360 Type records and then based it right here. 92 00:07:59,910 --> 00:08:04,520 So we pasted this one inside the type inside the print run at. 93 00:08:07,340 --> 00:08:08,330 All right, great, guys. 94 00:08:08,350 --> 00:08:12,620 So now we know how to change one variable into another. 95 00:08:12,830 --> 00:08:15,740 So as you can see, Python is a very dynamic language. 96 00:08:15,740 --> 00:08:20,030 So you can get us the flexibility that we want and the code. 97 00:08:21,020 --> 00:08:23,940 And let me show you just one last thing. 98 00:08:24,200 --> 00:08:32,720 Let's try to print this integer variable and then let's try to print this integer variable cast to float. 99 00:08:33,470 --> 00:08:34,920 Let's print their values. 100 00:08:34,940 --> 00:08:35,270 All right. 101 00:08:35,660 --> 00:08:37,760 So I'm just going to copy it. 102 00:08:39,820 --> 00:08:42,010 And then print and then based it. 103 00:08:46,890 --> 00:08:50,940 And I still do the same as here, here. 104 00:08:51,410 --> 00:08:58,650 Brent, let's copy this white sorry, copy this one and based it right here. 105 00:09:00,860 --> 00:09:08,600 And let's run it, so all right, so as you can see, guys here, we have a string variable. 106 00:09:12,440 --> 00:09:13,010 Well. 107 00:09:14,330 --> 00:09:16,120 Well, well, sorry about that. 108 00:09:16,320 --> 00:09:17,320 Oh, yeah, it's OK. 109 00:09:17,740 --> 00:09:21,040 So here we printed the integer variable. 110 00:09:21,070 --> 00:09:22,510 All right, this is the integer. 111 00:09:23,680 --> 00:09:31,460 Yep, so the values five, and then we printed here the integer variable cast to float. 112 00:09:31,660 --> 00:09:33,130 So this one is a float. 113 00:09:33,310 --> 00:09:40,450 So you see that after we cast it it from integer to float, it has a decimal point and we can just verify 114 00:09:40,450 --> 00:09:44,530 that indeed we have a float here, as we can see a decimal point. 115 00:09:46,210 --> 00:09:47,080 That's about it, guys. 116 00:09:47,110 --> 00:09:49,330 So now you know how to do casting. 117 00:09:49,510 --> 00:09:57,850 And this is a great ability and great power for the role for test cases that we going to write, and 118 00:09:57,850 --> 00:09:58,430 that's about it. 119 00:09:58,450 --> 00:10:01,750 So it's not very difficult, but it's a really useful tool. 120 00:10:02,140 --> 00:10:02,520 All right. 121 00:10:02,710 --> 00:10:03,870 See you in the next lecture.