Python - Conditional Statements
Why should I learn to solve Python: Conditional Statements technical interview questions?
Learn and practise solving Python: Conditional Statements technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical Python: Conditional Statements technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved Python: Conditional Statements technical interview questions and answers with a short answer description. You can download Python: Conditional Statements technical interview questions and answers as PDF files or e-books.
How do I answer Python: Conditional Statements technical interview questions from various companies?
You can answer all kinds of Python: Conditional Statements technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Conditional Statements technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
In Python, a conditional statement allows you to make decisions in your code based on whether a certain condition is true or false. The most common conditional statements are if
, elif
(else if), and else
.
Example: Using if
, elif
, and else
.
# Example
temperature = 25
if temperature > 30:
print("It's a hot day!")
elif 20 <= temperature <= 30:
print("It's a nice day.")
else:
print("It's a cold day.")
It's a nice day.
In this example, the program checks the value of the temperature
variable and prints a message based on the conditions specified. Since temperature
is 25, the output is "It's a nice day."
if
, elif
, and else
statements.
In Python, the if
statement is used to execute a block of code only if a certain condition is true. The elif
(else if) statement is used to check additional conditions if the previous conditions are false. The else
statement is used to execute a block of code if none of the previous conditions are true.
In this example, we'll determine the type of a number (positive, negative, or zero) using if
, elif
, and else
.
# Example
number = -15
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
The number is negative.
Here, the program checks if the number is greater than 0 using the if
statement. If true, it prints "The number is positive." If false, it moves to the elif
statement, which checks if the number is less than 0. If true, it prints "The number is negative." If both conditions are false, it goes to the else
statement and prints "The number is zero."
if
statement in Python?
To demonstrate a basic if
statement in Python, let's create a program that checks whether a number is positive or not.
# Example
number = 10
if number > 0:
print("The number is positive.")
The number is positive.
In this example, the program evaluates the condition number > 0
. If it's true, the indented block of code under if
will be executed and "The number is positive." will be printed. If the condition is false, the block will be skipped.
Indentation is a crucial aspect of Python's syntax, especially in conditional statements. It is used to define blocks of code and indicate the scope of statements within conditions. Let's illustrate this with an example:
# Example
number = 10
if number > 0:
print("The number is positive.")
print("This statement is inside the if block.")
print("This statement is outside the if block.")
The number is positive. This statement is inside the if block. This statement is outside the if block.
In the example, the indentation (spaces or tabs at the beginning of lines) defines the scope of the if
block. The two print
statements inside the if
block are executed only if the condition is true. The last print
statement, not indented, is executed regardless of the condition.