Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Class IX Robotics(Introduction to Data - Programming with Python) Lesson 4 Conditional Statements Session 2024--25

This document provides an introduction to conditional statements in Python, including if, if-else, nested if-else, if-elif-else, and ternary expressions. It explains the syntax and provides examples for each type of conditional statement. Additionally, it includes a worksheet with programming exercises to reinforce the concepts learned.

Uploaded by

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

Class IX Robotics(Introduction to Data - Programming with Python) Lesson 4 Conditional Statements Session 2024--25

This document provides an introduction to conditional statements in Python, including if, if-else, nested if-else, if-elif-else, and ternary expressions. It explains the syntax and provides examples for each type of conditional statement. Additionally, it includes a worksheet with programming exercises to reinforce the concepts learned.

Uploaded by

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

Class IX

Robotics (Introduction to Data and Programming with Python)


Lesson 4
Conditional Statements

Video Tutorials:
Python Programming Tutorial - Control structures
Python Programming Tutorial - if else Statements
Python Programming Tutorial - Nested if statements | if elif else Ladder

What are Conditional Statements?


Conditional Statements are statements in Python that provide a choice for the control flow
based on a condition. It means that the control flow of the Python program will be decided
based on the outcome of the condition.

1. If Conditional Statement in Python


If the simple code of block is to be performed if the condition holds then the if statement is
used. Here the condition mentioned holds then the code of the block runs otherwise not.
Syntax of If Statement:
if condition:
# Statements to execute if
# condition is true
# if statement example
if 10 > 5:
print("10 greater than 5")
print("Program ended")

2. If else Conditional Statements in Python


In a conditional if Statement the additional block of code is merged as an else statement
which is performed when if condition is false.
Syntax of Python If-Else:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
# if..else statement example
x=3
if x == 4:
print("Yes")
else:
print("No")

3. Nested if..else Conditional Statements in Python


Nested if..else means an if-else statement inside another if statement. Or in simple words
first, there is an outer if statement, and inside it another if – else statement is present and
such type of statement is known as nested if statement. We can use one if or else if
statement inside another if or else if statements.
# if..else chain statement
letter = "A"

if letter == "B":
print("letter is B")

else:

if letter == "C":
print("letter is C")

else:

if letter == "A":
print("letter is A")

else:
print("letter isn't A, B and C")

4. If-elif-else Conditional Statements in Python


The if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest of
the ladder is bypassed. If none of the conditions is true, then the final “else” statement will
be executed.
# if-elif statement example
letter = "A"

if letter == "B":
print("letter is B")

elif letter == "C":


print("letter is C")

elif letter == "A":


print("letter is A")

else:
print("letter isn't A, B or C")

5. Ternary Expression Conditional Statements in Python


The Python ternary Expression determines if a condition is true or false and then returns the
appropriate value in accordance with the result. The ternary Expression is useful in cases
where we need to assign a value to a variable based on a simple condition, and we want to
keep our code more concise — all in just one line of code.

# Python program to demonstrate nested ternary operator


a, b = 10, 20
print ("Both a and b are equal" if a == b else "a is greater than b"
if a > b else "b is greater than a")

Worksheet:
1. Write a program using python to read two int values from user and print the greatest
among them.
2. A company decided to give a bonus of 5% of salary to employee if his/her year of service
is more than 5 years. Ask the user for their salary and year of service and print the net bonus
amount.
3. Take input of age of 3 people by user and determine oldest and youngest among them.
4. A student will not be allowed to sit in exam if his/her attendance is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
5. Write a Python program to find those numbers which are divisible by 7 and multiple of 5,
between 1500 and 2700 (both included).
6. Write a Python program to check a triangle is equilateral, isosceles or scalene.
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.
7. Write a Python program to input a number from the user and check whether it is buzz
number or not.
8. Write a Python program to input a year and check whether it is leap year or not.

You might also like