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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:36:30+00:00 2026-05-27T10:36:30+00:00

In Python, there are several values that count as False : >>> if False

  • 0

In Python, there are several values that count as False:

>>> if False or None or 0 or 0.0 or '' or [] or {} or set():
...   print 'True :)'
... else:
...   print 'False :('
... 
False :(

So, somewhere in my code I’m doing something like this:

    some_var = other_var or 'default' # Some default value

other_var could be None or any other value, if it’s None, some_var should be filled with 'default'.

But I kept having some troubles with this, so, debugging my code I found out that other_var sometimes was 0 (or any other value that counts as False), and some_var was filled with 'default' instead of 0.

So, is there a way to accomplish what I’m trying rather than doing this?

if other_var is None:
    some_var = 'default'
else:
    some_var = other_var

No is a valid answer.

  • 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-27T10:36:30+00:00Added an answer on May 27, 2026 at 10:36 am

    There is the Conditional Expression (aka the “ternary operator”) syntax:

    some_var = 'default' if other_var is None else other_var
    

    Happy coding.

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

Sidebar

Related Questions

I am trying to parse some C# code to Python. There are several regular
Is there a Python class that wraps the file interface (read, write etc.) around
Is there a python library for encoding ascii data to 7-bit GSM character set
My Python code generates a random number between 0 and 35 and stores that
List Comprehension is a very useful code mechanism that is found in several languages,
I am writing a simple Python web application that consists of several pages of
I have a block of code that basically intializes several classes, but they are
My question is: are there minimisation algorithms, preferably implemented in Python, that can operate
There are several different ways to check if a Python dictionary contains a specific
I've seen the usage of exit status 3 in several python scripts that restart

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.