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 Python coding

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Python coding". In daily operation, I believe many people have doubts about how to use Python coding. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python coding". Next, please follow the editor to study!

Before we start coding, we need to understand two things: one is the coding rules, and the other is what data is.

The coding of this article refers to writing the Python high-level programming language. Since it is a language, there must be grammar, and you also need material. You can think of it as Chinese or English. The grammar of the language is equivalent to the rules of coding, which is the programming basis of Python. When the basic syntax is completed, we can enter the actual combat phase of the project.

Next, let's introduce the data.

To put it simply, data is anything in the computer, such as music, movies, articles, etc. Python programming is to process it using its own syntax rules and then present the desired data results. So you can think of a program or code as a clothing factory: cloth is the data used in the program, machines process data according to syntax, and clothes are the result of code output.

Sometimes there is too much data to understand. Classifying it is a method, which is very easy to understand and deal with.

Learn to write notes to facilitate you, me and him

Comments are the explanations that are added to the code. The code tells the reader what to do, while the comment tells the reader why. This is especially important when learning programming, especially at the beginning to get into the habit of writing comments, don't bother, because comments are easy for people who read the code to understand.

In practical work, projects are generally very large and need to be completed in collaboration. If you read it to the next person without writing a note, then reading can become a particularly painful thing. Sometimes the beauty of the code depends on whether the comments are comprehensive. However, the current general situation is that many programmers in China, even veteran programmers who have worked for many years, do not like to write notes. Because of the trouble, they thought it was more work. This view is wrong, because even if you write your own code, you may not remember what you wrote a few years later. In addition, some large company code comments are very comprehensive, such as Google, Oracle and so on.

In Python, a single-line comment is indicated by a pound sign (#), and the comment is what follows #; multiple-line comments are represented by a pair of three quotes ('') or a pair of three double quotes ("). Single-line comments are generally used for the explanation of a line, while multi-line comments are generally used for the interpretation of the entire text or a certain area of code, where three double quotes indicate the description of the entire document. For these comments, the Python interpreter does not perform specific content. In the following example, the Python interpreter ignores comments and outputs only Hello World.

Example: multi-line comments and single-line comments

This chapter is the third day: the contents of tamping the foundation mainly include: data types, variables, etc. Here is a multi-line comment that can write many lines. # printout Hello wordwords # Note: please leave a space print ('Hello wordstones') between # and the comment content

The result of the run is as follows.

Hello World!

Note: not every line of code needs comments, only key areas, such as new grammar points, key issues, important details, conclusions, and so on.

Constants and variables

Constant, as the name implies, is the amount that the value cannot be changed, such as 5, 10, etc., or the text of a string.

What corresponds to a constant is a variable, which, as its name implies, is a quantity whose value is changing all the time. Because the value is changing, we need to give it a name, which is an identifier. In Python programming, we call the identifier the variable name and use the equal sign (=) to associate the variable name with the value. The specific syntax is:

Variable name = value

Example:

# define the variable and use the print () function to print out # my_name is the variable name, Liu Dehua is the value # the variable name remains unchanged, but the value can be changed, for example, the value is changed to Jay Chou my_name = "Andy Lau" print (my_name) my_name = "Jay Chou" print (my_name)

The result of the run is as follows.

Andy Lau and Jay Chou

Note: variables are stored in memory. The Python language is case-sensitive, for example, my_name and My_name are two different variables for Python.

Variable naming rules

There are certain rules for variable naming. If you violate the rules, you will make an error, as shown below.

Variable names can only start with a letter or an underscore, not with a number, but can end with a number.

Example:

Introduction to variable rules: the first statement is wrong; the second statement is correct''3_log =' This is a log file'log_3 = 'This is a log file'

The result of the run is as follows.

3_log = 'This is a log file' ^ SyntaxError: invalid token

The variable name cannot contain spaces, otherwise it is considered a syntax error. For example, my name is wrong, and the solution is to connect it with an underscore (_) and become my_name.

You cannot use keywords in Python as variable names.

Variable naming method

Under the premise of conforming to the variable naming rules, the variable name had better be short and easy to understand, that is, the meaning of the variable name can be seen from it. For example, my_name must be easier to understand than a (never use a, b, c as variable names).

When a variable needs to be represented by more than two words, there are two common naming methods.

The first naming method

Hump case, that is, the first letter of the first word is lowercase, and the first letter of the second word is capitalized, such as firstName, lastName. You can also capitalize the first letter of each word, such as FirstName, LastName, CamelCase. It is also called the Pascal nomenclature.

The second naming method

Two words cannot be concatenated directly with a hyphen (-) or a space, but can be concatenated with an underscore, such as first_name or last_name.

At this point, the study on "how to use Python coding" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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