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 is the summary of the foundation of python?

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

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how the summary of the python foundation is, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

One of the design goals of Python is to make the code highly readable. Enterprise E: 1554 159. it is designed to use punctuation marks and English words commonly used in other languages to make the code look clean and beautiful. It does not need to rewrite declaration statements like other static languages such as C and Pascal, nor does it often have special cases and surprises like their syntax.

Indent

Python developers deliberately force programmers to develop good programming habits by preventing programs that violate indentation rules from being compiled. And the Python language uses indentation to indicate the beginning and exit of a block of statements (Off-side rules) instead of using curly braces or certain keywords. Increasing indentation indicates the beginning of a statement block, while decreasing indentation indicates the exit of a statement block. Indentation becomes part of the grammar. For example, the if statement:

Python3

According to the PEP, four spaces must be used to represent each level of indentation (it is not clear what the stipulation of four spaces is, the number of spaces can be customized in actual writing, but the number of spaces between each level of indentation must be equal). Although the use of Tab characters and other numbers of spaces can be compiled, it does not conform to the coding specification. Support for Tab characters and other numbers of spaces is only for compatibility with very old Python programs and some problematic editing programs.

Control statement

An if statement that runs a block of statements when the condition is set. Often used in conjunction with else, elif (equivalent to else if).

For statements, traversing iterators such as lists, strings, dictionaries, collections, and so on, processing each element in the iterator in turn.

The while statement, which runs the statement block in a loop when the condition is true.

Try statement, used in conjunction with except,finally to handle exceptions that occur while the program is running.

The class statement, which defines the type.

Def statements that define methods for functions and types.

The pass statement, indicating that the row is empty and no action is run.

The assert statement, which is used to test whether the running conditions are met during the debugging phase of the program.

The with statement, the syntax defined later by Python2.6, runs the statement block in a scenario. For example, encrypt before running a statement block, and then decrypt it after the statement block runs and exits.

The yield statement, used within the iterator function, returns an element. Since Python version 2.5. This statement becomes an operator.

Raise statement to create an error.

Import statement to import a module or package.

From import statement to import a module from a package or an object from a module.

The import as statement that assigns the imported object to a variable.

In statement to determine whether an object is in a string / list / tuple.

Expression.

The expression of Python is written in a way similar to that of CCompact +. It's just that there are some differences in writing.

The main arithmetical operators are similar to C _ plus. +, -, *, /, /, *,% means addition or positive, subtraction or negative, multiplication, division, division, multiplication, complement and remainder, respectively. > sum (x * x for x in range (10)) 285Python uses lambda to represent anonymous functions. The anonymous function body can only be an expression. For example: > add=lambda x, y: X + y > add (3pm 2) 5

Python uses y if cond else x to represent conditional expressions. It means that when cond is true, the value of the expression is y, otherwise it is x. The equivalent of cond?y:x in C++ and Java.

Python distinguishes lists (list) and tuples (tuple). The way list is written is [1magin 2pr 3], while the way tuple is written is (1pm 2p3). You can change the elements in the list, not the tuple. In some cases, the parentheses of tuple can be omitted. Tuple has special handling for assignment statements. Therefore, you can assign values to multiple variables at the same time, such as:

> XBM y = 1. XBI also assigns a value to XBI y. The final result: Xray 1, YQ 2.

In particular, you can use the following form to exchange the values of two variables:

> x, yellowy, x # final result: yellow1, xroom2

Python uses'(single quotation marks) and "(double quotation marks) to represent strings. Unlike Perl, Unix Shell, or Ruby, Groovy and other languages, the two symbols serve the same purpose. In general, if double quotes appear in a string, single quotation marks are used to represent the string; otherwise, double quotation marks are used. If none of them show up, choose according to your personal preference. The (backslash) that appears in the string is interpreted as a special character, such as\ n represents a newline character. The r before the expression indicates that Python does not interpret the\ that appears in the string. This type of writing is often used to write regular expressions or Windows file paths.

