Strangeness with type constructors in python -


In the Python I can work like this:

  d = dict () I = int () f = float () l = list ()   

but there is no constructor for strings

  & gt; & Gt; & Gt; S = string () traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; Name: Error: Name 'string' is not defined   

Why is it so? Or, is there a constructor for string types?

In addition, after the above definitions,

  d ['a'] = 1   

works, but

  & gt; & Gt; & Gt; L [0] = 1 traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; Index Error: List assignment index   

is not outside the category.

Can anyone explain this to me?

As noted by other people, you should see str , String

But to answer your second question, lists can not be extended to assignments. If you try to assign a list outside the limits, you get an error; On the other hand, the dictionary is in no sense; They have only defined keys and undefined keys

Think of a dictionary as a bag of objects, and of course, a list with a number of coaches as a tray. You can throw things in the bag at any time, but if there is no such compartment, you can not put anything in the tray bin. You have to make a compartment, use it or something similar. Since not in your "tray" so far, l [0] = x fails.

Comments