Python Decision Making
This is the 9th part of this article series- ‘PYTHON’. In this article we are going to learn about concepts of python decision making and very basics of looping using several types of examples, flow charts, code snippets etc.
So let’s explore it..
For getting the theme of Python, kindly go through my previous article.-
Chapter 1 | Python Properties
Chapter 2 | Python VS Other Languages
Chapter 3 | Environment and Setups
Chapter 4 | Python Basics 1.1
Chapter 5 | Python Basics 1.2
Chapter 6 | Python Strings
Chapter 7 | Python Lists
Chapter 8 | Python Operators
Chapter 9 | Python Decision Making..
Chapter 10 | Python Looping
Chapter 11 | Python Loop Control Statements
Chapter 12 | Python Tuples
Chapter 13 | Python Tuple Operations
Chapter 14 | Python Built-in Tuple Functions
Chapter 15 | Python Dictionary
Chapter 16 | Python Dictionary Operations
Chapter 17 | Python Function
Chapter 18 | Python Function Calling Arguments
Chapter 19 | Python Anonymous Function
Chapter 20 | Python Built-in Anonymous Function
Python Decision Making
Concept of python decision making is same like any other programming language. The logic and the functioning behind every logic also work in same fashion.
In general programming words we can say that decision making is dealing with several types of conditions that used to occurs at the time of program execution. Decision structures evaluate multiple expressions which produce result in from of either True or False.
Types of Decision Making
Like other programming languages Python decision making, is also of these three types..
Let’s explore them one by one with some sets of example.
An if statements generally consist of a Boolean expression (this results either in True or False.). An if statement can consist of one or more than one statements.
Here’s a functioning flow of if statement:
a = 2 b = a * 2 # Decision Making if .. else statement if (a>0): print (b)
if … else Statement
An if else statement also consist of a Boolean expression like 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.
Here’s a functioning flow of if else:
a = 2 b = a * 2 # Decision Making if .. else statement if (a>0): print (b) else: print ("Sorry!")
Now let’s make lil changes in the snippet and check how the other part of the if-else statement (which is else condition) works:
a = -2 b = a * 2 # Decision Making if .. else statement if (a>0): print (b) # This condition will work else: print ("Sorry!")
Take a look at the outputs-
Nested if statement
This can be stated as a combination of a if statements and if else statements. In this you can either use one if / else-if inside another if / else-if statements.
Here’s a functioning flow of nested if-else:
# 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!")
(Make changes to code to get some more exciting outputs!!)
Guidelines from Our 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.
“Keep calm and code python”.
I tried to make it interesting and interactive article and wish you guys going to like that, meanwhile if you have any suggestions then your welcome.
Till the next part, Keep Sharing & Tweaking! 🙂