I love both the type and the id commands in Python. They are self explanatory and can save you from a lot of trouble.
For example, you want to create an one item tuple containing “one” and combine with another tuple containing (9,9,0). See the code below.
Traceback (most recent call last):
File “<pyshell#237>”, line 1, in <module>
TypeError: can only concatenate tuple (not “str”) to tuple
What! we have a syntax error! What went wrong? use the type() command to find out!!
Oh, ho! Tuple 2 is a string object!! How! Well, we missed a comma! Correct the codes now.
(9, 9, 1)
>>> tuple2=(“one”,) #notice the use of comma
The id() function is used to return the memory address of an object, which I normally don’t use much, but I use the type() command most frequently. It helps me a lot debugging my codes. Sometimes, you can use the id() command to prove immutability of numbers in python. In other words, if you change the variable value, a new memory address will be created.
You can apply the same principle to lists. As lists are mutable, adding or deleting content does not change their memory address.
[1, 2, 3, 4, 5, 30]
[1, 2, 3, 4, 5]