Python 2.7 Learning notes built-in statements, functions, standard libraries

Source: Internet
Author: User

Using any development language for software development is inseparable from the built-in libraries (or APIs) provided by the language, and even the power and ease of use of built-in libraries will affect your choice of development language.

Python language, which provides a number of built-in features that you can use for development. There are several main types:

One, built-in statements

The most common thing we know is an assignment statement that assigns an expression to a variable by operator =.

In addition to assignment statements, Python provides some built-in statements, such as:

Print statement, which is used in the console output

Del statement, for deleting variables

Pass statement, empty statement, nothing to do

Second, built-in functions

Python also provides a number of built-in functions, such as

Len ("abc") #获取字符串的长度

Len ([+]) #获取列表中元素的个数

Note: Built-in functions and built-in statements are sometimes confusing, especially in the case of parameters, where parameters must be placed within () for the function, but for statements, the arguments may not be placed within (). such as,

Print ("Hello")

print "Hello"

The above two statements perform the same effect.

Three, standard library

Python offers a number of standard packages and modules that provide a wealth of functionality that is directly available for use. In fact, for Python, the community provides a large number of third-party libraries, but the standard library does not need to be downloaded by itself and can be used directly, while third-party libraries need to be downloaded to the environment themselves.

The following describes the standard libraries that are common in Python, and note that these libraries may vary with version changes.

1. SYS module

Variables and functions that are closely related to the Python interpreter can be accessed through the SYS module.

As used in the previous article, Sys.path can get a list of system paths to Python.

As with Sys.args, you can get a list of parameters that are passed to the Python script when the Python script executes. Such as:

The contents of test.py such as:

# Coding=utf-8 Import SYS Print sys.argv

Execute script, pass in 2 parameters
Python test.py Hello 123

The output information is as follows
[' test.py ', ' hello ', ' 123 ']

2. OS Module

You can obtain information about the operating system. Let's look at an example of getting the script execution path:

Suppose the contents of d:\demo\python\test.py are as follows

# Coding=utf-8 Import Sys,os print os.getcwd ()  # Gets the current path of the run script, note that the script is not necessarily the same path as print sys.path[0]  #  Gets the path where the script is located, is the full path, but does not include the script name print sys.argv[0]  # is the path and script name that gets entered at execution time, Whether to include paths, dependent on input at execution time

The above uses three functions, which have some differences in meaning, as explained in the comments. Let's take a look at the performance.

d:\demo\python>python test.pyd:\demo\pythond:\demo\pythontest.py

The above execution, because the current path is the path of the script file, so os.getcwd and Sys.path return the same value, and because command execution only entered the file name, then sys.argv[0] only return the file name.

d:\demo>python python\test.pyd:\demod:\demo\pythonpython\test.py

This is because of the difference in the current execution path, the execution mode and output have also changed.
Because the current execution path becomes D:\demo. So the script entered at execution time takes a python path. At this point, the values returned by OS.GETCWD and Sys.path are different.

SYS.ARGV[0] Returns the same value as the input.

d:\demo>python d:\demo\python\test.pyd:\demod:\demo\pythond:\demo\python\test.py

From this, it can be further seen that the value returned by sys.argv[0] is completely dependent on input.

There is a problem here, how do we get the full path to the script file (including the path + file name), obviously can not rely on sys.argv[0.

Fortunately, Python provides another way to get the full path of the executed script directly, including the file name. The method is:

Os.path.realpath (__file__) #获取脚本的全路径, including the file name. Independent of input

In addition, OS.SEP can be used to obtain the delimiter that the output operating system uses to separate paths, and for Windows and Linux different

3. Fileinput Module

Used to read content in a text file.

4. Data collection Module

In addition to our most commonly used lists, tuples, dictionaries, these are part of the python built-in language and can be used directly.

There are also set, deque and other data structures.

5. Time Module

The system date time can be obtained, and the date time and string conversions are implemented.

6. Random Module

can be used to generate random numbers.

7. Shelve Module

Can be used to save data to a file.

8. Re Module

The regular expression module in Python.

Above are some common standard modules, in addition to these, there are many standard modules, which are not listed here.

Python 2.7 Learning notes built-in statements, functions, standard libraries

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.