There are multiple ways when you can actually use if else in Python. The very basic usage is of course the execution of block of code conditionally:
if <condition>:
<body>
else:
<body>
The if block will be executed when the condition is true, otherwise else block comes into execution.
Another use case is when you want to assign values conditionally:
x = y if <condition> else z
In above conditional assignment, the variable x is assigned value of y if the condition is true, other wise it is assigned the value of z. It is widely used in comprehension and return statements as well: [i if i%2==0 else i+1 for i in <iterable>]
Another use case is using if only, this is used in comprehension to filter out some values:
x = [i for i in <iterable> if <condition>]
The above expression will include only the items from iterable for which condition holds true.
Another use case is for else, it is quite unique to Python which allows using of else block after a loop:
for i in <iterable>:
<loop body>
else:
<else body>
The else part will be executed if loop exits normally without a break statement that means the else part is executed even if the code doesn't enter the loop body.
foranywhere.forstatement in the second part of the example. The syntax is pretty consistent to yourb =statementi if i%2 else i+1work as ternary operator in python