fbpx

Category content:

Condition IF in Python

Condition IF in Python

We use the IF statement in programming to run a group of statements but only when a condition is met.

In this section, we want to introduce the syntax and examples of Condition IF in Python.

IF is one of the conditional commands that is very widely used in programming and learning it is recommended to everyone.

There are three forms of the IF In Python:

  1. if
  2. if  else
  3. if   elif   else

elif allows to check multiple statements for TRUE and if one the condition was True then it execute those statements.

else: if all conditions were False then the last condition in ‘else’ will execute at the end.

we must know the conditional elements and here there are some elements:

a==b              Is a equal to b?

a !=b              Is NOT a equal to b?

a < b              Is a smaller than b?

a > b              Is a greater than b?

 

for using IF we have three steps:

if condition1:

statement a

if it’s True then compiler run it and skip others condition

elif condition2:

statement b

and if last IF was False then compiler check this part and if it’s True then compiler execute it and skip other conditions

elif condition3:

statement c

we can have another IF and if last ELIF was False then compiler check this part and if it’s True then compiler execute it and skip other conditions

else:

statement d

All conditions were False, and compiler runs this.

 

For Example:

if a > b:

    print (‘Yes’)

elif a < b:

    print (‘No’)

else:

    print (‘Equal’)

 

Nested IF in Python:

We can merge several IF into a single IF statement and this is called Nesting in programming languages.

First Condition: if condition1:

Second Condition: if condition2:

Third Condition: if condition3:

statement a

elif condition3-1:

statement b

else:

statement c

 

let’s to see video from our YouTube channel

If you want to learn more Python, please click here.

Share :
630
keyboard_arrow_up