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

Luke Paireepinart rabidpoobear at gmail.com
Fri Jul 20 04:04:42 CEST 2007


elis aeris wrote:
> like, I am doing string substitution:
>
>
> if  x = 2243:
this will always evaluate to true.
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


More information about the Tutor mailing list