Nested if function python

Nested if function python

Nested-if Statement in Python : Nested-if statement is also the most important and easiest concept of control flow statements. This statement is very helpful, while or when we need to make a decision ( when we have more than one condition to check ) based on our problem.

A nested if is a flow control statement that’s the target of another if-statement. By nested-if statements, we mean to use an if-statement inside another if-statement. In Python, it is possible to place one if-statement inside the other if-statement.

We will learn about these flow control statements in detail.

Also, there are other control statements, These are given below:-

Let’s discuss all above statements in detail one by one:- If, if-else statements,if-elif, and if-elif-else we have discussed already, followed by this article ( if-statement , if-else statement,if-elif,if-elif-else), now we discuss nested-if:

(i). Nested-if statement : The nested-if statement’s execution is different from our previous control statements that we have discussed so far. The nested-if statement can be used inside the other if statement. This flow control statement is very helpful when we have to deal with a large number of conditions.

#nested-if statement example x = 21 if(x==21): #first if if(x > 20): #second if print(x) # output # 21

The above given example is the easiest example to understand the nested-if statement.

Читайте также:  Html script inf это

Note : Here we can also use anything else at the last. If both of the conditions inside if-statements remain false, then the statement inside else : block will execute. Below the given example of a nested if-else statement in python:

Example 1: Example of nested if-else statement in python.

#nested-if statement example x = 18 if(x==23): print(x) if(x==22): print(x) else: print("Inside Else Block") else: print("Condition get False, Hence it didn't enter if condition") # output # Condition get False, Hence it didn't enter if condition

Explanation :

  • In this example, we’re dealing with multiple if statements i.e. nested-if statements.
  • We are only allowed to enter the if statement, if the condition is True else we directly jump to the elif or else statement block.
  • Here in this example, our condition is false as it is checking for the value of x is equal to 23 which is not True. The value of x was 18.
  • And also there is no, elif block hence the flow control is directly jumped to else block and the result is returned as ‘Condition get False, Hence it didn’t enter if condition’.

Example 2: Write a program to display the greatest among three numbers using nested if statement.

a = 40 b = 80 c = 70 if(a>b): if(a>c): print("a is greater") if(b>a): if(b>c): print("b is greatest") if(c>a): if(c>b): print("c is greatest") # output # b is greatest

Explanation :

  • Here in this example, we are checking the greatest among three numbers using nested if condition.
  • We have a , b and c with initialised value 40, 80 and 70 respectively.
  • To compare each variable value we are using a nested if control statement.
  • Our approach is that suppose number a is greater than b means to be greatest it should also be greater than c hence we are checking both by using another if condition.
  • Hence the final output is obtained.
  • The best approach to find the greatest amount three number is the if-elif-else statement.

Other Control Flow Statements:

Источник

Python if. else Statement

In computer programming, we use the if statement to run a block code only when a certain condition is met.

For example, assigning grades (A, B, C) based on marks obtained by a student.

  1. if the percentage is above 90, assign grade A
  2. if the percentage is above 75, assign grade B
  3. if the percentage is above 65, assign grade C

In Python, there are three forms of the if. else statement.

1. Python if statement

The syntax of if statement in Python is:

if condition: # body of if statement

The if statement evaluates condition .

How if statement works in Python

  1. If condition is evaluated to True , the code inside the body of if is executed.
  2. If condition is evaluated to False , the code inside the body of if is skipped.

Example 1: Python if Statement

number = 10 # check if number is greater than 0 if number > 0: print('Number is positive.') print('The if statement is easy')
Number is positive. The if statement is easy

In the above example, we have created a variable named number . Notice the test condition,

Here, since number is greater than 0, the condition evaluates True .

If we change the value of variable to a negative integer. Let’s say -5.

Now, when we run the program, the output will be:

This is because the value of number is less than 0. Hence, the condition evaluates to False . And, the body of if block is skipped.

2. Python if. else Statement

An if statement can have an optional else clause.

The syntax of if. else statement is:

if condition: # block of code if condition is True else: # block of code if condition is False

