Placeholder Image

字幕表 動画を再生する

  • if that is the question hi today we're going to  be looking at if-else conditional statements in  

  • python for lesson two in the series of getting  started with python so let's get straight to it

  • hi guys i'm david and today we're doing  our second lesson in our course on how to  

  • get started in python and today we're going  to be looking at if statements with strings  

  • and integers so i guess the best idea is to  get to our computer and get started with it  

  • right this is the second lesson in the course  if you haven't done the first lesson on getting  

  • started with python i suggest you go to the  card above and click on that to do that first  

  • today we're going to be looking at if statements  so i've started a new folder and created a main.py  

  • just like in the first lesson and i guess we  should get started right as in the first lesson  

  • we're going to start with integers integers as you  know if you did the first lesson are a variable  

  • which is a number a number which can be any number  so we're going to start with i equals zero so what  

  • we actually want to do is ask python to print  out a statement on the screen if i equals zero or  

  • or if i equals one so let's get to that the good  thing about python is it's pretty plain english  

  • so let's go if i equals equals zero and then you  press the double dot to end the statement now what  

  • you're actually going to do now is press enter and  inside the visual studio they will automatically  

  • indent it everything that's indented will be part  of this if statement so we're going to write print

  • we're going to do double quotes here and we're  going to write i is equal to zero then we're going  

  • to go down to a line and we're going to press  backspace and write else now else means that if i  

  • if i is not equal to zero we're going to print  something else out so we're going to press enter  

  • again and that's going to indent the text printis not equal to zero so then we're going to save  

  • that and we're going to run the script so what's  happening here is your python scripts checking  

  • to see if i is equal to zero if it is will print  out like in the last lessons so let's go python  

  • main dot p y and as you can see i is equal to zero  because we said i up here now if we work it out as  

  • one then press save and rerun it it will say  i is not equal to zero so the if statement is  

  • working perfectly well okay so let's clear that  and move on to something a bit more complicated so  

  • what happens if i is more than zero so it could be  any number that's above zero okay so let's go i i  

  • is greater than zero so i is greater than  zero let's save that and see if that works

  • i is greater than zero of course it is because  it's one now what if we set this to zero  

  • and run again i is not greater than zero because  it is agile equal to zero so that works as well  

  • what about if i is equal to three nois greater than three so for this work  

  • i is not greater than three so  we're gonna offset i to four

  • and that's working absolutely perfectly wonderful  right it's because i is four so it is greater than  

  • three it should say three here but anyway you  know what i mean right okay fantastic so okay so  

  • now you've done your first if statement i know  it was simple but that's the great thing about  

  • python everything's written in very much plain  english so it's understandable and easy to code  

  • let's go on to something a little more complex  and add an else if statement so that's going  

  • to give us another condition to our conditional  statement so let's give that a go okay so we're  

  • back here i've done and then we can add an else  f now to do an else if we have to type l sif

  • i equals equals four and then we're going to  go here and we're going to do a print statement  

  • i is equal to four so now what we have is an if  statement saying that we have i is equal to three  

  • if it's not it may be equal to four and if it's  not it will print this so let's give this a go

  • there you go i is equal to 4 which is great  that's wonderful you've now done your first  

  • conditional statement in python so let's have  a look at maybe adding some strings instead  

  • of an integer okay what happens if we decide to  make this into a string so this is a just string  

  • so if we go here and then we type this  and then run it we're going to clear the  

  • terminal down here and run it see what happens  you see it's not equal to zero which is perfectly  

  • great because it's a string now so how would we  do it if we wanted to return a string that only  

  • inc contained the word this in it okay the best  way to do this is to go back to our if statement  

  • here and modify it i'm going to comment all this  out now to comment out all you have to do is do  

  • this like this highlight all and  then do control and that will  

  • that will comment out all of the team now comments  you may not have seen before comments will put a  

  • hashtag in front of the statement which means  the computer will see them but will not run them  

  • so it's very useful if you're adding tank for  another developer or reminder to yourself of  

  • what an actual function does so right  here we could write this is a function

  • for if string okay now we can get on and we  will know next time here that we come here  

  • anything below this line will work with anstring right okay okay to do this what we're  

  • going to do is we're going to create another  string which is going to be the found string  

  • okay found string is going to be the words that  we're going to be searching for this is okay so if

  • found in i we're going to type print

  • i found

  • do and then if not we're going to  do else and then else we'll print

  • not found okay so the idea is is if  we find the text this is inside the i  

  • string we'll type out i found you and if not  we'll type not found okay so let's give it a go

  • i found you so as expected python  actually found inside the string  

  • the text that we were looking for and then  printed out the statement that we wanted  

  • okay now we've done the simple strings  maybe we should look at some arrays  

  • an array is a list of strings or integers  all stocked into one variable so i guess  

  • that's the next idea okay let's have a look  at this then all right okay so array could be  

  • u equals and then we go over here in between  a square bracket so we're going to write apple

  • and then here and then maybe  we should write windows

  • and maybe what about doors okay maybeshould spill that quick okay so inside  

  • this one array we have several values okay  so how would we go about printing out these  

  • photos let's have a look at printing  them to the console if we go print  

  • you this is what you get back i'm going to comment  this out so we don't get the old comments here  

  • we're going to save okay let's print u to  see what happens okay as you can see we have  

  • the full array of all of the values in uh printed  out now what if i wanted just to print out apples  

  • so anything about arrays is that actually they  all start with zero so let's go and write u  

  • squared brackets zero and that will bring up  our first value which would be apple if we  

  • wanted pair it would be one if we wanted windows  it would be two try windows and go windows okay  

  • wonderful so now we have a look to the slide uh  now i've looked a little bit into arrays maybe  

  • we'd do an if statement with an array so how would  we go about doing an if statement for an array so  

  • we're going to do a found straight versus found  string again here and we're going to write apple

  • much like this string above we're going to  try if found in you and i'm going to print out

  • i found an apple and then we're going  to go down to next slide and right else  

  • all right statement which is going to be simply

  • nothing here okay cool right now let's  have a look see whether that works  

  • we're gonna go brian and i found an  apple fantastic that's brilliant so it's  

  • actually gone through our array and found  an apple inside our array that's wonderful  

  • okay we've had a bit of a look into if statements  and how they work we even went into slightly  

  • deeper into an array and we also looked at  adding comments to our script to actually getting  

  • into python and how it works I hope you enjoyed  this lesson it's part of a series that I'm going  

  • to be doing out every day of the week so I'll  see you tomorrow if you like the video thanks  

  • for liking it and please don't forget to subscribe  for the next videos have a great day cheers bye

if that is the question hi today we're going to  be looking at if-else conditional statements in  

字幕と単語

動画の操作 ここで「動画」の調整と「字幕」の表示を設定することができます

A2 初級

Learn Python For Beginners 2021 | Tutorial Part #2 | If Statements

  • 2 0
    Eric Lin に公開 2021 年 02 月 18 日
動画の中の単語