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 7987913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:17:03+00:00 2026-06-04T12:17:03+00:00

I am stuck at this particular example from dive into python Example 4.18. When

  • 0

I am stuck at this particular example from dive into python

Example 4.18. When the and−or Trick Fails

>>>>a = ""
>>>>b = "second"
>>>1 and a or b
>>>>'second'

Since a is an empty string, which Python considers false in a boolean context, 1 and ” evalutes to ”, and
then ” or ‘second’ evalutes to ‘second’. Oops! That’s not what you wanted.
The and−or trick, bool and a or b, will not work like the C expression bool ? a : b when a is false in a
boolean context.

Why does it says it isn’t what the user wants, I mean 1 and “” would evaluate to False, while “” or b will evaluate to “second”, that’s perfectly what should happen, I don’t understand why is it wrong?am I missing something?

  • 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-06-04T12:17:04+00:00Added an answer on June 4, 2026 at 12:17 pm

    No, you’re not missing something.

    The expr if cond else expr inline conditional was introduced to Python 2.5 in PEP 308; before that, conditionals had to be of the full form

    if cond:
        expr
    else:
        expr
    

    However, people noticed that expr and cond or expr sort-of worked as an inline conditional, as long as the first expressions happened to evaluate to boolean true. As most things in Python are true, this syntax generally tended to work — and then, of course, sometimes fail. This was in fact the motivation for adding the “proper” inline conditional.

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

Sidebar

Related Questions

Working with OWASP's ESAPI, I found myself stuck at this particular line of code.
I've come a bit stuck this afternoon with a bug in my web application
Im stuck with this little project in C# but basically my problem is this:
I have been stuck on this for days, and was wondering if anyone had
I'm kinda stuck on this decision. My project already uses Spring and Spring Blazeds
I've been stuck with this for weeks now and have no idea where I'm
I'm stuck with this. I'm self studying assenbler and translating some basics instructions. But
I've been stuck with this simple Sql query for last 3 hours and my
I've been stuck on this issue for several days - any help is much
I'm stuck with this pretty silly thing; I got a textfile like this; Hello::140.0::Bye

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.