If you see this, something is wrong
First published on Monday, Feb 23, 2026 and last modified on Tuesday, Feb 24, 2026
Conditional if elif else
This is the web output of the basic sample document.
Now let's see the conditionals in Python.
The statements if, elif and else.
We shall see an example of conditional that is the check of the parity of a number.
We shall use the modulo operator that gives the remainder of the Euclidean division.
n%2, n modulo 2, is 0 for n that is an integer that is even, 1 for n that is an odd integer, and none of these for n that is non-integer.
The function parity of n will be defined based on that operator, the modulo operator.
But let's see how to do this.
Launch Anaconda in Spyder and follow me.
We shall first use the modulo operator.
2 modulo 2 is 0.
2%23 modulo 2 is 1, because 3 is odd.
3%2101 modulo 2, that is odd, 101.
It is 1.
101%2And 10 modulo 2, 10 is even, and 10 modulo 2 is zero.
10%2minus 4 modulo 2, a negative even number.
It is zero.
(-4)%2And minus 5 modulo 2, phew, it is one, it is not minus one.
(-5)%2Zero modulo 2 is zero.
0%2And 2.5 modulo 2 is 0.5, it is not 0 and it is not 1.
2.5%2Now we shall make the function. We go into the editor.
file > new file.
We save it to give it a name and to place it in the right directory.
We call it Parity.py.
It is a script in order to define the function Parity.
So we define our function, 'def' Parity, of n.
Parameter n.
Now our conditional. 'If' the condition n modulo two is equal to, equal equal, zero, then n is even.
So we return the string into quotes, even.
'elif", else if, if the condition is not true and another condition is true.
The other condition is n modulo 2 is equal to 1.
Double point.
Return Odd, because if n modulo 2 is 1, the parity of n is that it is odd.
'Else', it is neither 0 nor 1, you return non-integer because n is not an integer and it has no parity.
def parity(n):
if n%2 == 0:
return 'even'
elif n%2 == 1:
return 'odd'
else:
return 'non integer'Then we save the file and we run it.
So we have our new function available.
We check it.
Parity of 2.
It is even, you know it.
It is even.
Parity(2)Parity of -4.
It is even too.
Parity(-4)Parity of 3.
It is supposed to be odd and it is odd.
Parity(3)Parity of -5.
It is supposed to be odd and it is also odd.
Parity(-5)And parity of -5.1.
It is not an integer, because 5.1 is not an integer, and it has no parity.
Parity(5.1)But what is the syntax of the conditionals in Python? You must have an if statement, if condition, double point after an indentation statement.
Then the statement executes itself when the condition is true.
You may have several statements aligned with the same indentation.
Then the several statements execute when the condition is true.
You may also have an else statement.
The statements 1 to k1 execute when the condition is false.
You may also set an elif statement between if and else statements.
The statements 1 to k2 execute themselves when condition 0 is false and condition 1 is true, and the statements 1 to k3 execute themselves when condition 0 and condition 1 are false.
In fact, you may have several elifs between if and else statements.
The statements of a given elif execute themselves if the previous conditions are false and the condition of the given elif is true.
And the statements 1 to k_{p + 1} are executed if all the conditions 0 to p are false.
What's coming now? What's coming now is the sequential loops for, with the range lists.