Python If Else conditional statement

 

In many cases, you don't want all the code to be executed in the program.

Instead, you might want some code to execute only when certain conditions are met, and a different set of code when conditions are not met.

This is where conditional statements come in handy.

Conditional statements allow you to control the logical flow of a program in a concise and compact way.

They are branches -- like a fork in the road -- that modify the way code executes and process decisions.

This tutorial introduces if, if.. in Python programming language through examples Basic knowledge of else and elif statements.

Let's start!

Basic if statement syntax

if statement in Python:

If this expression evaluates to True, run the code after the expression once. If the result is not True, do not run the subsequent code block

The general syntax of the basic if statement is as follows:

if condition:
    execute statement

The if statement includes:

  • The if keyword is used to start the if statement.
  • Then there is a condition. The condition can be evaluated as True or False. Parentheses (()) around conditions are optional, but they do help improve code readability when multiple conditions exist.
  • Colon: separate the condition from the following executable statement.
  • A new line.
  • The indentation of 4 spaces, which is specified in Python, is related to the body of the following statement.
  • Finally, the text of the statement. This is code that runs only if the statement is evaluated as True. There can be multiple lines of code in the body, in which case we need to note that they should have the same indentation.

Take the following example:

a = 1
b = 2

if b > a:
    print(" b is in fact bigger than a")

Output:

b is in fact bigger than a

In the above example, we created two variables , a , and , b, and assigned them the values , 1 , and , 2, respectively.

The phrase in the print , statement actually prints to the console because the condition , b > a , evaluates to True, so the code behind it runs. If the result is not True, the following code will not run.

If we do this instead:

a = 1
b = 2

if a > b
    print("a is in fact bigger than b")

No code is executed and nothing is printed to the console.

Python if.. How else} statements run

if statement runs code only when conditions are met.

What if we also want to run code when the conditions are not met? This is where the "else" part comes into play.

if.. The syntax of else} statement is as follows:

if condition:
    execute statement if condition is True
else:
     execute statement if condition is False

If.. in Python Else statement:

"When the {if} expression evaluates to True, the code after it is run. However, if it evaluates to False, the code after the} else} statement is run"

The else statement is written on a new line after the last line of indented code and is part of the if statement.

The code behind it also needs to be indented with four spaces to indicate that it is part of the {else} clause.

The code following the {else} statement is executed if and only if the} statement is False. If your {if} statement is True and therefore the code runs, the code in the} else} block will never run.

a = 1
b = 2

if a < b:
    print(" b is in fact bigger than a")
else:
    print("a is in fact bigger than b")

Here, the line "a is in fact large than B" after the else statement will never run. The result of the {if} statement before it is True, so only the code after {if} will run.

The else} block operates under the following conditions:

a = 1
b = 2

if a > b:
    print(" a is in fact bigger than b")
else:
    print("b is in fact bigger than a")

Output:

b is in fact bigger than a

Please note that you cannot write any other code between {if} and {else}. If you do, SyntaxError will appear:

if 1 > 2:
   print("1 is bigger than 2")
print("hello world")
else:
   print("1 is less than 2")

Output:

File "<stdin>", line 3
print("hello world")
^
SyntaxError: invalid syntax

How does elif in Python run

What if we want more than two options?

Now, instead of saying, "if the first condition is true, perform this operation, otherwise perform this operation", we say "if this is not true, try this operation, and if all conditions are not true, perform this operation".

elif stands for else, if.

The basic syntax is as follows:

if first_condition:
    execute statement
elif second_condition:
    execute statement
else:
    alternative executable statement if all previous conditions are False

We can use multiple elif statements. This gives us more conditions and more choices.

For example:

x = 1

if x > 10:
    print(" x is greater than 10!")
elif x < 10:
      print("x is less than 10!")
elif x < 20 :
      print("x is less than 20!")
else:
     print("x is equal to 10")

Output:

x is less than 10!

In this example, the if , statement tests a specific condition, the elif , block is the two alternatives, and the else , block is the last solution when all previous conditions are not met.

Note the order in which you write the elif statement.

In the previous example, if you wrote:

x = 1

if x > 10:
    print(" x is greater than 10!")
elif x < 20 :
      print("x is less than 20!")
elif x < 10:
      print("x is less than 10!")
else:
     print("x is equal to 10")

x is less than 20! This line will run because it appears first.

elif} statements make code easier to write. You can use it instead of if Else statement, because the program becomes more and more complex and larger.

If all the {elif} statements do not match and are False, the code after the} else} statement will be run only in this case.

For example, here is the case where the {else} statement will be run:

x = 10

if x > 10:
    print(" x is greater than 10!")
elif x < 10:
      print("x is less than 10!")
elif x > 20 :
      print("x is greater than 20!")
else:
     print("x is equal to 10")

Output:

x is equal to 10

summary

If you are interested in Python and want to learn more about Python and AIoT, solve testing problems, and get started guide to help you solve the puzzles encountered in learning python, we have technical experts here. If you are looking for a job, have just come out of school, or have worked, but often feel that there are many difficulties, feel that you are not proficient enough in Python, and want to continue learning. If you want to change careers, you are afraid that you won't learn, you can join us and get the latest interview materials of Python manufacturers and python crawlers, artificial intelligence and learning materials! Code CSDN

 

this is it!

These are Python if, if The basic principles of else and elif can help you start using conditional statements.

From here, statements can become more advanced and complex.

Conditional statements can be nested in other conditional statements, depending on the problem you want to solve and the logic behind the solution.

Thank you for reading this article. Have a good time!

Keywords: Python Back-end

Added by piyush23424 on Wed, 15 Dec 2021 14:48:52 +0200