How to Use If and If-Else Statements in Python

In this article, we will learn about Python conditional (If and if-Else) statements with suitable examples.

If you’re new to programming, you’ll want to concentrate on these control structures. Because real-world problem-solving challenges necessitate a thorough understanding of Python control structures.

if Conditional Statement

Let’s start with a hypothetical situation:

Students at my school have a passing grade of 35 out of 100. Teachers, on the other hand, elect to regulate the results by awarding no more than two grace marks.

This implies that if you received 33 or 34 points, you will receive 35 points. However, if you receive a score of less than 33, you will be label a failure. As a consequence, develop software that gives a maximum of two grace marks to the results.

def ModeratedMarks(marks, passMarks):
    if marks == passMarks-1 or marks == passMarks-2:
        marks = passMarks
    return marks
def main():
    passMarks = 35
    marks = int(input('Enter marks: '))
    newMarks = ModeratedMarks(marks, passMarks)
    print('Updated Marks: ', newMarks)

The above problem’s easy code is as follows. Because we called the main function, it will run first. We create a variable called passMarks and set its value to 35 inside the main function.

After that, get the user’s feedback. The number of marks in the input must be an integer. That’s why the input marks are wrap inside the int() procedure. Now, given the parameters: marks and passMarks, call the method ModeratedMarks(). Assign the function’s return value to a new variable called newMarks.

There are two arguments in the ModeratedMarks() function: marks and passMarks. We’re now using a conditional if statement. Set the value of the marks to passMarks if the value of the marks is one less than passMarks or two less than passMarks. Return the marks after exiting the conditional if statement.

The newMarks variable is allocate the return value of the ModeratedMarks() function (inside the main function). Simply print the revised marks now.

If we look at the end result, we’ll see something like this:

>>> Enter marks: 32
Updated Marks:  32
>>> Enter marks: 33
Updated Marks: 35
>>> Enter marks: 34
Updated marks: 35
>>> Enter marks: 35
Updated marks: 35
>>> Enter marks: 36
Updated marks: 36

Only the 33 and 34 grades are update. Because that’s what we set out to do.

Flow Diagram of if statement


The general form of if statement is:

if < condition >:
   < Sequence S of statements to be executed >

if-else Conditional Statement in Python

Let’s start with another hypothetical situation:

Create a program that verifies the user’s identity and grants them access to the system. Strings should be used as input parameters. A String should be returned as the message. If the password is correct, the message “Login Successful!!” will be shown. “Welcome to the Softhunt”. “Try a different password” should be shown in the event of a password mismatch.

Let’s write the code:

def authentication(password):
    if password == 'unstoppable':
        message = "Login Successful !! Welcome to the Softhunt"
        message = "Try a different password"
    return message
def main():
    print("Authentication System")
    passwordInput = input("Enter your password: ")
    checkPass = authentication(passwordInput)

The output of the code is:

>>>  Authentication System
Enter your password: unstoppable
Login Successful !! Welcome to the Softhunt
>>> Authentication System
Enter your password: abc
Try a different password

In the Python programming language, this is the fundamental code algorithm for the authentication system. The code looks a lot like the code we wrote before in this tutorial. The function authentication is called with the parameter passwordInput in the main function.

We verify whether the entered password is “unstoppable” or not in the authentication function. If you answered yes, print “Login Successful!! “Welcome to the Softhunt”. “Try a different password” if everything else fails.

Flow Diagram of if-else statement


General Form of the if-else conditional statement:

if <condition>:
    < sequence s1 of statements to be executed >
    < sequence s2 of statements to be executed >

If you want to learn more about python then visit the official documentation of python.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]

2 thoughts on “How to Use If and If-Else Statements in Python”

Leave a Comment