Thomas Wouters

45
reputation
1
8

----------------------------------------

Question 24 Level 1

Question: Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions. Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input() And add document for your own function

Hints: The built-in document method is doc

Solution: print abs.doc print int.doc print raw_input.doc

def square(num): '''Return the square value of the input number.

The input number must be integer.
'''
return num ** 2

print square(2) print square.doc

----------------------------------------

----------------------------------------

Question 25 Level 1

Question: Define a class, which have a class parameter and have a same instance parameter.

Hints: Define a instance parameter, need add it in init method You can init a object with construct parameter or set the value later

Solution: class Person: # Define the class parameter "name" name = "Person"

def __init__(self, name = None):
    # self.name is the instance parameter
    self.name = name

jeffrey = Person("Jeffrey") print "%s name is %s" % (Person.name, jeffrey.name)

nico = Person() nico.name = "Nico" print "%s name is %s" % (Person.name, nico.name)

----------------------------------------

----------------------------------------

Question: Define a function which can compute the sum of two numbers.

Hints: Define a function with two numbers as arguments. You can compute the sum in the function and return the value.

Solution def SumFunction(number1, number2): return number1+number2

print SumFunction(1,2)

----------------------------------------