Theme:
Why should there be a way?
The answer is: laziness is a virtue
Keywords for the method definition:
Def
Use callable to determine whether it is callable:
Copy Code code as follows:
x = 1
y = math.sqrt
Callable (x) #False
Callable (y) #True
About the return value of the method:
Copy Code code as follows:
def hello (name):
Return ' Hello, ' + name + '! '
There is an implementation of the algorithm: the first two numbers and is the number of the following
Copy Code code as follows:
Fibs = [0, 1]
For I in range (8):
Fibs.append (Fibs[-2] + fibs[-1])
Result:[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
This allows you to define a method implementation:
Copy Code code as follows:
def fibs (num):
result = [0, 1]
For I in Range (num-2):
Result.append (Result[-2] + result[-1])
return result
To jump out the execution of a command in a method:
Copy Code code as follows:
def test ():
print ' This is printed '
Return
print ' This isn't '
If the method doesn't return anything, it returns none.
About the parameters of a method
The question of whether or not the parameters of our incoming methods will be changed is encountered in many languages.
Example:
Copy Code code as follows:
def try_to_change (n):
n = ' Mr Gumby '
Name = ' Mrs. Entity '
Try_to_change (name)
Name # ' Mrs Entity '
The above example parameters are not changed, so those parameters are variable.
String number tuples are immutable types and we cannot change them. If we use a variable type as a parameter, we can implement the function that the parameter is changed within the method.
Copy Code code as follows:
def change (n):
N[0] = ' Mr Gumby '
names = [' Mrs Entity ', ' Mrs Thing ']
Change (names)
Names #[' Mr Gumby ', ' Mrs Thing ']
This part of the content is actually similar to Java, I used to have a blog: punch to reach
method, you can resolve the confusion of parameter meanings in the following ways:
Copy Code code as follows:
def hello_1 (greeting, name):
print '%s,%s! '% (greeting, name)
Hello_1 (greeting= ' Hello ', name= ' world ') #Hello, world!
A more named way to find someone:
Copy Code code as follows:
DEF init (data): #1
data[' i ' = {}
data[' middle '] = {}
Data[' last '] = {}
Def lookup (data, label, name):
return Data[label].get (name) #2
DEF store (data, full_name):
names = Full_name.split () #3
If len (names) = = 2:names.insert (1, ') #4
Labels = ' i ', ' Middle ', ' last '
For label, name in Zip (labels, names): #5
People = Lookup (data, label, name)
If people:
People.append (full_name) #6
Else
Data[label][name] = [Full_name]
Use:
Copy Code code as follows:
Mynames = {}
Init (mynames)
Store (mynames, ' Magnus Lie Hetland ')
Lookup (mynames, ' Middle ', ' lie ') #[' Magnus lie Hetland ']
Understand:
1,DATA data structure like this: {' middle ': {}, ' last ': {} ', ' The ' a ': {}}
The 2,get method finds values based on the build
3,string Split method, you can add a separator, the default is a space separator:
Copy Code code as follows:
Test = ' a,2,d '
Test.split (', ') #[' a ', ' 2 ', ' d ']
name = ' My xy dd '
names = Name.split ()
Names #[' my ', ' xy ', ' DD '
4,insert the value behind the insertion back, do not understand the replacement
Copy Code code as follows:
Names.insert (1, "")
Names #[' My ', ', ', ' xy ', ' DD '
Examples of 5,zip method descriptions:
Copy Code code as follows:
x = [1, 2, 3]
y = [4, 5, 6]
zipped = Zip (x, y) # (1, 4), (2, 5), (3, 6)
6, the name of the lookup already exists in the label, and the full name is added
About * and * * in Parameters
*: A parameter that represents any number
* *: Indicates dictionary parameter
Example:
Copy Code code as follows:
def print_params_2 (title, *params):
Print title
Print params
Print_params_2 (' params: ', 1, 2, 3)
The result is
Params:
(1, 2, 3)
Copy Code code as follows:
def print_params (**params):
Print params
Print_params (X=1, y=2, z=3)
The result is: {' Z ': 3, ' X ': 1, ' Y ': 2}
Combined with * You can improve the way you just stored your name:
Copy Code code as follows:
DEF store (data, *full_names):
For Full_name in Full_names:
names = Full_name.split ()
If len (names) = = 2:names.insert (1, "")
Labels = ' i ', ' Middle ', ' last '
For label, name in Zip (labels, names):
People = Lookup (data, label, name)
If people:
People.append (Full_name)
Else
Data[label][name] = [Full_name]
Call:
Copy Code code as follows:
D = {}
Init (d)
Store (d, ' Han Solo ')
Store (d, ' Luke Skywalker ', ' Anakin Skywalker ')
Lookup (d, ' last ', ' Skywalker ') #[' Luke Skywalker ', ' Anakin Skywalker ']
Summarize:
Although not used in the work, but a little time to learn, enrich themselves is also good oh.
Note can be used to look at learning, I hope that the future I see here, do not be discouraged, do not crazy, little bit of learning, may not be used in the future, but the ability to be patient may be the need for such a way to exercise out.
Let's move on!