Python Tutorial for Beginners Session 6 Decision Making in Python. To Learn or Teach Linux visit www.theskillpedia.com, A Marketplace for Learners and Trainers.
Class XI Python
Session 6 – Decision Making in Python
Decision Making Structures
We can take a decision if we have some condition as input with one or more alternative.
For example I shall go for a walk if the weather is good.
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true/
Optionally, other statements to be executed if the condition is determined to be false.
Decision Making Structure
General form of a typical decision making structure is:
Decision Making Structures – Conditions
Python assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
Python provides following decision making statements.
The if statement contains a logical expression using which data is compared and a decision is made based on the result of the comparison.
If the boolean expression evaluates to true, then the block of statement(s) inside the if statement will be executed.
If boolean expression evaluates to false, then the first set of code after the end of the if statement(s) will be executed.
if statements Flow Diagram
if statements Example
An else statement can be combined with an if statement.
An else statement contains the block of code that executes if the conditional expression resolves to 0 or a false value.
The else statement is an optional statement and there could be at most only one else statement following if.
if…else statements Flow Diagram
if…else statements Example
The elif Statement
The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true.
The elif statement is optional. There can be an arbitrary number of elif statements following an if.
The syntax of the if…elif statement is:
The elif Statement
Core Python does not provide switch or case statements as in other languages, but we can use if..elif…statements to simulate switch case.
The elif Statement Example
Python nested IF statements
In a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct. Syntax:
Python nested IF statements Example
Single Statement Suites
If the suite of an if clause consists only of a single line, it may go on the same line as the header statement. For Example :
For More Tutorials Subscribe to this Channel
For Free Online Learning Visit www.hometutor.net.in
For Corporate Training Requirements anywhere in the world,
Mail me at firstname.lastname@example.org or
Call me at +91 9312406920
Python (Programming Language), Computer Science (Field Of Study), Programming Language (Software Genre), if statement in python, if-else statement in python, if-elif statement in python, nested if in python