Two Conditions in If Statement Python
The most crucial feature of almost all programming languages is decision-making. As the name suggests, decision-making enables us to execute a specific block of code in response to a specific decision. Decisions are made in this case regarding the applicability of the specific conditions. The foundation of decision-making is condition checking.
If statement:
A specific condition is tested using the if statement. If this is true, an if-block of code will be run.
If-else statement:
If-else statements are similar to if statements, except that they also include a block of code that checks the condition in the false case. If the condition in the if statement is false, the else declaration will be executed.
Nested-If statement:
Use of an if statement is made possible by nested if statements. Inside of an outer unless the statement is an else statement.
Indentation in Python:
Parentheses are not permitted in block-level code in Python to simplify the code and make programming easier. A block in Python is declared using indentation. Two statements belong to the same block if they are indented at the same level.
The statements are typically given four spaces to be indented, which is standard in Python.
Python's indentation, which identifies a block of code, is its most frequently used feature. Each statement contained in a block should be indented to the same depth. We'll examine how Python's decision-making and other features are indented.
If statement:
A code block known as an "if-block" is executed if a specific condition is met by the if statement, which is used to test for that condition. The situation in an if assertion can be any sane logical affirmation that can be assessed as true or false.
Syntax:
if expression:
statement
CODE:
n=int(input())
if(n%2==0):
print("Given number is Even")
OUTPUT:
23855725434536
Given number is Even
Code for the Largest of three:
a = int(input("Enter a value: "));
b = int(input("Enter b value: "));
c = int(input("Enter c value: "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
OUTPUT:
Enter a value: 4
Enter b value: 6
Enter c value: 12
c is largest
If-else statement:
If the condition is false, the if-else statement's else block and if
The statement both are executed. If the test is successful, the if-block is run. Else will be executed if not.
Syntax:
if condition:
#set of statements
else:
#another set of statements in else-block
CODE:
num = int (input("Enter your Age "))
if num>=18:
print("Yes! You are eligible");
else:
print("No! You are not eligible");
OUTPUT:
Enter your age as 41
Yes! You are eligible
Code for checking whether the given number is even or odd:
num = int(input("Enter the number:"))
if num%2 == 0:
print("Given Number is a even number")
else:
print("Given Number is a odd number")
OUTPUT:
Enter the number:9
Number is a odd number
Code Explanation:
Taking a user-defined input as num, we divide the num by 2 if the resultant.
The remainder of the given number, divided by 2, equals 0, then the given number is even. In another case, it is an odd number.
Elif statement:
We can check multiple conditions with the elif statement and then execute a particular block of statements based on which is true. Depending on the program’s needs, we can use any elif statements. Elif usage, however, is not required.
Similar to an if-else-if ladder statement in C, the elif statement operates similarly.
After it, an if statement is required.
Syntax:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Example code:
marks = int(input("Enter the marks "))
if marks > 85 and marks <= 100:
print("Congrats! you scored grade A ")
elif marks > 60 and marks <= 85:
print("You scored grade B + ")
elif marks > 40 and marks <= 60:
print("You scored grade B ")
elif (marks > 30 and marks <= 40):
print("You scored grade C ")
else:
print("Sorry you are fail ?")
OUTPUT:
Enter the marks 97
Congrats! you scored grade A
Advantages:
- If-else statements assist us in programming decision-making and proper code execution.
- Additionally, it aids in code debugging.