How to Use If and If-Else Statements in Python

A tutorial on if and if-else conditional statements in Python.

Rohit Kumar Thakur
Python in Plain English
4 min readOct 21, 2021

--

Python if-else conditional Statement

In the last article, we learned about Python functions. Now, In this article, we will learn about Python conditional statements with suitable examples.

If you are new to the programming world, then you have to focus more on these control structures. Because the real-world problem-solving questions require a high drill of Python control structure knowledge.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

if Conditional Statement

Let’s start this with an example.

In my school, the passing marks of the students are 35 out of 100. But teachers decide to moderate the results by giving a maximum of two grace marks.

This means that, if you scored 33 or 34 marks, then it will be declared as 35 marks. But if you score less than 33 then you will be considered as fail. So, write a program to moderate the results by giving a maximum of two grace marks.

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)

main()

This is the simple code of the above problem. We called the main function so it will execute first. Inside the main function, we set a variable passMarks and assign the value of this variable as 35.

After that, take input from the user. The input marks must be integer. That’s why we wrap the input marks inside int() function. Now, call the function ModeratedMarks() with the parameters: marks and passMarks. Assign the return value of this function to a new variable newMarks.

Inside the ModeratedMarks() function, we have two parameters, marksand passMarks. Now, here we use conditional if statement. If the value of the marks is one less than passMarks or if the value of the marks is two less than the passMarks then set the value of the marks is equal to passMarks. Next, come out of the conditional if statement and return the marks.

This return value of the ModeratedMarks() function is assigned to the variable newMarks (inside the main function). Now, simply print the updated marks.

If we look at the outcome, then it will look 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 marks 33 and 34 get upgraded. Because that’s what we programmed for.

You can practice more questions here:

Python Code To Check If The Year Is A Leap Year

Palindrome Program In Python

Least Common Multiple (LCM) in Python

Python Function to Check If a Number is Prime or Not

Program to Find Out the Maximum of Three Numbers

Determine A Given Natural Number Is A Perfect Number Or Not Using Python

Greatest Common Divisor or HCF In Python

Flow Diagram of if statement

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 take another example.

Write a program that authenticates the user and allows access to the system. The input parameters should be strings. The return message should be a String. If the password is correct then render “Login Successful !! Welcome to the ninza’s arena”. In case of password mismatch, render “Try different password”.

Let’s write the code:

def authentication(password):
if password == 'unstoppable':
message = "Login Successful !! Welcome to the ninza's arena"
else:
message = "Try a different password"

return message

def main():
print("Authentication System")
print("======================")
passwordInput = input("Enter your password: ")
checkPass = authentication(passwordInput)
print(checkPass)
main()

The output of the code is:

>>>  Authentication System
======================
Enter your password: unstoppable
Login Successful !! Welcome to the ninza's arena
>>> Authentication System
======================
Enter your password: abc
Try a different password

BINGO. This is the basic code algorithm of the authentication system in the Python programming language. The code is very similar to the code we did earlier in this article. Here, in the main function, we call the function authentication with the parameter passwordInput.

In the authentication function, we check if the entered password is “unstoppable” or not. If yes then print “Login Successful !! Welcome to the ninza’s arena”. Else, “Try a different password”.

Flow Diagram of if-else statement

Flow Diagram of if-else structure

General Form of the if-else conditional statement:

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

Well, that’s it for this article. In the next article, we will talk about Python if-elif-else.

If this article sounds informative to you, make sure to follow and share it with your geek community.

Thank you for reading!

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).

More content at plainenglish.io

--

--