All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Python If Statement Multiple Conditions

Last Updated : Mar 11, 2024

Python If Statement Multiple Conditions

In this article we will show you the solution of python if statement multiple conditions, the "if" statement is a key control component in Python that enables you to run particular code blocks in response to specified circumstances.

To develop more complex reasoning, you can utilise numerous conditions inside of a "if" expression.

Using logical operators like "and" and "or," you could design complex decision-making tactics by means of combining severa instances.

For example, you can be part of criteria with the "and" operator if they all need to be met at the equal time. Now move to the concept of python if statement multiple conditions.

Step By Step Guide On Python If Statement Multiple Conditions :-

username = input("Enter your username: ")
password = input("Enter your password: ")
if username == "admin" and password == "password123":
    print("Login successful!")
elif username == "guest" and password == "guestpassword":
    print("Welcome, guest user!")
elif username == "john" and password == "pass123":
    print("Hello, John! You have a new message.")
elif username == "mary" and password == "marypass":
    print("Hi, Mary! You have 5 unread emails.")
    print("Invalid username or password.")
  1. The user authentication mechanism in this code example is straightforward and asks the user to input a username and a password.
  2. A prompt asking for our username is presented to us upon arrival. After entering our username, we click Enter.
  3. We then receive a popup requesting our password.
  4. We press enter after entering our password.
  5. The code next verifies that the supplied username and password are accurate.
  6. The message "Login successful!" is displayed if the username is "admin" as well as the password is "password123".
  7. It recognizes that both conditions are met in such cases.
  8. This proves that we were able to log in as the administrator.
  9. If the username and password do no longer in match of "admin" and "password123," the code moves on to the subsequent requirement.
  10. For instance, the code would realize the combination of the username "guest" & password "guestpassword" and show "Welcome, guest user!"
  11. This confirms that we were capable of log in as a guest user.
  12. The code proceeds to the next condition if the previous ones are not satisfied.
  13. For instance, the programme would recognise the combination "Hello, John! " if we entered "john" as the username and "pass123" as the password.
  14. You've got a fresh message. This signifies a fresh message is pending and that we have successfully logged in as John.
  15. Similarly, the code recognizes the combination of the user name "mary" and the password "marypass" and displays "Hi, Mary! Five of your emails are unread.
  16. The five unopened emails indicate that we have successfully signed in as Mary.
  17. If none of the aforementioned conditions are met, the code executes the else block and displays "Invalid username or password."
  18. This shows that the password and username that we submitted are incompatible, and we were unable to log in.

Conclusion :-

As a result, we were able to understand the idea of many conditions in an if statement in Python.

We also discovered that Python's multiple-condition if statements enable us to build flexible and sophisticated decision-making processes into our code.

We can express numerous criteria that must be met for a code block to execute by combining logical operators like "and" and "or."

This gives us the capacity to manage diverse events and take actions depending on varied configurations of conditions.

I hope this article on python if statement multiple conditions helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