How to omit parentheses in Python

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

How to omit parentheses in Python
How to wrap in python code
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to save text files in python
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to uninstall python plugin
How to understand python objects
How to use python tuples
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to create a Python virtual environment in Ubuntu 14.04
How to install Helm in Ubuntu
python how to view webpage code
How to use hanlp in ubuntu
How to use python thread pool
How to write python configuration file
How to install PHP7.4 in CentOS
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install HDP2.6 in Centos7.2