 console, studio, music @ Pixabay

# If Statement Python

## Python Conditional Statements: IF…Else, ELIF & Switch Case

Depending on whether a specific Boolean constraint evaluates to true or false, a Conditional Statement in Python will perform different computations or actions. In Python, conditional statements are handled through the use of IF statements.

In this tutorial, we’ll go over how to use conditional statements in Python programming.

## What is Python If Statement?

The if statement in Python is used to make decisions in various situations. A body of code is contained within the if statement, and it is only executed when the condition specified in the statement is true. The optional statement is executed if the condition is false and contains some code to handle the else condition.

You use the if-else statement in Python when you want to justify one condition while another condition is not true at the same time.

Python if Statement Syntax:

```if expression
Statement
else
Statement
Python if…else Flowchart``` Let’s see an example of Python if else Statement: This code is editable. Click Run to Execute

1#
2#Example file for working with a conditional statement
3#
4def main():
5 x,y =2,8
6
7  if(x < y):
8   st= “x is less than y”
9. print(st)
10
11if __name__ == “__main__”:
12 main()

Line 5 of code: We define two variables x and y = 2, 8 respectively.
7th line of code: The if statement in Python checks for the presence of the condition XY, which is true in this instance.
Line 8 of the code: This is the value of the variable st: “x is less than y.”
Code Line 9: The statement print st will print the value of the variable st, which is “x is less than y,” as shown in the example.

## What happens when “if condition” does not meet

During this step, we’ll look at what happens when the if condition in Python is not satisfied.

Conditional Statements in Python: IF, ELIF, and ELSE IF with nested IF
Line 5 of code: We define two variables x and y = 8, 4 respectively.
Line 7 of code: The if Statement in Python checks for the presence of the condition XY, which is False in this instance.
Line 8 of the code: There is no indication that the variable st is set to “x is less than y.”
Line 9 of the code: The line print st – is attempting to print the value of a variable that was never declared in the first place. As a result, we receive an error.

## How to use “else condition”

When you have to decide on one statement based on another, the “else condition” is frequently used. If one condition fails, there should be another condition that will allow the statement or logic to stand on its own two feet again.

1
`#`
2
`#Example file for working with conditional statement`
3
`#`
4
`1`
5
`#`
6
`2`
7
`#Example file for working with conditional statement`
8
`3`
9
`#`
10
`4`
11
`def main():`
12
`5`
13
`    x,y =8,4`
14
`6`
15

16
`7`
17
`    if(x < y):`
18
`8`
19
`        st= "x is less than y"`
20
`9`
21
`    else:`
22
`10`
23
`        st= "x is greater than y"`
24
`11`
25
`    print (st)`
26
`12`
27

28
`13`
29
`if __name__ == "__main__":`
30
`14`
31
`    main()`
32
`def main():`
33
`    x,y =8,4`
34

35
`    if(x < y):`
36
`        st= "x is less than y"`
37
`    else:`
38
`        st= "x is greater than y"`
39
`    print (st)`
40

41
`if __name__ == "__main__":`
42
`    main()`

Line 5 of code: We define two variables x and y = 8, 4 respectively.
Python’s if statement tests for the presence of the condition XY, which is false in this case. Code Line 7:
Code Line 9: The flow of programme control is transferred to the else condition in the code.
“x is greater than y” is set in the variable st in line 10 of the programme.
Line 11 of code: The print st command will print the value of the variable st, which is “x is greater than y.”

## When “else condition” does not work

There may be several instances in which your “else condition” does not produce the results you expect. It will print out the incorrect result because there is a logical error in the programme. The majority of the time, this occurs when you have to justify more than two statements or conditions in a single programming language.

This concept will be more easily grasped if you consider an example.

In this case, both variables are the same (8,8), and the program’s output is “x is greater than y,” which is erroneously stated. This is because it checks the first condition (known as the if condition in Python), and if it fails, it prints out the second condition (known as the else condition in Python). In the next step, we’ll look at how we can correct this oversight.#
#Example file for working with the conditional statement
#
def main():
x,y =8,8

if(x < y):
st= “x is less than y”
else:
st= “x is greater than y”
print(st)

if __name__ == “__main__”:
main()

## How to use the “elif” condition

The “elif” statement can be used to correct the error that was caused by the “else” condition previously. When you use the “elif” condition, you are instructing the programme to print out the third condition of possibility if the first two conditions are incorrect or fail.

1
`#`
2
`#Example file for working with conditional statement`
3
`#`
4
`def main():`
5
`    x,y =8,8`
6

7
`    if(x < y):`
8
`        st= "x is less than y"`
9

10
`    elif (x == y):`
11
`        st= "x is same as y"`
12

