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.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (170) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses