Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

You can also do this. It causes a new empty list to be created in each append call.

>>> li = []
>>> li.append([])
>>> li.append([])
>>> li.append([])
>>> for k in li: print(id(k))
...
4315469256
4315564552
4315564808

Don't use index to loop over a sequence.

Don't:

for i in range(len(tab)):
print(tab[i])

Do:

for elem in tab:


print(elem)

for will automate most iteration operations for you.

Use enumerate if you really need both the index and the element.

for i, elem in enumerate(tab):


print((i, elem))

Be careful when using "==" to check against True or False

if (var == True):
# this will execute if var is True or 1, 1.0, 1L

if (var != True):
# this will execute if var is neither True nor 1

if (var == False):
# this will execute if var is False or 0 (or 0.0, 0L, 0j)

if (var == None):
# only execute if var is None

if var:
# execute if var is a non-empty string/list/dictionary/tuple, non-0, etc

if not var:
# execute if var is "", {}, [], (), 0, None, etc.

if var is True:
# only execute if var is boolean True, not 1

if var is False:
# only execute if var is boolean False, not 0

if var is None:

GoalKicker.com – Python® Notes for Professionals 763

You might also like