0% found this document useful (0 votes)
48 views9 pages

Session #4-5

The document discusses conditional statements in Python such as if, else, and elif statements which allow code to execute different blocks of code based on whether a condition is true or false. It provides the syntax and examples of using if statements to check conditions, else statements to execute if the if condition is not met, and elif statements to check additional conditions if the if is not true. A number of challenges are also provided that involve getting user input to check various conditional logic.

Uploaded by

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

Session #4-5

The document discusses conditional statements in Python such as if, else, and elif statements which allow code to execute different blocks of code based on whether a condition is true or false. It provides the syntax and examples of using if statements to check conditions, else statements to execute if the if condition is not met, and elif statements to check additional conditions if the if is not true. A number of challenges are also provided that involve getting user input to check various conditional logic.

Uploaded by

Darkbox
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python #4-5

Overview

● Conditional statements
● If then else
Conditional statements

A conditional statement are statements that perform different


actions or computations depending on weather a boolean is
True or False.
IF statement
An if statement performs the actions or computations inside when the
condition is True ie. if condition is true then do this.

Syntax:

if condition:
Statement

TASK: Get 2 user inputs and check if the first one is bigger than the second
one
Else
An else statement performs the actions or computations if condition for
the if statement is not satisfied. There are no conditions for else.

Syntax:

else:
Statement

TASK: Get 2 user inputs and print the larger one.

HOW WOULD YOU CHECK FOR MULTIPLE CONDITIONS?


Else if
An else if statement performs the actions or computations if condition for
the if statement is not satisfied and if the condition for the else if is
satisfied. Else if requires a condition.

Syntax:

elif condition:
Statement

TASK: Get 2 user inputs, if they are not equal print the larger one otherwise
print that they are equal.
Challenges
1. Get input from the user and:
a. Check weather a number is positive, negative or zero.
b. All the years that are perfectly divisible by 4 are called as Leap years except the
century years. If the century year is divisible by 400 then that year is a Leap year.
Century year’s means they end with 00 such as 1200, 1300, 2400, 2500 etc. They
are divisible by 100. Check weather a year is a leap year or not.
c. A triangle is valid if sum of its two sides is greater than the third side. If three sides
are a, b and c, then three conditions should be met. Check weather three angles
of a triangle form a valid triangle
2. Ask the user input for 2 numbers and print the highest number
Challenges
3. A school has following rules for grading system:

0 to 25 -> F
26 to 45 -> E
46 to 50 -> D
51 to 60 -> C
61 to 80 -> B
81 to 100 -> A

Ask user to enter marks and print the corresponding grade.


Challenges
4. Take the value of length and breadth of a rectangle from the user and check
whether it is a square.

5. A shop will give discount of 10% if the total purchase is above Rs. 1000/-. Let
us assume that an item cost is Rs. 100/-. Ask the user to input the number of
items bought, print the total amount payable by the customer.

6. As the student to input marks scored in Computer Science, Maths, Physics. If


the average marks scored is greater than 95%, print that student can get
admission in the college.

You might also like