Getting started by Python (1)

xiaoxiao2021-03-05  22

The first chapter Python on Windows has 2 versions, 1, ActivePython (from Active State, not open source), 2, from Python, is open source

Chapter 2 1, the function in Python has a return value. If the function contains statement return, return return value; otherwise, return none

2, the parameters in the function are not required, Python is a dynamic-strong type language. His feature is that there is no need to declare the type of variable, but once the variable has a type (after being assigned), his type is determined. This is where many languages ​​are different, and the general language is dynamic-weak type or static-strong type.

3, multi-line comments of the function, can be a string from 3 double quotes. If a function is commented, the comment must be in the beginning of the function content.

4, the comment of the function is an attribute at which the function is running: __ DOC__

5, the search path of the Import is the folder defined in sys.path. >>> Import Sys >>> Sys.path View Search Path. The result of sys.path is a list, so all List methods are suitable for sys.path, you can let Python custom search path >> Sys.Path.Append ('d: / my / NEW / PATH ')

6. In Python, not all modules are ended. For example, the SYS module, SYS is a built-in module, which is written by C

7, Python, everything is an object. Some objects may not have attributes and methods, and some objects may not have subclasses. (?)

8, the regeneration of the code, there is no Begin ... End or {...} in Python to determine the code block, which relies on good code constraint to determine the code block, which is needed to adapt.

9, Print is the keyword, is a statement, just like IF, for. It is used to print the content it declared >>> Print "N =", 5 n = 5 >>> Print ("N", 5) ('N', 5) >>> Print ("Help") HELP >>> Print ("Help", 'Help', 'List') >>> Print ("Help",) ('Help',) >>> Print ["I", "LOVE" ] ['I', 'Love'] >>> Print (['OO']) ['oo'] >>> Print (['oo'], ['List', 'POI']) ([' OO '], [' List ',' Poi ']) >>> Li = [' 1 ', 2, 3] >>> Print Li [' 1 ', 2, 3] Print declared the content, with a comma Separate, each element is separated in a space in the result

10. Python does not support in-line assignment. That is, IF ((i = 4) == k), does not support 11, all modules have a built-in attribute: __ name__. The value of __name__ attribute is dependent on how you use this module, if it is an import module, then __name__ is the file name of the module (excluding the path and the suffix name); if the module is directly run, the __ name__ value is the default value " __main__ ". Therefore, the code can be tested by if __name__ == "__main__": declaration

Chapter III 1, Dictionary, Dictionary Dictionary Dictionary Dictionary is built-in, disorderly, which is similar to hashMap in Java. The keys in the dictionary are case related, keys and keys can be 2 different keys (different cases of letter K). The value corresponding to the key can be various data types. Similarly, the key is also a variety of data types.

Operation of the dictionary: 1. Definition >>> D = {'KEY': 1, 'My': 2} >>> D {'my': 2, 'key': 1} 2, new >>> D [3] = 'Hehe' >>> D {3: 'Hehe', 'My': 2, 'Key': 1} 3, Update >>> D ['my'] = {'a': ' Alpha '} >>> D {3:' Hehe ',' my ': {' a ':' alpha '},' Key ': 1} 4, Find >>> D [' Key '] 1 5, delete >>> DEL D ['My'] >>> D {3: 'Hehe', 'Key': 1} 6, Empty >>> D. Clear () >>> D {}

Like Del and Print when deleting, it is more like a statement, which can do this: >>> d = {'key': 1, 'my': 2} >>> DEL D ['key'], D ['my'] >>> d {}

2, list, list list is similar to ArrayList in Java, which is orderly.

Operation of the list: 1, definition >>> Li = ['a', 'b', 3, {'key': 'value'}] >>> Li ['A', 'B', 3, { 'Key': 'Value'}] 2, get A, positive (from left to right) >>> Li [0] 'a' >>> Li [3] {'key': 'Value'}> >> Li [4]

Traceback (MOST Recent Call Last): File "", line 1, in -toplevel - li [4] indexerror: list index out of range b, reverse tap (from right to left), li [- n] = li [len (li) - n] >>> Li [-1] {'key': 'value'} >>> li [-4] 'a' >>> Li [-5] traceback MOST Recent Call Last: file "", line 1, in -toplevel: List index out of range c, segment getting part list >>> Li ['A', 'b', 3, {'Key': 'Value'}] >>> Li [1: 3] ['B', 3] From Li [1] (value 'b'), to Li [3] (The value is '{' key ':' value '}') Previous >>> Li [1: -1] ['b', 3]

