WEBVTT 1 00:00:00.040 --> 00:00:01.070 - [Instructor] Okay, the other stuff 2 00:00:01.070 --> 00:00:03.030 is pretty straightforward. 3 00:00:03.030 --> 00:00:06.060 Here's what the syntax looks like for boolean expressions. 4 00:00:06.060 --> 00:00:10.050 You can use a double equal sign to test for equality. 5 00:00:10.050 --> 00:00:13.000 And you can also just use the word is. 6 00:00:13.000 --> 00:00:15.060 There's also a triple equal sign that you can use 7 00:00:15.060 --> 00:00:18.040 to compare actual objects together instead of their values 8 00:00:18.040 --> 00:00:21.000 but that's seen less commonly. 9 00:00:21.000 --> 00:00:22.090 True and false are a little weird 10 00:00:22.090 --> 00:00:24.070 in that they are capitalized in Python 11 00:00:24.070 --> 00:00:28.010 but only the first letter like so. 12 00:00:28.010 --> 00:00:30.070 And for operators like and, or, and not, 13 00:00:30.070 --> 00:00:32.090 you just use the words and, or, and not 14 00:00:32.090 --> 00:00:36.040 instead of special symbols. 15 00:00:36.040 --> 00:00:38.070 You can do something like a case or switch statement 16 00:00:38.070 --> 00:00:40.020 by using boolean expressions 17 00:00:40.020 --> 00:00:43.080 with an if, elif, else structure like this. 18 00:00:43.080 --> 00:00:46.050 In this example, we print "How did that happen" 19 00:00:46.050 --> 00:00:50.000 if one is three which always false, otherwise we move on 20 00:00:50.000 --> 00:00:52.000 to test whether one is greater than three, 21 00:00:52.000 --> 00:00:55.040 which should also be met with disbelief if it were true, 22 00:00:55.040 --> 00:00:56.090 when that test fails we fall back 23 00:00:56.090 --> 00:00:58.050 to our final else clause 24 00:00:58.050 --> 00:01:01.000 which prints "All is well with the world." 25 00:01:01.000 --> 00:01:03.020 Again, we use indents to associate code 26 00:01:03.020 --> 00:01:07.000 with specific clauses here. 27 00:01:07.000 --> 00:01:08.060 We touched on looping earlier. 28 00:01:08.060 --> 00:01:10.060 Let's go into more depth here. 29 00:01:10.060 --> 00:01:12.050 You can use the range function 30 00:01:12.050 --> 00:01:14.040 to automatically build up a list of values 31 00:01:14.040 --> 00:01:18.050 within a giving range like so. 32 00:01:18.050 --> 00:01:20.040 Again, we start counting from zero here 33 00:01:20.040 --> 00:01:26.040 so range 10 gives us back the values zero through nine. 34 00:01:26.040 --> 00:01:28.050 The continue key word within a loop means 35 00:01:28.050 --> 00:01:30.030 to skip the rest of the iteration 36 00:01:30.030 --> 00:01:32.020 and go straight to the next one. 37 00:01:32.020 --> 00:01:35.090 And the break key word means to stop the iteration early. 38 00:01:35.090 --> 00:01:39.010 In this example, we use continue to skip over the printing 39 00:01:39.010 --> 00:01:42.050 of each value if the value is one, and we use break 40 00:01:42.050 --> 00:01:45.050 to stop after we reach the value five, 41 00:01:45.050 --> 00:01:49.050 Study the output of zero, two, three, four, five. 42 00:01:49.050 --> 00:01:52.090 And you'll see that's exactly what it did. 43 00:01:52.090 --> 00:01:55.040 An alternative syntax is the while loop 44 00:01:55.040 --> 00:01:58.070 which iterates until some boolean expression is false. 45 00:01:58.070 --> 00:02:01.020 Here we setup a counter variable called x, 46 00:02:01.020 --> 00:02:05.000 and loop through printing x only while x is less than 10. 47 00:02:05.000 --> 00:02:10.070 Once it hits 10, the loop ends as you can see in its output. 48 00:02:10.070 --> 00:02:13.090 So try out a really simple activity that pieces together 49 00:02:13.090 --> 00:02:16.010 some of the stuff we've just talked about. 50 00:02:16.010 --> 00:02:19.000 Your challenge is to write a block of code here 51 00:02:19.000 --> 00:02:20.080 that creates a list of integers, 52 00:02:20.080 --> 00:02:23.000 loops through each element of the list, 53 00:02:23.000 --> 00:02:26.000 and only prints out even numbers in the list. 54 00:02:26.000 --> 00:02:28.010 This should just be a matter of rearranging some 55 00:02:28.010 --> 00:02:29.050 of the other code in this notebook. 56 00:02:29.050 --> 00:02:31.080 So even if your not comfortable with Python, 57 00:02:31.080 --> 00:02:33.060 I encourage you to give it a try. 58 00:02:33.060 --> 00:02:35.000 You'll feel a lot better with Python 59 00:02:35.000 --> 00:02:36.080 if you've written something yourself with it, 60 00:02:36.080 --> 00:02:41.030 no matter how small. 61 00:02:41.030 --> 00:02:44.050 Okay, so hopefully you didn't have much trouble with that. 62 00:02:44.050 --> 00:02:46.020 Here's my solution, and keep in mind 63 00:02:46.020 --> 00:02:48.020 there's more than one way to do it. 64 00:02:48.020 --> 00:02:52.030 Let's type in myList equals, in square brackets, 65 00:02:52.030 --> 00:02:56.060 zero, one, two, five, eight, three. 66 00:02:56.060 --> 00:02:59.070 And you can use any numbers that you want. 67 00:02:59.070 --> 00:03:04.060 Then we can say for number in myList, colon, 68 00:03:04.060 --> 00:03:08.020 of course you might choose different variable names, 69 00:03:08.020 --> 00:03:11.060 if number 70 00:03:11.060 --> 00:03:12.050 mod two 71 00:03:12.050 --> 00:03:15.090 is zero, colon, 72 00:03:15.090 --> 00:03:21.000 print number. 73 00:03:21.000 --> 00:03:23.090 And as you can see, we do get the expected output 74 00:03:23.090 --> 00:03:26.000 of just the even numbers here. 75 00:03:26.000 --> 00:03:28.030 Okay, so that's everything that's peculiar about Python 76 00:03:28.030 --> 00:03:29.030 for the most part. 77 00:03:29.030 --> 00:03:30.090 If you're coming from some other language, 78 00:03:30.090 --> 00:03:32.020 that should give you enough knowledge 79 00:03:32.020 --> 00:03:34.010 to understand the Python code I'll be showing you 80 00:03:34.010 --> 00:03:37.020 in this course and to work with that code yourself. 81 00:03:37.020 --> 00:03:39.020 But if you're really befuddled at this point, 82 00:03:39.020 --> 00:03:40.080 I do encourage you to grab a larger 83 00:03:40.080 --> 00:03:43.020 introductory Python course before moving forward 84 00:03:43.020 --> 00:03:44.030 in this one. 85 00:03:44.030 --> 00:03:46.030 An ability to read Python code is going 86 00:03:46.030 --> 00:03:48.060 to help this course make a lot more sense to you. 87 00:03:48.060 --> 00:03:50.070 But if you think you got the basics here, 88 00:03:50.070 --> 00:03:51.006 let's move forward.