Diving Into Python: Chapter 9

This article and the very next will explain the concept of decision-making and looping using flow charts and several examples.

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:

Diving into Python: Chapter 1
Diving into Python: Chapter 2
Diving into Python: Chapter 3
Diving into Python: Chapter 4
Diving into Python: Chapter 5
Diving into Python: Chapter 6
Diving into Python: Chapter 7
Diving into Python: Chapter 8

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

Descision Making if .. else statement

  1. if (a>0):  
  2. print (b)  
Output



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

Dscision Making if .. else statement
  1. if (a>0):  
  2. print (b)  
  3. else:  
  4. 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

Descision Making if .. else statement
  1. if (a>0):  
  2. print (b)  
  3.   
  4. This condition will work  
  5.   
  6. else:  
  7. 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:
  1. score = 190  
  2. if score < 160:  
  3. print ("Score is less than 190")  
  4.   
  5. if score == 180:  
  6. print ("CSK can chase it.")  
  7.   
  8. elif score == 160:  
  9. print ("MI can chase it.")  
  10.   
  11. elif score == 140:  
  12. print ("DD can chase it.")  
  13. elif score < 130:  
  14. print ("Score is less than 130")  
  15. else:  
  16. print ("What's the ideal score to chase??")  
  17.   
  18. 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!