1 00:00:00,000 --> 00:00:01,770 Let's learn about typecasting. 2 00:00:01,780 --> 00:00:05,790 Now, typecasting is basically taking one data type and changing 3 00:00:05,800 --> 00:00:07,400 it into another data type. 4 00:00:08,800 --> 00:00:13,100 So for instance, we could say 20 == 20, and this is 5 00:00:13,100 --> 00:00:14,200 going to return True. 6 00:00:14,280 --> 00:00:20,400 But if we said 20 == 20.0, True or False? Also True 7 00:00:20,410 --> 00:00:21,270 because it's a number. 8 00:00:22,900 --> 00:00:26,400 But what if we said 20 == "20", with the string? 9 00:00:26,450 --> 00:00:29,240 Python is looking at this and saying 20 is not "20". 10 00:00:29,250 --> 00:00:33,200 The reason for that is because this is a number, 11 00:00:33,200 --> 00:00:34,300 this is an integer. 12 00:00:34,360 --> 00:00:38,360 So integers and floats can be compared very easily, because 13 00:00:38,360 --> 00:00:41,200 really, if you just get rid of this, it is the exact same. 14 00:00:41,280 --> 00:00:44,110 But this is comparing an integer with a string. 15 00:00:44,110 --> 00:00:46,800 Those are fundamentally different data types. 16 00:00:48,000 --> 00:00:51,000 So what do we do here? If we ask someone for their age, 17 00:00:51,010 --> 00:00:54,810 so let's say 'age = input( 18 00:00:54,810 --> 00:00:57,200 "What is your age 19 00:00:57,280 --> 00:01:03,040 (enter a number)?', and I'm going to put the number 30 in here. 20 00:01:03,200 --> 00:01:07,000 We're now going to see that if I type in 'type(age)', 21 00:01:07,000 --> 00:01:08,000 this is going to be a string. 22 00:01:08,090 --> 00:01:11,250 I mentioned this quite a while ago that anytime you accept 23 00:01:11,250 --> 00:01:13,500 user input, it always comes in as a string. 24 00:01:13,560 --> 00:01:15,780 Program never knows what it's going to be. 25 00:01:15,790 --> 00:01:18,300 And even though I typed a number, and I suggested that it 26 00:01:18,310 --> 00:01:22,210 should be a number, a program doesn't know that. Programs are 27 00:01:22,210 --> 00:01:25,700 not that smart. And so it always comes back as a string. 28 00:01:26,800 --> 00:01:29,000 Now what if I said 'age'. 29 00:01:29,080 --> 00:01:31,360 Let's just verify. The age is in fact '30'. 30 00:01:31,360 --> 00:01:34,100 You can see it's got apostrophes around it telling us that 31 00:01:34,100 --> 00:01:37,200 it's a string. 'age == 30', 32 00:01:37,600 --> 00:01:41,700 It's False. But actually 'age' is 30. 33 00:01:41,900 --> 00:01:42,900 It's just a string. 34 00:01:43,060 --> 00:01:47,080 So what we can do in this particular situation, is we can 35 00:01:47,090 --> 00:01:49,960 change that to an integer. 36 00:01:49,970 --> 00:01:52,300 At least we can try to do that anyways. 37 00:01:52,460 --> 00:01:58,130 So if we just had 'int(age)', look at that. 38 00:01:58,140 --> 00:02:01,490 It is now 30. It doesn't have apostrophes around it. 39 00:02:01,500 --> 00:02:02,240 So let's go ahead and 40 00:02:02,240 --> 00:02:05,900 overwrite this. 'age = int(age)'. 41 00:02:06,100 --> 00:02:08,199 And then let's do 'type(age)'. 42 00:02:08,280 --> 00:02:10,050 We're going to see this is now an integer. 43 00:02:10,060 --> 00:02:14,610 And if we do, 'age == 30', because we have now typecasted 44 00:02:14,620 --> 00:02:17,850 this from a string, and we've changed it into an integer, and 45 00:02:17,860 --> 00:02:19,560 it's now comparing against an integer, 46 00:02:20,200 --> 00:02:21,200 it's True. 47 00:02:22,720 --> 00:02:25,570 And this is one of the reasons that we want to do typecasting. 48 00:02:25,760 --> 00:02:28,700 But there are several other reasons you might want to do 49 00:02:28,710 --> 00:02:29,780 some typecasting as well. 50 00:02:29,790 --> 00:02:33,020 For instance, let's say we have some sort of tuple and we've 51 00:02:33,030 --> 00:02:36,140 got numbers (1, 2, 3, 5)' 52 00:02:37,100 --> 00:02:39,900 And let's say for whatever reason, that was actually supposed to be a 4, 53 00:02:39,900 --> 00:02:40,900 that was a typo. 54 00:02:40,900 --> 00:02:45,300 Well, if we look at this, it's still a tuple of '(1,2, 3, 5)'. 55 00:02:45,480 --> 00:02:52,410 'tup', If we get the index of 0-1-2-3, we cannot overwrite this. 56 00:02:52,600 --> 00:02:54,000 And let's try this. 57 00:02:54,900 --> 00:02:55,900 Doesn't work. 58 00:02:55,920 --> 00:02:58,730 'tuple' object does not support item assignments. 59 00:02:58,840 --> 00:02:59,950 Can't do it. 60 00:02:59,960 --> 00:03:02,770 But just to confirm that we are actually working with an 61 00:03:02,780 --> 00:03:03,670 index that does work. 62 00:03:03,740 --> 00:03:04,880 Hey, look at that. 63 00:03:04,890 --> 00:03:06,260 It does in fact work. 64 00:03:06,260 --> 00:03:10,000 So let's say we have this tuple, and we wanted to change it. 65 00:03:10,040 --> 00:03:12,860 Well, we have a few different things we could do. 66 00:03:12,870 --> 00:03:14,780 We could just write a new tuple if we wanted to. 67 00:03:14,940 --> 00:03:18,570 We could just say, okay, 'tup' is now equal to 4. We 68 00:03:18,580 --> 00:03:24,530 could do that. Or we could change this to a list, and then 69 00:03:24,540 --> 00:03:26,360 back into a tuple. 70 00:03:26,360 --> 00:03:29,800 So we could do the tuple is going to change into a list. 71 00:03:30,040 --> 00:03:34,030 We could then edit the list, and convert it back to a tuple 72 00:03:34,030 --> 00:03:35,000 if we need to. 73 00:03:35,000 --> 00:03:40,400 So we're going to turn this into a list with the list data 74 00:03:40,500 --> 00:03:43,070 type, and you can see it looks actually just like a function, 75 00:03:43,100 --> 00:03:45,400 and put that tuple in there. 76 00:03:45,460 --> 00:03:49,340 Then we're going to say 'print(lst)', 77 00:03:50,200 --> 00:03:56,800 also 'print(type(lst))', just so that we're super 78 00:03:56,800 --> 00:03:58,800 super certain what we're working with here. 79 00:03:58,800 --> 00:04:02,100 Sure enough, this is now a list, it's a class 'list'. 80 00:04:02,400 --> 00:04:10,000 Cool. So now we can say list 0-1-2-3 is equal to 4. 81 00:04:11,700 --> 00:04:13,800 'lst'. It's now been changed. 82 00:04:13,870 --> 00:04:16,050 And we can overwrite tuple with this list. 83 00:04:16,200 --> 00:04:21,800 And so we can say 'tuple = tuple', and then put that list in there. 84 00:04:23,600 --> 00:04:24,899 Now, let's take a look at this tuple. 85 00:04:25,000 --> 00:04:26,100 It's now fixed. 86 00:04:26,180 --> 00:04:28,640 So all we did was we changed that tuple to a list, 87 00:04:28,650 --> 00:04:32,230 edited the list, and then changed that list back into a tuple. 88 00:04:33,280 --> 00:04:34,900 So that's one use case. 89 00:04:34,900 --> 00:04:38,300 A more general use case would actually be Booleans. 90 00:04:38,360 --> 00:04:43,400 So if you wanted to say, is 'name' in here, 'name' is not in 91 00:04:43,410 --> 00:04:44,420 here. So let's go ahead, 92 00:04:44,420 --> 00:04:48,000 and assign 'name = input( 93 00:04:48,040 --> 00:04:49,540 "What is your name?")' 94 00:04:49,550 --> 00:04:51,310 It's going to ask me for my name. 95 00:04:51,310 --> 00:04:52,300 My name is going to be 'Kalob'. 96 00:04:52,300 --> 00:04:54,900 [no audio] 97 00:04:54,900 --> 00:04:56,500 Yep, there it is, 'Kalob'. 98 00:04:56,540 --> 00:05:00,830 And what I can do here, is I can say 'if name' has something 99 00:05:00,840 --> 00:05:04,190 in it, but for some people, that can be a little bit confusing. 100 00:05:04,200 --> 00:05:06,500 'If name', well, yeah, there's always a name. 101 00:05:06,560 --> 00:05:09,500 If you're just looking at this, name is definitely assigned 102 00:05:09,510 --> 00:05:12,630 to something, which is not necessarily the case here, but 103 00:05:12,630 --> 00:05:14,600 it does look like it's always assigned to something. 104 00:05:14,600 --> 00:05:19,800 So what we can say is 'has_name = bool()', 105 00:05:19,800 --> 00:05:25,200 Put a 'name' in there and let's do 'type(has_name)'. 106 00:05:25,900 --> 00:05:26,900 It's now a Boolean. 107 00:05:27,200 --> 00:05:28,900 'has_name', is that True or False? 108 00:05:28,900 --> 00:05:30,900 [no audio] 109 00:05:30,900 --> 00:05:37,000 It's True. So now we can say 'if has_name', instead of 'name', 'print("The 110 00:05:37,080 --> 00:05:40,430 name is, name")', just like that. 111 00:05:41,800 --> 00:05:46,200 Now, one thing that I really like to do, is I like to turn 112 00:05:46,290 --> 00:05:50,860 strings into int, whenever I can, because I like my data types 113 00:05:50,860 --> 00:05:52,180 always being pristine. 114 00:05:52,380 --> 00:05:57,960 So 30, for instance, from earlier should not be a string, 115 00:05:58,300 --> 00:05:59,800 it should be an integer. 116 00:06:01,300 --> 00:06:05,000 And one of my favorite things to do, is to take a list and 117 00:06:05,000 --> 00:06:06,600 get rid of all the duplicates. 118 00:06:06,600 --> 00:06:10,300 So let's create a list of 'groceries'. 119 00:06:10,900 --> 00:06:14,200 So say we've got '['Milk', 'Bread', 'Eggs']', 120 00:06:14,200 --> 00:06:16,100 and let's say I wasn't paying attention, 121 00:06:16,150 --> 00:06:19,310 and I wrote 'Milk' the second time, and I wrote 'Eggs' the second 122 00:06:19,310 --> 00:06:24,400 time, and 'Oranges', and 'Chocolate'. 123 00:06:26,100 --> 00:06:27,200 I've got two duplicates in here. 124 00:06:27,280 --> 00:06:29,430 I've got 'Eggs' in here twice, and I've got 'Milk' in here twice. 125 00:06:29,480 --> 00:06:31,880 So let's go ahead, set this. 126 00:06:31,890 --> 00:06:35,660 We can see that 'groceries' is going to be exactly as we set 127 00:06:35,660 --> 00:06:39,600 it. But what we can do, is we can actually say this is a list, 128 00:06:39,600 --> 00:06:43,500 but let's just go ahead, turn this into a set. Run that. 129 00:06:43,510 --> 00:06:45,320 Run that. And look at that. 130 00:06:45,320 --> 00:06:47,700 It got rid of all the duplicates automatically 131 00:06:47,700 --> 00:06:50,300 for me. That is powerful. That is really nice. 132 00:06:50,300 --> 00:06:51,600 I don't have to loop through it, 133 00:06:51,600 --> 00:06:56,100 I don't have to create another list from a loop or anything like that, 134 00:06:56,130 --> 00:07:00,600 I can simply say this is now a set, and I want to make sure 135 00:07:00,610 --> 00:07:02,910 that everything in here is unique. 136 00:07:02,920 --> 00:07:05,620 Now, remember, sets don't hold their order, 137 00:07:05,630 --> 00:07:07,510 so there is a pro and a con here. 138 00:07:07,700 --> 00:07:11,600 But if you want to change it back into a list, you can absolutely do that too. 139 00:07:11,620 --> 00:07:14,530 Now, this is pretty crazy because we've got a list in here, 140 00:07:15,900 --> 00:07:18,500 we're casting it to be a set, so it's going to be completely 141 00:07:18,540 --> 00:07:21,690 unique. and then we're turning it back into a list so that 142 00:07:21,700 --> 00:07:23,500 it holds whatever order it has. 143 00:07:23,500 --> 00:07:25,400 So let's go ahead, run that, 144 00:07:25,400 --> 00:07:29,200 and it did not keep its order, but from now on, it will. 145 00:07:29,200 --> 00:07:31,500 So whatever this order ends up being, it will now be that 146 00:07:31,500 --> 00:07:34,400 order. We can always do 'groceries.', 147 00:07:35,900 --> 00:07:37,700 and where are you? 148 00:07:37,700 --> 00:07:39,500 We can do a little bit of sorting here. 149 00:07:39,500 --> 00:07:42,800 [no audio] 150 00:07:42,800 --> 00:07:47,100 'groceries', and there we go, sorted in alphabetical order, and that's 151 00:07:47,100 --> 00:07:48,400 more typecasting in here. 152 00:07:48,420 --> 00:07:50,640 So again, list to a set to a list, 153 00:07:50,640 --> 00:07:53,400 just to make sure that our initial list is totally unique. 154 00:07:53,480 --> 00:07:56,420 We've set it back to a list so that we have the '.sort' 155 00:07:56,450 --> 00:07:59,400 method available, and then we ran it so that our list is 156 00:07:59,410 --> 00:08:00,420 in alphabetical order. 157 00:08:00,420 --> 00:08:04,400 That is actually a very good use case for typecasting. 158 00:08:05,400 --> 00:08:08,200 Now, what I would like you to do, is I want you to experiment 159 00:08:08,250 --> 00:08:09,830 on your own just a little bit here. 160 00:08:09,840 --> 00:08:13,450 So I want you to create some sort of dictionary, and it's 161 00:08:13,460 --> 00:08:15,670 going to have to have keys and values in here. 162 00:08:15,800 --> 00:08:22,510 'key': 'value'; and put a second 'key' and a second 'value' in there, 163 00:08:22,680 --> 00:08:24,330 maybe call it 'person'. 164 00:08:24,340 --> 00:08:27,030 Anyways, you need some sort of dictionary, and then I want 165 00:08:27,030 --> 00:08:30,800 you to cast that dictionary as a list, cast it as a tuple. 166 00:08:30,800 --> 00:08:34,400 You can actually even go as far as to say something like 167 00:08:34,419 --> 00:08:41,400 'list(dictionary.items)', because if you remember, let's 168 00:08:41,400 --> 00:08:47,000 actually execute this. 'dictionary.items()'. 169 00:08:47,600 --> 00:08:51,200 This gives us a weird sort of data type called 'dict_items'. 170 00:08:52,799 --> 00:08:55,700 But what happens if you were to turn this into a list? 171 00:08:57,100 --> 00:08:59,900 I mean, it's already got a list on the inside, but look at that. 172 00:09:00,000 --> 00:09:02,000 It is now just a list. 173 00:09:02,500 --> 00:09:05,800 Anyways, try that with a list, 174 00:09:05,810 --> 00:09:06,800 try it with a tuple, 175 00:09:06,800 --> 00:09:08,000 see what you can do. 176 00:09:08,000 --> 00:09:10,900 What happens when you cast a dictionary as a string? 177 00:09:10,960 --> 00:09:12,190 That's an interesting one. 178 00:09:12,200 --> 00:09:13,400 Try that. See what happens. 179 00:09:13,460 --> 00:09:17,720 See if you get an error. And one last thing, errors are okay. 180 00:09:17,730 --> 00:09:20,330 It's a good way to learn what you can and cannot do in a 181 00:09:20,330 --> 00:09:21,300 programming language. 182 00:09:21,340 --> 00:09:23,740 There's nothing you're going to break with casting anyways. 183 00:09:23,750 --> 00:09:25,380 So give that a shot. 184 00:09:25,390 --> 00:09:28,470 Cast this as a string, a list, and a tuple, 185 00:09:28,600 --> 00:09:32,100 and when you've done that, let's head on over to the next lesson.