0基础学习pythonTASK4——函数

目录

1、函数初练习 

2、函数参数

3、变量赋值

4、函数可以返回一些东西

 5、一个函数里面调用函数


1、函数初练习 

#函数部分
def print_two(*args):
    arg1,arg2 = args
    print(f"arg1:{arg1},arg2:{arg2}")
    
def print_two_again(arg1,arg2):
    print(f"arg1:{arg1},arg2:{arg2}")
    
def print_one(arg1):
    print(f"arg1:{arg1}")
          
def print_none():
          print('I got nothin')
          
print_two('xiaosun','SX')
print_two_again('xiaoyang','SC')
print_one('xianghaung')
print_none()

在def的同一行给了我们函数一个名字,在本例子名字命名为print_two,一般函数命名时尽量和自己的项目相关,能代表本函数的意图,最好简短一些,

2、函数参数

函数参数可以分为位置参数、默认参数、可变参数,关键字参数。命名关键字参数,参数组合,

arg-1》》》》位置参数 这类参数在调用函数时位置要固定

arg2》》》》 爱arg2 = v,(默认参数= 默认值)

默认参数的值若没有传入,则被认为是默认值

默认参数一定放在位置参数后面,否则程序会报错

#函数参数练习
def practice(name,age='24'):
    print(f"我的名字是{name},我的年龄是{age}")
    
practice('xiaosun',age = 29)

*args>>>>可变参数,就是参数传入的个数是可变的,是不定长的参数,可以是0到任意个,自动组装为元组形式

#函数参数练习
def practice(name,age=8,*args):
    print(f"my name is{name},my age is{age},my score is{args}")
    
          
    
practice( 'xiaosun',  24, 175)

这是请教领航员11大佬,学习到一些东西,例如当函数参数不确定的话,可以使用*args和**kwargs,args没有k值,kwargs有k值,函数参数为*args,实际用的时候不用带,

每个变量的地位是同等的,*args和**kwargs除外,

#函数参数练习
def practice(name,age,*args,**kwargs):
    print(f"my name is{name},my age is{age},my score is{args},my height is {kwargs}")
    
          
    
practice(  'xiaosun', 24, 100, height=175)

************注意 :args参数返回是一个元组,**kwargs返回的是一个字典 

3、变量赋值

#函数练习
def cheese_and_crackers(cheese_count,boxes_of_crackers):#定义函数cheese_and_crackers
    print(f"You have {cheese_count} cheese!")
    print(f"You have {boxes_of_crackers}boxes_of_crackers!")
    print("Man that's enough for a party")
    print('get a blanket.\n')#调用函数时运行的程序,也就是打印出这四句话
    
print("we can just give the function numbers directly")
cheese_and_crackers(10,20)#10,20为上述函数赋值了两个变量,调用了函数总共打印出5句话


print("OR,we can use variable from our script")
amount_of_cheese = 10#给变量amount_of_cheese赋值10
amount_of_crackers = 50#给变量amount_of_crackers赋值50

cheese_and_crackers(amount_of_cheese,amount_of_crackers)#调用函数,值为10和50


print("we can even do math inside too")
cheese_and_crackers(10+20,5+6)#数学运算赋值

print("we can combine the two,variables and marh:")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+3000)#变量和数学运算组合

这个例子展示了我们可以给函数cheese_and_crackers赋值的几种不同方式,我们可以直接给他数字或者是变量。也可以是数学运算,甚至是数学运算和变量的结合。

函数的参数有点类似于我们给变量赋值时的符号,=,如果可以用=来定义一个东西,你就可以把她作为参数赋给函数

from sys import argv
input_file = '11.txt'


def print_all(f):
    print(f.read())
    
def rewind(f):
    f.seek(0)
    
def print_a_line(line_count,f):
    print(line_count,f.readline())
    
current_file = open(input_file)

print("Frist let's print the whole file:\n")
rewind(current_file)

print("Now let's rewind,kind of like a tape.")
current_line =1
print_a_line(current_line,current_file)

current_line +=1
print_a_line(current_line,current_file)


current_line =current_line+1
print_a_line(current_line,current_file)

seek(0)表示的是移动到问头位置,但是看到后重新理解下,seek()函数处理的是字节,不是行,

4、函数可以返回一些东西

def add(a,b):
    print(f"ADDING{a}+{b}")
    return a+b#返回两数之和
add(1,10)


def subtract(a,b):
    print(f"SUNTRACTING{a}-{b}")
    return a-b


def multiply(a,b):
    print(f"MULTIPLYING{a}-{b}")
    return a-b

def divide(a,b):
    print(f"DIVIDEING{a}-{b}")
    return a/b

print("Let's do some math with just function")

age = add(30,5)
height = subtract(78,4)
weight = multiply(90,2)
iq = divide(100,2)
print(f"age:{age},height:{height},multiply:{multiply},divide:{divide}")

what = add(age,subtract(height,multiply(weight,divide(iq,2))))
print("That's become:",what,"can you do it by hand?")

0基础学习pythonTASK4——函数_第1张图片

 5、一个函数里面调用函数

#递归函数
def f(n):
    if n ==1:
        return 1
    return n*f(n-1)
f(5)
lst = [1,2]
for i in range(9):
    lst.append(sum(lst[-2:]))
print(lst)
#斐波那契数列
def count(n):
    if n <=1:
        return n
    return count(n-1)+count(n-2)

lst = list()
for k in range(11):
    lst.append(count(k))
    
print(lst)

https://github.com/datawhalechina/unusual-deep-learning/blob/main/docs/5.CNN.md

你可能感兴趣的:(学习,python,开发语言)