0% found this document useful (0 votes)
57 views

Lab 3 Manual - Conditional Structures and Boolean Conditions

Uploaded by

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

Lab 3 Manual - Conditional Structures and Boolean Conditions

Uploaded by

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

What You Have Done So Far

In the previous labs, you learned how to:

● Use Data Types and Variables: Store and manipulate different types of data.
● Take Input and Display Output: Use input() and print() functions.
● Perform Arithmetic Operations: Apply basic arithmetic operators.

Example:

# Program to perform arithmetic operations


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

print("Sum:", num1 + num2)


print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)

Understanding Branching Use Case


In the above example, all arithmetic operations are performed every time the program runs. But
what if we only want to perform an operation based on the user’s input? We can use if
conditions to make decisions in the program.

Scenario:

● If the user wants the addition result, only the sum should be printed.
● If the user wants the subtraction result, only the difference should be printed.

We can write conditions for each operation separately using if statements.

Example:

# Program to perform specific arithmetic operations based on user


input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operation = input("Enter the operation you want (+, -, *, /): ")
if operation == "+":
print("Sum:", num1 + num2)

if operation == "-":
print("Subtraction:", num1 - num2)

if operation == "*":
print("Multiplication:", num1 * num2)

if operation == "/":
print("Division:", num1 / num2)

Explanation:

● Each if block checks for a specific operation and performs it only if the condition is met.

Branching Structures in Python

1. The if Statement

The if statement allows your program to execute a block of code only if a specific condition is
true.

Example:

age = int(input("Enter your age: "))


if age >= 18:
print("You are eligible to vote.")

2. The if-else Statement

An if-else structure allows your program to choose between two paths: one for a true
condition, and another for a false condition.

Example:

age = int(input("Enter your age: "))


if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

3. The if-elif-else Statement

When you need to check multiple conditions, you can use the if-elif-else structure. The
program will evaluate each condition in sequence and execute the corresponding block for the
first true condition.

Example:

marks = int(input("Enter your marks: "))

if marks >= 90:


print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: D")

Key Points:

● Use if for single conditions.


● Use if-else to handle two possible outcomes.
● Use if-elif-else when you have multiple conditions to check in sequence.

Understanding Scope in Python


Scope determines the accessibility of variables and code blocks in your program. In Python,
scope is defined through indentation.

● Indentation: Spaces or tabs at the beginning of a line to define a code block.


● Code Blocks: Groups of statements that are executed together, like the code inside an
if statement.

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

if num > 0:
print("Positive number")
square = num * num
print("Square:", square)
else:
print("Zero or negative number")

print("This message is outside the if-else block")

Explanation:

● Indented Code: The lines under if and else are indented, meaning they belong to
those conditional blocks.
● Variable Scope: The variable square is defined inside the if block and is accessible
there.

Key Points:

● Proper indentation is crucial in Python; it defines how your code is grouped.


● Be consistent with indentation (typically 4 spaces).

How to Write If Conditions


Conditional statements allow you to execute code based on certain conditions.

Comparison Operators:

● == : Equal to
● != : Not equal to
● > : Greater than
● < : Less than
● >= : Greater than or equal to
● <= : Less than or equal to

Logical Operators:

● and : True if both conditions are true


● or : True if at least one condition is true
● not : Inverts the truth value

Examples:

Using Comparison Operators:


age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

Using Logical Operators:

AND Operator:
num = int(input("Enter a number: "))
if num > 0 and num % 2 == 0:
print("Positive even number")
else:
print("Number does not meet both conditions")

OR Operator:
day = input("Enter the day: ")
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
else:
print("It's a weekday.")

NOT Operator:
is_raining = input("Is it raining? (yes/no): ")
if not is_raining == "yes":
print("You can go outside without an umbrella.")
else:
print("Don't forget your umbrella!")

Few Examples
Even or Odd Checker:
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")

Grade Evaluator:
marks = float(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")

Lab Questions
1. Ask the user to input three numbers, check which number is the largest, and print the
result.
2. Prompt the user to input a password and confirm it by taking input again, then check if
both passwords match and print an appropriate message.
3. Ask the user for their age and whether they are a citizen, then determine and print if they
are eligible to vote (18 or above are eligible to vote).
4. Write a program that asks for the current temperature, and print "It's a hot day" if it's
above 30 degrees, "It's a nice day" if it's between 20 and 30 degrees, or "It's a cold day"
if it's below 20 degrees.
5. Ask the user to enter a number and print "Number is within range" if it's between 1 and
100, otherwise print "Number is out of range."
6. Set a predefined username and password, ask the user to input their credentials, check
if they match, and print "Login successful" if they do, or "Login failed" if they don't.
7. Ask the user to input their annual income and calculate the tax based on the following
brackets: income up to $10,000 has no tax, income between $10,001 and $30,000 has a
10% tax, income between $30,001 and $100,000 has a 20% tax, and income over
$100,000 has a 30% tax, ensuring the program handles negative values.
8. Ask the user to input their weight (kg) and height (m), then calculate the BMI and
determine the category using nested if conditions: below 18.5 is underweight, 18.5–24.9
is normal weight, 25–29.9 is overweight, 30–34.9 is obesity class I, 35–39.9 is obesity
class II, and 40 and above is obesity class III.
9. Ask the user for their age, high school GPA, and whether they passed the entrance
exam (yes/no), then determine their eligibility for admission based on these conditions:
the applicant must be at least 18 years old, have a GPA of 3.0 or higher, and if the GPA
is below 3.0, they can still be accepted if they passed the entrance exam.
10. Ask the user for their age and whether they are a student (yes/no), then calculate the
movie ticket price based on the following: age 0–12 is $8, age 13–17 is $10, age 18–59
is $12, age 60 and above is $9, with a $2 discount for students, and ensure the program
handles invalid ages.
11. Ask the user for the package weight and destination (local/international), then calculate
the shipping cost based on these rules: for local shipping, up to 5kg is $5, 5–10kg is $10,
and over 10kg is $15; for international shipping, up to 5kg is $20, 5–10kg is $30, and
over 10kg is $50, with handling for invalid package weights.
12. Ask the user for the total shopping amount, apply a 10% discount if it's greater than
$100, and print the final amount to be paid.

You might also like