Introduction
Setup
Operators
variable
String
Number
Dtype & Comment
Lists
Tuples
Sets
Dictionaries
if else statement
For Loop
While Loop
iterator & iterable
Pre-Built functions
Function
Decorators
generators
Types of errors
Exceptions Handling
OOP
Regular expression
Create module
OS module
Date module
Random module
Math
Statistics module
Exercise 1
Exercise 2
Exercise 3
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
Learn MySQL
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
To use conditions inside a code if, elif, and else statements are used. For the first condition, always if statements is used. For the last condition, always else statements is used. If there are conditions more than 2 then between the first and last condition then always use elif statements. In condition, we will use comparison Operators.
1. Equals: a == b
2. Not Equals: a != b
3. Less than: a < b
4. Less than or equal to: a <= b
5. Greater than: a > b
6. Greater than or equal to: a >= b
In the if-else statement, we check conditions one by one. If the first condition is get matched then the code
inside of that condition gets executed.
If the first condition doesn't match then it doesn't check or go to that code written inside that condition.
It will go for the next condition but if the first condition get matched then it will don't check the other
conditions. It will go inside that matched condition and will run those codes written inside that condition.
If any condition doesn't get matched then the code written inside else will get automatically executed.
In example 1 we have x variable and it has a value of 5. So we wrote a condition that if x is equal to 5 then
print Yes x is 5 and if not then print x is not 5. In this case, x is 5 so it's printed Yes x is 5.
How this code works?
So in the first condition, it checks that if x is equal to 5. In this case, x is equal to five. So it goes
inside the first condition and executes the code written inside it. Because one condition gets matched so it
will never go to check another condition.
In example 2 we have x variable and it has a value of 6. So we wrote a condition that if x is equal to 5 then
print Yes x is 5 and if not then print x is not 5. In this case, x is not 5, so it's printed No x is not 5.
How this code works?
So in the first condition, it checks if x is equal to 5 or not. In this case, x is not equal to five so this
condition is false. Because we don't have any other condition so the code written inside else will
automatically get executed.
In example 3 you have x variable and it has a value of 6. What you want to do here is that, if x is less than
5 then print sorry and if x is equal to 6 then do square of x and if all this condition doesn't match then do
cube of x. The program will check the condition one by one and will give us a result. IN this case, x==6
condition is matched so you got the square of x.
How this code works?
So in the first condition, it checks that if x is greater than 7. In this case, x is less than 7. Because this
condition is false so it will check the next condition. Next condition x is equal to 6. This condition is true
so it will go inside this condition and execute code written inside it. So inside the second condition first
it will do a square of x and then print the value.
You can use if elif else inside an if elif or else. In nested conditions, the program will check first the parent condition or the outer condition and if the parent condition gets matched then it will check the inner or child condition. If the parent condition doesn't get matched then it will never check the child or inner condition.
Example:1In example 1 we have x variable and it has a value of 4. What we want to do is that, if x is less than 5 then if x is less than equal to 4 print, yes, and if not then print no. And if x is equal to 6 then do the square of x. If x is equal to 1 then divide x by 2 and if all these conditions don't get matched then do a cube of x. The program will check the conditions one by one condition and will give a result. In this case, x is less than equal to 4 so we got the result yes.
Example:2In the example, we have x variable and it has a value of 6. What you want to do is that, if x is less than 5 then, if x is less than equal 4 then print yes and if not then print no. If x is equal to 6 and if x<=3 then do the sum of x+2 and if x<=2 then do the sum of x+3 and if not then do the square of x.And if all conditions are wrong then do cube of x. In this case, x is equal to 6 and because x<=3 and x<=2 are wrong, so we got the resulting square of x.
To use more than one condition together and the code written inside of the condition will run, if at least one condition is true, then we will use or.
Example:In the example, we have a variable x and the value of x is 6. We use the condition that if x is greater than 10 or less than equal to 6 then execute the code inside it or go next. In this case, the second condition(x< 7) gets matched so we got the result Yes x is 6.
To use more than one condition together and the code written inside will run if all the conditions are true, then we will use and.
Example:In the example, we have a variable x and its value is 6. We will use the condition that if x is less than 10 and less than 7 then print Yes x is 6. If not then print x is not 5. Here x< 10 is true and x< 7 is also true so we got the result Yes x is 6.
If, elif and else statements cannot be empty. But if you have one for some reason with no content then put the pass statement inside if, elif and else to avoid getting an error.
Example: