Diving into Python | Chapter 9
Hello guys!
This is the 9th part of the article series about "Python". In this article series, you will learn Python step-by-step and easily.
Getting Theme
For getting the theme of Python, kindly go through my previous articles:
Decision-Making and Looping
This article and the very next will explain the concept of decision-making and looping using flow charts and several examples. So let's explore it.
Decision-Making
The concept of decision-making in Python is the same as in any other programming language. The logic and the functioning behind every logic also works in the same fashion.
In general programming words, we can say that decision-making is dealing with several types of conditions to be determined during program execution.
Decision structures evaluate multiple expressions that produce results in the form of either True or False.
Types of Decision-Making
Python provides the following 3 types of decision-making:
Let's explore them one by one with some examples.
If Statement
An if statement generally consists of a Boolean expression (this results either in True or False.). An if statement can consist of one and only one statement inside it.
Flow
Here's a functioning flow of an if statement:
For Example
a = 2
b = a * 2
Decision Making if .. else statement
if … else Statement
An if-else statement also consists of a Boolean expression, like an if statement, but there is a catch; in this type of decision-making, there is an additional statement, else, that only works when the Boolean expression results in False.
Flow
Here's a functioning flow of an if-else:
Example
a = 2
b = a * 2
Decision Making if .. else statement
- if (a>0):
- print (b)
- else:
- print ("Sorry!")
Output: 4
Now let's make a few changes in the snippet and check how other parts of the if-else statement (that is the else condition) works:
a = -2
b = a * 2
Decision Making if .. else statement
- if (a>0):
- print (b)
-
-
-
- else:
- print ("Sorry!")
Output: Sorry!
Output Window
Nested if statement
This can be stated as a combination of if statements and if-else statements. In this, you can either use one if / else if inside another if / else if statements.
Flow
Here's a functioning flow of a nested if-else:
Example
The following is an example dedicated to Pepsi IPL:
- score = 190
- if score < 160:
- print ("Score is less than 190")
-
- if score == 180:
- print ("CSK can chase it.")
-
- elif score == 160:
- print ("MI can chase it.")
-
- elif score == 140:
- print ("DD can chase it.")
- elif score < 130:
- print ("Score is less than 130")
- else:
- print ("What's the ideal score to chase??")
-
- print ("Find out, @ #IPL!")
Output:
(Make changes to the code to get some more exciting outputs!)
Guidelines from my Side
- Do as much as code you can.
- Code anything you want, the best way to learn
- Don't just study things, try to learn them.
- Work on your concepts
- Work on the fundamentals of any technology or stuff you want to learn.
Moto
“Keep calm and code Python”.
I tried to make this an interesting and interactive article and wish you guys like it, meanwhile if you have any suggestions then your welcome.
Until the next part, keep sharing!