Basic technical framework of python Programming Language
Python id method to view unique identifiers, memory address >>> a = "str" >>> B = 2 >>> id () 140223954701072 >>> id (B) 36610848 view the data type of the variable; >>> type (B) <type 'int' >>>> type () <type 'str'> element 1: python saves all data as a memory object in python. The variable actually points to the reference dynamic type of the Memory Object: at any time, as long as necessary, A single object reference can rereference a different object (which can be different data types). The built-in function type () the data type "=" used to return a given data item is used to bind the variable name to an object in the memory. If the object exists beforehand, it is bound directly. Otherwise, the referenced object is created by "="> name = "tom" >>> test = "tom" >>> id (name) 140223953962208 >>> id (test) 140223953962 208 element 2: Command rule for Object Reference (variable) variables 1. It can only contain letters, numbers, and underscores, it cannot start with a number. 2. It is case sensitive. 3. It is forbidden to use the reserved words python2 and python3. It is a convention. 1. Variable names starting with a single underscore (_ x) it will not be imported by the from module import * Statement. 2. Variable names with underscores (_ x _) are system-defined variable names, it has a special significance for the python interpreter. 3. Variable names (_ x) that start with but end with two underscores without underscores (_ x) are local variables of the class. 4. In interactive mode, variable name "_" is used to save the result of the last expression. Note: The variable name has no type, and the object has >>> 1 + 12 >>> print _ 2 element 3: combination Data Type 1. Data Structure: a collection of data elements that are organized together in a certain way (for example, numbering an element). 2. Commonly Used combination data type sequence types in python. 1. List: create with [], such as ['call', 'me', 'ishmeal ','. ']. The list is a mutable object. The original object is supported. Modify; >>> l1 = ["This", "is", "", "pig"] >>> l1 [0] 'eas' >>> l1 [0] [0] 'T' and points to the memory object; the address space of the list remains unchanged in the memory. Note: The list stores pointers, and the Pointer Points to the data elements of the memory object. The data elements in the list are immutable; changed to reference; >>> print l1 ['C', 'is ', 'A', 'sheep'] >>> id (l1) 140223953808704 >>> l1 [3] = "pig" >>>> id (l1) 140223953808704 >>>> print l1 ['C', 'is, 'A ', 'pig'] 2. tuples: Create using (), such as ('one', 'two'). tuples are immutable objects; >>> t1 = ("This ", "is", "a", "pig") >>> t1 [0] 'others'> t1 [0] [0] 't'3. The string also belongs to the sequence type set type. Set ing type dictionary string slices: >>> name = "firefox" >>> name [0] 'F' >>> name [0: 2] 'F' >>>> name [0: 3] 'fir >>> name [] 'I' >>> name [: 2] 'ffi '>>> name [2:] 'refox' specifies the step of the string; >>> name [] 'frfx' element 3 2: the list of composite data types is a variable sequence, and the tuples are an unchangeable sequence. in python, the composite data type is also an object. Therefore, it can be nested with ['hello', 'World, 3] in essence, the list and metadata groups do not actually store data, but store objects that reference python objects and can have specific "methods (functions) that can be called) "tuples, lists, strings, and other data types are" large-sized ", that is, the length can be measured using the built-in function len (); factor 4: logical operators 1. Basic functions of any programming language during logical operations 2. Python provides four logical operations 1, Identity operator is: determines whether the left-side object reference is the same as the right-side object reference. It can also be used with none. >>> name = "Tom" >>>> test = "Tom" >>>> name is testTrue >>> test = "firefox" >>> name is testFalse >>> type (name) is type (test) True 2. Comparison operator <, >,< =,> -,! =, = Member operator in, or not in: test the logical operator and, or, not element 5 of a member relationship: control Flow Statement 1. control flow statement is the basic control mechanism of procedural programming language 2. Common Control Flow Statement of Python if boolean_expression1: suite1elif boolean_expression2: suite2... while 12 while boolean_expreesion: suitefor .. in for variable in iterable: suite try element 6: Arithmetic Operator 1. Python provides complete arithmetic operation set 2. Many Python data types can also use enhanced value assignment operators, for example, + =,-=, etc. 3. For the same function, the performance of the enhanced value assignment operator is better. 4. The int type of Python is immutable. Therefore, the actual process of assigning values to enhancement types is to create a new object to store the results and run the variable name Re-binding element 7: In the output/output reality, programs with actual functions must be able to read the input (such as from the keyboard or file) and generate the output, and write it to the terminal or file. python input/output python3: print () function python2: print statement input () raw_input () >>> raw_input ("plz input a name:") plz input a name: firefox 'Firefox '>>> a = raw_input ("plz input a name:") plz input a name: firefox >>> print afirefox python interpreter provides the standard file objects in 3, which are standard input, standard output, and standard errors. in the sys module, the python interpreter uses sys. stdin, sys. stdout, sys. provided in the form of stderr; print accepts a comma-separated list of objects, and automatically adds A line break. If not required, add a comma after the last element. >>> a = "this is a" >>> B = "pig" >>> print, bthis is a pigprint "string % format1 % format2 .... "% (variable1, variable2 ...) % Can be followed by modifiers (if any, only in the following order) % [name] [flags] [width] [. precision] typecode is a key name in the brackets that belongs to the dictionary. It is used to select one or more _: To Indicate left alignment. The default value is right alignment +: it indicates that it contains a number symbol, and the integer will also contain "+" 0: indicates a zero fill. >>> num = 7.9 >>> print "The num is % f" % numThe num is 7.900000 >>>> print "The num is % d" % numThe num is 7 >>>> num2 = 7.12 >>>> print num7.9 >>>> print num27.12 >>>> print "The nums are % d and % f" % (num, num2) the nums are 7 and 7.120000 >>> print "The nums are % d and % f" % (num, 3.1) the nums are 7 and 3.100000 >>> print "The nums are % e and % f" % (num, 3.1) the nums are 7.900000e + 00 and 3.100000 >>> print "The name is % s. "% nameThe name is Tome. >>> print "The name is % s. "% numThe name is 7.9. # data type conversion: 1. Display Conversion: >>> Test = str (num) >>> type (test) <type 'str' >>>> type (num) <type 'float' >>>> print num7.9 for Python string formatting use "character % format 1% Format 2 characters" % (variable 1, variable 2 ), % format indicates the type of the accepted variable. Simple example # example: String formatting Name = '17jo 'print 'www. % s.com '% Name> www.111cn. netName = '17jo 'Zone = 'com 'print' www. % s. % s' % (Name, Zone)> when formatting a string on www.111cn.net, there are different format symbols after the semicolon, representing different types of conversion. The specific symbols are shown below. Format Symbol: Type % s string % d/% I decimal integer or long integer % u unsigned integer or decimal integer % o octal integer or long integer % x/% X hexadecimal integer or long integer (% X: uppercase letters) % e/% E scientific count; Floating Point Number % f/% F Floating Point Number % output % literal % c single character % r same as repr () the generated string % g, G index less than-4 or higher precision is to use % e or % E, otherwise, when using the % f format symbol as a number, you can add a number and a fill gap, such: % [0] [total digits] [.] [number of decimal places] to set the style to be converted. The usage is as follows: # example: number formatting nYear = 2018 nMonth = 8 nDay = 18 # formatting date % 02d number converted to two integer without entering 0 print '% 04d-% 02d-% 02d' % (nYear, nMonth, nDay)> # output result fValue = 8.123 print '% 06.2f' % fValue # reserved width is 6 decimal floating point type> 008.12 # output print '% d' % 10 # output decimal> 10 print' % o' % 10 # output octal character> 12 print '% 02x' % 10 # output two hexadecimal notation, lowercase letter vacancy completion> 0 aprint '% 04X' % 10 # output 4-digit hexadecimal notation, uppercase letter vacancy completion> 000 Aprint '%. 2e' % 1.2888 # The floating point type is output by scientific notation to retain 2 decimal places> 1.29e + 00 here, we have demonstrated how to replace the specified field. You can also add a colon after the formatting indicator to perform exact formatting. For example: python: format # Field 0: left justify, pad to 15 characters # Field 1: right justify, pad to 6 charactersfmt = '{}$ {1:> 6} 'fmt. format ('registration', 35)-> 'registry $ 35' fmt. format ('utorial ', 50)-> 'tutorial $ 50' fmt. format ('banquet ', 125)-> 'banquet $125' format indicator can be referenced through nesting. Fmt = '{0: {1}' width = 15fmt. format ('invoice #1234 ', width)-> 'invoice #1234' width = 35fmt. format ('invoice #1234 ', width)-> 'invoice #1234' can specify the alignment of the string of the desired length. Effect character: <(default) left alignment> right alignment ^ center alignment = (used only for numbers). After the decimal point, the format indicator can contain a display type to control the format. For example, a floating point can be formatted in a general format or expressed in a power. >>> '{0: g }'. format (3.75) '3. 75 '>>>' {0: e }'. format (3.75) '3. there are many display types for 75366e + 00. 2.6 contains a complete list of documents. Some examples are listed here. 'B'-binary. Outputs the number based on 2. 'C'-character. Convert the integer to the corresponding Unicode string before printing. 'D'-a decimal integer. Outputs the number based on 10. 'O'-octal. Outputs the number based on 8. 'X'-hexadecimal. The number is output based on 16, and the digits above 9 are lowercase letters. 'E'-power symbol. Print numbers in scientific notation. Use 'e' to represent the power. 'G'-General format. Output the value in fixed-point format. When the value is very large, print it in the power form. 'N'-number. When the value is an integer, it is the same as 'D', and the value is the same as that of 'g. The difference is that it inserts a digital separator according to the region settings. '%'-Percentage. Multiply the value by 100 and print it in the fixed-point ('F') format. The value is followed by a percentage sign. Classes and types can define a _ format _ () method to control how to format yourself. It accepts a format indicator as the parameter def _ format _ (self, format_spec): if isinstance (format_spec, unicode): return unicode (str (self) else: return str (self) also has a built-in format () method that can be used to format a value. It calls the _ format _ () method of the type and transmits the formatted indicator as a parameter. >>> Format (75.6564 ,'. 2f ') factor 8: function creation and calling 1. python uses def statements to define functions 2. functions can be parameterized, call by passing different parameters. 3. Each python function has a return value. The default value is none, you can also use "return value" to clearly define the return value. 4. The def statement creates a function object and creates a function object that points to the function object. The referenced function is also an object, which can be stored in the combined data type, it can also be passed as a parameter to other functions. callable () can be used to test whether a function can be called (): Call the operator >>> def testFunc (arg1 ):... print arg1.... >>> callable (testFunc) True >>> testFunc ("hello world! ") Hello world! >>> Test = "firefox" >>> testFunc (test) firefox