练习

myfunc.py
#-*- coding:utf-8 -*-
#It will return a tuple type date,because the function need to return two objects.                                             #defining a function and seting a default value which must be in the end of the function paramaters.
def hello(s,d1='default value one',d2='default value two'):
    return 'a',1,s,d1,d2                                                                                                       
#defining a function that includes a variable parameter as tuple or list to use.
def mysum(*nums):
    s=0
    for temp in nums:
        s+=temp

    return s
#defining a function that includes a variable paramater so called "the key Parameter" as dict to use.
def printInform(name,age,**key):
    print(name,age,key,sep='\n')
def printInformAndCheckKeyParameters(name,age,**key):
    if 'IQ' in key:
        print("I have IQ")
    print(name,age,key,sep='\n')
#Habit and IQ are key paramter and are already restricted its key name by the char of '*'. If restricted key parameter already
had a default value, you can overlook it in performing function.
def printInformAndRestrictKeyParameters(name,age,*,Habit='sport&coding',IQ):
    print(name,age,Habit,IQ,sep='\n')
#If the parameter series already had a variable parameter before restricted key parameters, you can overlook the char of '*'.
def printInformAndRestrictKeyParametersInAnotherWay(name,age,*nums,Habit,IQ):                                                      print(name,age,sum(nums),Habit,IQ,sep='\n')
#The definition order:basic parameters > default parameters by user definition > variable parameters > key parameters > restric
ted key parameter that are already named.
2_lesson.py
#!/usr/bin/env python3
#-*- coding:utf-8 -*-

# It will extend the function of hello from the file of 2_lesson_func.py
from myfunc import hello
from myfunc import mysum
from myfunc import printInform
from myfunc import printInformAndCheckKeyParameters                                                                            from myfunc import printInformAndRestrictKeyParameters
from myfunc import printInformAndRestrictKeyParametersInAnotherWay                                                             #from myfunc import *                                                                                                          
#performing this function and observing its outcome.
print(hello('Jason'))
print(hello('Jason Liu',d2='I changed the default value two.'))                                                                #performing function
ls=[1,2,3,4,5]
t=(1,2,3,4)                                                                                                                    print(mysum(ls[0],ls[1],ls[2],ls[3],10))
print(mysum(*ls))
print(mysum(*t))

mp={'Habit':'sport&coding','IQ':99999}
print(printInform("Jason",22,Habit='sport&coding',IQ=99999))
print(printInform("Jason",22,**mp))
print(printInformAndCheckKeyParameters("Jason",22,**mp))
#---dict: dictionary is a key-value type date like Java's Map List. Its key is implemented by Hash algorithm and its key must b
e a constant type.
dic={'one':'Jsaon','two':123,"three":True}
print(f"{dic['one']}")
print("{0}".format(dic['two']))
#recover value
dic['one']="Jason Liu"
print("%s" % dic["one"])
print(dic.get('one'))
#Getting element and return a default value when the objec is not found.
print(dic.get('five','Not found'))                                                                                             
                                                                                                                               #---set: a list of that each element is unique and it will filter repetitive elements.                                         #It need a list type data.
s=set([1,1,2,3,'hello'])
print(s)

Q.E.D.


仰望星空,还需脚踏实地。