Python for Hacking | Part 4 | concept of conditional statements and recursion

In this post, we are going to discuss the concept of Conditional statements and Recursion in Python. We will be going to discuss both the topics in detail and will know exactly how and where to use them properly.

Conditional statements are basically used to check whether a condition is true or not. If a statement is true, the piece of code behind the true part is executed and if false, the part of the program after the false statement is executed.

Conditional statements are very useful when we have a set of different conditions and we have to check that which of those statements are true and they change the behavior of the program accordingly.

The simplest form is the if statement:

x =2
if x > 1:
print('x is greater than 1')

In the above example, we are checking the condition that whether the value of x is greater than 1 or not. If the value of x is greater than 1, we will get 'x is greater than 1' on our screen. If it's not greater than 1, the program will end without doing anything. Since in the above example, we have the value of x as 2 which is greater than 1 so, we will get the print statement on our screen.

NOTE: In the above example, see the logical expression (x>1). Python uses the following logical conditions which are the same as in the Mathematics:
• equal to: a == b
• not equal to: a!=b
• less than: a < b
• less than or equal to: a <= b
• greater than: a > b
• greater than or equal to: a>=b

Now, let's discuss each and every logical condition in detail:

1. equal to: 'a == b'

This operator '==' is used to check whether one entity is equal to another entity or not. Don't get confused with the '=' operator which is generally used in mathematics to check the equality condition.
In Python or many another programming languages, the equality condition is checked by the double equal sign '=='. Single equal sign '=' in Python is used to assign a value to an entity (for eg. a variable).
If you used 'a = b' to check the equality condition, the Python interpreter will give you an error.

2. not equal to: 'a != b'

This operator '!=' is used to check whether an entity is not equal to another entity. If an entity is not equal to another entity, then the part of the code followed by the 'true' condition will be followed. It is the opposite of the 'equal to' operator.

3. less than: 'a < b'

This operator '<' is used to check whether an entity is smaller than another entity. If the entity is smaller then, the part of the code which is meant to be run for the true case will be executed.

4. less than or equal to: 'a <= b'

This operator '<=' is used to check whether an operator is less than or equal to another operator or not. The true part of the code, in this case, will be executed when the first variable is either equal to or less than the second operator. Remember that there is no such thing as equal to or less than '=<'. If you will use it, you will get an error.

5. greater than: 'a > b'

This operator '>' is used to check whether an entity is greater than another entity or not. If the entity is greater then, the part of the code which is meant to be run for the true case will be executed.

6. greater than or equal to: 'a >= b'

This operator '>=' is used to check whether an operator is greater than or equal to another operator or not. The true part of the code, in this case, will be executed when the first variable is either equal to or greater than the second operator. Remember that there is no such thing as equal to or greater than '=>'. If you will use it, you will get an error.

elif, else

* else

'else' is used when we have more than one possibilities and we have to determine which part of the code will be going to execute depending on the given condition to be tested.

for example,

x = 5
if x>1:
print('x is greater than 1')
else:
print('x is either equal or lesser than 1')

In the above example, we have initialized x to 5. Then we have to check whether x is greater than 1 or not. There are two possibilities of this event that either x will be less than 1 or x will be greater than 1, so we have used the keyword 'else' along with 'if' statement to check this condition.

* Elif

'elif' is used when we have more than two possibilities for a given condition and we need more than two branches.

For example,

x=2
y=4
if x < y:
print('x is less than y')
elif x >y:
print('x is greater than y')
else:
print('x and y are equal')

In the above program, we have initialized x to 2 and y to 4. Here, we have to check three conditions. Remember, in order to check two conditions we can easily do it by using the 'else' keyword but in order to check more than three conditions, we will need a new keyword called 'elif'. 'elif' is basically an abbreviation for else-if.

In the above example, we have to check whether x is greater or smaller or equal to y. That's why we have used the keyword 'elif'. Look carefully, the use of 'elif' in the above example. We can use 'elif' any number of times in the program depending on the number of possibilities of the condition to be tested. But, remember that the condition must be started with an 'if' keyword and must end with an 'else' keyword. You can use any number of 'elif' keyword between them. The statements in the example look like a chain of if-else-if statements, that's why they are also known as chained conditionals

Nested conditionals

One conditional statement can be nested within the other. It means we can easily start one conditional statement inside a conditional. This is called nesting or nesting conditional statements.

For example,

x = 1
y = 2
if x == y:
print('X is equal to Y')
else:
if x > y:
print('X is greater than Y')
else:
prinr('X is less than Y')

In the above example, we have nested one 'if-else' conditional statement inside an 'if' conditional statement.

Recursion

We have already discussed and learned about the Functions and we talked about declaring, defining and calling them in a program. We also know that a function can call another function in a program.

But, do you know that a Function can call itself any number of time in the program. This property of a Function to call itself in a program is called Recursion.

For example,

def printN(n):
if n == 0:
print('Terminate')
else:
print(n)
printN(n-1)

printN(2)

In the above example, we have done recursion by calling the function printN() within the function itself (see in the second last line).

In this post, we have learned about conditional statements, how to use them, different types of conditional statements and their usage. We have also learned about Recursion and it's practical usage with the help of an example.

If you have any doubt, you can ask in the comment section.

Share:

Email Subscription

Delivered by FeedBurner

Name

Email *

Message *

Featured Post

How hackers can hack your car?

How hackers can hack your car? For a considerable length of time, AUTOMAKERS and programmers have thought about a smart assault that ... 