Python If Statement Multiple Conditions
Last Updated : Aug 24, 2023
IN - Python
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.") else: print("Invalid username or password.")
- The user authentication mechanism in this code example is straightforward and asks the user to input a username and a password.
- A prompt asking for our username is presented to us upon arrival. After entering our username, we click Enter.
- We then receive a popup requesting our password.
- We press enter after entering our password.
- The code next verifies that the supplied username and password are accurate.
- The message "Login successful!" is displayed if the username is "admin" as well as the password is "password123".
- It recognizes that both conditions are met in such cases.
- This proves that we were able to log in as the administrator.
- If the username and password do no longer in match of "admin" and "password123," the code moves on to the subsequent requirement.
- For instance, the code would realize the combination of the username "guest" & password "guestpassword" and show "Welcome, guest user!"
- This confirms that we were capable of log in as a guest user.
- The code proceeds to the next condition if the previous ones are not satisfied.
- For instance, the programme would recognise the combination "Hello, John! " if we entered "john" as the username and "pass123" as the password.
- You've got a fresh message. This signifies a fresh message is pending and that we have successfully logged in as John.
- 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.
- The five unopened emails indicate that we have successfully signed in as Mary.
- If none of the aforementioned conditions are met, the code executes the else block and displays "Invalid username or password."
- This shows that the password and username that we submitted are incompatible, and we were unable to log in.
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.