 Hey everybody, it's Brian and welcome to the fifth Python tutorial. Before we begin, I kind of wanted to point out on Python's website because I've already been getting questions. There is a full set of documentation on their site. They even have a beginner's guide, a developer's guide, and even non-English documentation. So if English isn't your primary language, you can find a lot of good stuff out there. And I mean, I eat a lot of it. I mean, there's just tons and tons and tons. So, all right. Without further ado, let's just jump into PyCharm. And we're going to make a new Python file. The very descriptive video five. What we're going to cover today is going to branch off from what we've been learning. We're going to build upon everything we've learned. So far, everything we've learned has been a very simple one-liner, you know, print something. Well, we're going to embark on a journey, if you will. We're going to learn about conditions. Usually, when someone has a condition, it's not a good thing. But in programming, a condition is a very good thing. A condition is like an if statement. If I'm hungry, then go to the kitchen and get something to eat, you know, that kind of thing. So that's what we're going to learn today. Before we start with conditions, we have to learn about scope. So what is scope? And I'm going to say Java. That was embarrassing. Can't spell Java. In Java, you'll have these brackets. It's called a C-style language. And if you've tried these before, you probably don't like the brackets. I personally love them. The brackets are there for a reason. You'll have your statement. Don't worry if you don't understand what that means. You're just saying if, and then you've got start bracket and end bracket. Now, you're looking at this from a Python perspective point. That's a dictionary. No, that's not a dictionary. That's scope. What we're saying is scope one. There's actually scope zero. And there's actually three scopes in this little picture here. So what is scope? You remember how we talked about variables and something that'll change? Well, a variable is controlled by scope. And what I mean by that is if you declare a variable here in scope zero, it's available in scope zero, scope one, and scope three. If you declare it in scope one, it's pretty much only allowed in scope one. That being said, that's what these brackets do. They tell you that, hey, this is scope. There's something in here. So you can have, let's actually just make another Java if statement here. I'm going to format this correctly so we can see what's going on. There's actually, you know, scope four. So what you can do now is you can define a variable in scope zero, and it's available in all the scopes. You can define it in scope one, and it's only available in scope one. You can define it in scope four, and it's only available in scope four. That keeps things very, what's the word I'm looking for, encapsulated within your code. Now, Python does away with these little brackets that people just seem to hate. But you notice how things are indented here. Like actually, let's nestle an if statement in here. This is about what it would look like. So now we've got five scopes. You're probably going, why is he showing me Java? This is a Python tutorial. Well, I'm doing it simply because these brackets are very illustrative of what's going on. You notice how things are indented, and you get this little line in PyCharm that shows you the indentations. Python does not have these brackets. Python works with what's called white space. It uses white space specifically for scope. For example, let's get rid of these little brackets here. And you begin to see what a Python program would look like. You notice how things are indented. That tells you right there, that's your scope. And PyCharm draws this beautiful little line to say, hey, here's the scope of this, here's the scope of that, here's the scope of this, et cetera, et cetera. Pretty neat, huh? Well, if you're coming from other languages, that's going to be very confusing, and that's why I wanted to really illustrate that. So we're going to make an if statement a condition. We're going to really hammer out the if statements of this tutorial. We're going to say x equals, why not, 9 if x equals 9 colon. And then we're going to print 9 is here. Now, there's some things you should be aware right off the bat. Notice how the double equals sign. If you have a single equals sign, you're actually assigning a value. You're saying x is now 9. We want double equals. That's how we say if it is equal to. We're not saying it is equal. We're saying if it is equal to. A little bit confusing, but you've got to understand that. You need this colon here, and then there's your print statement. So what we're doing here is we're defining scope. Notice how my little cursor stays on the scope line. Python uses this white space to determine scope. Keep that in mind at all times. Let's actually run this. And so we've got 9 is here and here. All right, pretty neat. I'm actually going to get rid of the and here just because it's bugging me. And then we're going to show inequality. Somebody emailed me last night and said, hey dude, your keyboard is like insanely loud. I'm like, yeah, but I really like this keyboard. Once you get used to a keyboard, it's really hard to just go somewhere else. If x, and then we're going to say the not operator, not equal to 9. Don't forget that colon at the end. Print. 9 no more. Now remember that x is now 8 because we're assigning it. And we're saying if it's not equal 9, then we're going to print something. 9 no more. Some common mistakes you're going to make, you'll forget the colon. And then you go to run this and then it's going to go boom, syntax error and valid syntax. And then you're sitting here going, what? Just bear in mind, pretty much rule of thumb, anytime you're going to make a scope, you need that colon in there. Now, remember x equals 8. Actually, just so I don't confuse you guys, x equals 8. And we're going to get a little creative here. We're going to say if x greater than 10, and we're going to say else. Now what's going on here? Some of you that are into other programming languages already know. Oh my gosh, this is so boring. And I apologize, but we got to start somewhere. We're saying if x is greater than 10, then run this scope. Else, meaning if it's not greater than 10, run this scope. It is, of course, less than 10. So we ran this scope, but not this one. That's where scope comes into play. You make a condition. You determine which chunk of code to run. That's the basis of polymorphic algorithms. You can decide based on a number what happens. For example, if we were to change this to x equals 3. No, actually let's say 30 because we want to show that it's greater than. Now suddenly it's greater than. That's polymorphism right there. Now we're going to show boolean operators. And we're going to say name equal Brian. Age equal 40. I think my girlfriend just got home. If name equals Brian. And age equal 40. Remember the colon at the end? Oops, misspelled that. There can be only one else. Oops, are not Brian. You don't have to use my name and age. You can use your own just to illustrate the purposes here. So what's going on here? We're doing and. And is a boolean operator. We're ending in memory. In other languages it looks like the double ampersand. But in Python it's just literally the word and. So we're saying both of these have to be equal. The name has to equal Brian and the age has to equal 40. In order for this scope to execute. Else this scope will execute. Let's run that. There could be only one because sure enough Brian at 40. Now if I change that to 41. You are not Brian. Let's change that back. Now we're going to do the magic of copy and paste here. We're going to say age equal 21. And we're going to do the or operator here. And we'll say you and I have something in common. We're going to say my mouse would quit acting up. We have nothing in common. So we're going to or here. And in other languages it's this double bar. But in Python it's just the word or. We're changing age 21. So now it's not going to work with the and. So you're going to say either the name is Brian or the age is 40. If so you and I have something in common. Otherwise we have nothing in common. You and I have something in common because name is still Brian. Now if I change this name. You and I have nothing in common. You can see how that kind of works. And let's kind of dive into lists a little bit here. We're going to do a little list checking. And we're going to say. Let's change that variable X to dog cat fish. Maybe if I can do that right. And we're going to say if cat. We're going to use the in operator. We have a cat. Else print. No cats. How sad. Why not? Because I really like cats. Sorry all you dog lovers out there. I just really like cats. So we have a cat. Because there's a cat in X. The in operator will literally search that for you. I'm pretty sure under the hood is just calling index of. And if the index of is not throwing exemption. Then it's doing something. So we're going to take that cat out of there and run this again. No cats. How sad. So you can see how the in operator here works. And. We're going to. Do the is operator. Now this takes a little bit of explaining. I'm going to make two lists here. A. Equal. Literally copy and paste this here. So we have two variables. With the exact same values. And I'm going to say if. A. Equal B. Then. Print. They are. The same. Else. Print. They are not the same. Get your scope there. Let's run this and find out what happens here. They are the same. Now. That's the equal sign. We're going to do is. The is. Operator is a little bit different than equality is determines if it seeks exact same object. When we run this. They are not the same object. They're the same but they're not the same object. What that really means is. They have the same value. But they are two different objects in memory. So when you change a it's not going to change B. Now if you say. A. Equal B. Guess what's going to happen. They're still not the same object because what we've done is we've copied from one to the other. So you can still interchangeably modify those in memory. Pretty crazy huh. Now just because I know I'm going to get an email we're going to say. Missile if statements. How do you make a statement within a statement. Well usually you join an activist group and grow your hair out long and hand out pamphlets at the airport. But we're going to just stick with the statement here. So let's say let's make. Hmm I need some more variable names. Let's go name equal Brian. Age equal 40. They're not new variables. I just want to illustrate what's going on here. And pet equal cat. Why not. So we're going to say if name. Equals Brian. Notice I forgot that. And what I typically like to do is just flesh out my scope first. That way I don't get confused what's going on. And these don't have to be on the same lines. You can space things out. If. Age. Equal 40. So that in a nutshell is what a nestled if statement looks like. And you can keep going and going and going. For example. Pet equal cat. I like that. You have a pet cat. Print. Go get. A cat. Alright so what's going on here. Let's explain this nestled if statement. This really demonstrates scope and Python. Remember how I started this whole conversation with the brackets. Well if we were in another language there would be brackets all over the place. Python does away with that. We just have white space. You can see that line showing hey here's the scope for this. Here's the scope for that. Just because they're on the same line does not mean they're the same scope. For example that scope and that scope are different. You have to really know where you're at. Now why does Python do this? Why does Python make this drastic change from other languages and not file. Follow the brackets. The C style. Well for a couple reasons. First off it's a lot less you have to type. Also there's no hard fast rule in other languages saying you have to indent. I've seen people do things like X equal 4. You can get some really just gnarly unreadable code. You have to look at this bracket. That's calling this function. Python does away with all that. It's going to be maintainable. It's going to be readable. It's going to be future proof. Now what is future proof? It means when you come back to this code six years from now and you don't remember what you wrote. You can go here's that scope and it just follows along. Your brain just follows it and you understand. We've got our nestled if statements. This is a beautiful example of polymorphism at work. We've got three variables. Name, age and pet. Name equal Brian. It's going to print hello Brian. Age is 40. It's going to say you are 40 years old. If not it will say you're not 40. Or you're not Brian depending on what we've changed. Then we've got our cat. Let's run this. Hello Brian. You're 40 years old. You have a pet cat. We'll say instead of cat I have a dog. Go get a cat because it's running this scope and not that one. If we change this to let's say 30. Hello Brian. You are not 40 years old. It just skips over this whole chunk and runs you are not 40 years old. It's a pretty good example of nestled if statements in polymorphism. That's all for this tutorial. Be sure to visit my website voidrums.com. I actually did get a chance to go out and put the Python tutorials out here. I've got the source code for this and other languages out there as well. This site is funded by your donations so if you're not hurting for money feel free to donate a dollar or two. Also join the voidrums Facebook group. There's 200 of us out there and we'd love to help each other.