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

What are the Python practices suitable for beginners

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

Share

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

This article mainly explains "what are the Python practices for beginners". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what Python practices are suitable for beginners.

1. Docstring

Docstring is the abbreviation of Python document string. Docstring is the first statement of a defined module, function, class, or method in triple double quotation marks. This is the smallest example of docstring in a function.

Deffoo (): "This function doesnothing." Passprint (foo.__doc__) # Thisfunction does nothing.

The documentation string for the function should contain (one line) a brief introduction to the purpose, and the following paragraphs describe the function calling convention. There are a variety of styles, but this is one of my favorite templates:

Defsum_of_squares (nums): "" Compute the sum of squares of a list of numbers. Args: nums (`list`of `int` or `float`): a `list`of numbers. Returns: ans (`int`or `float`): Sum of squares of `nums`.Raises: AssertionError: If `nums` contain elements that are not floats nor ints. "" Try: ans = sum ([x**2for x in nums]) except: raiseAssertionError ('Input should be a list of floats or ints.') Return ans

Rawdocstring.py initiated by GitHub

2. F-string (formatted string)

You may be used to using the following command to format a string, that is,% or format ().

Name = 'World'' Hello% s'% name # Hello World' Hello {} '.format (name) # Hello World

Abandon them. Once you need to print multiple variables in a longer string, the code will quickly become confusing and difficult to understand. In any case, these formatting methods are not simple.

Pythonf-string is a game-changing tool introduced by Python 3.6. This is a readable and advanced string format syntax that embeds an expression in a string. This is done through the statement f'{expr}', where the expression is enclosed in curly braces within the f string. The expression begins with f, which precedes the single quotation mark.

Example:

Name = 'World' print (f'Hello {name}') # Hello World

You can write any grammatically valid expression in curly braces. You can also call functions in expressions!

A = [1mai 2.2 is 3] print (f'Sum of squares of {a} is {sum_of_squares (a)}') # Sum of squares of [1mai 2.2 Magi 3] is 14.84

3. Naming convention

Naming things is one of the most difficult things in computer science. You ran out of ideas. But do not know how to name temporary mediation variables But you're not the only one who can't name it.

Although difficult, there are some naming conventions in Python to "narrow" the selection of variables. They help enhance code consistency, readability, and reusability.

Therefore, if the letters themselves have no meaning, you should no longer use a single lowercase letter to name all variables, such as a, x, and so on. In addition, you should name them with meaningful, easy-to-understand and easy-to-recognize words, such as replacing uspr with user_profile.

Here are 6 tips for naming things:

Avoid naming things with a single letter, such as O, I, l. The reason is obvious.

Both variable and function names should be lowercase.

Words in variable or function names are separated by an underscore.

Private variables (for example, within a class) may start with an underscore

Words in category names should be concatenated and capitalized, such as MarioKart.

Constant names should be capitalized, such as GOLDEN_RATIO.

Thank you for your reading, the above is the content of "what is the Python practice for beginners"? after the study of this article, I believe you have a deeper understanding of what the Python practice is suitable for beginners, 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

Development

Wechat

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

12
Report