PROGRAM-10
[Link]- Write a program with simple if statement.
THEORY- The simple if statement in Python is used to run a block of code
only when a specific condition is true. It begins with the if keyword followed by
a condition and a colon, with the code block indented underneath. If the
condition is true, the code runs; if false, it is skipped. This helps control the
program’s flow based on different situations and is a key part of decision-
making in Python.
CODE-
temperature = 40
if temperature > 25:
print("It's a hot day!")
OUTPUT-
[Link]- Write a program with if – else statement.
THEORY- The if-else statement in Python is used to perform one action when
a condition is true and a different action when it is false. It starts with an if
followed by the condition and a colon. If the condition is true, the indented code
under the if block runs. If the condition is false, the code under the else block
runs instead. This structure allows a program to make clear, two-way decisions.
Proper indentation is important to define the blocks of code under if and else.
CODE-
age = 17
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
OUTPUT-
[Link]- Write a program with if -elif-else ladder.
THEORY- The if-elif-else ladder in Python is used when you need to check
multiple conditions in sequence. It starts with an if statement, followed by one
or more elif (else if) blocks, and ends with an optional else block. Each
condition is checked in order, and the first one that is true gets executed. If none
of the conditions are true, the else block runs. This structure is useful when
there are several possible outcomes based on different conditions. Proper
indentation is required to define the code blocks under each part of the ladder.
CODE-
marks = 82
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: F")
OUTPUT-
[Link]- Write a program with nested if statement.
THEORY- A nested if statement in Python is an if statement placed inside
another if or else block. It is used to check multiple conditions one after another,
where the second condition is only checked if the first one is true. This allows
for more specific decision-making. Proper indentation is very important in
nested if statements to clearly define which condition belongs to which block.
CODE-
num = 12
if num > 0:
if num % 2 == 0:
print("Positive and Even")
else:
print("Positive and Odd")
else:
print("Not a positive number")
OUTPUT-
5. OBJECTIVE- Write a program with short hand if .
THEORY- A shorthand if statement in Python is a concise way to write simple
conditional expressions in a single line. It is typically used when the action to be
performed is brief and easily understood. The basic form is if condition: action,
and it can also be extended to if-else using the format action_if_true if condition
else action_if_false. This makes the code shorter and cleaner, especially for
simple decisions. However, it’s best used for readability and not recommended
for complex logic.
CODE-
x = 10
y = 20
print("x is greater") if x > y else print("y is greater")
OUTPUT-