Python supports list cutting (list slices), which allows you to get a portion of the complete list. The types that support cutting operations are str, bytes, list, tuple and so on. Its syntax is... [left:right] or... [left:right:stride]. Assuming that the value of the nums variable is [1, 3, 5, 7, 8, 13, 20], the following statements are true:

Nums [2:5] = = [5,7,8] cuts from elements with subscript 2 to elements with subscript 5, but does not include elements with subscript 5.

Nums [1:] = = [3, 5, 7, 8, 13, 20] cut to the last element.

Nums [:-3] = [1, 3, 5, 7] cuts from the original element to the penultimate element.

Nums [:] = [1, 3, 5, 7, 8, 13, 20] returns all elements. Changing the new list will not affect nums.

Nums [1:5:2] = = [3,7] cuts from the element with subscript 1 to the element with subscript 5 but does not contain the element with subscript 5, and the step size is 2.

Function

Python's functions support recursion, default parameter values, and variable parameters, but do not support function overloading. In order to enhance the readability of the code, you can write "Documentation Strings" (or docstrings for short) after the function to explain the function of the function, the type and meaning of the parameter, the type and range of the return value, and so on. You can use the built-in function help () to print out the help for using the function. For example:

Method of object

The method of an object is a function that is bound to an object. The syntax for calling object methods is instance.method (arguments). It is equivalent to calling Class.method (instance, arguments). When defining an object method, you must explicitly define the first parameter, which typically uses the self name to access the object's internal data. The self here is equivalent to the this variable in Candlesome Java, but we can also use any other legal parameter names, such as this and mine, etc. Self is not exactly the same as the this in Candlemagne Java. It can be regarded as a habitual usage. We can pass in any other legal name, such as:

Python knows some special method names that start with "" and end with "" that are used to implement operator overloading and a variety of special functions.

Types

Python adopts dynamic type system. At compile time, Python does not check whether the object has the called method or property, but does not check until run time. So an exception may be thrown when manipulating an object. However, although Python uses a dynamic type system, it is also strongly typed. Python forbids operations that are not clearly defined, such as adding strings to numbers.

Like other object-oriented languages, Python allows programmers to define types. To construct an object, you only need to call the type like a function, for example, for the previously defined Fish type, use Fish (). The type itself is an object of the special type type (the type type itself is also a type object), and this special design allows reflection programming of the type.

Python has a rich set of data types built in. Compared with Java and C++, these data types effectively reduce the length of code. The following list briefly describes the Python built-in data types (for Python 3.x):

1240

In addition to various data types, the Python language also uses types to represent functions, modules, types themselves, methods of objects, compiled Python code, runtime information, and so on. Therefore, Python is very dynamic.

Mathematical operation

Python uses operators similar to C and Java to support the mathematical operations of integers and floating-point numbers. At the same time, it also supports complex operations and integer operations of infinite digits (actually limited by the ability of the computer). Except for the absolute value function abs (), most mathematical functions are in the math and cmath modules. The former is used for real operations, while the latter is used for complex operations. You need to import them first when using them, such as:

> import math > print (math.sin (math.pi/2)) 1.0

The fractions module is used to support fractional operations, and the decimal module is used to support high-precision floating-point operations.

Python defines that the value of the remainder operation a% b is in the open interval [0, b]. If b is negative, the open interval becomes (b, 0]. This is a very common way of definition. But it actually depends on the definition of divisible. In order to make the equation: B * (a / b) + a% b = a true, divisible operation needs to take a value in the direction of negative infinitesimal. For example, the result of 7 / 3 is 2, while the result of (- 7) / 3 is-3. This algorithm is different from many other programming languages, it should be noted that their division operation will take a value in the direction of 0.

Python allows you to write two comparison operators in conjunction with each other, as is commonly used in mathematics. For example, a < b < c is equivalent to a < b and b < c. C++ 's result is different from Python in that it first calculates a < b, gets one of 0 or 1 values based on the size of the two, and then compares it with c.

This is the end of the summary on the basis of python. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can 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

Database

Wechat

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

12
Report