1 00:00:07,820 --> 00:00:12,750 Welcome back. It's time for us to learn about a new Python type. 2 00:00:12,750 --> 00:00:14,940 So far, we've seen integers, 3 00:00:14,940 --> 00:00:17,370 which are good for storing round numbers, floats, 4 00:00:17,370 --> 00:00:20,040 which are good for storing decimal numbers, strings, 5 00:00:20,040 --> 00:00:23,400 which are good for storing sequences of characters like words, 6 00:00:23,400 --> 00:00:24,660 in lists and tuples, 7 00:00:24,660 --> 00:00:27,510 which are good for storing sequences of other items. 8 00:00:27,510 --> 00:00:32,205 In this lesson, we're going to learn about a new Python type called Booleans. 9 00:00:32,205 --> 00:00:35,010 Booleans are good for storing truth values and that 10 00:00:35,010 --> 00:00:37,900 is whether something is true or false. 11 00:00:37,900 --> 00:00:41,915 In fact, there are only two possible values for Booleans, true and false. 12 00:00:41,915 --> 00:00:44,780 This is in contrast with pretty much every other type. 13 00:00:44,780 --> 00:00:47,830 So, in integer might be negative 10, 14 00:00:47,830 --> 00:00:51,260 five or one of any infinite number of values, 15 00:00:51,260 --> 00:00:54,560 but Booleans are always either true or false. 16 00:00:54,560 --> 00:00:58,099 As a result, we almost never write a Boolean literal, 17 00:00:58,099 --> 00:01:03,220 instead we typically write expressions that compute values of Booleans. 18 00:01:03,220 --> 00:01:08,255 So again, Booleans can only have one of two values, true or false. 19 00:01:08,255 --> 00:01:11,420 We can write a Boolean literal by saying capital 20 00:01:11,420 --> 00:01:14,780 F false for false or capital T true for true. 21 00:01:14,780 --> 00:01:20,015 So, if I print out the value of false and you can see that I get false, 22 00:01:20,015 --> 00:01:25,510 if I print out the value capital T true, then I get true. 23 00:01:25,510 --> 00:01:30,055 Now, just to be sure that these are Booleans and not strings or any other type, 24 00:01:30,055 --> 00:01:33,010 then I can say print out the type of 25 00:01:33,010 --> 00:01:41,845 the expression truth and then print out the type of the expression false. 26 00:01:41,845 --> 00:01:46,750 So, I should expect both of these to print out something like Boolean, 27 00:01:46,750 --> 00:01:51,175 so when I run my code and I can see that type of true is class. 28 00:01:51,175 --> 00:01:57,655 This is short for Boolean and type of false is also bool short for Boolean. 29 00:01:57,655 --> 00:02:02,950 One thing to note about Boolean literals is that they're entirely different from strings. 30 00:02:02,950 --> 00:02:07,630 So, before I print out the type of this expression capital T true and then 31 00:02:07,630 --> 00:02:12,220 I print out the type of this expression capital T true but in quotation marks, 32 00:02:12,220 --> 00:02:18,265 then I should see that this is a Boolean and this is a string. 33 00:02:18,265 --> 00:02:23,290 It's a string whose contents just so happened to be a Boolean expression true, 34 00:02:23,290 --> 00:02:25,480 but to Python, this is just a sequence of 35 00:02:25,480 --> 00:02:29,135 characters starting with capital T and then R-U-E. 36 00:02:29,135 --> 00:02:32,920 So, when I run my code I can see that the type of 37 00:02:32,920 --> 00:02:40,070 this expression is a Boolean and the type of this expression is a string. 38 00:02:44,540 --> 00:02:48,320 So again, we almost never actually write Boolean 39 00:02:48,320 --> 00:02:52,160 literal expressions like capital T true or capital F false. 40 00:02:52,160 --> 00:02:56,755 Instead we write expressions whose values evaluate to be a Boolean. 41 00:02:56,755 --> 00:02:59,020 So, we're going to start with one kind of 42 00:02:59,020 --> 00:03:02,850 Boolean expression called a comparison operator. 43 00:03:07,010 --> 00:03:15,290 A comparison operator compares one thing on the left and then one thing on the right. 44 00:03:15,290 --> 00:03:18,410 So for example, the first comparison operator 45 00:03:18,410 --> 00:03:21,860 that will learn is that is equal to operator. 46 00:03:21,860 --> 00:03:27,200 So, the value of this overall expression is going to be true, 47 00:03:27,200 --> 00:03:37,460 if the thing on the left is equal to the thing on the right, 48 00:03:37,460 --> 00:03:40,554 but it's going to be false otherwise. 49 00:03:40,554 --> 00:03:42,700 So, let's see this in action. 50 00:03:42,700 --> 00:03:49,450 So on line one, I print out is the value of five equal to the value of five, 51 00:03:49,450 --> 00:03:51,040 so that thing on the left is five, 52 00:03:51,040 --> 00:03:54,610 that thing on the right is five and so I should expect the value of 53 00:03:54,610 --> 00:03:59,485 this overall expression to be true because five is equal to five. 54 00:03:59,485 --> 00:04:01,765 Here on line two, 55 00:04:01,765 --> 00:04:05,845 a print out is the value of five equal to the value of six, 56 00:04:05,845 --> 00:04:10,240 so I should expect the value of this overall expression to be false. 57 00:04:10,240 --> 00:04:12,770 Let's see if that's the case. 58 00:04:13,710 --> 00:04:17,315 There are other comparison operators that we can use. 59 00:04:17,315 --> 00:04:21,390 So, in addition to equals equals, there's the opposite, 60 00:04:21,390 --> 00:04:23,250 which is not equal, 61 00:04:23,250 --> 00:04:28,550 we write not equal with an exclamation point equals. 62 00:04:28,550 --> 00:04:33,390 So, that's true if L is not equal to. 63 00:04:33,440 --> 00:04:42,080 So, if the value 64 00:04:42,080 --> 00:04:44,315 of L is not equal to the value of 65 00:04:44,315 --> 00:04:48,110 R then the value of this expression is going to be true. 66 00:04:48,110 --> 00:04:52,025 So, if I write is five not equal to six, 67 00:04:52,025 --> 00:04:58,280 then I should expect this to be true and I can see when I run my code that it is. 68 00:04:58,280 --> 00:05:02,105 In addition to equals equals and not equal to, 69 00:05:02,105 --> 00:05:06,830 there's less than, less than or equal to, 70 00:05:06,830 --> 00:05:10,575 greater than, and greater than or equal to. 71 00:05:10,575 --> 00:05:19,130 In all of these expressions expect one thing on the left and one thing on the right. 72 00:05:22,500 --> 00:05:26,405 So, let's write out a few more Boolean expressions. 73 00:05:26,405 --> 00:05:33,245 So, I can say print out is the value of the expression three less than the value of four. 74 00:05:33,245 --> 00:05:35,495 I should expect that to be true. 75 00:05:35,495 --> 00:05:41,615 I can print out is the value of four less than or equal to the value of four, 76 00:05:41,615 --> 00:05:45,260 I expect this to be true because four is equal to four. 77 00:05:45,260 --> 00:05:52,105 I can print out is the value of five greater than the value of six, 78 00:05:52,105 --> 00:05:56,435 I should expect this to be false and if I print out is the value 79 00:05:56,435 --> 00:06:00,770 of seven greater than or equal to the value of eight, 80 00:06:00,770 --> 00:06:05,060 I should expect this to be false as well So, 81 00:06:05,060 --> 00:06:09,245 you can see that these are the most common types of comparison operators that we have, 82 00:06:09,245 --> 00:06:12,065 equal to, not equal to, less than, 83 00:06:12,065 --> 00:06:13,370 less than or equal to, 84 00:06:13,370 --> 00:06:15,605 greater than, greater than or equal to. 85 00:06:15,605 --> 00:06:18,830 They compare the thing on the left with the thing on 86 00:06:18,830 --> 00:06:23,150 the right and the value of the overall expression is going to be a Boolean, 87 00:06:23,150 --> 00:06:26,280 again, either true or false. 88 00:06:29,260 --> 00:06:32,815 So, let's answer a multiple choice question. 89 00:06:32,815 --> 00:06:35,345 Which of the following is a Boolean expression? 90 00:06:35,345 --> 00:06:37,025 Select all that apply. 91 00:06:37,025 --> 00:06:42,169 So, the value of this expression true is a Boolean literal expression, 92 00:06:42,169 --> 00:06:45,230 so I should say this is a Boolean. 93 00:06:45,230 --> 00:06:47,510 The value of this expression, 94 00:06:47,510 --> 00:06:51,170 three equals equals four is true if three is equal to four, 95 00:06:51,170 --> 00:06:53,465 but because those two aren't equal, 96 00:06:53,465 --> 00:06:54,785 it's going to be false, 97 00:06:54,785 --> 00:06:57,440 but this is still a Boolean expression, 98 00:06:57,440 --> 00:07:00,640 so I'm going to say yes, this is a Boolean expression. 99 00:07:00,640 --> 00:07:06,440 This addition between three and four has the value seven that's not a Boolean, 100 00:07:06,440 --> 00:07:10,325 that's an integer, so this is not a Boolean expression. 101 00:07:10,325 --> 00:07:12,710 The value of this expression, 102 00:07:12,710 --> 00:07:16,250 three plus four equals equals seven is 103 00:07:16,250 --> 00:07:23,240 a Boolean expression and the value of this expression is the string false and so, 104 00:07:23,240 --> 00:07:25,430 this is not a Boolean expression. 105 00:07:25,430 --> 00:07:27,755 So, I'm going to say A, 106 00:07:27,755 --> 00:07:33,720 B, and D are all Boolean expressions. 107 00:07:34,840 --> 00:07:39,260 One thing that I want to note really quickly with this equals equals 108 00:07:39,260 --> 00:07:41,810 Boolean expression is that this is entirely 109 00:07:41,810 --> 00:07:45,095 different than saying something like X equals three. 110 00:07:45,095 --> 00:07:47,270 So, if I say X equals three, 111 00:07:47,270 --> 00:07:49,740 this is an assignment, 112 00:07:50,390 --> 00:07:53,460 that assigns X to three, 113 00:07:53,460 --> 00:07:57,075 if I say X equals equals three, 114 00:07:57,075 --> 00:08:03,860 then this is a comparison and the value of 115 00:08:03,860 --> 00:08:10,160 this overall expression is going to be a Boolean and if X is equal to three, 116 00:08:10,160 --> 00:08:11,525 it's value's going to be true, 117 00:08:11,525 --> 00:08:13,520 if X is not equal to three, 118 00:08:13,520 --> 00:08:15,070 then it's going to be false. 119 00:08:15,070 --> 00:08:18,470 But again, the way that we distinguish between assignment 120 00:08:18,470 --> 00:08:22,010 and comparison is assignment uses one equals, 121 00:08:22,010 --> 00:08:24,680 comparison uses two equals. 122 00:08:24,680 --> 00:08:28,110 That's all for now, until next time.