Conditional statements in Python allow you to control the flow of your program based on certain conditions. There are mainly three types of conditional statements in Python: if, elif (short for "else if"), and else.
if statement:
The if statement is used to execute a block of code only if a certain condition is true. The syntax looks like this:
if condition:
Input is Shown Below :
#Lets understand the conditional statements
#Code by IAH (Infinity Aggarwal Harshul)
x = 10 #lets take an input and store value inside x variable
if x>5: #Lets take if condition that if x variable is greater than 5 then
print("x is greater than 5") #print the function if condition is true
#The Output will show the results according
#to condition statement that as input is greater than 5 than yes x is greater than 5
#
Output is Shown Below :
2. if-else statement:
The if-else statement allows you to execute one block of code if the condition is true, and another block of code if the condition is false. The syntax looks like this:
if condition:
else:
For example: Input is Shown Below :
Code :
#Lets understand the conditional statements
#Lets try out second if - else condition statement
#Code by IAH (Infinity Aggarwal Harshul)
x = 10 #lets take an input and store value inside x variable
if x>5: #Lets take if condition that if x variable is greater than 5 then
print("x is greater than 5") #print the x is greater than 5
else : #else if x is not greater than 5
print("x is not greater than 5") #print that x is not greater than 5
#The Output will show the results according
#to condition statement that if input x is
#greater than 5 than yes x is greater than 5
#else if input is less than 5 then print not greater than 5
Output is Shown Below :
3. if-elif-else statement:
The if-elif-else statement allows you to check multiple conditions and execute a block of code corresponding to the first condition that is true. The elif is short for "else if". The syntax looks like this:
if condition1:
elif condition2:
else:
For example: Input is Shown Below :
Code :
#Lets understand the conditional statements
#Lets try out third if - elif - else condition statements
#Code by IAH (Infinity Aggarwal Harshul)
x = 10 #lets take an input and store value inside x variable
if x>5: #Lets take if condition that if x variable is greater than 5 then
print("x is greater than 5") #print the x is greater than 5
elif x == 5: #lets take elif condition that if x is equal to 5
print("x is equal to 5") #print x is equal to 5
else : #else if x is not greater than 5
print("x is not greater than 5") #print that x is not greater than 5
#The Output will show the results according
#to condition statement that if input x is
#greater than 5 than yes x is greater than 5
#else if input is less than 5 then print x is less than 5
#and if input is equal to 5 then print x is equal to 5
Output is Shown Below :
These conditional statements allow you to control the flow of your program based on different conditions, making your code more flexible and powerful. Remember to indent the code blocks properly to indicate which code is part of which condition.
Comments
Post a Comment