程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Lesson 017: Functions - Lego building blocks of Python? After class test questions and answers

編輯:Python

Test questions :

0. You've heard of DRY Do you ?

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 !

1. Is to repeat a piece of code , Why do I use functions ( Instead of simply copying and pasting ) Well ?

Using functions has the following benefits :

  • Can reduce the amount of code ( It only takes one line to call the function , Copy and paste requires N Multiple code )
  • Can reduce maintenance costs ( The function only needs to be modified def Part content , Copy and paste need to be modified everywhere )
  • Make the preface easier to read ( No one wants to see a program repeat 10000 lines “I love FishC.com”

2. Can a function have multiple arguments ?

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 !

3. What keywords are used to create functions , What to pay attention to ?

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

4. How many parameters does this function have ?

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

5. What will be printed by calling this function ?

>>> 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 ).

use one's hands :

0. Write a function power() Simulate built-in functions pow(), namely power(x, y) To calculate and return x Of y The value of the power .


def power(x,y):
return x ** y

1. Write a function , Using Euclidean algorithm ( Brain tonic link ) Find the greatest common divisor , for example gcd(x, y) The return value is a parameter x And parameters y Maximum common divisor of .


def gcd(x, y):
while y:
t = x % y
x = y
y = t
return x
print(gcd(4, 6))

2. Write a function that converts decimal to binary , Required “ except 2 Remainder ”( Brain tonic link ) The way , Results and calls bin() Return the string form as well .

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))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved