Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Python programming style     - Linux foundation tutorial: how to modify the host name on CentOS or RHEL 7 (Linux)

- Node.js developers must know four JavaScript concepts (Programming)

- Construction LVM-based raw device Oracle10g Database on Oracle Linux 5.11 (Database)

- Linux system Iptables Firewall User Manual (Linux)

- CentOS 6.5_x64 install Oracle 11g R2 (Database)

- The basic principle of pointers in C ++ (Programming)

- CentOS6.3 build a Python 3.3 environment to access Oracle 11gR2 (Database)

- GDB remote connections RX Probe online debug program (Programming)

- Jetty JNDI Development combat (Linux)

- Ansible module Know (Linux)

- Strategy Games Ubuntu installation of Wesnoth 1.12.1 (Linux)

- MySQL fuzzy query: LIKE and REGEXP pattern mode (Database)

- How to create a secure and easy to remember password (Linux)

- Sniffer install applications in Linux supplement (Linux)

- CentOS 6.5 / Linux restart network card being given Determining if ip address x.x.x.x is already in use (Linux)

- Linux (RHEL5) general optimization system security (Linux)

- Use Epoll develop high-performance application server on Linux (Server)

- Docker build their own private warehouses (Linux)

- On the PC goes heavy security watch your startup items (Linux)

- Linux installation skynet issue summary (Linux)

 
         
  Python programming style
     
  Add Date : 2017-08-26      
         
         
         
  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__":
     
         
         
         
  More:      
 
- Build RubyMine + Ruby On Rails + MySQL development environment under Windows (Server)
- ORA-600 [kcbz_check_objd_typ] Error Handling (Database)
- Linux Getting Started tutorial: XWindow what (Linux)
- Docker Basic Concepts (Linux)
- Upgrading Oracle 11.2.0.1 to 11.2.0.3 (Database)
- Lambda expressions of evolution (Programming)
- Ubuntu 14.04 build Hadoop 2.5.1 standalone and pseudo-distributed environment (32-bit) (Server)
- Build Python3.4 + PyQt5.5.1 + Eric6.1.1 development platform under Mac OS X 10.11.1 (Server)
- Linux 6 use UDEV binding shared storage (Linux)
- Let the terminal under Mac OS X as like Linux has displayed a variety of colors (Linux)
- MariaDB 10.1 and MySQL 5.7 in general performance on commodity hardware (Database)
- Linux into single user mode to modify the administrator password (Linux)
- Ubuntu Apache2 setting, problem solving css, pictures, etc. can not be displayed (Server)
- [JavaScript] catch (ex) statements of ex (Programming)
- Linux Systemd-- To start / stop / restart services in RHEL / CentOS 7 (Linux)
- GDB remote connections RX Probe online debug program (Programming)
- Seven Steps to Help Google Chrome Speed - (Linux)
- Linux kernel network subsystem analysis (Programming)
- CentOS 6.4 installation environment to build Scrapy 0.22 (Linux)
- Eclipse-ee Tomcat browser can not start Tomcat, and Web project service deployment (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.