Saturday, 16 February 2019

Conditional Statements in Python

Conditional statement in python can be used to take decisions based on certain conditions in your program.

In this blog we will describe you four most commonly used forms of conditionals.We will encounter more of these forms and variations in your professional programming life.
  • If Then
  • If Then Else
  • If Then Else If Else
  • Nested If Then
If Then :

As the name indicates it is quite simple.If a certain condition is satisfied, then perform some action.

Python Code:
accountBalance = 0
if accountBalance < 1000
  print("Close Account!")

Output:
Close Account!

In the example above, we are checking account balance in the if statement. If the account balance comes to less than 1000, we are printing a line to close the account.

In a real world scenario this can be the case to check an account has minimum balance.

If Then Else:

An extra else statement can be added to an if condition, to execute what should happen if the if condition is not satisfied.

Python Code:
accountBalance = 1001
if accountBalance < 1000:
  print("Close Account!")
else:
  print("We love having you with us!")

Output:
We love having you with us!

In the above example, if the account balance is >=1000, we are printing a warm message.

If Then Else If Else:

If a primary if condition is not satisfied, we can add an Else if statement in between to check another condition if required.

Python Code:
accountBalance = 1000002
if accountBalance < 1000:
    print("Close Account!")
elif accountBalance > 1000000:
   print("We really love having you with us. Please find a Europe tour cruise package in your mailbox!")
else:
   print("We love having you with us!")


Output:
We really love having you with us. Please find a Europe tour cruise package in your mailbox!

In the above example, we added an else if condition.
For high valued customers who have more than a large threshold of account balance with us, we printed a separate message.

Nested If Then:

We can nest multiple If Then statements. Be careful when using this as it can become a mess to read . You can use && or || operators to avoid it in some scenarios.

Python Code:
accountBalance = 600
if accountBalance < 1000:
    if accountBalance < 500:
        print("Close Account!")
      else:
        print("You better maintain a minimum balance Pal! You got 5 days time.")
elif accountBalance > 1000000:
 print("We really love having you with us.Please find a Europe tour cruise package in your mailbox!")
else:
 print("We love having with us!")

Output:
 You better maintain a minimum balance Pal! You got 5 days time.

In the above example, we added a nested if statement where we are further checking if account balance is less than 500. If it is not, then we are giving a friendly and warm warning message.

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)