1 00:00:00,450 --> 00:00:03,250 Hello and welcome to this video. 2 00:00:03,300 --> 00:00:09,340 In this video I'm going to introduce you to casting in Python. 3 00:00:09,600 --> 00:00:22,050 What is casting casting is the process of converting the value of a variable from one data type to another. 4 00:00:23,280 --> 00:00:32,000 Usually you can do this by using various types of built in functions in Python. 5 00:00:32,300 --> 00:00:43,410 The three common functions that can be used for casting are int which converts a value to an integer 6 00:00:44,040 --> 00:00:44,660 an integer. 7 00:00:44,670 --> 00:00:46,340 Basically a whole number. 8 00:00:46,380 --> 00:00:53,970 You can also use a function called float which converts a value to a floating point number floating 9 00:00:53,970 --> 00:00:56,970 point is a decimal number. 10 00:00:56,970 --> 00:01:08,400 You also have a function called T R which is short for string a converts a value to a string or text. 11 00:01:08,400 --> 00:01:16,560 I'm going to show you some examples of casting and I'm going to be using my Python interactive shell 12 00:01:17,130 --> 00:01:24,810 if you on the windows just open up your command prompt and just typing python and press enter and that 13 00:01:24,810 --> 00:01:28,420 gives you access to the interactive shell. 14 00:01:28,590 --> 00:01:37,830 If you're on a Mac you can type in Python 3 on your terminal and that will give you access to the interactive 15 00:01:37,980 --> 00:01:38,460 show. 16 00:01:38,840 --> 00:01:42,040 So I want to start off by creating some variables. 17 00:01:42,060 --> 00:01:48,480 I'm gonna create a variable and we'll call it X and I'm going to give it a value of 70 which is going 18 00:01:48,480 --> 00:01:50,250 to be a string. 19 00:01:50,250 --> 00:01:52,110 So I'm going to put quotes around it. 20 00:01:52,110 --> 00:01:57,010 Anything that has a quote rounded is treated as a string. 21 00:01:57,120 --> 00:02:02,200 So I press enter to that and I'm going to add another variable called this one. 22 00:02:02,220 --> 00:02:09,670 Why now give it's also a string value so I'll do single quote. 23 00:02:09,690 --> 00:02:13,260 Make this a negative number. 24 00:02:13,350 --> 00:02:24,230 I'm making negative 20 so this at the closing quotes of these two variables are text or strings. 25 00:02:24,240 --> 00:02:27,930 The values are treated as a string. 26 00:02:28,470 --> 00:02:42,820 Now if I use a print statement failed to print and I pass in the value of x plus y and presenter you 27 00:02:42,820 --> 00:02:47,530 can see is giving me 70 minus 20. 28 00:02:47,530 --> 00:02:54,270 That's because it's treating the values as string that is treating them as text. 29 00:02:54,340 --> 00:03:01,830 I can now do some casting here by converting the values into an integer which is a whole number. 30 00:03:02,170 --> 00:03:11,080 So to convert the values into an integer I can do the print statement again and inside the print function. 31 00:03:11,090 --> 00:03:21,280 I'm going to nest the int function and inside that in function I'm going to pass in the variable x and 32 00:03:21,280 --> 00:03:26,550 then I'll add my addition and then I'll do the int function again. 33 00:03:27,310 --> 00:03:32,900 And inside the parentheses for that in function a passing variable Y. 34 00:03:32,920 --> 00:03:42,100 Now if I run this again I should get a proper result you can see now is giving me 50 which is a whole 35 00:03:42,100 --> 00:03:43,690 number on integer. 36 00:03:43,690 --> 00:03:51,070 So I was able to do some casting by converting the value original value which was a text value in to 37 00:03:51,700 --> 00:03:55,800 an int which is an integer or a whole number. 38 00:03:55,860 --> 00:03:56,200 All right. 39 00:03:56,200 --> 00:04:05,520 So that's an example of doing casting and changing a value to an integer or a whole number. 40 00:04:06,430 --> 00:04:10,600 Let me show you an example of converting to a float. 41 00:04:10,610 --> 00:04:17,170 So I'm going to create two other variables will not create a variable C and I'm going to give it a value 42 00:04:17,170 --> 00:04:19,010 of seven. 43 00:04:19,240 --> 00:04:21,940 I'm going to create a variable code D. 44 00:04:22,420 --> 00:04:25,170 I'm going to give it a value of eight. 45 00:04:25,180 --> 00:04:28,330 So these values are integers. 46 00:04:28,330 --> 00:04:37,060 If I want to convert the values into a float a float is a decimal number I can use the function code 47 00:04:37,140 --> 00:04:37,650 float. 48 00:04:37,660 --> 00:04:44,950 So inside my print function I'm going to nest a function called float which will convert the values 49 00:04:45,100 --> 00:04:49,180 into a decimal or a floating point number. 50 00:04:49,210 --> 00:04:56,830 So inside our float function I'm going to passing the first variable which is C All right. 51 00:04:56,880 --> 00:05:06,140 And then I will do an addition and I add the function float again I'm passing the second variable which 52 00:05:06,230 --> 00:05:08,090 is D. 53 00:05:08,210 --> 00:05:14,180 Now if I press enter it should give me fifteen point five. 54 00:05:14,180 --> 00:05:17,990 The point 0 here makes it a floating point number. 55 00:05:17,990 --> 00:05:23,450 So anything with a decimal he's treated as a floating point number. 56 00:05:23,450 --> 00:05:29,930 So that's an example of casting that converts to a floating point number. 57 00:05:30,290 --> 00:05:37,790 I'm going to do one more when I create a couple of variables again a creative variable e I'm going to 58 00:05:37,790 --> 00:05:39,710 give it a value of nine. 59 00:05:40,520 --> 00:05:51,660 I'll press into a crate on another variable f I'm going to give it a value of say eight point five so 60 00:05:51,660 --> 00:05:56,820 this the values are the first value for E. 61 00:05:56,830 --> 00:06:05,510 That's an integer whole number and the value for F is a floating point number to convert the values 62 00:06:05,600 --> 00:06:06,740 into a string. 63 00:06:06,740 --> 00:06:14,300 I need to use a function called S T R so I'm going to type in print which is a print function and inside 64 00:06:14,300 --> 00:06:15,690 the print function. 65 00:06:15,680 --> 00:06:18,740 I'm going to add the SDR function. 66 00:06:18,740 --> 00:06:25,690 So inside the parentheses for the SGI function are passing the first variable e. 67 00:06:26,080 --> 00:06:26,630 Okay. 68 00:06:26,660 --> 00:06:28,290 And then I'll do a comma. 69 00:06:28,560 --> 00:06:40,480 I'll add a text that says and aloud to a comma after that okay. 70 00:06:40,520 --> 00:06:52,970 And then I'll add another SDR function and inside the parentheses are passing variable f Okay and then 71 00:06:52,970 --> 00:06:54,940 I'll do a comma. 72 00:06:54,950 --> 00:07:03,320 Now add another text that will be added to the variable values. 73 00:07:03,320 --> 00:07:05,990 So that is it. 74 00:07:05,990 --> 00:07:15,290 I'm just gonna close that and I'll close the parentheses so I press enter and you can see here it says 75 00:07:15,920 --> 00:07:19,640 nine and a point five are strings. 76 00:07:19,640 --> 00:07:29,890 So I was able to convert the values of the variable E and F in to string by using the PCR function. 77 00:07:30,320 --> 00:07:39,740 So that's basically what casting is where you change the value of a variable from one data type to another. 78 00:07:39,740 --> 00:07:42,000 Thanks for watching and bye for now.