Python - if - Conditional statements
| S.No. | Statement & Description for if condition |
|---|---|
| 1 | if statements: An if statement consists of a boolean expression followed by one or more statements. |
| 2 | if-else statements: An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE. |
| 3 | nested - if statements: You can use one if or else if statement inside another if or else if statement(s). |
Example:num = 15if ( num == 15 ) : print("Your given number is 15")Result:Your given number is 15
Python supports the usual logical conditions from mathematics:
- Equals: a == b
- Not Equals: a != b
- Less than: a < b
- Less than or equals: a <= b
- Greater than: a > b
- Greater than or equals: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
Python relies on indentation (whitespace at the beginning of a line) to define the scope in the code. Other programming languages often use curly brackets for this purpose.
The elif keyword is pythons way of saying, if the previous conditions were not true, then try this condition below.
Example:num_1 = 25num_2 = 12if num_1 > num_2:print("num_1 is greater than num_2")elif num_1 == num_2:print("num_1 and num_2 are equal")Result:num_1 is greater than num_2
The else keyword catches anything which isn't caught by the preceding conditions.
Example:num_1 = 15num_2 = 20if num_1 == num_2:print("num_1 and num_2 are equal")elif num_1 > num_2:print("num_1 is greater than num_2")else:print("num_1 is smaller than num_2")Result:num_1 is smaller than num_2
Example:if num_1 > num_2: print("num_1 is greater than num_2")Result:num_1 is greater than num_2
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.
Example:num_1 = 222num_2 = 333print("Python") if num_1 > num_2 else print("learning")Result:learning
The and keyword is a logical operator and is used to combine conditional statements.
Example:num_1 = 100num_2 = 99num_3 = 101if num_1 > num_2 and num_3 > num_2:print("Both conditions are True")else:print("Not fallowing condition")Result:Both conditions are True
The or the keyword is a logical operator and is used to combine conditional statements.
Example:num_1 = 100num_2 = 101num_3 = 102if num_1 > num_2 or num_3 > num_2:print("one of the conditions is True from both conditions")else:print("Not fallowing condition")Result:one of the conditions is True from both conditions
You can have if statements inside if statements, this is called nested if statements.
Example:num_1 = 333if num_1 >= 111:print("Given number is greater than 100")if num_1 >= 222:print("also, the number is above 200")else:print("conditions are not fallowing.")Result:Given number is greater than 100 also, the number is above 200
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
Example:num_1 = 100num_2 = 111if num_1 > num_2:passResult:No output and no error it just passes the statement.

No comments:
Post a Comment