>>> Li [2:] [3, {'Key': 'Value'}] >>> Li [: 2] ['A', 'B'] >>> Li [:] ['A', 'b', 3, {'Key': 'Value'}]

At sections, if an index is exceeded, or before the index position is behind the rear index, there will be no abnormality, just return an empty list, as follows: >>> Li ['A', 'B', 3, {'Key': 'Value'}] >>> Li [2: -3] [] >>> Li [1: 0] [] >>> Li [100:] []

3, add 3 new techniques A, add content, append. This method is at the last additional element (any type) >>> Li.Append ('new') >>> li ['a', 'b', 3, {'key': 'value'}, ' New '] B, insert content, insert. This method adds an element in the specified location of the list >>> li.insert (1, 'first') >>> Li ['A', 'first', 'b', 3, {'Key': 'Value' }, 'new'] c, extended list, extend. The method is at the end of the list, the additional list (parameter is a list type), and the elements in the new list are arranged in the initial list >>> Li.extend ('45 ') >>> Li [' A ', 'First', 'B', 3, {'Key': 'Value'}, 'New', '4', '5'] >>> li.extend (45) Traceback (MOST Recent Call Last): File "", line 1, in -toplevel - li.extend (45) typeerror: list.extend () Argument Must Be ingile >>> li.extend (['KKK', 'LLL']> >> Li ['A', 'First', 'B', 3, {'KEY': 'Value'}, 'New', '4', '5', 'KKK', 'LLL']

Note that when you accept a string to do parameters, it is particularly special, equivalent to the following code: >>> Li ['a', 'b', 3, {'key': 'value'}] >> > li.extend (List ('123')) >>> Li ['A', 'B', 3, {'KEY': 'Value'}, '1', '2', '3'] List The function decomposes a string to list 4, index method index, or in expression that contains each character, and the parameters of INDEX can be any type. If the value of the index exists in the list, the position of the first appearance of this value is returned; if there is no existence in the list; the return of the in expression is Boolean TRUE and FALSE (case related). >>> li.index (3) 2 >>> 'x' in li false 5, delete methods Remove and POP. Remove Removes an element in the list, if there is a plurality of modifications, only one of the most upwards, returns the entire list after the removal; the POP removal list last element, return to the modified element. 6, other operations , * and Extend methods, * is multiple

3, sequence, tuples 1, definition >>> li = ('a', 'b', 3, {'key': 'value'} >>> Li ('A', 'B', 3, { 'Key': 'Value'})

2, get, the same list

There is no way to sequence, it is faster than the list of operations.

Built-in function tuple accepts list, return sequence, list accept sequence, return to the list.

Variable Python can utilize sequences, and the list is assigned to multiple variables at once, and the value of 2 side elements is the same. >>> (x, y, z) = ('1', '2', '3') >>> x '1' >>> Y '2'

4, formatted strings >>> a = 'x' >>> b = 1 >>> '% s =% D'% (A, b) 'x = 1'

(a, b) is a sequence, which can only be sequences. >>> '% s =% d'% [A, B]

Traceback (MOST Recent Call Last): File "", line 1, in -toplevel- '% s =% d'% [A, B] Typeerror: not enough arguments for Format String

D: integer; s: string;

>>> Print "Today's stock price:% f"% 50.462550.462500 >>> Print "Today's stock price:% .2F"% 50.462550.46f: floating point number (defined a valid bits, such as%. 2f is a decimal point 2, default is 6);

5, the list of list generates a new list after a list loop operation, the original list does not change >>> Li = [1, 2, 3] >>> ['x =% D'% k for k in Li] ['x = 1', 'x = 2', 'x = 3'] >>> li [1, 2, 3]

6, the list of lists, Join is not a list of characters in characters '#' link list: '#'. Join ([.......]) Join method is not a list method, but a string method.

Some rules in the Boolean environment: * 0 is a fake, other numbers are true * empty string "" is a fake, other strings are true * empty list [] is a fake * empty dictionary {} is a fake * empty sequence () is false

转载请注明原文地址:https://www.9cbs.com/read-38304.html

New Post(0)