PYTHON

If, Elif, and Else Statements in Python

If Elif Else Statements

The if/elif/else structure may be a common thanks to control the flow of a program, allowing you to execute specific blocks of code counting on the worth of some data.

if statement:

If the condition following the keyword if evaluates as true, the block of code will execute. Note that parentheses aren’t used before and after the condition check as in other languages.

if True:
  print('If block will execute!')
x = 5

if x > 4:
  print("The condition was true!") #this statement executes

else statement:

You can optionally add an else response which will execute if the condition is false:

if not True:
  print('If statement will execute!')
else:
  print('Else statement will execute!')

Or you can also see this example:

y = 3

if y > 4:
  print("I won't print!") #this statement does not execute
else:
  print("The condition wasn't true!") #this statement executes

Note that there’s no condition following the else keyword – it catches all situations where the condition was false

elif statement

Multiple conditions are often checked by including one or more elif checks after your initial if statement. Just confine mind that just one condition will execute:

z = 7

if z > 8:
  print("I won't print!") #this statement does not execute
elif z > 5:
  print("I will!") #this statement will execute
elif z > 6:
  print("I also won't print!") #this statement does not execute
else:
  print("Neither will I!") #this statement does not execute

Note: only the first condition that evaluates as true will execute. Even though z > 6  is true, the if/elif/else block terminates after the first true condition. This means that an else will only execute if none of the conditions were true.

Nested if statements

We can also create nested if’s for deciding . Before preceding please ask the href=’https://guide.freecodecamp.org/python/code-blocks-and-indentation’ target=’_blank’ rel=‘nofollow’>indentation guide once before preceding.

READ  SimpleHTTPServer Explained: How to Send Files Using Python

Let’s take an example of finding variety which is even and also greater than 10

python 
x = 34
if x %  2 == 0:  # this is how you create a comment and now, checking for even.
  if x > 10:
    print("This number is even and is greater than 10")
  else:
    print("This number is even, but not greater 10")
else:
  print ("The number is not even. So point checking further.")

This was just an easy example for nested if’s. Please be happy to explore more online.

While the examples above are simple, you’ll create complex conditions using boolean comparisons and boolean operators.

We can also use if-else statements inline python functions. the subsequent example should check if the amount is bigger or equal than 50, if yes return True:

python 
x = 89
is_greater = True if x >= 50 else False

print(is_greater)

Output

>
True
>

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

X