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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:31:10+00:00 2026-06-02T21:31:10+00:00

After having a lot of trouble trying to reduce nested ifs (and a script

  • 0

After having a lot of trouble trying to reduce nested ifs (and a script that stopped working), I realized perhaps I am having the wrong idea of what and how boolean stuff works in python.

I had this (working fine but contorted):

if (not gotside):
    if (j > 1):
        if (j < a.shape[1] - 1):
            if a[i, j+unit]:
                print "only now I do stuff!"

And tried this (looking sreamlined by not working as desired):

if (not gotside) and (j > 1) and (j < a.shape[1] - 1) and a[i, j+unit]:
    print "I'm well tested but not so indented..."

Then I tried to use “or” instead of “and”, but didn’ work, because (later I found) when you use x and y or even x or y you get one of x, y, and not one of True, False, acording to docs.

So, I don’t know how I can put a handful of tests one after another (preferrably in the same line, with boolean operators) in a way that the whole expression returns False as soon as the first test evaluates to False.

Thanks for reading!

  • 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-02T21:31:11+00:00Added an answer on June 2, 2026 at 9:31 pm

    Your example should work.

    if x and y and z:
        ...
    

    Should happen only if none of x, y and z evaluate to False, and in Python, and short-circuits, so will return the False value as soon as one item fails. We can show this quite easily:

    >>> def test(n):
    ...     print("test", n)
    ...     return n
    ... 
    >>> test(1) and test(2)
    test 1
    test 2
    2
    >>> test(0) and test(2)
    test 0
    0
    >>> test(0) and test(2) and test(3)
    test 0
    0
    >>> test(1) and test(0) and test(3)
    test 1
    test 0
    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a lot of trouble with Ruby after coming back to it from
I'm working with Core Data and having a lot of trouble's getting data into
I am having a lot of trouble trying to retrieve the matrix element A(m,n)
I am having a lot of trouble installing extensions in Joomla, after looking into
Folks, I am having some trouble working with the After hook. I have organized
After having a period of logshipping failures going unnoticed (due to a stopped SQL
I'm having the weird problem that after having javascript inject some dom-elements the css-rules
I'm trying to use MVC for a new project after having been around the
I'm trying to add a UIButton to a UIView, but am having some trouble
We're having a lot of trouble tracking down the source of \u2028 (Line Separator)

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.