First, the Cognition functionHelp (method name) help (Round)1. Functional2. Hide Details3. Avoid writing Duplicate code4. Organizing code custom functionsThe definition of function and its operation characteristicsThird, how to let the function return multiple resultsIv. sequence unpacking and chain assignmentFive, must parameters and keyword parametersVi. Default ParametersSeven, variable parametersEight,
# Universal parameters: With one star and two stars# #*args, **kwargs generally use this expressiondef func (*args, **kwargs):## def func (**kwargs,*args): # Two stars and one star's position cannot be interchanged, must one star in front, two stars in the back. print (Args,type (args)) print (Kwargs,type (Kwargs)) Func (11,22,33,44,k1=k2= "v2") # #默认情况会自动把11, 22,33,44 This parameter is encapsulated in args, automatically k1=" v1 ", k2=" V2 " this parameter is encapsulated inside the kwargs.
Python imitates the message sending function of the web version, and python sends messages
This version of the web version is cumbersome, but not difficult, without encryption throughout the process. If you are interested, you can try to have fun. If you are interested, you can perfect it and make some interesting things.
Development Environment: Windows 10Develo
Python uses the first-class function to implement the design mode, and the python Design Mode
Case study: Restructuring the "Policy" Model
Some design patterns can be simplified if the functions used as first-class objects are reasonably used, and the "Policy" pattern is a good example.
Classic "policy" Mode
UML class diagram for processing order discounts in
Python-Day4 achieves simple shell sed replacement function, python-day4sed
Code:
1 f = open ('yesterday', 'R', encoding = 'utf-8') 2 f2 = open ('Yesterday. bak ', 'w', encoding = 'utf-8') 3 old_str = input ('Enter the character to modify :') 4 replace_str = input ('Enter the character to replace with: ') 5 for line in f. readlines (): 6 line = line. replace (ol
# default parameter: Must be placed at the end of the formal parameter list# def send (name,xx = "OK"):# ...# # using default parameters# Send ("Eric") #对形式参数的第一个元素赋值, the second element uses the default parameters.# # Specify Parameters# Send ("Eric", "No") #对形式参数的第一个元素赋值, the default parameter of the second element is re-assigned.## def send (mail_addr,xx = "OK", content,): #xx = "OK" in the middle will be an errordef send (mail_addr,content,xx ="OK"):#默认参数需要放到参数列表最后, xx = "OK" is re-assig
calls to the n n Iterato R. This have the effect of dividing the input into n-length chunks.
zip()Should only is used with unequal length inputs if you don ' t care about trailing, unmatched values from the longer iterab Les. If Those values is important, use itertools.zip_longest() instead.
zip()In conjunction with the * operator can is used to unzip a list:
>>>
>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = Zip (x, y) >>> list (zipped) [(1, 4), (2, 5 ), (3, 6)]>>> x2, y2 = Zip (*z
In computer programming, a Callback function, or Callback, refers to a reference to a piece of executable code that is passed to other code through function parameters. This design allows the underlying code to call a subroutine defined at the higher level. There are two types of callback functions:
The code is as follows:
Blocking callbacks (also known as synchronous callbacks or just callbacks)Deferr
#!/usr/bin/python# Filename:filereader.pyImport Sysdef readfile (filename):' Print a file to the standard output. 'f = file (filename)While True:line = F.readline ()If Len (line) = = 0:BreakPrint line,F.close ()If Len (SYS.ARGV) print ' No action specified. 'Sys.exit ()If Sys.argv[1].startswith ('--'):option = sys.argv[1][2:]if option = = ' Version ':print ' Version 1.2 'elif option = = ' help ':print ' \Prints files to the standard output.Any number
Python mail sending function, python mail sending
Import smtplibFrom email. mime. text import MIMEText_ User = "1147016115@qq.com" # sender_ Pwd = "wcpxldrtuthagjbc" # QQ mail authorization code_ To = "1208832227@qq.com" # recipient
Msg = MIMEText ("Hellow, This is my first Email! ") # Email contentMsg ["Subject"] = "come form xieolei! "# Subject displayed by rec
Python format function, python string format
1. The format can accept infinite parameters, and the positions can be unordered:
In [1]: "{}{}". format ("hello", "world") # do not set the location, In the default order Out [1]: 'Hello World' In [2]: "{0} {1 }". format ("hello", "world") # specify the location Out [2]: 'Hello World' In [3]: "{1} {0} {1 }". format ("
Python implements multi-thread brute-force cracking and vro login function code sharing, and python Multithreading
At runtime, upload the user.txt passwd.txt file in the directory. Otherwise, an error is reported. No exception handling is added to the program. Code is frustrating .....Copy codeThe Code is as follows:# Coding: UTF-8-Import base64Import urllib2Impo
Python Regular Expressions implement the calculator function, python Regular Expressions
Requirements:
The user enters an operation expression and the terminal displays the calculation result.
Code:
#! /Usr/bin/env/python3 #-*-coding: UTF-8-*-"the user inputs the computation expression, show the calculation result "" _ author _ = 'jack' import rebracket = re. com
The libraries that manipulate dates in Python are: datetime, TIME
In any language, the date function is definitely the most commonly used function. Directly below the instance code
#datetimeimport datetime# Current Date now = Datetime.datetime.now () print (Now.strftime ('%y-%m-%d%h:%m:%s ')) print ( Now.strftime ('%y-%m-%d ')) #string convert to Datetimetime_st
Problem:There are two special situations in the Function Definition of Python: The *, * form.For example, Def myfun1 (username, * keys) Or Def myfun2 (username, ** keys.
Explanation:* This parameter is used to pass any parameter without a name. These parameters are accessed in a tuple format.
** It is used to process and pass any parameter with a name. These parameters are accessed by dict. *
Applicat
There is no independent function in C #, only the concept of a class (dynamic or Static) method, which refers to a member of a class that performs calculations or other behavior. In Python, you can define a dynamic or static member method of a class in a way similar to C #, because it supports full object-oriented programming as it does in C #. You can also write Python
This article mainly introduces how to further understand function programming in Python. this article further discusses some key points of function programming in Python, from the IBM official technical documentation, for more information, see the most difficult question: "What is
Analog Grep-rl "python" F:\xuyaping this command#查看xuyaping文件夹所有的绝对路径import Osg=os.walk ("f:\\xuyaping") #g为迭代器for i in G: # Print (i) #i为文件路径 for J in I[-1]: file_path= "%s\\%s"% (i[0],j) print (File_path)Program Output Result:F:\xuyaping\xuyaping.txt.txtf:\xuyaping\xuyaping1.txt.txtf:\xuyaping\a\a.txt.txtf:\xuyaping\a\a1\a1.txt.txtf:\ Xuyaping\a\a1\a2\a2.txt.txtf:\xuyaping\b\b.txt.txtThe code is as follows:#模拟grep-rl "
-------------------Program Operation Principle-------------------1, the module built-innameproperty of the main module whose value isMain, import the module whose value is the module name1, the creation time, the py file is newer than the PYc file, then the new generation PYc.2, Magic Num, do the pre-run version test, the version is different to regenerate PYc.3. Pycodeobject object, string in source code, constant value, bytecode instruction, corresponding relation of original code line number.
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.