0% found this document useful (0 votes)
3 views5 pages

Control Statements in Python

This document covers control statements and native data types in Python, focusing on decision-making statements such as if, if-else, if-elif-else, and nested if-else statements. It explains how these statements control the flow of execution based on conditions and provides syntax and examples for each type. Additionally, it introduces the match statement for pattern matching, similar to switch-case statements in other languages.

Uploaded by

btechcse06vivek
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Control Statements in Python

This document covers control statements and native data types in Python, focusing on decision-making statements such as if, if-else, if-elif-else, and nested if-else statements. It explains how these statements control the flow of execution based on conditions and provides syntax and examples for each type. Additionally, it introduces the match statement for pattern matching, similar to switch-case statements in other languages.

Uploaded by

btechcse06vivek
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit - 2 :Control Statements and

Native Data types in Python

Introduction to Python Decision making


statements
A decision making statement in Python is used to control the flow of execution of a program
by executing specific blocks of code depending on whether a condition is True or False.

Topic-1: if, if…elif and else Statement


1️. if Statement

An if statement in Python is used to execute a block of code only when a given condition is
true.

Syntax:

if condition:
statement

Example:

age = 18

if age >= 18:


print("You are eligible to vote")

Flow Diagram:
2️. if-else Statement

An if-else statement in Python is used to execute one block of code when a condition is
true and another block when the condition is false.

Syntax:

if condition:
statement1
else:
statement2

Example:

num = 10

if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Flow Diagram:

3️. if-elif-else Statement

An if-elif-else statement in Python is used to check multiple conditions and execute the
block of code corresponding to the first true condition.

Syntax:

if condition1:
statement1
elif condition2:
statement2
else:
statement3

Example:

marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")

Flow Diagram:

4️. Nested if else Statement

A nested if-else statement is an if-else statement placed inside another if or else


block. It is used when a decision depends on multiple conditions, where the second condition
is checked only after the first condition.

Syntax:

if condition1:
if condition2:
​ statement1
else:
​ statement2
else:
​ if condition2:
​ statement1
else:
​ statement2

Example:

age = 20
citizen = True

if age >= 18:


if citizen:
print("Eligible to vote")
else:
print("Not a citizen")
else:
print("Under age")

Topic-2: match Statement in Python (Match–Case)


The match statement in Python is used for pattern matching. It allows a program to compare
a value against multiple cases and execute the block of code that matches the value. It works
similar to the switch-case statement in other programming languages.

🔹 Syntax
match variable:
case value1:
statement1
case value2:

You might also like