Conditions and If statements in Python
Following usual logical conditions from mathematics are supported by Python:
We can use these conditions in many ways and most commonly these can be used in 'if statements and loops'
a = 33 b = 200 if b > a:      print("b is greater than a")
An "if statement" is always written with the use if keyword.
In the above example we have 2 variables, a and b. The variable a is assigned value 33 and the variable b is assigned the value 200. After assigning the values an if statement is used to check that whether variable b is greater then a or not. As we know that 200 is greater than 33 so it will print on the screen "b is greater than a".
a = 33 b = 200 if b > a:     print("b is greater than a")
Indentation in Python
In Python we use identations with the use of white spaces to define the scope in the code. Unlike any other programming language, in python we use identations instead of curly brackets that are used in most of the programming languages.
If you will write if statement without any identation then it will raise an error at the time of execution of the code.
Elif a = 33 b = 33 if b > a:      print("b is greater than a") elif a == b:      print("a and b are equal")
The elif keyword is used in python for fulfillihng the purpose of running various condition at same time and the the conition which is true, then the identation of that condition will execute. The meaning of the keyword "elif " is that "if the previous conditions were not true, then try this condition".
In the above example 'a is equal to b ', so, the if statement is not true as it will be true is b is greater than a, so the elif statement will be executed as it fulfills the conition of equal to. So " a and b are equal" will be printed on the screen.
a = 200 b = 33 if b > a:      print("b is greater than a") elif a == b:      print("a and b are equal") else:       print("a is greater than b"
Else
The else keyword is used in the situation in which the condition specified in the if block is false and we want to do some action to be performed even after that the that is done with the else statement.
In this example since a is greater than b, so the if condition will not and also a is not equal to b so elif statement will also not work so then else statement will execute and will print on the screen "a is greate than b".
if a > b: print("a is greater than b")
Short Hand If
In case you have only one statement to be executed then you can put it on the same line as an if statement.
One line if statement can be written as follows:
Short Hand If ... Else print("A") if a > b else print("B")
If you have only one line inn the if and the else block to be exected then you can place them in a single line as follows:
And if a > b and c > a:      print("Both conditions are True")
"And" is a logical operator that is used for the purpose of combining the conditional statements:
For e.g. if we want to test that if a is greter than b AND if c is greater than a, then we write the following code for that:
Or if a > b or a > c:      print("At least one of the conditions is True"
"Or" is a logical operator that is also used for the purpose of combining the conditional statements: