Python学习笔记
03python_function
print("defFunction" )
def my_function (x) :
if x>=0 :
return x
else :
return -x
x = input("please input the value of x:" )
print(my_function(int(x)))
def my_fun01 (y) :
pass
def my_fun02 (x) :
if not isinstance(x,(int,float)):
raise TypeError("bad operand type" )
if x!=100 :
print("not 100" )
else :
print("hundred" )
print(my_fun02("hehe" ))
print("defFunction" )
def my_function (x) :
if x>=0 :
return x
else :
return -x
x = input("please input the value of x:" )
print(my_function(int(x)))
def my_fun01 (y) :
pass
def my_fun02 (x) :
if not isinstance(x,(int,float)):
raise TypeError("bad operand type" )
if x!=100 :
print("not 100" )
else :
print("hundred" )
print(my_fun02("hehe" ))
print ("abs求绝对值" )
print (abs (100 ))
print (abs (-200 ))
print (abs (12.43 ))
print (max(1 ,2 ,3 ,5 ,-100 ))
print (float("123.200" ))
print (int ("12" ))
print (int (12.45 ))
print (str(100 ))
print (bool(1 ))
print (bool("" ))
print ("函数的别名" )
a = abs
print (a(-123 ))
n1 = 123141341
print (hex (n1))
print (n1,'\n' ,n1)
def power (x) :
return x*x
print(power(25 ))
def power (x,n=2 ) :
i = 1
for t in range(n):
i = i * x
return i
print(power(5 ,3 ))
print(power(25 ))
print("*********************************" )
def reroll (name,grade,age=6 ,city="xian" ) :
print("name:" ,name)
print("grade:" ,grade)
print("age:" ,age)
print("city:" ,city)
reroll("xiaoming" ,14 )
reroll("xiaohong" ,99 ,8 )
reroll("yangcongtou" ,100 ,9 ,"beijing" )
reroll("lanmao" ,13 ,city="shanghai" )
print("*****************坑*******************" )
def add_end (L=[]) :
L.append("END" )
print(L)
add_end([1 ,2 ,3 ])
add_end()
add_end()
add_end()
print("*******************调整*******************" )
def add_end_change (L = None) :
if L == None :
L = []
L.append("END" )
print(L)
add_end_change([1 ,2 ,3 ])
add_end_change()
add_end_change()
add_end_change()
print("*******************可变参数********************" )
def add_add (numbers) :
sum = 0
for i in numbers:
sum = sum+i
print(sum)
return sum
add_add([1 ,23 ,4 ,5 ,78 ])
def add_add (*numbers) :
sum = 0
for i in numbers:
sum = sum+i
print(sum)
return sum
add_add(1 ,2 ,3 ,4 ,567 ,1 )
add_add()
Lisa = [1 ,2 ,3 ,4 ,5 ,123 ,6 ,7 ]
hels = (1 ,2 ,4 ,45 ,4 ,7 ,88 ,9 )
add_add(Lisa[0 ],Lisa[1 ],Lisa[2 ],Lisa[3 ],Lisa[4 ],Lisa[5 ],Lisa[6 ],Lisa[7 ])
add_add(*Lisa)
add_add(*hels)
print("********************关键字参数*********************" )
def person (name,age,**kv) :
print({"name" :name,"age" :age,"other" :kv})
person("Aono" ,12 )
person("Aono" ,12 ,city="xian" )
person("Aono" ,12 ,city="xian" ,grade=100 )
extr = {"city" :"beijing" ,"grade" :123 }
person("Aono" ,12 ,**extr)
person("Aono" ,12 )
print("************************命名关键字参数*************************" )
def person (name,age,**kw) :
if "city" in kw:
print("jao" )
print("has_city" )
pass
if "grade" in kw:
print("hao" )
print("has_grade" )
pass
print("name:" ,name,"age:" ,age,"other:" ,kw)
person("Aono" ,24 ,addr="dianzizhengjie" ,zipcode=123456 )
person("Aono" ,24 ,addr="dianzizhengjie" ,zipcode=123456 ,city="xian" )
print("限制关键字参数为city和grade" )
def person (name,age,*,city,grade) :
print(name,age,city,grade)
person("Aono" ,24 ,city="xian" ,grade=100 )
def person (name,age,*,city="xian" ,grade) :
print(name,age,city,grade)
person("Aono" ,24 ,city="xian" ,grade=100 )
person("Aono" ,24 ,grade=100 )
print("*************************参数组合****************************" )
print("*************************必选参数、默认参数、可变参数、关键字参数和命名关键字参数****************************" )
print("*************************顺序:必选参数、默认参数、可变参数/命名关键字参数和关键字参数****************************" )
def fun01 (a,b,c = 0 ,*d,**e) :
print("a:" ,a,"b:" ,b,"c:" ,c,"d:" ,d,"e:" ,e)
fun01(1 ,2 ,[23 ,45 ],k="123" ,f = 12 )
r = {"name" :"Aono" ,"age" :23 ,"city" :"xian" ,"grade" :122 ,"school" :"jiliUniversity" }
def fun02 (*d,**e) :
print("d:" ,d,"e:" ,e)
fun02(**r)
print("*********************递归函数*********************" )
def fact (n) :
if n == 1 :
return 1
else :
return n*fact(n-1 )
print(fact(5 ))
print(fact(100 ))
print("*********************尾递归(类似于循环while)*********************" )
def fact_head (n) :
return fact_tail(n,1 )
def fact_tail (num,product) :
if num == 1 :
return product
else :
return fact_tail(num-1 ,num*product)
print(fact_head(5 ))
print(fact_head(100 ))
print("汉诺塔" )
print("设柱子A、B、C,有四个盘子,我们要想的是,第四个盘子从A-C就必须经历前三个盘子从A-B的经历,第三个盘子从A-B就必须经历前两个盘子从A-C,第二个盘子要经历从A-C就必须经历第一个盘子从A-B" )
def hanuo (n,a,b,c) :
if n == 0 :
return None
elif n == 1 :
print(a,"->" ,c)
else :
hanuo(n-1 ,a,c,b)
hanuo(1 ,a,b,c)
hanuo(n-1 ,b,a,c)
hanuo(9 ,"A" ,"B" ,"C" )
假设有四个:
3要做底:遍历前2
2要做底:遍历前1
1要做底:无需遍历
1移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
2移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
1要做底:无需遍历
1移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
3移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
2要做底:遍历前1
1要做底:无需遍历
1移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
2移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
1要做底:无需遍历
1移动判断另外两个位置有没有大于本身的数,如果有,移动到较小的,没有,移动到100
上边的去死!!!