The if. else statement evaluates the given condition :

If the condition evaluates to True ,

If the condition evaluates to False ,

How if. else statement works in Python

  • the code inside else is executed
  • the code inside if is skipped

Example 2. Python if. else Statement

number = 10 if number > 0: print('Positive number') else: print('Negative number') print('This statement is always executed')
Positive number This statement is always executed

In the above example, we have created a variable named number . Notice the test condition,

Since the value of number is 10, the test condition evaluates to True . Hence code inside the body of if is executed.

If we change the value of variable to a negative integer. Let’s say -5.

Now if we run the program, the output will be:

Number is negative. This statement is always executed.

Here, the test condition evaluates to False . Hence code inside the body of else is executed.

3. Python if. elif. else Statement

The if. else statement is used to execute a block of code among two alternatives.

However, if we need to make a choice between more than two alternatives, then we use the if. elif. else statement.

The syntax of the if. elif. else statement is:

if condition1: # code block 1 elif condition2: # code block 2 else: # code block 3

How if. elif statement works in Python

  1. If condition1 evaluates to true , code block 1 is executed.
  2. If condition1 evaluates to false , then condition2 is evaluated.
    1. If condition2 is true , code block 2 is executed.
    2. If condition2 is false , code block 3 is executed.

Example 3: Python if. elif. else Statement

number = 0 if number > 0: print("Positive number") elif number == 0: print('Zero') else: print('Negative number') print('This statement is always executed')
Zero This statement is always executed

In the above example, we have created a variable named number with the value 0. Here, we have two condition expressions:

Here, both the conditions evaluate to False . Hence the statement inside the body of else is executed.

Python Nested if statements

We can also use an if statement inside of an if statement. This is known as a nested if statement.

The syntax of nested if statement is:

# outer if statement if condition1: # statement(s) # inner if statement if condition2: # statement(s)
  • We can add else and elif statements to the inner if statement as required.
  • We can also insert inner if statement inside the outer else or elif statements(if they exist)
  • We can nest multiple layers of if statements.

Example 4: Python Nested if Statement

number = 5 # outer if statement if (number >= 0): # inner if statement if number == 0: print('Number is 0') # inner else statement else: print('Number is positive') # outer else statement else: print('Number is negative') # Output: Number is positive

In the above example, we have used a nested if statement to check whether the given number is positive, negative, or 0.

Table of Contents

Источник

Python If . Else

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in «if statements» and loops.

An «if statement» is written by using the if keyword.

Example

In this example we use two variables, a and b , which are used as part of the if statement to test whether b is greater than a . As a is 33 , and b is 200 , we know that 200 is greater than 33, and so we print to screen that «b is greater than a».

Indentation

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Example

If statement, without indentation (will raise an error):

Elif

The elif keyword is Python’s way of saying «if the previous conditions were not true, then try this condition».

Example

In this example a is equal to b , so the first condition is not true, but the elif condition is true, so we print to screen that «a and b are equal».

Else

The else keyword catches anything which isn’t caught by the preceding conditions.

Example

a = 200
b = 33
if b > a:
print(«b is greater than a»)
elif a == b:
print(«a and b are equal»)
else:
print(«a is greater than b»)

In this example a is greater than b , so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that «a is greater than b».

You can also have an else without the elif :

Example

Short Hand If

If you have only one statement to execute, you can put it on the same line as the if statement.

Example

Short Hand If . Else

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

Example

One line if else statement:

This technique is known as Ternary Operators, or Conditional Expressions.

You can also have multiple else statements on the same line:

Example

One line if else statement, with 3 conditions:

And

The and keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b , AND if c is greater than a :

Or

The or keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b , OR if a is greater than c :

Not

The not keyword is a logical operator, and is used to reverse the result of the conditional statement:

Example

Test if a is NOT greater than b :

Nested If

You can have if statements inside if statements, this is called nested if statements.

Example

if x > 10:
print(«Above ten,»)
if x > 20:
print(«and also above 20!»)
else:
print(«but not above 20.»)

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

Источник

Оцените статью