Lesson 6 - The IF Statement in Python (Basics)

Опубликовано: 26 Май 2020
на канале: Practical Python Solutions
85
2

🎙DON'T FORGET TO SUBSCRIBE TO MY CHANNEL!
   / @practicalpythonsolutions-b4478  
------

This video goes over the fundamental concepts of the IF statement in Python .
We're going to look at the IF statement in this video and how it can test for certain conditions in our code. Also, we’ll look at how we can test for multiple conditions with the ELIF statement. We’ll write a little game to bring these concepts together.

We'll also look at lists with the IF statement and explore List Comprehension. And, finally, we’ll throw in some Boolean logic for good measure.

The if statement is a test. If this, then that. It tests to see if something is true or false, with the subsequent commands being executed based on the result. It's the core of programming logic.

In the last lesson we worked with a list of our favorite games. We had five
games in our list and were able to print each item within the list with the FOR statement.
But, what if we wanted to test for a certain condition before printing?

You also want to note that this is case sensitive - so if you typed a lower case minecraft
you won't get a result. One way to get around this with a string comparison, is to change the
case of both the variable and the test. So when we run it now we should get
“yes you have the game” This lower case method makes the game variable lowercase and makes the test condition lowercase. So regardless of which way they were
originally this will be a match if the characters are the same. You can also test for exclusions
in your list by using the "not equal to operator. This is achieved by using the exclamation point followed by the equal sign. Now we're testing to see if Minecraft is not in the list
This means we should get four statements here because there are five items in the list and four are not minecraft.

Integers:
You can also do a lot of things with integers with the if statement. This code will
will loop through the numbers in My_Int list ( for Num in my Int_int) and if the number is
equal to three, we'll multiply it by ten and print each number. Now, note where the print
statement is in this block of code. If you put the print statement under here, it will only print if the number is equal to three, but because I moved it under the original FOR statement here, it will print all the numbers, even the ones that were
multiplied by 3
This is why indent indentation is so important, the location of statements within a block of code can make all the difference. As you can see…only numbers that were equal to 3 were multiplied by 10

We can use the Elif (which is short for "else if") and the else statement when you need to check for multiple conditions. So, for example, if we use it with our integer list, like shown here….
Here we have: if number equal to 3 multiply by 10, if equal to 4 multiply by a hundred, multiply everything else by 1000. So if there are no matches, we're going to
multiply the number by a thousand. so let's run this and as you can see the number that's equal to 3 is multiplied by 10 the number is equal to 4 for the Elif is multiplied by a hundred and the rest are multiplied by thousand. You'll find that evaluating multiple conditions
in Python is quite common,

I have a little game here to show us how to test for multiple conditions and to show us some of the pitfalls we can encounter. The game is very simple - it asks the user to guess a number between 1 and 20. Our secret number is 7 so based on how close they are to that number, we provide the user with feedback. So, for example, if they guessed the number, we
congratulate them and tell them they're good. If they get within 5 numbers of our
number and we tell them they're getting warm and if they're outside of that range
we tell them they're not close.

It's tempting to do this with multiple if statements like shown here. However, this program is
obviously flawed and you might see where the flaw is but let's run it and see
what happens. So if we pick a number and we pick 5 that's close to 7 so we're
getting warm. If we pick a number 17, for example, that's outside of the range and the user is informed that they are not not even close. All that looks well so far, but the problem happens when we pick 7, the correct number. When we enter 7 we get 2 responses because, a) it is the right number and b) it is also warm because it is in close range. The reason for this error
in the logic is because having multiple IF statements means that all of them are
are tested. So in this case, the 7 is tested here and is validated as the correct number.
But it’s also tests here and is validated again because it is within range. It's within five
numbers of our number. The last line is also tested but it is false because it's not outside that range.


Смотрите видео Lesson 6 - The IF Statement in Python (Basics) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Practical Python Solutions 26 Май 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 85 раз и оно понравилось 2 людям.