In this blog I am going to tell something about working with the conditions that Python has.
There are three different conditions in Python: if, else and elif. I am going to explain them all by using them in a code. For exercise 1 that we previously made to calculate what the date of the next day is if you enter the date of today. I used a lot of if, elif and else statements. Here you can see that example (it is not completely finished, because the leap year is not been processed in the code).
I am going to explain the use if, elif and else also with a basic example. First you need some input to work with in your condition. So for this example let’s say a=int(input).
To use if, you just type if and after that a space and your condition, end with a colon. Let’s say: if a<5: After you condition you need to press enter and say what you want if the condition is true, let’s say: print(‘smaller than 5’). And then you get:

As you can see, if the input is smaller than 5 (4), it prints smaller than 5, if you give as input 6, it is not printing anything.
In the next example I am going to demonstrate the use of elif. You can use elif in the case you want the code to do something else in the case it doesn’t satisfy with the if that you made. We are going to use elif in the cases the input is between or the same as 5 and 10. You say elif with a space behind it and then you need to make your condition. Ours is in this case: a>=5 and a<=10 , behind it you need to end with a colon again. After that you need to enter and type what you want you code to do. We want to print between ar the same as 5 and 10. You get as result:

You see that if the input is lower than (3), it still prints smaller than 5, if the input is the same or between 5 and 10 (5 and 8) it prints between or the same as 5 and 10 and if the input is bigger than 10 it still prints nothing. As you can see, if you want your code to check multiple conditions you can divide them by using and, then it has to satisfy with both conditions. In the same way is it also possible to use or, then it has to satisfy with only one of the conditions.
If you want your program to print something else in all the other cases, you can use else. You always put else under if and elif. So in our code we can use else if the input is bigger then 10. To use else, you just type else and you put a colon behind it. On the next line you put what you want your code to do if the input doesn’t satisfy with the if and elif conditions. An example:

You can see in the example above that it still give the same output as before we added the else, only if the input is bigger than 10 (12) it gives the output: doesn’t satisfy with the conditions above.
A couple of thinks you always need to be think of and be aware of:
– Always end your if, else or elif statement with colons.
– Make sure that you are once tabbed when you are typing what the code needs to do if it satisfies the condition. If you are typing all the way to the lift (same level as the if is staying), it is not going to work.
– Do not use an end as you have to do with some others programs.
Hopefully you learned again a lot of this blog, thanks for reading!