VOOZH about

URL: https://thenewstack.io/python-for-beginners-and-or-operators/

⇱ Python for Beginners: And/or Operators - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-01-25 09:05:03
Python for Beginners: And/or Operators
tutorial,
Python / Software Development

Python for Beginners: And/or Operators

In Python programming, both the and/or operators make it possible to test conditions and decide which execution path your program will take.
Jan 25th, 2022 9:05am by Jack Wallen
👁 Featued image for: Python for Beginners: And/or Operators

We’re back to Python. It’s almost as though we never left. So far, we’ve covered a lot of ground with lists, saving input to a file, functions and if/else statements, accepting input from users, and more. We’re going to continue our journey with this fantastic and easy-to-learn language with yet another very popular feature found in many languages… the and/or operators.

Remember last time, when we talked about the if/else statement that went something like:

if X is true then Y else Z?

In other words, if X = 2, then X is an even number, else it’s an odd number.

If/else statements are absolutely crucial to understand, as they make it possible for functions to execute, depending on the outcome of a condition.

We can do something similar with operators, which actually makes it possible to create more complex statements. Instead of being limited to an if/else statement like if X = 2 then X is an even number, else it’s an odd number, we can do something like if X = 10 and Y = 12, then both X and Y are odd numbers, else they might not be.

That’s a rather silly example because X could be 10 and Y could be 20, both of which are even numbers. To make this a bit more clear, we could do something like:

if X <= 2 and Y <= 2, then the sum of the two numbers is less than 5.

You use these logical operators on conditional statements that are either true or false. Both the and/or operators make it possible to test conditions and decide which execution path your program will take. This is straight-up Boolean logic, which allows you to create expressions that evaluate if something is true or false.

It’s important to understand the following Boolean concepts with Python:

  • A Boolean is a value that can be either true or false.
  • Boolean values are either True or False (capitalization matters).
  • Boolean variables are variables that can be either True or False and are used as flags to indicate if a specific condition exists.
  • Boolean expression is an expression that returns either True or False.
  • Boolean context can be if conditions and/or while loops used to evaluate a Boolean value.
  • Operands are objects within an expression connected by an operator.
  • Boolean logical operators are AND, OR, and NOT.

In their most basic form, the operators work like this:

  • X or Y
  • X and Y
  • X not Y

It’s then important to talk about true and false because it might seem a bit odd at first. With Python, if one subexpression (in our example above, subexpressions are X and Y) is True, then the expression is True. For an expression to evaluate to False, both expressions must be False. Here’s how that works.

  • 10 < 20 and 10 > 5 = True
  • 10 < 20 and 10 > 20 = True
  • 10 < 5 and 10 > 30 = False

Let’s move that to Python code, shall we? Instead of writing a program out of the gate, let’s open the Python console with the command:

python3

You should now see:

>>>

Type the following:

exp1 = 10 < 20

Then, type:

exp1

Python should print out:

True

Why? Because 10 is less than 20.

Okay, now type:

exp2 = 10 > 5 

Type exp2 and Python will once again print out True.

Now, type:

exp3 = 10 > 20

Follow this with:

exp3

Python should print out:

False

What we now have is the following:

  • exp1 = True
  • exp2 = True
  • exp3 = False

Let’s use our operators. Type:

exp1 or exp2

That should print out True because both subexpressions are true. Next, type:

exp1 or exp3

This will still print out True because one of our subexpressions is True.

Let’s create a fourth subexpression with:

exp4 = 10 < 5

Now, type:

exp3 or exp4

Guess what Python reports? False. Why? Because both subexpressions are false.

Now, let’s migrate this into a Python application. Exit the Python console with:

exit()

Create a new file with:

nano operators.py

We’re going to create a simple program that will print out if one or both expressions are true. Most of this should already be familiar to you. Our program looks like this:

👁 code

You should already know the outcome of this program because although the value for value_y isn’t less than 20, the value for value_x does equal 10. And because one of the subexpressions is True, the results are true. Save and close the file and run it with:

python3 operators.py

Let’s allow the user to enter values for both our variables. We do this with the lines:

x = int(input("Type a value for x "))
y = int(input("Type a value for y "))

Next, we use an if/else statement like so:

👁 code

Our entire program looks like:
👁 code

Save and close the file. Run the program with:

python3 operators.py

You will be asked to input 2 numbers. Once you do that, the program will compare the numbers and print “If either x is greater than or equal to 10 or y is less than or equal to 20, the expression is True” if one subexpression is true or “The expression is false” if both subexpressions are false.

The following inputted numbers would reveal the following results:

  • 10 10 = True
  • 1 10 = True
  • 1 30 = False

But what if we change or to and? Does that change our outcomes? Yes. Our new application would be:
👁 codeblock

With the above program, our results would be:

  • 10 10 = True
  • 1 10 = False
  • 1 30 = False

Why the change? Because the and operator returns True only if both operands are true, whereas the or operand returns True if both operands are true.

And there you go, the and/or operators. You’ll want to play around with these for a while until you grasp how they work. But knowing how these functions will be important as you advance deeper into the realm of Python.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.