DEV Community

Max
Max

Posted on

Python If, If-Else Statements Tutorial

Python if statement

In Python, if statement is used for conditional execution of code based on certain conditions.

The syntax for the if statement

if condition:
    # code to execute if condition is true
Enter fullscreen mode Exit fullscreen mode

Here, the condition is a boolean expression that can be evaluated to True or False. If the condition is true, then the code inside the if statement is executed.

## Check if a number is positive
num = 5

if num > 0:
    print("The number is positive")
Enter fullscreen mode Exit fullscreen mode

Output:
The number is positive

In the above example, the value of num is checked to see if it is greater than 0. If the condition is true, then the message "The number is positive" is printed.

## Check if a number is even or odd
num = 4

if num % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")
Enter fullscreen mode Exit fullscreen mode

Output:
The number is even

In this example, the value of num is checked to see if it is even or odd. If the number is divisible by 2 (i.e. num % 2 == 0), then it is even and the message "The number is even" is printed. Otherwise, the number is odd and the message "The number is odd" is printed.

The else keyword is used here to provide an alternative code block to be executed if the condition in the if statement is false.

That's it! This is how you can use if statement in Python to execute code based on certain conditions.

Python If-Else Statements

In Python, the if-else statement is used to execute a code block based on a condition.

The basic syntax of if-else statement is as follows:

if condition:
    # execute this block if the condition is true
else:
    # execute this block if the condition is false
Enter fullscreen mode Exit fullscreen mode

Here's an example that uses an if-else statement to check if a number is positive or negative:

# Check Positive or Negative
num = int(input("Enter a number: "))

if num >= 0:
    print("The number is positive or zero")
else:
    print("The number is negative")
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number: 5
The number is positive or zero
Enter fullscreen mode Exit fullscreen mode

In this example, if the value of num is greater than or equal to zero, the program prints "The number is positive or zero." If the value of num is less than zero, the program prints "The number is negative."

Here's another example that uses nested if-else statements to check a student's grade:

# Find Student Grade
marks = int(input("Enter the marks obtained: "))

if marks >= 90:
    grade = "A+"
elif marks >= 80:
    grade = "A"
elif marks >= 70:
    grade = "B"
elif marks >= 60:
    grade = "C"
elif marks >= 50:
    grade = "D"
else:
    grade = "F"

print("The grade obtained is", grade)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter the marks obtained: 92
The grade obtained is A+
Enter fullscreen mode Exit fullscreen mode

if statement marks >= 90 is true for value 92

In this example, the program first checks if the marks are greater than or equal to 90. If they are, it assigns the value "A+" to the variable grade. If not, it checks if the marks are greater than or equal to 80, and so on. If the marks are less than 50, the program assigns the value "F" to the variable grade.

Explore Other Related Articles

Python Function Tutorial
Python break and continue tutorial
Python While Loop tutorial
Python For Loops, Range, Enumerate Tutorial
Python if-else Statements Tutorial

Top comments (0)