Fix Python – What is a good practice to check if an environmental variable exists or not?

I want to check my environment for the existence of a variable, say “FOO”, in Python. For this purpose, I am using the os standard library. After reading the library’s documentation, I have figured out 2 ways to achieve my goal:
Method 1:
if “FOO” in os.environ:
pass

Method 2:
if os.getenv(“FOO”) is not None:
pass

I would like to know ….

Fix Python – How to check if a string contains an element from a list in Python

I have something like this:
extensionsToCheck = [‘.pdf’, ‘.doc’, ‘.xls’]

for extension in extensionsToCheck:
if extension in url_string:
print(url_string)

I am wondering what would be the more elegant way to do this in Python (without using the for loop)? I was thinking of something like this (like from C/C++), but it didn’t work:
if….

Fix Python – What’s the scope of a variable initialized in an if statement?

I’m new to Python, so this is probably a simple scoping question. The following code in a Python file (module) is confusing me slightly:
if __name__ == ‘__main__’:
x = 1

print x

In other languages I’ve worked in, this code would throw an exception, as the x variable is local to the if statement and should not exist outside of it. But this co….

Fix Python – Why does python use ‘else’ after for and while loops?

I understand how this construct works:
for i in range(10):
print(i)

if i == 9:
print(“Too big – I’m giving up!”)
break
else:
print(“Completed successfully”)

But I don’t understand why else is used as the keyword here, since it suggests the code in question only runs if the for block does not complete, which is the opp….

Fix Python – How to test multiple variables for equality against a single value?

I’m trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say:
x = 0
y = 1
z = 3
mylist = []

if x or y or z == 0:
mylist.append(“c”)
if x or y or z == 1:
mylist.append(“d”)
if x or y or z == 2:
mylist.appen….