Interpreting face objects dynamic semantics 1989 birth Guido Sky Circus scripting language Advanced dynamic programming Language- Interactive mode (program in-memory cache, cannot save) Text mode #!/usr/bin/python file path source code . py byte code compiled into. PYC (import py_compile) py_compile.complie.compile ("1.py") & nbsp Optimization Code Source code is optimized to. PYO (Python-o-M py_compile 1.py) Linux command line input variable is an area in memory, is a reference to the data or a label such as a A1 a_1 _a 1 variable value changed when label is removed integer division/Fractional part is truncated, use normal division to precede program with from _future_ Import Division divisible symbol//take remainder symbol% Power exponentiation operation symbol **2**3 function table The 2,3 is called the built-in function 2*2 is an expression print 2*2 is a statement expression is something statement is doing something the program mainly includes expressions and statements if 1 ==1:print ' One equals one ' modules can be seen as enhancements to Python functionality, >>import math >>math.floor (32.6) Ceil>>from Math Import Floor >>floor (32.5) do not use module prefix int float Long is not a normal function, is a type object, int parameter is rounded down automatically when rounding You can use variable reference functions or most objects in Python such as Foo=math.floor foo (32.5) imaginary numbers, negative cmath module processing text mode Ctrl+f5 run Script run start pound bang >> #!/usr/bin/env python script add executableProperty chmod a+x hello.py "Let's Go" or ' let\ ' go ' escaped. Single-Quote double-quoted string with plus sign
values are converted into stringsTwo mechanisms: 1. function str 2. function repr, the resulting string is converted to a legitimate Python expression. int can convert a string to a numeric value. Print repr ("hello,world!") >> ' Hello,world ' Print str ("Hello,world") >>hello,world the third type is: Temp=2print "the Temperature is "+temp print a sentence containing numbers, use the anti-quotes, this sentence will be wrong, should be as follows: print" The temperature is "+ ' temp ' are all three ways to convert Python values into strings. Name = input ("What is your name?") print ' Hello, ' + name + '! ' The program runs in an error, instead of Raw_input (), use the Raw_input function whenever possible, and it puts all the input into the string as the raw data. Three quotation marks can be used to document comments long strings: span multiple lines, use three quotation marks instead of plain quotes, or use three double-quote raw strings: the backslash is not treated in a special way, such as the pathname C:\now, which can be escaped with a backslash c:\\now. The original string is available for the long path, and the original string does not treat the backslash as a special character, such as print R ' C:\now '. The quoted backslash is included in the output string when the quotation marks are escaped from the original string. You cannot enter a backslash at the end of the original string. Unicode string: Stored as a 16-bit Unicode character, such as: U ' hello,world! ' escape: backslash \ Plus newline character \ n, newline character escaped, which is ignored, not its original meaning, can be added to the string is normally not directly added content The first line of Python cannot be indented, and Python is more rigorous. Bitwise AND & Bitwise OR |. Logical operation and OR '
python data type: numeric string (immutable defined)
list []
(single element variable
tuples () (including numbers, strings, and other types, single-element immutable) dictionaries (mapping types, dictionary objects, and individual elements are mutable, key immutable)
string & sequence operations: index value a[1:3:2]
slices
There's no end to the beginning, and the last one is step a[-1] represents the last element
tuple parentheses. List of brackets, list add element list.append. Dictionary curly braces.
objects and classes: A property is an object description, and the method is an object's Operation
Process Control: TRUE code block for logic Real Capital T four blocks emptyLoop iteration (repeating an action instruction) recursive range (len (' Hello ')) gets the index dictionary traversal of the sequence: break jumps out of the loop, continue jumps out of the loop, exit () jumps out of the entire program. Pass placeholder for loop limit, conditional judgment loop available to the Whilewhile function is a statement group: def function name (argument list): The lower function body functions The name of multiple words when the first letter uppercase, the definition of the parentheses are the formal parameters, the argument is called when the parameter is an actual argument # Coding:utf8 (Linux) Chinese encoding GBK (win) Local variables and global variables declare global variables within the function, with global x tuple as parameters passed in, direct transmission is not possible, the number of parameters difference. If t= (' name ', ' Milo ') tuples. F (*t). Dictionary value is f (**d), d={' name ': ' Milo ', ' age ' = 30}, and the dictionary key and parameter one by one corresponding function return value default is None, function has multiple return, only the first print '%s:%s '% x, y incoming x, Y value f (x, *args) redundant parameters, in addition to x remaining in the args tuple, if the parameter is x=40, need to be stored in a dictionary, with F (X,*args,**kwargs) In addition to testing, multi-use return without the print Lambda anonymous function: Quickly define the minimum function of a single line,,, Omit the function definition part, simplify the function, omit the function name,, for example: g = lambda x,y:x*y before the colon is a parameter, can be multiple, separated by commas, the colon is followed by a return value, return is a function object
ReduceThe function takes the number from the list successive, using function f to reduce (f,l) reduce (lambda x,y:x*y, l) L = range (1,6) = [1,2,3,4,5] switch statement: A multi-branched program (Python does not Provides a switch structure, which can be implemented using a dictionary) first, a dictionary is defined, followed by a get () call to the dictionary to get the corresponding expression operator = {' + ': Jia, '-': Jian, ' * ': Cheng, '/': Chu}def F (x,o,y): Print Operator.get (o) (x, Y) F (3, ' + ', 2)
python built-in functionsDivmod Get quotient and die (remainder) Pow take power ip=192.168.1.1 ip.split ('. ') =zip and map mapping sequence pairs, the map element does not complement none, the first argument can be an expression processing. The zip element is not sufficient to map the item, cannot data processing module: Imported script import module, using (module. function in the module) calls the function in the module, does not want the module function to execute the INPUT statement print __name__ (built-in property) if __name__ = = ' __main__ ': the direct call module to find the module priority: current path = = "lib==" Site-packagepython modules can be organized into packages >> Packages folder According to directory, package under __INIT__, Package extension script
Regular Expressions: embedded in Python, small, highly specialized, limited programming language string processing via the RE module: character matching, ordinary character OFDM character definition regular expression is the definition of a string t[io]p is top and tip t[^io]p is the tip and top take counter ^ line end * 0 or more times, + one or more times,? Or no +? Minimum match {m,n} minimum m max n
Python notes Grooming