Description

Like some situation comes in out real life in which we have to take some decision as to what we have to do next. In the same way, in programming also we take the decision.

In Python, if else elif statement is used for decision making.

The if else elif statement in Python is used to perfrom the conditonal statement in programs or decision making in program.

If statement in Python.

The if statement is used to perform to check the condition in program and perform the some operation which depends upon condition correct or not.

Syntax of if statement in Python.

if condition:
    body
  • It is used to test the condition and the result is based on the condition.
  • If the condition is true its body will execute otherwise does not execute.

For Example:

n = int(input("Enter the Number : "))
if n > 10:
    print("Number is Greater than 10")

Example 2:

Check given number is positive or negative or zero.

n = int(input("Enter the Number : "))
if n > 0:
   print("Given number is Positive")
if n < 0:
   print("Given number is Negative")
if n == 0:
   print("Given number is Zero")

if else statement in Python.

The if else statement is used for when two condition in perform in program. The if else statement in perfrom two different operation in Python programming language. If condition is true then executed the correct condition statement otherwise else statement.

Syntax of if else statement in Python.

if condition:
    # Execute the body of if.
else:
    # Execute the body of else.
  • It is used to text the condition and gives the output in both situation either condition true or false.
  • If the condition is true body of if will execute otherwise body of else execute.

For Example:

age = int(input("Enter the age : "))
if age > 18:
    print("You can Drive")
else:
    print("You can not Drive")

Check given number is even or odd.

n = int(input("Enter the number : "))
if n % 2 == 0:
    print("Given number is Even")
else:
    print("Given number if Odd")

if elif else statement in Python.

This statement is used for when we have multiple cases to be performed the statement for different condition. In this statement has been cases multiple.

Syntax of if elif else statement.

if condition:
    statement1
elif condition1:
    statement2
elif condition2:
    statement3
else:
    statement4
  • It is used to test the condition.
  • If executes only one condition at a time.
  • The condition which is true first from the top will execute.

For Example:

age = int(input("Enter the Age : "))
if age>18 and age<30:
    print("Your Age is between 18 to 30")
elif age == 18:
    print("Your Age is 18")
else:
    print("Age is below 18 or above 30")

Short hand if statement in Python.

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

a = int(input("Enter the number 1: "))

b = int(input("Enter the number 2: "))

if a > b: print("a is greater than b")

Short hand if else statement in Python.

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

a = int(input("Enter the number 1: "))

b = int(input("Enter the number 2: "))
print(a) if a>b else print(b)

Ternary Operators or Conditional Expression in Python.

For Example:

a = 130
b = 120
print(a) if a > b else print("Both are Equal") if a == b else print(b)

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Everything about of Loops in Python | For loop | While loop | Nested Loop

A loop is used for iterate the sequence. It is used to iterate List, String, Dictionary, Tuple. Iterating over a sequence is called traversal.

Function | Docstring | Lambda | Kwargs in Python

In Python programming, a function is a block of statements that makes our code readable. If we do that function in the program, then we do not have to write the same statement of code again and again.

What is Python? How Python is interpreted? What are the tools that help to find bugs or perform static analysis? What are Python Decorators?

Python Program is a gerneral purpose of high level, intereted, interative and object-oriented scription programming language developed by Guido Van Rossum in  1991.