Python Beginner's Considerations __python

Source: Internet
Author: User
Tags exception handling python script

Python is all about the object.


1. Each import statement tries to import only one module to avoid importing multiple modules at a time

2. If the statement is too long, you can use a continuation character ' \ ' at the end of the line to indicate that the following line still belongs to the current statement. However, it is generally recommended to include multiple lines of content in parentheses.

3. Generally, both sides of the operator. It is recommended that spaces be separated between function arguments and commas, and a blank line is suggested to increase readability between different blocks of code, different function definitions, and different class definitions.

4. Appropriate use of exception handling to improve the robustness and fault tolerance of the code, but not too dependent on exception handling, appropriate display of judgment is necessary.


5. You cannot use keywords as variables, you can import Keyword,print keyword.kwlist See all of the Python keywords.

6. It is not recommended to use the system's built-in module name as a variable. You can view all of the module types and functions within Dir (__builtins__).

7. Variable names are case-sensitive.

8. Use the PIP tool to manage the Python extensions library. Previous versions of Python3.4.0 and Python2.7.9 need to install the PIP command before it can be used, and after that the installation package has integrated the command and does not need to be installed.

To see if the PIP command has a duplicate path, cmd command: where PIP.

From the results, you can see that there will be two paths, one for the python3.x path and one for the python2.x path.

Copy environment variable path to TXT, remove related path about python3.x, save, open a console, input pip, you can see help information, the problem is solved perfectly.

Multi-version PIP usage:


So, there's a real two-version python in the environment and the PIP is installed, so how do you install the package with PIP for the specified version of Python? The method is as follows:


python2-m pip Install xxx


Or


python3-m pip Install xxx


Call the corresponding PIP program by specifying the python version.
Important: use python-m pip install xxx to install a third-party module.


The __name__ property of the 9.python script

If the script is imported as a module, its __name__ property is automatically set to the module name, and if the script runs independently, its __name__ property value is set to __main__. Suppose there is only one line of code in the file a.py:

Print __name__

The result is either idle or running directly at the command prompt: __main__. But if the change file is imported as a module, the result is a.

With the __name__ property, you can add the following code to your program when you write a module that contains a large number of functions and you do not want it to run directly to the following program:

If __name__ = ' main ':

print ' Please use me as a module '

This will be prompted when the program runs directly. Using the import statement to import the module can use the class method and so on.


10.python of package

In Python, a. py file is called a module, and what happens if different people write the same module names. To avoid module name collisions, Python introduces a method of organizing modules by directory, called Packages (Package), and when packages are introduced, all modules do not conflict with others, as long as the top-level package name does not conflict with others. Now, the name of the abc.py module becomes MYCOMPANY.ABC, and similarly, xyz.py's module name becomes mycompany.xyz. Note that each package directory will have a __init__.py file, which must exist, otherwise Python will use this directory as a normal directory instead of a package. __init__.py can be either an empty file or a Python code, because the __init__.py itself is a module, and its module name is MyCompany.

Similarly, you can have multiple levels of catalogs that form a multi-level package structure. Each layer should have a __init__.py file, that is, each directory in the package must contain a __init__.py file, which can be empty, only to indicate that the directory is a package. The primary purpose of the __init__.py file is to set the __all__ variable and the code needed to initialize the package, where the object defined in the __ALL__ variable can be used from. When import*, all are imported correctly.

11.

The first line of the scripting language is to point out that you want the code in your file to run it with what executable program, and that's simple.

#!/usr/bin/python: is to tell the operating system to execute this script, call the Python interpreter under/usr/bin;

#!/usr/bin/env python (Recommended): This is used to prevent operating system users from not installing Python in the default/usr/bin path. When the system sees this line, it first looks in the env settings to find the Python installation path, and then calls the interpreter program in the corresponding path to complete the operation.

#!/usr/bin/python is equivalent to writing dead python path;

#!/usr/bin/env Python will go to the environment settings to find the Python directory, recommend this writing

12. Note: #coding =utf-8 = no spaces on either side.

13.pycharm Registration
(1) In the activation interface of the License server input: http://idea.liyang.io/; or: Click Help→register→license Sever, enter Http://idea.liyang.io
(2) In the browser's address bar input: http://idea.lanyus.com/, the URL, do not need to modify the user name, click to get the registration code. Copy the registration code, paste it into the Activation Code input box in the registration interface, and click OK.

14.

There are no CMP functions in the Python 3.X version, and if you need to implement the comparison functionality, you need to introduce operator modules that fit any object, including the following methods:
Operator.lt (A, B)
Operator.le (A, B)
Operator.eq (A, B)
Operator.ne (A, B)
Operator.ge (A, B)
Operator.gt (A, B)
Operator.__lt__ (A, B)
Operator.__le__ (A, B)
Operator.__eq__ (A, B)

Operator.__ne__ (A, B)
Operator.__ge__ (A, B)
Operator.__gt__ (A, B)
Instance
>>> Import operator
>>> operator.eq (' Hello ', ' name ');
False
>>> operator.eq (' Hello ', ' hello ');
True

CMP (x, y) functions are not available in python3.x and can be replaced with the following functions:

Operator.lt (A, b)           Lt (A, B) is equivalent to a < b Operator.le (A, b)           LE (a,b) equivalent to a <= b Operator.eq (A, b
)           eq (A , b) equivalent to a = = B
operator.ne (A, b)           ne (a,b) equivalent to a!= b
operator.ge (A, B)           GT (A,B) equivalent to a > B
operator.gt (A, B)           GE (A, b) is equivalent to a>= b

15. Bubble sort

#!/usr/bin/python #-*-coding:utf-8-*-array = [9,2,7,4,5,6,3,8,1,10] L = len (array) for I in Range (L): fo R j in Range (L-i): If array[l-j-1]<array[l-j-2]: array[l-j-1],array[l-j-2]=array[l-j-2],array[l-j-1 For I in Range (L): Print Array[i 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.