Fix Python – Only add to a dict if a condition is met

I am using urllib.urlencode to build web POST parameters, however there are a few values I only want to be added if a value other than None exists for them.
apple = ‘green’
orange = ‘orange’
params = urllib.urlencode({
‘apple’: apple,
‘orange’: orange
})

That works fine, however if I make the orange variable optional, how can I prevent it….

Fix Python – Elegant way to check if a nested key exists in a dict?

Is there are more readable way to check if a key buried in a dict exists without checking each level independently?
Lets say I need to get this value in a object buried (example taken from Wikidata):
x = s[‘mainsnak’][‘datavalue’][‘value’][‘numeric-id’]

To make sure that this does not end with a runtime error it is necessary to either check every….

Fix Python – Instance variables vs. class variables in Python

I have Python classes, of which I need only one instance at runtime, so it would be sufficient to have the attributes only once per class and not per instance. If there would be more than one instance (which won’t happen), all instance should have the same configuration. I wonder which of the following options would be better or more “idiomatic” P….

Fix Python – What is a None value?

I have been studying Python, and I read a chapter which describes the None value, but unfortunately this book isn’t very clear at some points. I thought that I would find the answer to my question, if I share it there.
I want to know what the None value is and what do you use it for?
And also, I don’t get this part of the book:

Assigning a value ….

Fix Python – More elegant way of declaring multiple variables at the same time

To declare multiple variables at the “same time” I would do:
a, b = True, False

But if I had to declare much more variables, it turns less and less elegant:
a, b, c, d, e, f, g, h, i, j = True, True, True, True, True, False, True ,True , True, True

Is there a better / elegant / convenient way to do this?
This must be very basic, but if I do used….

Fix Python – How do I create a multiline Python string with inline variables?

I am looking for a clean way to use variables within a multiline Python string. Say I wanted to do the following:
string1 = go
string2 = now
string3 = great

“””
I will $string1 there
I will go $string2
$string3
“””

I’m looking to see if there is something similar to $ in Perl to indicate a variable in the Python syntax.
If not – what is the clea….