13
`    else:`
14
`        st="x is greater than y"`
15
`    print(st)`
16

17
`if __name__ == "__main__":`
18
`    main()`

Code Line 5: We define two variables, x and y, which are both equal to eight.
Line 7 of code: The if statement checks for the presence of condition XY, which is false in this instance.
Line 10 of code: In the else if condition, the flow of programme control is initiated. It determines whether x==y, which is the case.
Line 11 of code: The value of the variable st is “x is the same as y.”
Code Line 15: The if Statement is no longer in effect, and the flow of programme control continues (it will not get to the else statement). Additionally, print the variable st. The result is “x and y are the same,” which is correct.
How to run a conditional statement with the least amount of code
In this step, we will see how we can condense the conditional statement to make it easier to read. As an alternative to executing code for each condition individually, we can combine them into a single piece of code.

## How to execute the conditional statement with minimal code

Line 2 of code: We define two variables x and y = 10, 8 respectively.
Code Line 3: If XY is less than or equal to y, the variable st is set to “x is less than or equal to y,” otherwise it is set to “x is greater than or equal to y.” The st variable in this x>y relationship is set to “x is greater than or equal to y.”
In code line 4, the variable st is printed and the appropriate output is produced
As opposed to having to write long blocks of code for conditional statements, Python allows you to write shortcode and to the point instead.
Python if there is a nesting of Statement
The following example demonstrates how to use nested if statements. Statement Python

1

2
`def main():`
3
`    x,y = 10,8`
4
`    st = "x is less than y" if (x < y) else "x is greater than or equal to y"`
5
`    print(st)`
6

7
`if __name__ == "__main__":`
8
`    main()`

Uncomment Line 2 in the preceding code and comment Line 3 to rerun the code.

Python’s Switch Case Statement is an example of a conditional statement.

## Switch Case Statement in Python

When a switch statement is used, the value of a variable is compared to the values specified in case statements, which is a multiway branch statement.

The switch statement is not available in the Python programming language.

Switch Case in Python is implemented through the use of dictionary mapping.

if statements are used to handle conditional statements in Python, and we have seen various other ways to use conditional statements, such as the Python if else statement over on this website.

Example

```function(argument){
switch(argument) {
case 0:
return "This is Case Zero";
case 1:
return " This is Case One";
case 2:
return " This is Case Two ";
default:
return "nothing";
};
};

```

For the above Switch case in Python

```def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")

if __name__ == "__main__":
argument = 1
print (SwitchExample(argument))
```

Python 2 Example

The above codes are Python 3 examples, If you want to run in Python 2 please consider the following code.

```# If Statement
#Example file for working with conditional statement
#
def main():
x,y =2,8

if(x < y):
st= "x is less than y"
print st

if __name__ == "__main__":
main()

# How to use "else condition"
#Example file for working with conditional statement
#
def main():
x,y =8,4

if(x < y):
st= "x is less than y"
else:
st= "x is greater than y"
print st

if __name__ == "__main__":
main()

# When "else condition" does not work
#Example file for working with conditional statement
#
def main():
x,y =8,8

if(x < y):
st= "x is less than y"
else:
st= "x is greater than y"
print st

if __name__ == "__main__":
main()

# How to use "elif" condition
#Example file for working with conditional statement
#
def main():
x,y =8,8

if(x < y):
st= "x is less than y"

elif (x == y):
st= "x is same as y"

else:
st="x is greater than y"
print st

if __name__ == "__main__":
main()

# How to execute conditional statement with minimal code
def main():
x,y = 10,8
st = "x is less than y" if (x < y) else "x is greater than or equal to y"
print st

if __name__ == "__main__":
main()

# Nested IF Statement
total = 100
#country = "US"
country = "AU"
if country == "US":
if total <= 50:
print "Shipping Cost is  \$50"
elif total <= 100:
print "Shipping Cost is \$25"
elif total <= 150:
print "Shipping Costs \$5"
else:
print "FREE"
if country == "AU":
if total <= 50:
print "Shipping Cost is  \$100"
else:
print "FREE"

#Switch Statement
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")

if __name__ == "__main__":
argument = 1
print SwitchExample(argument)```

This statement is used when one of the conditions is true or false and you want to print the result when that condition occurs.
“else condition” is used when you want to print out a statement if your first condition fails to meet the requirement, but your second condition does.
“if condition” – This condition is used when there is a third possibility for the result. You can use multiple elif conditions in your code to check for the 4th, 5th, and 6th possible outcomes.
By declaring all conditional statements in a single statement and running the code, we can use the least amount of code to execute conditional statements.
Python If Statements can be nested, they should be.