|
Summary:
1, Python statements and syntax
2, Python identifier
3, the basic programming style
First, Python statements and grammar
1, can be anywhere from the beginning of a line
2, continued
\:
'' ': Closed operator, single statement across multiple lines
3, the code group
, Indent the same set of statements to form a block of code
, The first line to keywords, such as if, while, etc., to the end of the colon
, Python uses indentation to separate the code group, the same code group of code must be strictly left-aligned, otherwise it will cause a syntax error
, Placing multiple statements on the same line
;: Use a semicolon as a delimiter
, The module
Each Python script file can be treated as a module; the name of the module is also the script name
The code in the module can be a direct implementation of the script, it can be similar to the code library functions can be implemented by other modules import (import) because the module will be the moment of import will be implemented; so the modules are generally stored Is some of the code can be called;
#! / Usr / bin / python2.6
# Coding = utf-8
Name = "Tom"
Def prinName (a):
Print a
# Indent the same set of statements constitute a block of code; num with the previous code block has nothing to do
Num = 4
Print num
Print prinName (name)
# Import module is the need to specify the module directory path;
[Root @ Oracle ~] # cat mod.py
#! / Usr / bin / python2.6
Def prinName (a):
Print a
B = "Black"
Print a + b
[Root @ oracle ~] # cat import.py
#! / Usr / bin / python2.6
#
Import mod
Mod.prinName ()
Second, the identifier
1, the identifier is allowed in the computer language as a name of a valid string collection
Some of these are keywords, which are language identifiers, and therefore reserved words, and can not be used for other purposes
Python also has a set of identifiers called "built-in", which are not recommended, although they are not reserved words.
2, Python identifier
The first character can only use letters or underscores
The remaining characters can use letters, numbers, or underscores
Distinguish character case
Third, Python basic programming style
1, Notes
Neither lack of comments, but also to avoid excessive comments
2, the document
Python allows document strings to be obtained dynamically via __doc__
3, indentation
Uniform indentation of 4 characters
4, the identifier name
See the name of knowledge
>>> str .__ doc__
'Str (object) -> string \ n \ nReturn a nice string representation of the object. \ NIf the argument is a string, the return value is the same object.'
>>>
>>>
>>> print str .__ doc__
Str (object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object
Python naming convention
1, with a single underscore at the beginning of the variable name (_x) will not be imported from the module import * statement
2, before and after the underline variable name (__x__) is a system variable name, the interpreter has special significance
3, beginning with two underscores, but not underlined at the end of the variable name (__x) is the class of local variables
4, interactive mode, only a single underscore variable name (_) used to save the results of the last table expression
>>> a = "I love:"
>>> b = "pig"
>>> print a + b
I love: pig
>>> print a .__ add __ (b)
I love: pig
Class str (basestring)
| Str (object) -> string
|
| Return a nice string representation of the object.
| If the argument is a string, the return value is the same object.
|
| Method resolution order:
| Str
| Basestring
| Object
|
| Methods defined here:
|
| __add __ (...)
| X .__ add __ (y) < ==> x + y
|
| __contains __ (...)
| X .__ contains __ (y) < ==> y in x
|
| __eq __ (...)
| X .__ eq __ (y) < ==> x == y
|
| __format __ (...)
| S .__ format __ (format_spec) -> string
|
| __ge __ (...)
| X .__ ge __ (y) < ==> x> = y
|
| __getattribute __ (...)
| X .__ getattribute __ ( 'name') < ==> x.name
|
| __getitem __ (...)
| X .__ getitem __ (y) < ==> x [y]
Python file structure
#! / Usr / bin / python2.6 # (1) Starting line
# Coding = utf-8 #mod .__ doc__ Referencing class documentation
# "This is test module" (2) Module documentation (document string)
Import sys # (3) module import
Import os
Debug = true
Name = "Tom" # (4) global variables
Class FooClass (object): # (5) class definition (if any)
"Foo class"
Pass
Def test ():
"Test function"
Foo = FooClass () # instantiate the class
If debug:
Print 'ran test ()'
Def prinName (a): # (6) Function definition (if any)
Print a
# Indent the same set of statements constitute a block of code; num with the previous code block has nothing to do
Num = 4
Print num
Print prinName (name)
If __name__ == '__main __' # (7) the main program
Test ()
PrinName ()
Python file main program
1, the main program
This code is executed regardless of whether the current module is imported by another module or executed directly as a script
Note: All modules have the ability to execute code
The highest level python statement (without indentation) is executed when the module is imported, whether or not it really needs to be executed
3, the appropriate approach: In addition to those who really need to implement the code, all the function codes are established through the function, so
Only in the main program module to write a large number of top-level executable code
There should only be less top-level execution code for the module being imported
4, __ name__ indicates how the module should be loaded Each module has a built-in variable named __name__, which changes depending on how this module is called
If the module is imported, the value of __ name__ is the name of the module
If the module is executed directly, the value of __ name__ is "__main__": |
|
|
|