0% found this document useful (0 votes)
2 views

Python Programming unit 4

This document covers Unit 4 of Python programming, focusing on conditional statements such as if/else, elif, and nested conditions. It explains the syntax, usage, and common errors associated with these statements, emphasizing the importance of indentation and correct operators. Additionally, it provides guidance on avoiding logical and syntax errors in conditional programming.

Uploaded by

bittugarg308
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Programming unit 4

This document covers Unit 4 of Python programming, focusing on conditional statements such as if/else, elif, and nested conditions. It explains the syntax, usage, and common errors associated with these statements, emphasizing the importance of indentation and correct operators. Additionally, it provides guidance on avoiding logical and syntax errors in conditional programming.

Uploaded by

bittugarg308
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PYTHON

PROGRAMMING
Unit 4 Conditional Statements
Contents
• conditional programming like Boolean processing
• if/else statement
• compound if/else statement
• nested conditions
• multi way decision statement
• errors in conditional statements
Python Conditions
• Python supports the usual logical conditions from mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b

• These conditions can be used in several ways, most commonly in "if statements"
and loops.
• An "if statement" is written by using the if keyword.
Conditional Statements
if statement
• The general syntax for a basic if statement, looks something like this.
• if condition: execute statement
• An if statement consists of, the “if” keyword, which starts the if
statement.
• Then comes a condition. A condition can evaluate to either True or
False. Parentheses () surrounding the condition are optional, but they
help improve the readability of the code, when more than one
condition is present.
• Next comes the colon. The colon that separates the condition, from the
executable statement.
• Then comes a new line.
• This new line, has an indentation of 4 spaces, which is a default
requirement in Python. The levels of indentation can increase, based
on the block of code being executed.
• This block of code, will run only if the statement evaluates to be True.
if statement
• In the example here, we created two
variables, “a” and “b”, and assigned
them the values, 1 and 2, respectively.
• Here, the value b is greater than a.
• The phrase in the print statement, only
gets printed, because the condition b >
a, evaluated to True. If it was not True,
The phrase in the print statement will
not get printed.
if….else statement
• An if statement, runs code only when a condition is met. Nothing happens
otherwise.
• What if, we also want code to run, when the condition is not met? That's
where, the else part comes in.
• The syntax of an if..else statement, looks like this.
if condition:
execute statement if condition is True
else:
execute statement if condition is False
• An if..else statement in Python mean "When the if expression evaluates to
True, then execute the code that follows it. But if it evaluates to False, then
run the code that follows the else statement".
• The else statement, is written on a new line after the last line of indented
code, and it can't be written by itself. An else statement is part of an if
statement. The code following next, also needs to be indented with 4 spaces
to show it is part of the else clause.
• The code following the else statement, gets executed, if and only if, the if
statement is False. If the if statement is True, and the code was executed,
then the code in the else block, will never run.
if….else statement
• Here, the line of code following the else
statement, print("a is greater than b "), will never
run.
• The if statement that came before it Is True,
hence only that code has be executed.
• Be aware, that you cannot write any other code
between if and else statements. You'll get a
SyntaxError, if you do so.
elif statement
• elif stands for, else if.
• The elif statement, adds another "decision" branch, to an if-else
condition.
• When you want to evaluate multiple expressions, then you can use
elif statement.
• The basic syntax looks like this.
if first_condition:
execute statement
elif second_condition:
execute statement
else:
alternative executable statement if all previous conditions
are False
• We can use more than one elif statement, in our program. This
gives us more conditions, and more options.
elif statement
• In this example, the if statement, tests
a specific condition,
• That is, whether x is greater than 10.
• the elif blocks are two alternative
conditions, and the else block, is the
last solution, when all the previous
conditions have not been met.
• Be aware of the order in which you
write your elif statements, as it can be
confusing sometimes.
Nested if else statement
• We can have an if…elif…else
statement inside another if…elif…else
statement. This is called nesting in
computer programming. Any number
of these statements can be nested
inside one another. Indentation is the
only way to figure out the level of
nesting. This can get confusing, so it
must be avoided if we can.
Errors in Conditional Statements
• Syntax Errors
• Indentation Errors
Python uses indentation to define the scope of
code blocks. Forgetting to indent or incorrect
indentation will raise an IndentationError.
• Missing Colons
Each conditional statement (if, elif, else) must end
with a colon (:). Forgetting the colon results in a
SyntaxError.
• Incorrect Operators
Using the wrong comparison operator (e.g., using =
instead of == for equality) leads to unexpected
results or errors.
Errors in Conditional Statements
• Logical Errors
• Incorrect Conditions
Writing conditions that don't accurately
represent the logic you want to
implement. This might lead to code
executing in unintended ways.
• Order of Conditions
In if-elif-else chains, the order of
conditions matters. A more specific
condition should come before a more
general condition.
• Missing else clause
Sometimes forgetting an else clause can
lead to unexpected behavior if none of the
previous conditions are met.
Errors in Conditional Statements
• To avoid these errors
• Pay attention to indentation.
• Double-check your comparison operators.
• Test your code with different inputs to ensure it behaves as expected.
• Use a linter to catch potential errors before running the code.

You might also like