How to Use Conditions in Python IF Logic

How to Use Condition Statement in IF logic in Python

The condition is a variable you can use in the IF logic whether to continue the logic or to skip the logic in python. Here’s the best example for your reference.


Python logic that uses condition statement

Below program checks input password with the actual. If matches, it prints password correct, else, it prints password incorrect.

# Python logic shows how to use condition statement
password = "admIn"
condition = (password == "admin")

# if condition is equal to True, which means the password is correct
if condition:
    print("password correct")
#if condition is equal to False, which means the password is not correct.
if condition == False:
    print("password incorrect")

The output is as below.

The password that has given in the condition statement is ‘admin’. So the result is False. In this case, the result is False because Python is case-sensitive, so admIn is not the same as admin.

That means the first if statement evaluates to False, so its print statement is skipped, and program control flows to the next if statement. Hence it has produced the output below:

password incorrect


** Process exited - Return Code: 0 **
Press Enter to exit terminal

From the above scenario, it tells how a password validations deal in Python.

The bottom-line

The example demonstrates how to check conditions in the IF logic in Python.

References

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Start Discussion

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.