Python Learning notes 5-python module

Source: Internet
Author: User
Tags vars

The Python module is divided into system-built modules, third-party modules, and user-written modules

By default, Python's third-party modules are installed in the Python installation directory under site-packages and stored as files or directories

The user module, the program modularization to distinguish the function and the structure, the code definition has the very good help

By default, the Python runtime only loads a few of the system's built-in modules, and you can use VARs () to view

>>> print VARs () {' __builtins__ ': <module ' __builtin__ ' (built-in);, ' __package__ ': None, ' __name__ ': ' __ Main__ ', ' __doc__ ': none}>>> vars () {' __builtins__ ': <module ' __builtin__ ' (built-in);, ' __package__ ': None, ' __name__ ': ' __main__ ', ' __doc__ ': none}>>> import time>>> print VARs () {' __builtins__ ': < Module ' __builtin__ ' (built-in), ' __package__ ': None, ' time ': <module ' time ' (built-in), ' __name__ ': ' __main_ _ ', ' __doc__ ': None}

The Python program can be run as a module or it can be poured into a module.

If you call main, you are directly running the program itself now.

The file name to be poured is terminated with a. Py and requires a location where Python python environment variables can be searched


Pythonpath environment variable Set the path of the Python import module

When running on an interactive Dile or command line, the user's CWD is added to the Pythonpath variable by default

0 o'clock modification of the sex can be modified Sys.path

>>> import sys>>> sys.path[' E:\\note\\python ', ' d:\\python27\\lib\\idlelib ', ' c:\\windows\\ System32\\python27.zip ', ' d:\\python27\\dlls ', ' d:\\python27\\lib ', ' D:\\python27\\lib\\plat-win ', ' D:\\Python27\\ Lib\\lib-tk ', ' d:\\python27 ', ' d:\\python27\\lib\\site-packages ']>>> sys.path.append ('/tmp ') >>> sys.path[' E:\\note\\python ', ' d:\\python27\\lib\\idlelib ', ' c:\\windows\\system32\\python27.zip ', ' D:\\Python27\\ DLLs ', ' d:\\python27\\lib ', ' D:\\python27\\lib\\plat-win ', ' d:\\python27\\lib\\lib-tk ', ' d:\\python27 ', ' D:\\ Python27\\lib\\site-packages ', '/tmp ']

Assignment statement, variables name=variables values

You must not use System keywords and special symbols when defining variable names, and variable values can be arbitrary data objects

The variable name is an unknown type, and the type of the variable name depends on the type of the given value

Chained assignment

a=b=c=100

Assign the number 100 to A,b,c

Sequence Unpacking Assignment

name,age,address= (' Keyman ', ' Beijing ')

Sequence unpacking assignment variable name and variable value must be equal


The import statement is also an assignment statement

Improt socket after the socket module is poured, assign value to Scoekt

The import statement imports the Python module and the module should be placed in the Pythonpath directory

Import module name or import module name as alias name

From module name import method or from Module name import method as alias name

When the import time #导入time模块import #导入time模块, and the alias T is used, and the method of the later use of the duration module, it will be replaced with T, for example T.time () from math import sqrt #将 The Sqrt method in the Math module imports the from math import sqrt as Q #将math模块中的sqrt方法导入, and takes the alias Q

Python reserved keywords can be checked using the keyword module

The Kwlist method under the keyword module can print all reserved keywords in the system

Iskeyword under the keyword module to determine if the object is a keyword

>>> Import keyword>>> Print keyword.kwlist[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' by ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' with ', ' yield ']>>> print keyword.iskeyword (' for ') true>>> Print Keyword.iskeyword (' list ') False

Print is a statement in python2.6, 2.7, and Python,print is a function in python3.0

Print prints multiple objects consecutively, separated by ', '

If you want to use multiple print statements, but the results need to be displayed on one line, you need to add ', ' after the print statement

print ' A ' print ' B ', print ' C ' print ' d ', ' E ' print (' a ')

Execution results are

AB CD ea

EXEC statement

exec is primarily used to execute code inside a string containing python

EXEC ("Print ' Hello World '")

Eval statement

Eval is primarily used to execute arithmetic expressions that contain Python in a string.

EXEC ("Print (1+100-20)/2")

EXEC (' print ' Hello World ') print eval (' 5+4*3 ')

Execution results are

Hello World17

This article is from the "Raffaele" blog, make sure to keep this source http://raffaele.blog.51cto.com/6508076/1570275

Python Learning notes 5-python module

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.