2016/09/19

Source: Internet
Author: User
Tags mul

1. Python Video

1) Multilayer Decorative Device

user_info = {}def Check_login (func):d ef inner (*args, **kwargs): If User_ Info.get (' Is_login ', None): ret = func (*args, **kwargs) return retelse:print (' please login ') return innerdef check_admin ( Func):d EF inner (*args, **kwargs): If User_info.get (' type ', None) = = 2:ret = Func (*args, **kwargs) return Retelse:print (' No Permission ') return Inner@check_login@check_admindef index (): # managerprint (' index ') def Home (): # userprint (' home ')  def login (): User = input (' Input username\n>>> ') pwd = input (' input password\n>>> ') if user = = ' admin ' and PWD = = ' admin ': user_info[' is_login '] = trueuser_info[' type '] = 2else:if USER = = ' Wayne ' and pwd = = ' Phuck ': user_info[' Is_ Login '] = trueuser_info[' type '] = 1def main (): While TRUE:INP = input (' 1.login 2.information 3.management\n>>> ') If InP = = ' 1 ': Login () elif INP = = ' 2 ': Home () elif INP = = ' 3 ': Index () Main () 

2) string formatting
-percent semicolon
%[(name)][flags][width].[     Precision]typecode
1. Incoming parameters in sequence
2. Specify the name of the incoming parameter
3. After the decimal point is reserved
4. If placeholders appear, write% only,%
-format mode
[[Fill]align] [Sign] [#] [0] [Width] [,] [. Precision] [Type]

# S1 = ' I am%s '% ' alex ' # s1 = ' I am%s age%d '% (' Alex ', ' s) # S1 = ' I am% (name) ' is ' age ' d '%{' name ': ' Alex ', ' age ': 18}# S1 = ' percent%.2f '%99.97623# s1 = ' I am% (PP). 2f '%{' pp ': 123.425556}# s1 = ' I am%.2f% '%123.43556# print (s1) S1 = ' I A  M {}, age {}, {} '. Format (' Seven ', ', ' Alex ') S1 = ' I am {}, age {}, {} '. Format (*[' Seven ', ' Alex ']) S1 = ' I am {0}, age {1}, Really {0} '. Format (' seven ', +) S1 = ' I am {0}, age {1}, really {0} '. Format (*[' seven ', +]) S1 = ' I am {name} ', age {age}, Rea lly {name} '. Format (name= ' seven ', age=20) S1 = ' I am {name}, age {age}, really {name} '. Format (**{' name ': ' Seven ', ' Age ': 20}) S1 = ' I am {0[0]}, age {1[1]}, really {0[2]} '. Format ([1,2,3],[11,22,33]) S1 = ' I am {: s}, age {:d}, Money {: F} '. Format (' SEv En ', 18,8888.1) S1 = ' I am {: s}, age {:d} '. Format (*[' seven ', ") S1 = ' I am {name:s}, age {age:d} '. Format (name= ' seven ', age=18 S1 = ' I am {name:s}, age {age:d} '. Format (**{' name ': ' Seven ', ' age ':] S1 = ' numbers:{: #b},{: #o},{: #d},{: #x},{: #X},{:%} '. Format (15,15,15,15,15,15,87623,2) S1 = ' numbers:{0:b},{0:o},{0:d},{0:x},{0:x},{1:%} '. Format (15,16) S1 = ' numbers:{num:b},{num:o},{num:d},{num:x}, {num:x},{num:%} '. Format (num=15) print (S1)

  

3) Generator
-When a function call returns an iterator, the function is called the generator (generator);

-If the function contains yield syntax, then this function will become the generator;

>>> def func (): ... yield 1 ... Yield 2 ... Yield 3 ... Yield 4...>>>>>> temp = func () >>> Temp<generator object func at 0x00000207cc064a40> >>> temp.__next__ () 1>>> temp.__next__ () 2>>> temp.__next__ () 3>>> temp.__next__ ( ) 4>>> temp.__next__ () Traceback (most recent): File ' <stdin> ', line 1, in <module> Stopiteration>>>

  

def func ():p rint (111) yield 1print (222) yield 2print (333) Yield 3ret = func () r1 = ret.__next__ ()  # Enter function to find yield, Get data behind yield print (r1) r2 = ret.__next__ () print (r2) r3 = ret.__next__ () print (r3) # R4 = ret.__next__ () # Print (R4)

  

Def myrange (ARG): start = 0while true:if Start > Arg:returnyield StartStart + = 1ret = myrange (3) R = ret.__next__ () print ( r) R = ret.__next__ () print (r) r = ret.__next__ () print (r) r = ret.__next__ () print (R)

4) iterators
-Iterators are a way to access the elements of a collection.
-The Iterator object is accessed from the first element of the collection until all the elements have been accessed and finished.
-Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration.
-One of the great advantages of iterators is that all elements in the entire iteration are not required to be prepared beforehand.
An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed.
This feature makes it ideal for traversing large or infinite collections, such as several G files
Characteristics
The visitor does not need to care about the structure inside the iterator, but simply continues to fetch the next content through the next () method
A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
You can't go back halfway through the interview.
Facilitates recycling of large data sets, saving memory

>>> a = ITER ([1,2,3,4,5]) >>> A<list_iterator object at 0x00000207cc08ffd0>>>> a.__ next__ () 1>>> a.__next__ () 2>>> a.__next__ () 3>>> a.__next__ () 4>>> a.__next__ () 5 >>> a.__next__ () Traceback (most recent): File "<stdin>", line 1, in <module>stopiteration


5) recursion

def mul (n): If N>1:res = n * Mul (n-1) return reselse:res = 1return RESR = mul (8) print (R)

  

6) Module
-PY: module (first import, after use)
-Built-in modules
-Custom Modules
-third-party modules
-Other: Class Library
-Why a module: categorize the code.
-the basis for importing modules:

Import sysfor item in Sys.path:print (item) sys.path.append (' e:\\ ')

  

-The importance of the module name (do not duplicate the built-in module)
-Import Module:
Single module: Import
Nested under folder: from XXX import xxx
from xxx import xxx as ooo
-third-party module installation:
-Installation via PIP3: PIP3 Install requests
-Via Source: $ python setup.py Install
7) Module-serialization
-JSON is used to convert between [string] and [Python basic data type]
-The JSON module provides four functions: dumps, dump, loads, load

# import json# dic = {' K1 ': ' v1 '}# print (DIC, type (DIC)) # # res = json.dumps (DIC)  # Convert Python's basic data type to String type # print (res,t Ype (RES) # # # S1 = ' {"abc": 123} '  # outside single quotes, inside double quotes # dic2 = json.loads (S1)  # Converts a Python string type to a basic data type # print (Dic2, type ( DIC2)) Import Jsonli = [11,22,33]json.dump (li, open (' db ', ' W '))  # Dump: List converted to string, then write to file Li = json.load (open (' db ', ' r '))  # Load: Read the file and convert the string to the list print (type (li), Li)

  

-Weather-dependent JSON data for weather-based APIs

Import Requestsimport json_test1response = Requests.get (' http://wthrcdn.etouch.cn/weather_mini?city= Beijing ') response.encoding = ' utf-8 ' Print (Type (response.text)) dic = Json_test1.loads (response.text) print (Type (DIC))

  

-Pickle

2016/09/19

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.