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

python8

The document provides an overview of conditional statements in Python, explaining how they allow execution of code blocks based on specific conditions. It covers the importance of indentation, common mistakes, and the syntax for if-else statements, including examples of correct and incorrect code. Additionally, it emphasizes that no code should be placed between an if statement and its corresponding else statement.

Uploaded by

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

python8

The document provides an overview of conditional statements in Python, explaining how they allow execution of code blocks based on specific conditions. It covers the importance of indentation, common mistakes, and the syntax for if-else statements, including examples of correct and incorrect code. Additionally, it emphasizes that no code should be placed between an if statement and its corresponding else statement.

Uploaded by

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

7/27/24, 9:00 PM Revolutionizing the Job Market | NxtWave

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=36b50c7e-806a-4834-859f-1c031bdf9942&s_id=375ee542… 1/6
7/27/24, 9:00 PM Revolutionizing the Job Market | NxtWave

Conditional Statements

Block of Code

A sequence of instructions are called block of code.


Python executes code in a sequence.

Condition

An expression that results in either

True or False

Examples

i. 2 < 3
ii. a == b
iii. True

Conditional Statement

Conditional Statement allows you to execute a block of code only when a


specific condition is

True

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=36b50c7e-806a-4834-859f-1c031bdf9942&s_id=375ee542… 2/6
7/27/24, 9:00 PM Revolutionizing the Job Market | NxtWave

Conditional Block

Block of code which executes only if a condition is

True is called Conditional Block.

Indentation

Space(s) in front of the conditional block is called indentation.

Indentation(spacing) is used to identify Conditional Block.

Standard practice is to use four spaces for indentation.

Possible Mistakes

Each statement inside a conditional block should have the same indentation
(spacing).

Wrong Code

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=36b50c7e-806a-4834-859f-1c031bdf9942&s_id=375ee542… 3/6
7/27/24, 9:00 PM Revolutionizing the Job Market | NxtWave

PYTHON

1 if True:
2 print("If Block")
3 print("Inside If")

Output

IndentationError: unexpected indent

Correct Code
PYTHON

1 if True:
2 print("If Block")
3 print("Inside If")

If - Else Syntax

When If - Else conditional statement is used, Else block of code executes if


the condition is

False

Using If-Else

Code

PYTHON
1 a = int(input())
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=36b50c7e-806a-4834-859f-1c031bdf9942&s_id=375ee542… 4/6
7/27/24, 9:00 PM Revolutionizing the Job Market | NxtWave
2 if a > 0:
3 print("Positive")
4 else:
5 print("Not Positive")
6 print("End")

Input

Output

Positive
End

Possible Mistakes in If-Else

Else can only be used along with if condition. It is written below if


conditional block

Code
PYTHON

1 if False:
2 print("If Block")
3 print("After If")
4 else:
5 print("Else Block")
6 print("After Else")

Output

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=36b50c7e-806a-4834-859f-1c031bdf9942&s_id=375ee542… 5/6
7/27/24, 9:00 PM Revolutionizing the Job Market | NxtWave

SyntaxError: invalid syntax

Warning

Note: No code is allowed in between if conditional block and else


statement.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=36b50c7e-806a-4834-859f-1c031bdf9942&s_id=375ee542… 6/6

You might also like