[Python notes] Python Learning Note Four

Source: Internet
Author: User
Tags first string

Module

In Python, a. py file is a module

The advantage of using a module is that it greatly improves the maintainability of the code and can be referenced elsewhere.

You can also avoid collisions between a function name and a variable name.

Package (packages) must have a __init__.py file under each package directory

This file can be empty, the file corresponding to the module name is the current directory name is the package name

Use of modules

Python has a lot of built-in modules

#!/usr/bin/env python#-*-coding:utf-8-*-#write a module that belongs to you
# The first string of a file is treated as a document comment for the module'a test module'
# module author
__author__='Joyjoe'ImportSYSdefTest ():
# SYS.ARGV use a list to store all parameters of the command line, the first element is always the filename args=SYS.ARGVifLen (args) = = 1: Print 'Hello world!' elifLen (args) ==2: Print 'Hello,%s'%args[1] Else: Print 'Too many arguments!'
# When you run a file on the command line, the Python interpreter __name__ the variable to __main__
# This will fail if the module is imported elsewhere
# typically used to run testsif __name__=='__main__': Test ()

Aliases can be set when importing modules

Try :     Import Cstringio as Stringio except importerror:     Import Stringio
Try :     Import JSON except importerror:     import Simplejson as JSON

Scope

In Python, variables such as __xxx__ are specified as special variables, such as __name__, __author__, __doc__, etc.

Python also does not have a function access modifier in a class or module

Third-party module installation

Using the official recommended PIP can also be used Easy_install

Python is a very powerful tool library for processing images Python Imaging library (registered on the official website name PiL)

Pip Install PIL

Import= Image.open ('test.png')print  Im.format, Im.size, Im.modeim.thumbnail ((max.)) Im.save ('thumb.jpg'  JPEG')

Common third-party libraries:

Mysql-python

NumPy

Jinja2

Module loading process: The Python interpreter searches all installed built-in modules and third-party modules in the current directory, and the search path is stored in the PATH variable of the SYS module

Import SYS Print Sys.path

Python provides the __future__ module to import the next new version of the feature into the current version

To accommodate string representations in python3.x, use the new syntax in Py2.7 by unicode_literals

#still running on Python2.7 from __future__ Importunicode_literalsPrint '\ ' xxx\ ' is Unicode?', Isinstance ('XXX', Unicode)Print 'u\ ' xxx\ ' is Unicode?', Isinstance (U'XXX', Unicode)Print '\ ' xxx\ ' is str?', Isinstance ('XXX', str)Print 'b\ ' xxx\ ' is str?', Isinstance (b'XXX', str)

Python3, all characters are considered Unicode if you need to use a binary string, you must add a prefix b

In Python3, all divisions are precision divisions

From __future__ Import Division

[Python notes] Python Learning Note Four

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.