I'm a python newbie and am using The Python Bible 7 in 1 written by Florian Dedov to learn. I've been doing well but I've hit a barrier with list functions as described in the book and would like help. I can't figure out how to use the list(element) function in the book it is described as typecasts element into list. My attempt and error have been as follows
numbers= [10,22,61,29]
print(numbers(29)) which gives me type error 'list' object is not callable
How would you use this function properly?
CodePudding user response:
print(numbers(29))
needs to be print(numbers[29])
The round brackets (i)
are asking the interpreter to call a function with argument i
and square brackets [i]
is looking for index i
in an object with indexes such as a list.
CodePudding user response:
You used the wrong function in your example. The Book describes List(iterable)/ List() as List(element) so that means that the proper use of the "List(element)" function as the book describes it is to convert an iterable (like a set) into a list. The book would call this conversion a typecast. To summarize the correct use of this function would be to convert an iterable into a list.
A proper example of this would be
C=list("string")
Print(C)
This returns ['s', 't', 'r', 'i', 'n', 'g']