Home >Backend Development >Python Tutorial >What are the basics of python language?

What are the basics of python language?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-12 16:12:585926browse

What are the basics of python language?

#What are the basic knowledge of python language? Let me introduce them one by one below:

Data types

Commonly used data types: number, string, list (array), tuple (tuple), dict (dictionary)

Number (number)

The main numbers include: int (signed integer), long( Long integer type), float (floating point number), complex (plural number)

var1 = 1(int)
var2 = 51924361L(long)
var3 = 1.2(float)
var4 = 3e+26J(complex)

String

Strings are identified by single quotes or double quotes. Numbers, letters, and underscores can be placed inside quotation marks or double quotation marks

Related recommendations: "python video tutorial"

str1 = "123456789"
str2 = '我们都在学Python'

Arrays and tuples

List (list) and Tuple (tuple) are the most frequently used data types in Python.

Lists can complete the data structure implementation of most collection classes. It supports characters, numbers, strings and can even contain lists (i.e. nested).

Lists are marked with [ ] and are the most common composite data type in Python.

Tuples are marked with (), and internal elements are separated by commas. However, tuples cannot be assigned values ​​twice and are equivalent to read-only lists.

The difference between List (list) and Tuple (tuple): List is a mutable sequence, and Tuple is an immutable sequence.

To put it bluntly, List can be added, deleted, modified, and checked, but tuple can only be viewed. (Immutable)

Dictionary

Dictionary is to find the value based on the key

Conditional judgment statement

Conditional judgment: Simply put, it is executed if the condition is met, and not executed if it is not satisfied. This is the same as our life. For example, when buying fruits, chestnuts, watermelons are 10 yuan each, and apples are 5 yuan each, and bananas 3 yuan each. If you bring 3 yuan, you can buy a banana, and if you bring 5 yuan, you can buy an apple.

A simple sentence: Execute from top to bottom. If you are satisfied, it will end. If you are not satisfied, go down.

Loop (iteration)

Loops are often used. The main reason for loops is efficiency. Think about it, if you want to get 1 to 10 It is impossible for us to output the numbers one by one. This would waste computer resources. So we directly throw a number to the computer, and it will automatically retrieve and output range (10): it is to generate an array, that is to say, as long as it is a sequence We can all iterate.

Function (function)

Scope:

Python uses the concept of namespace to store objects, this The namespace is the area in which objects operate, and different objects exist in different scopes. The following are the scoping rules for different objects:

  1. Each module has its own global scope.

  2. The object defined by the function belongs to the local scope and is only valid within the function and will not affect the objects in the global scope.

  3. The assigned object is of local scope unless declared using the global keyword.

Parameter classification:

Default parameter: def function(ARG=VALUE)

Tuple parameter: def function( *ARG)

Dictionary parameters: def function (**ARG)

Rules:

  1. The default value must be in non After the default parameters;

  2. In a single function definition, only one tuple parameter (*ARG) and one dictionary parameter (**ARG) can be used.

  3. tuple parameters must come after the connection parameters and default parameters.

  4. Dictionary parameters must be defined last.

Summarize the function:

Points to note when defining:

[1]Indentation and colon

[2]If the defined function has not thought about what it wants, follow the grammatical rules and add a pass to avoid reporting errors

[3]Return represents The function has ended and the value returned to the caller

Parameters:

[1]Default parameters: Reduce the difficulty of using the function

[2]Can Variable parameters: to facilitate the improvement of our development efficiency

[3]Keyword parameters: mainly expand the function of the function

Recursive function:

Recursive It just calls itself continuously until the conditions cannot be met. The order of execution is to call multiple facts. When it is finally executed, the last fact is executed first, the result is returned (this result is used as the parameter of the second to last fact), and then the penultimate fact is executed. Two facts, and so on...

Module

The codes written by python all end with .py files, and this .py file It is the module. The purpose of this is to facilitate use

Usage rules:

[1]import our module name

[2] If we just want to use a specific function in the module, use from module name import module specific method name

[3] Sometimes the name of the module is very long and it is not very convenient for us to use it. Use Import module name as alias is to use as to give an alias to the module we use here. It is more convenient to use

[4] There is also the problem of module path: if it is not under the same path, we need to import it The full path of the module, not needed if the environment is configured

The above is the detailed content of What are the basics of python language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn