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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check what is the value in $tweet similar problem http://bytes.com/topic/php/answers/875805-mysql_num_rows-argument-not-being-recognized-any-help-would-appreciated… May 17, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer Classes mean is a ... (indefinite) IDs mean is the… May 17, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer No. If you only want to do something when an… May 17, 2026 at 12:16 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Both C# and Python allow named arguments, so you can write something like: foo(bar:1)
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()
In python you can do something like this to import a module using a
I'm wondering if Python has anything like the C# anonymous classes feature. To clarify,
In Python, functions like str(), int(), float(), etc. are generally used to perform type
Is there a Python class that wraps the file interface (read, write etc.) around
I want to write some python package installing script in Python into virtualenv. I
I want to write a python function decorator that tests that certain arguments to
I want to read and write python-source-files from the file system in a thread-safe

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.