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

Python Conditional Statements Exercises

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)
17 views5 pages

Python Conditional Statements Exercises

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

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")

Common questions

Powered by AI

Python uses conditional statements to evaluate the condition if a number is greater than, less than, or equal to zero. The if statement checks if the number is greater than zero, printing 'Positive number.' An elif condition checks if it equals zero, printing 'Zero.' Otherwise, it prints 'Negative number' .

Handling invalid input in Python through conditional checks is crucial to ensure program robustness, prevent computational errors, and guide user input effectively. By checking inputs like score negative values or color range, errors and misbehavior during program execution are minimized, ensuring accurate data processing and enhancing user experience by providing meaningful feedback .

Chained conditional statements with 'elif' are beneficial when multiple conditions should be evaluated sequentially, executing only the first true condition. This reduces redundancy and improves readability, especially when categorizing inputs, such as mapping numbers to colors or handling multiple exclusive options like test scores where multiple criteria categorize results distinctly .

A leap year in Python can be determined using nested conditional statements. A year is a leap year if divisible by 400 or divisible by 4 but not by 100. The first condition checks if divisible by 400, making it a leap year. Then, it checks if divisible by 4 and not by 100, again making it a leap year. If neither condition is met, the year is not a leap year .

In Python, nested conditionals can determine the largest of three numbers. Firstly, an outer if compares the first two numbers. An inner if, nested within the first, compares the first and third numbers if the first is larger than the second. Similarly, if the second number is larger, another nested if checks against the third. This hierarchical approach optimizes comparisons efficiently .

Using Python's conditional statements, compare each number against the others. The first condition checks if the first number is greater than or equal to both the second and third numbers, setting it as largest. The elif condition checks if the second number is greater than or equal to both the first and third numbers. Otherwise, the third number is the largest by default. Nested if-else can refine this by breaking down comparisons further .

Python conditional statements can verify ATM withdrawal limits by comparing the input withdrawal amount against a predefined limit. An if statement checks if the amount exceeds 2000, printing a warning if true. Otherwise, it confirms a successful transaction. This provides a simple structure to enforce withdrawal constraints programmatically .

Python identifies odd and even numbers using conditional statements that check the remainder when dividing by 2. A number with a remainder of zero upon division by 2 is even, managed by an if statement. An else statement addresses numbers not divisible by 2, identifying them as odd .

Python uses conditionals to recommend vehicle servicing by comparing kilometer readings. An if statement checks if the reading exceeds 2500 kilometers, suggesting servicing if true. Otherwise, it advises servicing only after reaching 2500 kilometers. This method programmatically enforces maintenance schedules based on usage .

Python employs conditional statements to evaluate exam results. An if statement verifies if marks exceed 50, indicating a passed exam. An elif checks for valid marks entry, suggesting retaking the exam for scores equal to or below 50. This structure allows automatic evaluation of student performance .

You might also like