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

2. Python Program Exercises_DECISION MAKING

Uploaded by

toggodavies
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)
21 views

2. Python Program Exercises_DECISION MAKING

Uploaded by

toggodavies
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/ 5

PYTHON PROGRAMMING

Lab Exercises -2
Decision Making/ Conditional Statements
1. Check whether the given number is a Positive or Negative number.
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
2. Check whether the given number is Odd or Even.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

3. Find the Largest of 3 nos.


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

4. Leap year or not


# To get year (integer input) from the user
year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)


# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))

# not divided by 100 means not a century year


# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print("{0} is not a leap year".format(year))
5. Vehicle to be serviced or not
km_reading=int(input("Enter the kilometer value:"))
if km_reading>2500:
print("Vehicle service is recommended")
else:
print("Service after 2500km")
6. ATM withdrawal allowed or not
withdrawal_amnt=int(input("Enter the amount:"))
if withdrawal_amnt>2000:
print("Exceeding daily withdrawl limit")
else:
print("Transaction Successful")
7. Exam Result
marks=int(input("Enter the marks scored"))
if marks<0:
print("Enter valid marks")
elif marks>50:
print("Cleared the exam")
else:
print("Resit for the exam")
8. Chained conditional using elif
color = int(input("Enter the color value:"))
if color == 1:
print("Violet Color")
elif color == 2:
print("Indigo Color")
elif color == 3:
print("Blue Color")
elif color == 4:
print("Green Color")
elif color == 5:
print("Yellow Color")
elif color == 6:
print("Orange Color")
elif color == 7:
print("Red Color")
else:
print("Invalid Color Entry!")

9. Largest of 3 numbers using nested if


num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))
num3= int(input("Enter Third Number:"))
if(num1 > num2):
if(num1 > num3):
print("Number 1 is largest")
else:
print("Number 3 is largest")
else:
if(num2 > num3):
print("Number 2 is largest")
else:
print("Number 3 is largest")

You might also like