Basic Python programs to practice conditional statement(If ,Elif,Else)
One of the essential constructs of Python is the if-else statement, which allows developers to control the flow of a program based on conditions. Scroll to the End of this blog for If-else programs to practice.
The if-else statement is a control flow statement that evaluates a condition and executes a block of code if the condition is true or false. The basic syntax of an if-else statement in Python is:
if (condition):
# code to be executed if condition is True
else:
# code to be executed if condition is False
In the above syntax, the "condition" is the expression or value that is evaluated as true or false. If the condition is true, the code inside the if block is executed. Otherwise, the code inside the else block is executed.
Let's take an example to understand the if-else statement in Python:
num = 10
if num > 0:
print("Positive number")
else:
print("Negative number")
In the above code, we have defined a variable "num" and checked if it is greater than zero using the if statement. If the condition is true, it prints a "Positive number". Otherwise, it prints a "Negative number".
Apart from the if-else statement, Python also supports elif statement, which stands for "else if". The elif statement is used when there are multiple conditions to be checked. The basic syntax of the elif statement in Python is:
if condition1:
# code to be executed if condition1 is true
elif condition2:
# code to be executed if condition2 is true
else:
# code to be executed if all conditions are false
In the above syntax, "condition1" is the first condition to be checked. If it is true, the code inside the if block is executed, and the other conditions are not evaluated. If condition 1 is false, then "condition2" is checked. If it is true, the code inside the elif block is executed. Otherwise, the code inside the else block is executed.
Let's take an example to understand the elif statement in Python:
num = 0
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
In the above code, we have checked if the variable "num" is greater than zero using the if statement. If it is true, it prints a "Positive number". If the condition is false, it checks if the num is equal to zero using the elif statement. If it is true, it prints "Zero". Otherwise, it prints a "Negative number".
In conclusion, the if-else statement is a fundamental construct in Python that allows developers to control the flow of a program based on conditions. It can be used to write complex programs that execute different blocks of code based on different conditions. The elif statement provides an efficient way to check multiple conditions in a program. Understanding the if-else statement is essential for any Python developer to write efficient and effective programs.
Problems to Practice conditional (If,Else,Elif) statements
Write a program to check whether a number (inputs should be from the user) is positive or negative.
Write a program to check whether a person is a senior citizen or not (get age input and if it is greater than 60 then "senior citizen" should be printed).
Write a program to check the number of days in a month.
Write a program to check whether the given input is odd or even if it is an even number print "even", if it is an odd number print "odd".
Write a program to check the grade of the marks obtained by students.
If the mark is above 90 grade is A,If the mark is above 80 and below 90 grade is B,
If the mark is above 70 and below 80 grade is C,
If the mark is above 60 and below 70 grade is D,
If the mark is below 60 grade is E.
Write a program to check if the value of the input(A) is 50 print "It is fifty", if the input is greater than 10 print "greater than 10" then if it is greater than 20 print "greater than 20" if it is less than 20 print "Not above 20".
Hint: Nested IF concept should be used.Write a program to check whether the value 52 is in the list or not, If it is in the list print "yes", if not in the list print "No" A = [45, 22, 78, 63, 85, 52, 96 ].
Hint: Use the Membership operator.Write a program to check whether the given year is a Leap year or not.
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example,2018 is not a Leap Year
1700 is not a Leap Year
1600 is a Leap Year
2012 is a Leap Year.Print wonders of the world in an ASCII format according to the user's input.
for example, if the user gives the Taj mahal or India.
To get ASCII art visit this website ASCII artI Y | T | .'. .' '. .'. (__ ) _;-----;_ (__ ) |A| .'::::: '. |A| .===. /::::::: \ .===. |/ | | :::::::: : | |/ | |/ | | ,' ', |::::::: | ,' ', | |/ | |/ | ( ) (:.___)\:::____________/(:.___) ( ) |/ | |===| H I |(_)V__)-------------(__V(_)| I H |===| |/ | |=| Y_.----| _______________ |----._Y |=| |/ | |/ | |:| | | __ | [,-------------,] | __ | | |:| |/ | |/ | |:| | |/ \| [| _.-._ |] |/ \| | |:| |/ | |===| |:| |'|| || [| / '::\ |] || ||'| |:| |===| |/ | |:| |_||__|| [] | :::| [] ||__||_| |:| |/ | |/ | |:| | | __ | [] | :::| [] | __ | | |:| |/ | |/ | |:| |.|/ \| [] | :::| [] |/ \|.| |:| |/ | |/ | |:| | || || [] | ::| [] || || | |:| |/ | |/__|_____|:|___| ||__||_[]__|_____::|__[]_||__||_|___|:|_____|/__| |/ |-------------------------------------------------------|/ | |/ | |/ | |/____|_________________________________________________snd___|/____|
"Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do." - Steve Jobs