WEBVTT 1 00:00:00.230 --> 00:00:01.500 Hi and welcome back. 2 00:00:01.500 --> 00:00:03.390 In this video, we're going to learn about 3 00:00:03.390 --> 00:00:06.993 two Python keywords, and and or. 4 00:00:08.270 --> 00:00:11.173 Let's start by asking the user for their age, 5 00:00:12.370 --> 00:00:14.920 then we're going to determine whether the user 6 00:00:14.920 --> 00:00:18.630 has a age good enough to learn programming, 7 00:00:18.630 --> 00:00:21.730 so what we'll do is, can_learn_programming, 8 00:00:22.870 --> 00:00:26.530 and we'll say age is greater than zero. 9 00:00:26.530 --> 00:00:30.000 Here we have created a boolean comparison, 10 00:00:30.000 --> 00:00:31.820 age greater than zero, 11 00:00:31.820 --> 00:00:35.530 and we have given it a name, can_learn_programming, 12 00:00:35.530 --> 00:00:37.960 however, sometimes users can be too old 13 00:00:37.960 --> 00:00:42.960 to learn programming, so we can use the Python keyword and, 14 00:00:43.207 --> 00:00:46.500 and for example, age less than 150, and what this does 15 00:00:46.500 --> 00:00:50.060 is it says if the age is greater than zero 16 00:00:50.060 --> 00:00:53.000 and the age is less than 150, 17 00:00:53.000 --> 00:00:55.433 then the whole thing is true. 18 00:00:56.510 --> 00:00:59.670 If the age is not greater than zero 19 00:00:59.670 --> 00:01:01.940 then the whole thing is false, 20 00:01:01.940 --> 00:01:04.230 and if age is greater than 150 21 00:01:04.230 --> 00:01:05.873 then the whole thing is false. 22 00:01:10.970 --> 00:01:14.890 So, if we enter our age here, then we will get, 23 00:01:14.890 --> 00:01:16.620 You can learn programming: True. 24 00:01:16.620 --> 00:01:18.070 By the way, let me say at this point, 25 00:01:18.070 --> 00:01:20.010 there is no limit to learning programming, 26 00:01:20.010 --> 00:01:21.726 you can learn programming at any age, 27 00:01:21.726 --> 00:01:24.260 and that's because programming is all about problem solving, 28 00:01:24.260 --> 00:01:27.690 and it's not really all that much about learning syntax 29 00:01:27.690 --> 00:01:31.180 or working with any difficult tools or anything like that. 30 00:01:31.180 --> 00:01:33.440 Alright, now that we've got and out of the way, 31 00:01:33.440 --> 00:01:35.840 we're gonna learn more about it in just a moment, 32 00:01:35.840 --> 00:01:38.450 let's ask the user again for their age, 33 00:01:38.450 --> 00:01:41.930 and now we ask them whether they are usually retired, 34 00:01:41.930 --> 00:01:44.720 so we will have usually_not_working, 35 00:01:45.702 --> 00:01:47.580 and this will be if the age is less than 18, 36 00:01:47.580 --> 00:01:49.680 people are usually not working, 37 00:01:49.680 --> 00:01:52.210 or if the age is greater than 65, 38 00:01:52.210 --> 00:01:55.220 people are usually not working because they're retired. 39 00:01:55.220 --> 00:02:00.220 So then we can print, At age, you are usually not working, 40 00:02:01.270 --> 00:02:04.633 and then usually_not_working, like that. 41 00:02:05.610 --> 00:02:09.720 So let's run this, and if we enter 34, then we will get, 42 00:02:09.720 --> 00:02:13.410 At 34, you are usually not working: False. 43 00:02:13.410 --> 00:02:15.600 If we run it again and we type 10, 44 00:02:15.600 --> 00:02:18.290 At 10, you are usually not working: True, 45 00:02:18.290 --> 00:02:19.400 although you are going to school, 46 00:02:19.400 --> 00:02:21.970 which is pretty complicated as well at times. 47 00:02:21.970 --> 00:02:25.050 Now, there is a small problem with this boolean, 48 00:02:25.050 --> 00:02:30.050 usually you don't want to write booleans as negatives 49 00:02:30.800 --> 00:02:33.980 because it can get quite confusing to read later on, 50 00:02:33.980 --> 00:02:38.980 so ideally, you would want to have usually work instead. 51 00:02:40.130 --> 00:02:42.280 If you have a boolean like this, 52 00:02:42.280 --> 00:02:44.980 then it's easier to think about, just the same way 53 00:02:44.980 --> 00:02:48.930 that in English, double negatives can get quite confusing, 54 00:02:48.930 --> 00:02:52.580 in programming, working with negatives is an added strain 55 00:02:52.580 --> 00:02:55.420 on your brain that you want to avoid generally. 56 00:02:55.420 --> 00:02:59.060 So, we can rewrite this to usually_working, 57 00:02:59.060 --> 00:03:01.360 and what that means is that they have to be 58 00:03:01.360 --> 00:03:03.990 age greater than or equal to 18, 59 00:03:03.990 --> 00:03:07.990 or age less than or equal to 65, 60 00:03:07.990 --> 00:03:10.363 and that is when people are usually working. 61 00:03:15.200 --> 00:03:19.250 So if we run this then we write 65 then, 62 00:03:19.250 --> 00:03:21.430 At 65 you are usually working: True, 63 00:03:21.430 --> 00:03:25.690 so that's unfortunate, but that is how it goes these days. 64 00:03:25.690 --> 00:03:28.260 So, how does it work internally? 65 00:03:28.260 --> 00:03:31.690 Here we've got another function called bool, 66 00:03:31.690 --> 00:03:36.690 this is similar to int and str in that it takes in the value 67 00:03:36.760 --> 00:03:39.000 and converts it to a boolean, 68 00:03:39.000 --> 00:03:43.963 so what will we get if we turn 35 into a boolean? 69 00:03:45.960 --> 00:03:47.210 Well, you get True. 70 00:03:47.210 --> 00:03:52.210 So, what if you turn the string Rolf into a boolean? 71 00:03:53.020 --> 00:03:54.370 Well, you get True as well. 72 00:03:55.360 --> 00:03:57.060 So what happens if you change these 73 00:03:57.060 --> 00:03:59.650 to zero and empty string? 74 00:03:59.650 --> 00:04:01.670 Now you get False. 75 00:04:01.670 --> 00:04:05.380 So in Python, there are some values that, 76 00:04:05.380 --> 00:04:10.180 when converted into a boolean, evaluate to True or False. 77 00:04:10.180 --> 00:04:15.020 Most values evaluate to True, but things like zero, 0.0, 78 00:04:15.020 --> 00:04:18.573 empty string, things like that evaluate to False. 79 00:04:19.470 --> 00:04:23.830 So here's how and and or work for real. 80 00:04:23.830 --> 00:04:28.830 If you have True and False, what and is doing 81 00:04:29.600 --> 00:04:33.440 is it's gonna look at the first value, is it True, 82 00:04:33.440 --> 00:04:36.490 and if the first value is True, 83 00:04:36.490 --> 00:04:39.440 then it's gonna give you the second value. 84 00:04:39.440 --> 00:04:44.150 So, we will say x equal True and False, and then print x, 85 00:04:44.150 --> 00:04:47.310 and what this is gonna do is, again, look at the first value 86 00:04:47.310 --> 00:04:50.210 it's True, so it gives you the second one, 87 00:04:50.210 --> 00:04:53.530 so if we print x, you get False, 88 00:04:53.530 --> 00:04:55.500 and this is very easy to double-check 89 00:04:55.500 --> 00:04:57.130 with something that isn't True and False, 90 00:04:57.130 --> 00:05:00.810 so we'll do 35 and zero, and again, 91 00:05:00.810 --> 00:05:02.420 it's gonna look at the first value, 92 00:05:02.420 --> 00:05:06.203 it's gonna look and see if it's True, and if it is, 93 00:05:06.203 --> 00:05:10.930 then it's gonna give you the second one, so you get zero. 94 00:05:10.930 --> 00:05:13.750 Remember, it knows that this is a True value 95 00:05:13.750 --> 00:05:18.570 because if you pass it through bool, it evaluates to True. 96 00:05:18.570 --> 00:05:23.510 So and, the way it works is it gives you the first value 97 00:05:24.770 --> 00:05:29.340 if it is False, if the first value is True, 98 00:05:29.340 --> 00:05:33.760 then it gives you the second value, so if we put zero first 99 00:05:33.760 --> 00:05:37.540 and 35 after, again it gives you the first value 100 00:05:37.540 --> 00:05:38.673 which is the zero. 101 00:05:39.900 --> 00:05:42.530 Or works in slightly the opposite way. 102 00:05:42.530 --> 00:05:46.220 Or will give you the first value if it is true, 103 00:05:46.220 --> 00:05:48.703 otherwise, it will give you the second value. 104 00:05:49.550 --> 00:05:52.290 So, if the first value is True, 105 00:05:52.290 --> 00:05:54.290 it will give you the first value, 106 00:05:54.290 --> 00:05:55.900 otherwise, it'll give you the second value, 107 00:05:55.900 --> 00:05:58.903 so here it gives you the 35 because 35 is True. 108 00:05:59.970 --> 00:06:01.853 And if you do the other way around, 109 00:06:02.880 --> 00:06:07.880 zero is False so it gives you the second value, again, 35. 110 00:06:10.610 --> 00:06:13.690 You can use this in quite a lot of places actually, 111 00:06:13.690 --> 00:06:18.533 for example, imagine you've got a name and a surname, 112 00:06:19.780 --> 00:06:21.300 and it just so happens that the user 113 00:06:21.300 --> 00:06:25.773 hasn't given you their name, so you can do something like, 114 00:06:26.830 --> 00:06:31.633 greeting could be name or Mr. Surname, 115 00:06:35.890 --> 00:06:37.170 assuming that you know that the person's 116 00:06:37.170 --> 00:06:38.563 gonna be man of course. 117 00:06:41.120 --> 00:06:45.100 So you can then run this, and you get Mr. Smith. 118 00:06:45.100 --> 00:06:45.933 Why? 119 00:06:45.933 --> 00:06:50.210 Because name is an empty string, which means it is False, 120 00:06:50.210 --> 00:06:53.290 or it is False to you when you pass it through bool, 121 00:06:53.290 --> 00:06:55.890 so instead of giving you the first value, 122 00:06:55.890 --> 00:06:58.163 the or gives you the second value. 123 00:06:59.320 --> 00:07:03.580 This is even better if you add in user input. 124 00:07:03.580 --> 00:07:06.430 Let's run this again, and now I'm going to not give them 125 00:07:06.430 --> 00:07:09.110 my name because I care deeply about my privacy, 126 00:07:09.110 --> 00:07:10.820 so I don't want to give them my name, 127 00:07:10.820 --> 00:07:12.973 but I will give them my surname, 128 00:07:14.940 --> 00:07:17.743 and then it prints out Mr. Salvatierra there. 129 00:07:19.210 --> 00:07:20.870 So, situations like this one, 130 00:07:20.870 --> 00:07:24.720 which may seem useless to you at the moment, 131 00:07:24.720 --> 00:07:27.390 are actually all over programming, 132 00:07:27.390 --> 00:07:30.810 so knowing how and and or work internally 133 00:07:30.810 --> 00:07:32.380 can be really useful. 134 00:07:32.380 --> 00:07:36.100 So again to recap, and gives you the first value 135 00:07:36.100 --> 00:07:39.900 if it is False, and otherwise it gives you the second one, 136 00:07:39.900 --> 00:07:43.100 and or gives you the first value if it is True, 137 00:07:43.100 --> 00:07:45.000 otherwise it gives you the second one. 138 00:07:46.900 --> 00:07:51.780 Finally, you've also got not, and that essentially 139 00:07:51.780 --> 00:07:56.410 does the opposite, so not False is gonna be True, 140 00:07:56.410 --> 00:07:59.780 and not True is going to be False. 141 00:07:59.780 --> 00:08:02.360 So this is how the not keyword operates, 142 00:08:02.360 --> 00:08:06.820 it goes in front of a boolean and it does the opposite 143 00:08:06.820 --> 00:08:09.020 of whatever that boolean is, 144 00:08:09.020 --> 00:08:13.960 so for example, not bool of 35 will give you False, 145 00:08:13.960 --> 00:08:18.470 because bool 35 is True, so not True is False. 146 00:08:18.470 --> 00:08:23.190 If you do not of 35, you also get False 147 00:08:23.190 --> 00:08:25.940 because Python realises that not is something 148 00:08:25.940 --> 00:08:28.100 that you use with a boolean, 149 00:08:28.100 --> 00:08:32.090 so it turns the 35 into a boolean before applying not. 150 00:08:32.090 --> 00:08:33.220 I appreciate that this video 151 00:08:33.220 --> 00:08:34.940 has been a little bit confusing, 152 00:08:34.940 --> 00:08:36.740 do bear with me as we go into 153 00:08:36.740 --> 00:08:38.370 more advanced programing territory, 154 00:08:38.370 --> 00:08:39.530 and if you have any questions, 155 00:08:39.530 --> 00:08:42.820 ask them away in the course Q&A, we're always there to help. 156 00:08:42.820 --> 00:08:44.000 Thanks for joining me in this one, 157 00:08:44.000 --> 00:08:45.750 and I'll see you in the next video.