answer :DRY It is a recognized guiding principle for programmers :Don’t Repeat Yourself.
Arm your mind quickly , Pick up function , Don't copy a piece of code again !
Using functions has the following benefits :
answer : Tolerable , In theory, you can have as many as you want , It's just that if there are too many arguments to a function , The probability of errors in the call will be greatly increased , Therefore, the programmer who writes this function will also be greeted by his ancestors , therefore , Try to keep it simple , stay Python In the world of , Simplification is the king !
answer : Use “def” keyword , Note that the function name should be followed by parentheses “()”, Then the colon is next to the parentheses “:”, Then the indented part belongs to the content of the function body
def MyFun((x, y), (a, b)):
return x * y - a * b
answer : If you answer two , Congratulations on your mistake , The answer is 0, Because writing like this is wrong !
Let's analyze it , The arguments to a function require variables , And here you try to use “ Yuan Zu ” It is infeasible to transmit in the form of .
I think if you write this , You're supposed to mean this :
>>> def MyFun(x, y):
return x[0] * x[1] - y[0] * y[1]
>>> MyFun((3, 4), (1, 2))
10
>>> def hello():
print('Hello World!')
return
print('Welcome To FishC.com!')
Hello World!
Because when Python Execute to return At the time of statement ,Python Think that this is the end of the function , Need to return ( Although there is no return value ).
def power(x,y):
return x ** y
def gcd(x, y):
while y:
t = x % y
x = y
y = t
return x
print(gcd(4, 6))
def Dec2Bin(dec):
temp = []
result = ''
while dec:
quo = dec % 2
dec = dec // 2
temp.append(quo)
while temp:
result += str(temp.pop())
return result
print(Dec2Bin(62))