0

Is there a shorter form for something like:

if v or v == 0:

v can be None or an int. If it is not None or 0, go in.

3
  • 3
    You can use if v ? (but your code example contradicts your explanation somewhat...) Commented Dec 4, 2014 at 14:22
  • Your condition is false when v = None. Commented Dec 4, 2014 at 14:22
  • I don't believe you will be able to get shorter than that. You have 2 very different conditions. Commented Dec 4, 2014 at 14:28

2 Answers 2

4

What about:

if v is not None:
    # code

To test it:

l = [None, 0, 13, -123]

for i in l:
    if i is not None:
        print i

Ouput:

0
13
-123
Sign up to request clarification or add additional context in comments.

1 Comment

Difficult to tell what the OP is really after, but this doesn't quite match If it is not None or 0, go in.
-1

None and 0 all represent false, other int except zero are represent true. If you just don't want false,you can use:

if not v:
     pass

1 Comment

if not v is when it is false - the opposite of If you just don't want false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.