0% found this document useful (0 votes)
20 views17 pages

4 Decision Making

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)
20 views17 pages

4 Decision Making

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
You are on page 1/ 17

Decision Making

If Else Statement
• Sequence: executes one statement after the
other from beginning to the end of the
program.
• decision making or selection is implemented
with the help of if..else statements
• The order of execution of the statements in a
program is known as flow of control.
What if we want to get a positive value as an output
irrespective of the values entered for num1 and num2?
•If the condition is true, then the indented statement gets
executed.
•The indented statement implies that its execution is
dependent on the header.
•There is no limit on the number of statements that can
appear under an if block.
Many a times there are situations that require multiple
conditions to be checked and hence may lead to many
alternatives. In such case we can chain the conditions using
if...elif.
•else is optional
•Number of elif is dependent on the number of conditions
•If the first condition is false, the next is checked, and so on. If one of the
condition is true, the corresponding statement(s) executes, and the block
ends.
In most of the programming languages, all the statements inside a block are
put inside curly brackets.

However, Python uses indentation for block or nested block structure.

Leading whitespace (spaces and tabs) at the beginning of each statement is


called indentation.

In Python, the same level of indentation associates statements into a single


block of code.

The interpreter checks indentation levels very strictly and throws up syntax
errors if indentation is not correct.

It is a common practice to use a single tab for each level of indentation.


Example (with correct indentation): In the program below, the if-else
statement has two code blocks and the statements in each block are
indented with the same amount of spaces or tabs.

a=5
b=6
if a > b :
print ("a is larger") # Block1
print ("Bye")
else:
print ("b is larger") # Block2
print ("Bye Bye")

Output
b is larger
Bye Bye
Let us write a program to create a four function calculator with the
following requirements:

The program should

•accept any one of the four arithmetic operator ( +,-,*,/)

•displays a message "Please enter any one of the operator(+,-,*,/)"


,if the user enters anything other than the mentioned operator.

•display only positive difference in case of the operator “-”

•displays a message "Please enter a value other than 0" if the user
enters the second number as 0.

•accept two float numbers from the user.


Lab exercises
1. Write a program that takes as input the name and age of the user
and displays a message that the user is eligible to vote or not.

2. Write a program to check if the number entered by the user is


even or odd.

3. Write a program to check if the year entered by the user is a leap


year.

4. A garment shop if offering 10% discount on garments of girls and


5% discount on garments of boys. In case the age of the child is
below 5 years the discount offered is 15% irrespective whether
the customer is a girl or a boy. Write a program that takes as
input the name, age, gender, price of items bought and displays
the net payable amount.
5. Write a program in to find the grade of a student when grades are
allocated as under :
Percentage of Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E

6. Write a program to calculate the income tax of an employee in an


organization where the conditions given are as follows:
Income Tax = 0 if Income < 100,000
Income Tax = 10% if Income <= 300,000
Income Tax = 20% if income >= 300000

Take as input the Total Taxable Income of the employee and display
the tax payable by the employee.
• A number is even if it is perfectly divisible by 2. When the number is
divided by 2, we use the remainder operator % to compute the
remainder. If the remainder is not zero, the number is odd.

# Python program to check if the input number is odd or even.


# A number is even if division by 2 give a remainder of 0.
# If remainder is 1, it is odd number.

num = int(input("Enter a number: "))


if (num % 2) == 0:
print(num, "is an even number")
else:
print(num, "is an odd number")

Output
Enter a number: 43
43 is Odd
• A leap year is exactly divisible by 4 except for century years (years ending
with 00).
• The century year is a leap year only if it is perfectly divisible by 400. For
example,
❑ 2017 is not a leap year
❑ 1900 is a not leap year
❑ 2012 is a leap year
❑ 2000 is a leap year
# Python program to check if the input year is a leap year or not
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, ”is a leap year“)
else:
print(year, ”is not a leap year“)
else:
print(year, ”is a leap year“)
else:
print(year, ”is not a leap year“)

Output
2000 is a leap year

You might also like