Placeholder Image

字幕列表 影片播放

  • Hey, cso Joe.

  • It's like a here.

  • Welcome to video number two off my python tutorial for absolute beginners in this video.

  • First of all, I'm gonna ask her a question I received on my last video.

  • And then I'm gonna give you some additional commands that you can use in Jupiter notebook, for example, for adding and deleting cells.

  • And remember that you don't necessarily have to use different notebook to follow this course you can use, for example, part charm, which is a popular I d for python.

  • After that, I'm gonna cover if l statements how to use them and what they are.

  • And by the end of this video, you'll be able to create a simple B m I calculator, which can tell if a person is overweight or not, given the person's height and weight, I'm gonna put on the outline off this video below, so you don't have to watch the whole thing if you don't want to.

  • And you can go to see sojo dot io slash python to to download the simple files I create in this video.

  • All right.

  • First of all, let me answer a question I received on my last video.

  • The money as REM Sorry for the pronunciation says currently I'm learning Web development.

  • What do you think about it?

  • Should I keep going or start another language like Python?

  • So I wanted to answer this particular question because I thought some other people might have the same question.

  • Maybe you started learning c++ or job already on your wondering.

  • Should I keep going with it, or should I take this course for Python instead?

  • I would say Keep going with whatever you're doing, as long as you enjoy and then if you want, you can take this course at the same time because some of the concepts I'll teach are gonna be the same regardless of which language you're using.

  • So, for example, the method for swapping two variables I talked about in the last video You can use that in c++ or Java or in any other language you're lining.

  • Okay, with that out of the way, let's now talk about juvenile notebook.

  • First of all, launch did a notebook through Anaconda Navigator or with any other I thought you'd like to use and then navigate to desktop.

  • Let's create a new folder here.

  • I'm gonna call this Python tutorial, too.

  • Navigate to that folder within Messed up and click new at the top right corner Click Pylon three.

  • And let's call this file if else statements, click rename and then I'm just gonna maximize my browser window.

  • So the only command we learned in the last video was this one right here for running or executing a cell.

  • Once I click it, it executes or runs all the commands within the given.

  • Sell this green box right here.

  • So if we have hello, one here and then hello to here.

  • And the print function will execute both of these lines.

  • And you might have noticed that after running this, sell it automatically as a new cell below it.

  • And what if we wanted to do it?

  • This cell For that, you can use this bun that looks like scissors and for adding sales.

  • You can use this bun right next to it that looks like a plus button, and you can add as many cells as you want on.

  • One interesting thing here is that you can execute these cells in any order you like.

  • So let's say you have print a in this cell on a equals one in this.

  • So you might say, Well, if we try to execute this so it will give us an error, let me actually try it again because A is not defined yet.

  • So it'll give us on air.

  • Say name error.

  • Name A is not defined, but before executing the cell print A.

  • If we execute this cell right below it to define a to be one, it would give us an error anymore.

  • Okay, let me know.

  • Delete a bunch of cells here with this scissor spun.

  • And let's add one cell below the first cell on Let's say we have b e close to here on Let's run it with the wrong button right here.

  • And of course, this way be is defined to be too.

  • Now what happens if we delete the cell with this bone right here on?

  • If we try to print, be right here with print Prentice's B.

  • What happens is actually, we can still print it because even though the cell containing this statement because two is dilated, the state off, this Jupiter notebook is actually still preserved.

  • So when I say the state off this Jupiter notebook, what I mean, is pretty much all the variables that are stored in this stupid a notebook.

  • And the state of this Jupiter notebook is not stored on this browser window.

  • Because, like I said in the last video, this browser window is just a user interface for the Jupiter notebook.

  • The state's off.

  • This Jupiter notebook is stored in something called the Colonel off this jibber notebook that's basically represented by this command line interface.

  • This might look slightly different if you're using Windows on the Colonel is what Excuse your code once you click the rum bun and also stores all the variables that you've defined.

  • So to clear out the state of the stupid A notebook or to clear out all the variables you have to find, you need to click colonel at the top and then click Restart.

  • Actually, I like to use the command.

  • It'll restart and clear open because it restarts.

  • The colonel on clears out everything that's been printed.

  • So this colonel has been restarted.

  • Now on, if we try to print be now, it'll give us an error because name A is not defined yet.

  • Also, remember that there's one colonel for each notebook file.

  • So if we had another notebook file on if we restart the colonel for this notebook file, that won't affect the other notebook at all, because that no book will have a separate colonel.

  • Okay, So just to recap, we've learned how to use four commands into better notebook.

  • The first is running a cell, and then the leading sells adding cells as well US restarting the Colonel and clearing the output.

  • Okay, let's not delete all the sales we have here.

  • Andi, we start to colonel and clear the output.

  • And let's dive into our main topic today.

  • If l statements.

  • Let me give you a quick example Here A is equal to one on B is equal to two.

  • And what if we wanted to show a message?

  • That's something like a is less than be by printing this string on Lee.

  • If Ace actually less than beat, we can do this with, if a less than be Colin.

  • So that's if space a space less than space be column enter into a new line on print.

  • A is less than beat on.

  • You need to invent this new line by four spaces, so that's 1234 You could actually use a different number of spaces, for example, three spaces or five spaces.

  • But using four spaces is a convention in Python.

  • So let's stick with four spaces in this video.

  • So these two lines, if a lesson be prints A's lesson be this whole thing is called an if close.

  • And after this, if clothes, you could have more code.

  • For example, print.

  • Not sure if a is less than be.

  • And so this if condition on Lee applies to this line in this example on Python knows that because of the Collin sign here, as well as the four spaces that we have before this print mine.

  • So let's try running to sell because A is currently us, then be we should be able to print a lesson.

  • Be on we do on Let's see what happens if a three in that case, A is not less than be so.

  • This cell should Onley print this line right here.

  • Not sure if a is less than be on.

  • That's indeed the case, and it's actually possible to have multiple lines within the if Klaus.

  • So instead of just having print A is less than be we're gonna write.

  • Print double quotes is definitely less than be on in this statement.

  • Python knows that these two lines are part of the IV clause because both of these lines have four spaces in front of them.

  • So let's see if that works.

  • By changing a 21 again on Since Ace listen be, we should be able to print these two lines again.

  • So we see that ACE Lesson B and es Stephanie less than be so in a different language.

  • You might see an if close with princesses on with curly brackets, but in python were using a column as well as four spaces in front of each line.

  • You need to be really careful about the number of spaces in front of each line, because if we had, for example, four spaces in front of the first line on five spaces in front of the second line, it'll give us an error.

  • So the air says indentation error, unexpected Indians.

  • Let's fix that by deleting one space here and now we have forced business in front of each line, so it's gonna work again.

  • Okay, let's take a look at a few more examples here.

  • The 1st 1 is this.

  • Sea is equal to three.

  • Andi is equal to four.

  • If we write, see less than de colon.

  • Four spaces print double quotes, See is less than D.

  • And let's add something outside this if block by writing print outside the East block.

  • And once we write, this will print this line c'est les in de Onley when C is less than deep.

  • What if we wanted to add a new line here that says, See, it's not less dandy.

  • And what if we wanted to put it on Lee?

  • If see, it's not less than deep.

  • We can do that by writing else colon right after the if, Klaus, and note that there's no spaces before this else line and insert four spaces before this line to show that this line is part of the else Claus.

  • We could also have multiple lines within the else calls on right print double coats.

  • I don't think C is less than D, so this whole block says.

  • If c'est les in de, then print c'est les Cindy.

  • And if that's not the case or else, print these two lines instead, so currently see It's definitely less than D, so we should see this line print.

  • It sees lesson D on this line outside the East block, and we see that sees Listen, Dee and outside the East block on once we change the value of C 25 so that sea is not less than D, we should be able to see these two lines instead on we do see it's not less Andy and I don't think sees less than D.

  • Take a look at another example here is equal to seven on F is equal to eight.

  • So we're gonna write pretty much the same thing after that.

  • So if he is less than F, then Prince is less than f on else.

  • Kahlan Prince.

  • He is not less than f.

  • So here.

  • What if we wanted to deal with three cases instead of only two cases?

  • For example, what if we wanted to deal with the case?

  • Where is lesson F on is equal to F on e's greater than F separately?

  • So those three cases we can do that with ill If which stands for else.

  • If so, right l if e double equal sign.

  • If New Line print parentheses, double quotes is equal to F on.

  • Let's change what seemed the else close to EA ease greater than F.

  • So this block now says, if he's less an F, then print is less an F on.

  • If that's not the case, check the next condition.

  • E is equal to F.

  • A note here that we're using the double eagle sign instead of a single equal sign.

  • So the difference is that when we use the single equal sign, for example, right here we're assigning what's on the right hand side to the left hand side.

  • So in Python and many other languages, the single equal sign is an assignment operator.

  • Now, if you want to check if he is equal to F similar to the way we can check if a is less than F, we need to use the double equal sign.

  • So if you're not sure about which one to use, just remember that in this line we're not assigning f T e.

  • We're evaluating instead, if e is equal to F.

  • So this whole Block says if he's less an f print this line, and if that's not the case, check the next condition.

  • If e is equal to F.

  • Then print this line right here.

  • Prince is equal to F on.

  • If none of these conditions are true, so it's not true.

  • That is less an F on he's equal to f.

  • Then else print is greater than F because that's the only possibility that's left here.

  • Now let's just make sure this whole block works.

  • Currently he is less than if so, we see these less an f on what if is equal to f so e and F or both go to eight, then we see easy go to f right here on What if he is greater than F So East 10 on F is eight.

  • We see ease Great Adam F right here and actually after the If Klaus we could have multiple l if causes so after the first l.

  • If we can write l if let's say e greater than f plus 10.

  • So this just means e is greater than F bye.

  • More than 10.

  • So right now he's not greater than F by more than 10.

  • We should still see he's greater than 10.

  • Once we run this cell and we do on once we changed e to, for example, 20.

  • Well, see, he's greater than F by more than 10.

  • So there's actually another way to deal with three or more separate cases without using the L if clauses, so to show you.

  • Let's define new virals here H and G seven and eight.

  • So we're gonna write if G is less than a TSH colon.

  • Print G is less than a tch, and so far it's the same.

  • But after this, we're gonna write else instead off.

  • If and within this else Claus, we can create a new if close and say, if G is equal to H, the double eagle sign Print G is equal to H and after that else, colon print G is greater than H, so let's see how this block works.

  • First, we're checking if G is less than a TSH, and if that's the case, we print G is less than a tch on.

  • If that's not, the case will go to the else Kloss on their four lines in the else Claus.

  • And if you look at this time, the if Klaus, there are four spaces in front of it.

  • And if you look at this print line that says G is equal to H.

  • That's within the if close within the else close.

  • So there are four spaces here and then therefore additional spaces here before the print line.

  • So they're eight spaces total here and similarly, for else, there are four spaces here because this else Claus is within this else close.

  • And then they're eight spaces before the print line here because there are already four spaces before the else close, and then therefore spaces after that to show that this print line is within this else Kloss.

  • Okay, let's make sure this works right now.

  • G is less than eight, so wish you see d is less than a tch we do on If we have G being equal to H, we should see G is equal to age, and we see that on If G is greater than age, we should see this line being printed.

  • We do Okay, let's not create a simple B M I calculator.

  • B m.

  • I is basically a number that can tell if a person is overweight or not, depending on their height and weight.

  • So let's say, hypothetically speaking, we have a person named y K on his height in meters again, hypothetically speaking is two meters on.

  • His weight in programs is 90 now.

  • If you only know someone's height in feet and inches and weight in pounds, just use an online calculator to convert it so you can just search for feet and inches.

  • Two meters.

  • You should be able to find a good online calculator for that.

  • Okay, now the formula for finding the B M I is the person's weight in kilograms divided by this person's height in meters squared onto express height in meters squared.

  • You could either right hide M times, height, M or you can write hi m double star, too.

  • And this double star just means to the part off.

  • Let's print this person's be Am I here with print double quotes be am I and then be Am I so this person would be not overweight if this person is B M iess less than 25.

  • So in that case, if be, am I less than 25?

  • Let's print this person's name is not overweight on else.

  • If b m I is greater than or equal to 25 we're going to parade this person is overweight.

  • Let's see if y que is overweight here.

  • Okay, he's not.

  • But what if he had 110 kilograms off?

  • Wait, They're his B m.

  • I would be 27.5.

  • And why Kay would be overweight.

  • Okay, if you like to support me so I can keep making videos like this full time, just go to see estelle joe dot io slash p a t to find my picture on page on.

  • If you want to make sure that you don't miss my future videos like this one, just goto siesta joe dot io slash news to sign up to my newsletter on If you want to download some simple files from this video, go to C s.

  • Nigel that io slash python too.

  • I'm y que from c s still go on.

  • I will see you in the next video.

Hey, cso Joe.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

如何在Python中使用If Else語句 (Python教程 #2) (How to Use If Else Statements in Python (Python Tutorial #2))

  • 6 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字