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 statements does Python have?

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

Share

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

This article will explain in detail what sentences there are about Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

I. Common commands

Before introducing the Python statement, let's introduce a few useful Python commands.

Dir (module name or class name or variable name or expression name): gets a list of attributes for the current module, variable corresponding type, expression evaluated value corresponding class

Type (variable name or expression name): gets the corresponding type of the evaluated value of the variable or expression

Help (class name) or help (class name). Property or method name) or help (variable. Property or method name), such as: help (name.count): give a description of the help corresponding to the parameter. If it is a system type or class instance, use the variable name. Method or property to find help for that type or class.

II. Suggestions for naming Python

1. File name: all lowercase, underscore can be used

2. Function name: lowercase, underscore style words can be used to increase readability. For example, myfunction,my_example_function, mixed case is not recommended.

3. Variables: the variable names are all lowercase, and the words are connected by an underscore.

III. The basic program statements of Python

1. Comment statement

Comments in Python start with # and can only be commented on on a single line. If there are multiple lines, you can add a # sign on each line.

Note: this statement is wrong. There is also a way to support multiline comments in Python, but this way does not appear for comment purposes.

2. Import statement

The import statement introduces a defined module for the module that needs to be used. There are two grammars:

Import module name

From module name import method or property name

From module name import *

The difference between the three is that the first method introduces the entire module, and you can use all the properties and methods of the module, which need to be called with the module name before using it; the second method is to introduce only a property or method. The third way introduces all methods and properties with the * sign. Second, the properties and methods introduced in the second and third ways are used without module name. However, this method cannot be used if multiple modules and reference modules have the same methods or properties.

For example:

> import math

> > sqrt (4)

Traceback (most recent call last):

File "", line 1, in

Sqrt (4)

NameError: name 'sqrt' is not defined

> > math.sqrt (4)

2.0

> from math import sqrt

> > sqrt (4)

2.0

3. Def function definition statement

The function definition in Python starts with def, and the syntax is as follows:

Def function name (variable list):

... . Sentence style

Note: the parenthesis of the function must be followed by a colon, the declaration of the function must be written at the top, and the declaration of the statement and the function must be indented by 4 spaces (the above example is represented by four dots, see the content of the nesting format section of the Python code below). The return of the function can be returned with return, or it can end automatically without specifying return.

Example:

Def add (N1 and N2):

Return n1+n2

4. If conditional statement

The syntax is as follows:

If condition 1:

... . Content 1 executed

Elif condition 2:

... . Content 2 executed

Elif condition 3:

... . Content 3 executed

Else:

... . Content 4 executed

Note: the overall usage of if is similar to c, except that each keyword statement must be followed by a colon and each execution must be indented by four spaces.

For example:

If add (1B4) > 10:

... .print ('and greater than 10')

Else:

... .print ('and less than or equal to 10')

5. For loop statement

There is a big difference between for statement and C language, which involves some complex data types "iteration" and "sequence". We will not explain them here, but we will learn more about complex data types later. Only the basic syntax is introduced here:

For iterating_var in sequence:

... . Sentence block Wuxi Gynecology Hospital ranking http://www.csfk0731.com/

6. While loop statement

The while loop is similar to the c language with the following syntax

While conditional expression:

... . Sentence block

Note:

1. A conditional expression must be followed by a colon

2. The while statement supports break and continue similar to c

3. Statement blocks and while conditional statements must be indented by 4 spaces

4. In addition to the differences with c language in format, only pre-while statements are supported in Python, unlike C language while statements, which can start or end in the loop body.

Example:

> while True:

Name=input ('Your name:')

If (len (name) > 0):

Print ('welcome youthful girls')

Else:

Print ("Good bye!")

Break

Output:

Your name:jwp

Welcome you,jwp!

Your name:Lisi

Welcome you,Lisi!

Your name:

Good bye!

A supplementary description of the use of an interface, in an interactive way, the input statement at the end of the need to enter twice, otherwise the editor thinks that the input is not over.

7. Code nesting format of Python

In fact, this part has already been mentioned in the previous part, which mainly includes the following contents:

Python denotes code nesting by indentation. Each indent represents the same indentation of the block as the upper nested code. The indentation can be the TAB key and a specified number of spaces. This indented code block is similar to the code block enclosed by {} of the C language. Like C language nesting, you can use curly braces once more, the same indented code block can be indented more than once on the basis of the upper code, as long as the indentation of the code block is consistent.

There is no mandatory requirement for Python indentation. It only requires that the body of the same statement must be indented consistently. We can even use different indentation methods for different conditions of the same if statement. We only need to satisfy the same code block of the same layer, and there is no indentation in the first layer, but it is recommended to use the same number of indentation characters in one indentation when writing code, twice, and so on.

There can be no spaces before the first sentence style, otherwise the compilation will report an error.

Python's function definition, if, while, and for statements must be followed by a colon, followed by an indentation of the code.

The code nesting of Python code indentation is very different from that of c language, but the use of indented nested code helps to improve the readability of the code, and you can know the same level of statement body scope without having to match parentheses. Just note that when editing the same file with different editors, the same TAB key may have different indents, and some editors will forcibly convert consecutive N spaces to TAB keys, which may lead to indentation inconsistencies.

This is the end of this article on "what sentences in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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