This article mainly introduces how to declare a single element in Python to contain tuples. This article is a summary of practical experience. If this problem is not met, it may not be noticed, if you need a friend, you can see this loss when debugging the program. I don't know why Python uses parentheses to declare the boundary. It is estimated that no suitable symbol can be found. Brackets are used to declare the list, curly brackets are used to declare the dictionary, and the element number can only be declared using brackets. Friends who have programming experiences in other languages know that brackets represent priority in other languages, and Python can also be used to indicate priority, which leads to the following idiotic problems.
The Code is as follows:
# Encoding = UTF-8
Obj = ('tuple ')
Print obj
Print type (obj)
Print len (obj)
Execution result
The Code is as follows:
Tuple
5
I originally wanted to declare a tuples with only one element, while Python probably thought that you only declare a string, so the result is converted into a tuples using the obj variable. This error is very invisible and cannot be debugged.
Solution: Add a comma at the end.
The Code is as follows:
# Encoding = UTF-8
Obj = ('tuple ',)
Print obj
Print type (obj)
Print len (obj)
Execution result
The Code is as follows:
('Tuple ',)
1
Use the tuple Keyword: unexpected results will be returned.
The Code is as follows:
# Encoding = UTF-8
Obj = tuple ('tuple ')
Print obj
Print type (obj)
Print len (obj)
Execution result
The Code is as follows:
('T', 'U', 'P', 'l', 'E ')
5