What are Python Conditional Statements? Day 4

Published by Anshul Verma on

Python programming language is extremely easy to learn. The lesser the lines of codes and reduced is the complexity. It provides readability and improved efficiency. Concepts like control statements and conditional statements help in clarity when a user wants to test multiple expressions. In this article, we will discuss the concept of if and else in Python. Following are the topics discussed in this article :

What Are Python Conditions?

Conditions in python are logical conditions which we use in if and else statements. Following are some logical conditions in python.

  1. Equal to – x == y
  2. Not equal to – x != y
  3. Less than – x < y
  4. Less than or equal to – x <= y
  5. Greater than – x > y
  6. Greater than or equal to – x >= y

These are the conditions we often use while declaring a test expression for if and else statements in python.

What Is ‘if’ And ‘else’ In Python?

An if statement is used to test an expression and execute certain statements accordingly. A program can have many if statements present in a program. The subsequent or the penultimate statement that follows is an else statement, which executes a statement in case all the test expressions in the program are false. To understand the control flow of an if and else statement, take a look at the flow chart of how the execution takes place.

end of flow chart, take the next step

Flow Control And Syntax.

If you look carefully, you can figure out that the execution starts with the test expression, when it is true the execution will enter the body of if and execute all the statements. But when the expression is false, the execution will move to the body of else and execute the statements in the else block.

https://intellipaat.com/mediaFiles/2019/02/python14.png

Just like an if statement, we can add an else if or elif statement in python to test out more expressions in order to optimize the code and increase the efficiency of our code. Lets take a look at the syntax to understand how we can declare a if else statement block in python.

Indentation Matters in Python:

Python cares about indentation. A code block is a series of lines of code that is indented uniformly. Python determines where a code block begins and ends by this indentation.

Other languages use parentheses or semi-colons to mark the beginning or end of a code block.

Indenting your code uniformly is required in Python. If you do not do this correctly, your code may have a hard time diagnosing issues.

One other word of warning. Do not mix tabs and spaces. IDLE will complain if you do and your code may have hard to diagnose issues. The Python style guide (PEP8) recommends using 4 spaces to indent a code block. You can indent your code any number of spaces as long as it is consistent. However, 4 spaces are usually recommended.

This code would be better if you handled both conditions though. Let’s find out how to do that next!

Syntax:

if (test expression) :

# statement to be executed

elif (test expression 2):

# statements of elif block

else:

# final statement

  1. To understand how we can use if else in python lets take a look at a program to find out if a number is even or odd.

To check the number, we are using a logical condition in the test expression, which will check the remainder and if the remainder is 0 it will print the statement in the if block. Otherwise it will go to the next expression which is the elif statement. Since 10 divided by 2 leaves the remainder as 0, the output is ‘even number’.

Shorthand ‘if’ And ‘else’:

If you have only one statement to execute you can put it in a single line.

if a > b: print(“greater “)

Short hand else :

print(“greater”) if a > b else print(“smaller”)

You can use multiple else statements as well.

print(“greater”) if a > b else print(“equal”)  if a ==b else print(“smaller”)

Using logical operators in if else :

a = 10

b = 20

if a > b and a > 10 :

print(“greater”)

elif a < b or a < 10:

print(“smaller”)

else:

print(“not a valid number”

In this program we have used multiple test expression inside if statements. This is how we can use nested if else in python.

Nested If-Else

https://intellipaat.com/mediaFiles/2019/02/python15.jpg

A few examples :

Logical Operators :

Logical operators allow you to chain multiple expressions together using special keywords.

Here are the three logical operators that Python supports:

  • and – Only True if both the operands are true
  • or – True if either of the operands are true
  • not – True if the operand is false

AND Operator :

For a condition to be true, both the operands need to be true. In this example, the if condition implies that only if the user is female and is over 40 years old, then the condition is found to be true. In this case, the condition returns false despite the gender being female as the criteria for age is not met where age needs to be above forty. So, it moves on to the next block but the condition is false again as it does not meet the gender criteria. Hence, it proceeds to the else block which is rendered true since all the preceding conditions are false.

OR Operator :

The OR operator is less strict as compared to the AND operator because unlike the AND operator where both operands need to be true to return true, an OR statement requires only one operand to be true to return true. In this example, if the weather is either hot or dry, it would print “Summer”. Otherwise, if it is either cold or chilly, it would print “Winter”.

NOT Operator :

A NOT operator is used to reverse the result where if the result is true, then it returns false and vice versa.

Python Membership and Identity Operators | in, not in, is, is not :

There are some special operators that you can use in conditional expressions.

  • is – True when the operands are identical (i.e. have the same id)
  • is not – True when the operands are not identical
  • in – True when the value is in the sequence
  • not in – True when the value is not in the sequence

The first two are used for testing identity. You want to know if an item refers to the same object (or id) or not. The last two are for checking membership, which means you want to know if an item is in a sequence or not. Sequences in Python refer to such things as lists, strings, tuples, etc.

Let us take them one-by-one :

  1. ‘is’ operator – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. Example :

  1. ‘is not’ operator – Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. Example :

  1. in operator : The ‘in’ operator is used to check if a value exists in a sequence or not. Evaluates to true if it finds a variable in the specified sequence and false otherwise. Example :

  1. ‘not in’ operator- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. Example :

That’s all for today. In our next blog we’ll be learning loops with many interesting problems….

Till then,

Stay Safe, Stay Happy & Keep Coding

IF YOU WANT TO KNOW ABOUt DATA SCIENCE click here.

CONNECT WITH THE AUTHOR ON LINKEDIN.

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT LEARNING.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US