Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use commas in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use a comma in Python". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use a comma in Python".

1 comma split list, tuple, each element in the dictionary

As you all know in the list, the square brackets, or square brackets, enclose several elements, which are separated by commas.

Attention, it must be an English comma. Sometimes it is easy to make mistakes when you switch between Chinese and English input methods incorrectly. in fact, it is easier to tell them apart: fat in Chinese and thin in English. If you finish typing the comma and find that there is a little more space behind you, be careful.

Tuples are similar to lists, except with parentheses, that is, parentheses, where each element is separated by a comma.

The dictionary uses the structure corresponding to kv. Each group is separated by a colon and appears in pairs. A pair is actually an element, or item, and each item is separated by a comma.

2 add a trailing comma at the end of lists, tuples and dictionaries

What I just said is that each element is separated by a comma. Why should we say a trail here?

Note that this is an officially approved and recommended way of writing. Let's look directly at the code:

[1, 2, 3,]

('await,' baked, 'clocked,)

D = {

"A": [1,5]

"B": [6, 7], # last trailing comma is optional but good style

}

If there are many elements in lists, tuples, or dictionaries, we often need to wrap lines to multiple lines, making it easier to add more elements, so that commas are added to each line to make it easier to write code. This is the first benefit.

Another benefit is to avoid forgetting the hard-to-diagnose errors caused by commas. For example:

X = [

"de8ug"

"jack"

"tom"

]

If you forget to write a comma, it is easy to make mistakes. You might as well write it all in the first place. And this is a list of strings. I'm used to wrapping lines. I write quotation marks and commas first, then copy multiple lines down with a direct shortcut, and then add data.

3 comma as a string pause symbol and a function parameter separator

A general function has multiple parameters, which are separated by commas when writing code. It should be noted here that these are all English commas. Sometimes, it is important to note that there may be a Chinese sentence in some parameters, and there are also commas, such as printing data, menu descriptions, or writing logs. In simple words, the code will be directly in English, Chinese sentences in advance to write a template, put into different variables, easy to use, later modification can also be unified.

Thank you for your reading, the above is the content of "how to use a comma in Python". After the study of this article, I believe you have a deeper understanding of how to use a comma in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report