Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:38:32+00:00 2026-05-15T15:38:32+00:00

In python if you write something like foo==bar and spam or eggs python appears

  • 0

In python if you write something like

foo==bar and spam or eggs

python appears to return spam if the boolean statement is true and eggs otherwise. Could someone explain this behaviour? Why is the expression not being evaluated like one long boolean?

Edit: Specifically, I’m trying to figure out the mechanism why ‘spam’ or ‘eggs’ is being returned as the result of the expression.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:38:33+00:00Added an answer on May 15, 2026 at 3:38 pm

    The operators and and or are short-circuiting which means that if the result of the expression can be deduced from evaluating only the first operand, the second is not evaluated. For example if you have the expression a or b and a evaluates to true then it doesn’t matter what b is, the result of the expression is true so b is not evaluated. They actually work as follows:

    • a and b: If a is falsey, b is not evaluated and a is returned, otherwise b is returned.
    • a or b: If a is truthy, b is not evaluated and a is returned, otherwise b is returned.

    Falsey and truthy refer to values that evaluate to false or true in a boolean context.

    However this and/or idiom was useful back in the days when there was no better alternative, but now there is a better way:

    spam if foo==bar else eggs
    

    The problem with the and/or idiom (apart from it being confusing to beginners) is that it gives the wrong result if the condition is true but spam evaluates to a falsey value (e.g. the empty string). For this reason you should avoid it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Both C# and Python allow named arguments, so you can write something like: foo(bar:1)
Until like one hour ago, I was convinced that in python Foo ().bar ()
I'd like to be able to write something like this in python: a =
In Python, it is tedious to write: print foo is + bar + '.'
I need to create a file with python, in the directory: foo/bar/baz/filename.fil The only
I am learning Python (I have a C/C++ background). I need to write something
I typically write my scripts with a structure like s #!/usr/bin/python import stuff def
Python optparse works very good when script usage is something like this %prog [options]
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.