There are three most common parentheses in the Python language, namely: parentheses (), square brackets [], curly brackets {}; their functions are also different, and they are used to represent different Python basic built-in data types.
1、 Parentheses () in Python:
Represents the tuple meta-ancestor data type, which is an immutable sequence. The creation method is very simple, most of the time it is enclosed in parentheses.
2、 Brackets [] in Python:
Represents the list data type, the list is a variable sequence. The creation method is simple and special.
3、 Curly braces {} in Python:
Represents the dict dictionary data type. Dictionary is the only built-in mapping type in Python. The values in the dictionary have no special order, but they are all stored under a specific key. The keys can be numbers, strings or even tuples.
In Python, there are two cases where parentheses can be omitted.
Use generator as the only parameter of the function
Tuples as the keys of the dictionary
Examples are as follows
Normal version
s = sum((i for i in range(10)))
Omit parentheses
s = sum(i for i in range(10))
Normal version
s = “”.join((i for i in “hello world”))
Omit parentheses
s = “”.join(i for i in “hello world”)
dictionary
s = {(1, 2, 3): “hello world”}
print(s[(1, 2, 3)], s[1, 2, 3])
Output result
hello world hello world
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts