Continue learning

  • 2020-04-02 09:35:55
  • OfStack

Topic:
Why is there a way?
The answer is: laziness is a virtue
Key words:
def
Use callable to determine if it is callable:
 
x = 1 
y = math.sqrt 
callable(x) #False 
callable(y) #True 

About the return value of the method:
 
def hello(name): 
return 'Hello, ' + name + '!' 

There is an implementation of the algorithm: the sum of the first two Numbers is the number that 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:
 
def fibs(num): 
result = [0, 1] 
for i in range(num-2): 
result.append(result[-2] + result[-1]) 
return result 

Jump out of command execution in the method:
 
def test(): 
print 'This is printed' 
return 
print 'This is not' 

If the method returns nothing, it returns None
The argument to the method
The question of whether the parameters of the methods we pass in have been changed or not is one that we encounter in many languages.

Example:
 
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 which parameters are variable.
A String number tuples is an immutable type and we cannot change them. If we use variable types as parameters, we can implement the function that the parameters are changed within the method.
 
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 had a blog post before: just hit it

Method to pass parameters, we can solve the confusion of parameter meaning through the following ways:
 
def hello_1(greeting, name): 
print '%s, %s!' % (greeting, name) 
hello_1(greeting='Hello', name='world') #Hello, world! 

A set of methods for finding people with more names:
 
def init(data): #1 
data['first'] = {} 
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 = 'first', '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:
 
MyNames = {} 
init(MyNames) 
store(MyNames, 'Magnus Lie Hetland') 
lookup(MyNames, 'middle', 'Lie') #['Magnus Lie Hetland'] 

To understand:

1, the data structured data like this: {' middle ': {},' last ': {},' first ': {}}
2. The get method finds the value according to the establishment
3. The split method of string can add a separator. By default, the space is the separator:
 
test = 'a,2,d' 
test.split(',') #['a', '2', 'd'] 
name = 'my xy dd' 
names = name.split() 
names #['my', 'xy', 'dd'] 

4. When you insert, the next value moves backwards. Do not interpret it as a replacement
 
names.insert(1, '') 
names #['my', '', 'xy', 'dd'] 

5. Example of zip method description:
 
x = [1, 2, 3] 
y = [4, 5, 6] 
zipped = zip(x, y)#(1, 4), (2, 5), (3, 6) 

6, the name of search already exists in this label, so add the full name
About * and ** in parameters


* : represents an argument of any number

** : represents the dictionary parameter

Example:
 
def print_params_2(title, *params): 
print title 
print params 
print_params_2('Params:', 1, 2, 3) 

As a result,

Params:
(1, 2, 3)
 
def print_params(**params): 
print params 
print_params(x=1, y=2, z=3) 

The result: {'z': 3, 'x': 1, 'y': 2}
Combine * to improve the way you just stored names:
 
def store(data, *full_names): 
for full_name in full_names: 
names = full_name.split() 
if len(names) == 2: names.insert(1, '') 
labels = 'first', '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:
 
d = {} 
init(d) 
store(d, 'Han Solo') 
store(d, 'Luke Skywalker', 'Anakin Skywalker') 
lookup(d, 'last', 'Skywalker')#['Luke Skywalker', 'Anakin Skywalker'] 

Conclusion:

Although not in the work, but some time to learn, enrich themselves is also good oh.
Notes can be used to review the study, I hope the future here, don't be discouraged, don't be arrogant, bit by bit of learning, may not be useful in the future, but the ability of patience may be needed to exercise in this way.
Let's move on!

Related articles: