Python conditional statement
Generally, if we want to automate any logic, we will be using three different ways to make the logic work on the code. They are,
Python if/else block:
Used to write condition statements based on validation.
Syntax
->
If
condition
Statements
Else
Statements
Here indentation plays a major role in writing the blocks of
statements. So we need to maintain a proper indentation to segregate the
blocks.
Below are the few conditions we use it in if/else block
>>> if(True): ... print("Hello") ... else: ... print("Not True") ... Hello >>> if(False): ... print("Hi") ... >>>
>>> x = 10
>>> if (x<=10):
... print('True')
...
True
>>> if (n>10):
... print('Greater')
...
>>> if (x==10):
... print('Equal')
...
Equal
>>>
>>> a=5 >>> if(a): ... print("a is 5") ... a is 5 >>> b=0 >>> if(b): ... print("b is 0") ... >>>
>>> a,b,c = 1,4,7 >>> if (a>b) and (a>c): ... print("a is larger") ... else: ... print("a is smaller") ... a is smaller >>>