[Tutor] python: how do I create a list of definitions?

Eric Brunson brunson at brunson.com
Fri Jul 20 04:14:11 CEST 2007


Luke Paireepinart wrote:
> elis aeris wrote:
>   
>> like, I am doing string substitution:
>>
>>
>> if  x = 2243:
>>     
> this will always evaluate to true.
>   

Good eye, I missed that completely...

However, that will actually throw an exception.

 >>> if x = 1:
  File "<stdin>", line 1
    if x = 1:
         ^
SyntaxError: invalid syntax

Guido did it that way just to avoid programming errors like that.  
Python ain't C (thankfully).

> x is being assigned the value of 2243.  2243 is being returned by the 
> assignment.
> You can observe this in the following situation:
>  >>> y = x = 2243
>  >>> y
> 2243
>
> As you can see, (x = 2243) assigns the variable name to the integer 
> 2243, then assigns y to this integer object as well.
> so in essence you're saying
> if 2243:
> which is the same as saying 'if ' and anything nonzero, which is True.
> so basically the following line
>   
>>     string = string + "e"
>>     
> is always being executed.
>   
>> if  x = 2234:
>>    string = string + "p"
>>     
> same with this one.
>   
>> how do I create this:
>> list = [
>>             (2342,p)
>>             (4234,e)
>>           and so forth,
>>        ]
>>
>> so I can use it like this:
>>
>> for a in range(10):
>>     If x = list[a][0]:
>>     
> If is invalid.  Python is case sensitive.  'if' and 'If' are not the same.
> Also, you're using an assignment instead of a comparison again.
>   
>>         string = string + list[a][1]
>>
>>
>> ?
>>     
> You could probably solve this easily with a dictionary.
> -Luke
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list