Python-note and python-note

Source: Internet
Author: User

Python-note and python-note

Python notes ------------- []

 

 

List Parsing

>>> [(X, y) for x in range (3) for y in range (5)]

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2 ), (2, 3), (2, 4)]

Generator expression (loading only one element to the memory at a time)

Return the iteratable object

 

Multi-Inheritance

(The breadth of the new category is preferred, and the depth of the classic category is preferred)

S = (1, 2, [2, 3])

I = s # Changes in the previous copy id (I) = id (s) s will affect I

I = copy. deepcopy (s) Deep copy

>>> D = [(x, y, z, a, v, c) for x in range (9) for y in range (9) for z in range (9) for a in range (9) for v in range (9) for c in range (9)] brute force cracking

Isinstance (s, str) checks whether the object s is 'str' object

 

For I in range (10:

If I = 2:

Continue

Print I

 

Class Girl (human ):

Def _ init _ (self, name, old ):

Print 'we are people', name

Switch = {

'-': A-B,

'+': A + B

}

Print Switch. get (c, 'Please input +-*/') # No error is returned if you use the dictionary get.

 

>>> Hello [: 3:-1]

'Dlrow o'

>>> Hello [3:-1]

'Lo worl'

>>> Hello [4:]

'O world'

>>>

 

S = 'abcdefghjk'

For I in range (0, len (S), 2 ):

Print S [I] # tips

--------------------------------------------

L1 = [1, 2, 3]

L2 = [1, 2, 3, 4]

Returns the value of L1, which must not be in L2.

Set (L1). difference (L2 )--------------------------------------------------------

 

Enumerate ()

Using the enumerate () function, you can get the subscript and element in each loop at the same time:

S = 'abcdefghjk'

For (index, char) in enumerate (S): print index print char

In fact, in each cycle of enumerate (), a value table (tuple) containing two elements is returned, and the two elements are assigned to the index and char respectively.

With open (file) as myfile:

Print myfile. read () # No need to close myfile

------------------------------------------------

Class num (object ):

Def _ init _ (self, value ):

Self. value = value

Def getNeg (self ):

Return-self. value

Def setNeg (self, value ):

Self. value =-value

Def delNeg (self ):

Print ("value also deleted ")

Del self. value

Neg = property (getNeg, setNeg, delNeg, "I'm negative ")

 

X = num (1.1)

Print (x. neg) x. neg =-22

Print (x. value)

Print (num. neg. _ doc __)

Del x. neg

The decorator Can process a function, method, or class.

Def decorator (F ):

Def new_F (a, B ):

Print ("input", a, B)

Return F (a, B)

Return new_F

# Get square sum

@ Decorator

Def square_sum (a, B ):

Return a ** 2 + B ** 2 # square_sum = decorator (square_sum)

Def func (formal_args, I = 0, * args, ** kwargs ):

Print formal_args

Print I

Print args

Print kwargs

The above is a decoration without Parameters

Def Func1 (F ):

Def new_fun ():

Print ('function1 ')

F ()

Return new_fun

Def Func21 (F ):

Def new_fun2 ():

Print ('function2 ')

F ()

Return new_fun2

@ Func1

@ Func21

Def Func4 ():

Pass

 

Print (0b1110) # binary, starting with 0 B

Print (0o10) # octal, starting with 0o

Print (0x2A) # hexadecimal format, starting with 0x

Environment configuration $ export PYTHONPATH = $ PYTHONPATH:/home/vamei/mylib

 

Install non-standard packages

Python standard library is installed with Python. When we need a non-standard package, we need to install it first.

(1) use Linux repository (Linux environment)

This is a good starting point for installing Python additional packages. You can search for possible Python packages in Linux repository (for example, search for matplot in Ubuntu Software Center ).

(2) Use pip. Pip is a package management program that comes with Python. It connects to Python repository and searches for possible packages.

For example, use the following method to install, uninstall, or upgrade web. py:

$ Pip install-I http://mirrors.aliyuncs.com/pypi/simple web. py $ pip uninstall web. py $ pip install-I http://mirrors.aliyuncs.com/pypi/simple -- upgrade web. py

If your Python is installed in a non-standard path (using $ which python to confirm the path of the python executable file), such as/home/vamei/util/python/bin, you can use the following method to set the path of the pip installation package:

$ Pip install-I http://mirrors.aliyuncs.com/pypi/simple -- install-option = "-- prefix =/home/vamei/util/" web. py

 

Str1 = ['21 + 23 = ', '2017 =', '32*12 = ']

E = []

For s in str1:

S2 = s + str (eval (s [:-1])

E. append (s2)

Print (e)

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.