1 00:00:00,000 --> 00:00:01,860 Okay, let's talk about Booleans. 2 00:00:01,870 --> 00:00:04,860 Booleans, in my opinion, are the easiest data type. 3 00:00:05,040 --> 00:00:08,189 They're easier than strings, which are really just sentences. 4 00:00:08,400 --> 00:00:11,700 They're easier than lists or even tuples, which you can't 5 00:00:11,700 --> 00:00:13,100 really do a whole lot with. 6 00:00:13,320 --> 00:00:18,250 A Boolean is either a True or False answer, and it actually just 7 00:00:18,260 --> 00:00:21,160 has to be the word True or the word False. 8 00:00:21,190 --> 00:00:24,460 And in Python it actually has to be a capital T and a capital 9 00:00:24,470 --> 00:00:25,420 F. That's important. 10 00:00:25,600 --> 00:00:29,800 So let's say I've got a variable called 'is_30', because that's 11 00:00:29,800 --> 00:00:30,900 my age right now. I'm 30. 12 00:00:30,950 --> 00:00:33,460 I can set that to True with the capital T. 13 00:00:34,930 --> 00:00:38,140 Then I can say 'is_30', comes back as True. 14 00:00:38,470 --> 00:00:43,600 The 'type(is_30)' is a bool, it's a Boolean. 15 00:00:43,630 --> 00:00:45,820 So bool is short for boolean. 16 00:00:46,060 --> 00:00:47,740 We can also try another one. 17 00:00:47,750 --> 00:00:49,750 Let's say are you in space? 18 00:00:49,750 --> 00:00:51,800 So 'is_in_space'. 19 00:00:52,080 --> 00:00:53,670 I am currently not in space. 20 00:00:53,680 --> 00:00:55,320 I am definitely on Earth right now. 21 00:00:55,330 --> 00:01:02,570 And what I can do is print the 'type(is_in_space)', and that's 22 00:01:02,580 --> 00:01:03,890 going to come back as a Boolean. 23 00:01:04,220 --> 00:01:07,680 And as a second print parameter I can just put in here what 24 00:01:07,690 --> 00:01:11,650 it actually is, 'is_in_space', and this will come back as False. 25 00:01:12,640 --> 00:01:15,520 There it is, class bool and False. 26 00:01:15,760 --> 00:01:17,860 Now let's take a look at this as an object. 27 00:01:17,860 --> 00:01:21,100 We talked a while ago that anytime you see class, this is an object 28 00:01:21,140 --> 00:01:27,240 So let's use this 'is_in_space.', and hit tab. 29 00:01:27,780 --> 00:01:32,510 And then you can see really not a whole lot of options in 30 00:01:32,520 --> 00:01:35,630 here. We can get the 'real' number, we can get a 'numerator', 31 00:01:35,640 --> 00:01:37,370 we can convert it to byte, 32 00:01:37,380 --> 00:01:39,200 we can get the bit_length if we wanted to. 33 00:01:39,580 --> 00:01:43,120 But generally speaking, this is not super useful. 34 00:01:43,130 --> 00:01:44,710 This is actually supposed to be a function. So 35 00:01:44,720 --> 00:01:47,520 anytime you see this, it says 'function', and then whatever 36 00:01:47,530 --> 00:01:50,700 that is supposed to be, just put parentheses at the end. 37 00:01:50,840 --> 00:01:53,450 The 'bit_length' of this is 0. 38 00:01:53,840 --> 00:01:58,830 And let's do 'is_30', because this is going to be an interesting thing. 39 00:01:58,840 --> 00:02:04,170 'is_30.bit_length' is 1. 40 00:02:04,700 --> 00:02:07,800 And this is where the idea of ones and zeros come up in 41 00:02:07,800 --> 00:02:09,100 computer and computer science. 42 00:02:09,300 --> 00:02:12,570 So anything that's True is equal to 1 and anything that's 43 00:02:12,580 --> 00:02:14,070 False is equal to 0. 44 00:02:14,190 --> 00:02:16,770 That's where we get ones and zeros from, is the 'bit_length'. 45 00:02:16,940 --> 00:02:20,660 So False has no length at all, and True actually has one. 46 00:02:20,670 --> 00:02:21,920 So you got ones and zeros. 47 00:02:22,080 --> 00:02:24,990 Yes or no, true or false, black and white. 48 00:02:25,140 --> 00:02:27,510 That's really all a Boolean is. 49 00:02:27,540 --> 00:02:30,120 Now let's take a look at some typecasting. 50 00:02:30,130 --> 00:02:34,250 I want to get this idea out there a little earlier than just 51 00:02:34,260 --> 00:02:35,600 waiting for that video to show up. 52 00:02:35,610 --> 00:02:37,520 So we can actually do some typecasting here. 53 00:02:37,520 --> 00:02:41,700 We can say of 'course = "Python for Everybody". 54 00:02:41,700 --> 00:02:45,700 Now we know that 'course' is a string, and if we do this 'type 55 00:02:46,100 --> 00:02:51,530 (course)', it says it's a str, but we can typecast things 56 00:02:51,800 --> 00:02:55,900 as a Boolean with the 'bool()' function, 'bool()'. 57 00:02:55,960 --> 00:02:57,790 Let's throw this in here. 58 00:02:57,800 --> 00:03:00,890 'course'. 'course = True'. 59 00:03:00,920 --> 00:03:02,600 What if we have an empty string, though? 60 00:03:02,800 --> 00:03:07,480 So let's say we ask someone for their 'name', and that 'name' 61 00:03:07,540 --> 00:03:09,220 came back as empty. 62 00:03:09,230 --> 00:03:11,350 Let's say they just answered with absolutely nothing 63 00:03:11,360 --> 00:03:16,130 for some reason. We can do 'bool(name)', and it's False. 64 00:03:16,140 --> 00:03:20,030 So in Python, and this is good to know right now, in Python, 65 00:03:20,040 --> 00:03:21,980 when you have an empty string, it's False. 66 00:03:22,360 --> 00:03:25,150 When you have a string with anything in it, it is True. 67 00:03:25,440 --> 00:03:26,880 You can even do this. 68 00:03:26,890 --> 00:03:28,770 Just throw in an empty list, '[]' if you want to. 69 00:03:28,900 --> 00:03:33,580 An empty list is False, but a list with even just one item 70 00:03:33,590 --> 00:03:34,920 in it is True. 71 00:03:34,920 --> 00:03:37,290 You can do the same thing with a tuple. 72 00:03:37,420 --> 00:03:40,750 So let's just make sure that this understands it's a tuple. 73 00:03:40,750 --> 00:03:45,300 That's why I put that extra comma in there, and I had one 74 00:03:45,300 --> 00:03:46,700 too many things. There we go. 75 00:03:46,700 --> 00:03:48,440 That one's True. 76 00:03:48,650 --> 00:03:52,870 But if we have an empty tuple without a comma because there's 77 00:03:52,880 --> 00:03:54,840 no items in there, it's False. 78 00:03:55,190 --> 00:03:57,990 Again, we can do the same thing with a dictionary. 79 00:03:58,200 --> 00:04:02,400 If there is nothing in a dictionary, you could even consider this a set. 80 00:04:03,200 --> 00:04:04,200 It's False. 81 00:04:04,310 --> 00:04:08,380 But as soon as you put something in there, anything, it's 82 00:04:08,390 --> 00:04:12,760 True. So behind the scenes, Python is able to evaluate any 83 00:04:12,790 --> 00:04:15,100 data type as either True or False, 84 00:04:15,110 --> 00:04:16,930 it can turn it into a Boolean. 85 00:04:17,220 --> 00:04:20,550 Now, this is really important, because down the road, we're 86 00:04:20,560 --> 00:04:22,920 going to be learning about comparison operators. 87 00:04:22,930 --> 00:04:25,860 And a program is always looking for something to be True. 88 00:04:26,079 --> 00:04:27,850 If it's True, take an action. 89 00:04:27,860 --> 00:04:29,620 If it's False, take a different action. 90 00:04:29,720 --> 00:04:32,630 But for the time being, all you need to know is that Booleans 91 00:04:32,640 --> 00:04:34,970 really are True and False. 92 00:04:34,970 --> 00:04:35,900 And that's it. 93 00:04:36,120 --> 00:04:37,740 That's all there is to a Boolean. 94 00:04:37,740 --> 00:04:39,500 Nice and simple.