初学python一些会用到的零件4函数【参考小甲鱼的书学习】

#创建与调用函数

#def myFirstFunction():

#    print("会创建函数了,我很开心")#用关键字def创建函数,记得加小括号与冒号

#myFirstFunction() #调用创建的函数,实现函数内所有的功能,会创建函数了,我很开心

#for i in range(3):

#    myFirstFunction()#调用了3遍,会创建函数了,我很开心,会创建函数了,我很开心,会创建函数了,我很开心

# 函数的参数 ,参数使得函数具有灵活性与个性化,参数可以有多个,但是实现的功能与参数的意义要写好注释

#1个参数的情况

#def mySecnodFunction(name):

#    print(name+"是个大胖子!")

#mySecnodFunction("")  #郭是个大胖子!

##2个参数的情况,只需要用逗号隔开就好

#def add(num1,num2):

#    print(num1 + num2)

#add(1,2) #3

    

#函数的返回值,返回一些数据来报告执行结果

def add(num1,num2):

    return num1+num2

add(1,2)

##形参和实参

##形参是函数创建和定义的过程中小括号里的参数,实参是函数被调用的过程中实际传递进来的参数

#函数文档,是为了让别人更好的理解你的函数,描述函数的功能

##关键字参数

#普通的参数叫做位置参数,容易搞乱位置而出错,用关键字参数解决这个问题#

#def saysomething(name,words):

#    print(name + "->" + words)

#saysomething("I","love you forever!") #I->love you forever!

#saysomething(name = "I",words = "love you forever!")# 关键字参数要多打几个字,但可以避免出bugI->love you forever!

##默认参数,默认参数是在定义的时候赋予了默认值的参数

#def saysomething(name = "I",words = "love you forever!"):

#    print(name + "->"+ words)

#saysomething() #I->love you forever!

#收集参数,即可称为可变参数,不知道这个函数有几个参数,要遭参数前面加上*

#def test(*params):

#    print("%d个参数" % len(params))

#    print("第二个参数是:",params[1])

#test("L","O","V","E") #4个参数

                      # 第二个参数是: O

#若在收集参数后还需要指定其他参数,就应该使用关键参数来指定

#def math(*params,extra):

#    print("收集参数是:",params)

#    print("位置参数是:",extra)

#math("I","YOU","HE","SHE",extra = "them")#收集参数是: ('I', 'YOU', 'HE', 'SHE')

#                                         #位置参数是: them

##也可以通过默认参数来指定,这样不易出错

#def math(*params,extra = "them"):

#    print("收集参数是:",params)

#    print("位置参数是:",extra)

#math("I","YOU","HE","SHE","them")#收集参数是: ('I', 'YOU', 'HE', 'SHE', 'them')

#                                 #位置参数是: them

#*既可以打包,也可以解包

def test(*params):

    print("%d个参数:"%len(params))

    print("第二个参数是:",params[1])

a = [1,2,3,4,5]

test(*a)  # *表示实参需要“解包”后才能使用

#5个参数:

#第二个参数是: 2

#def test():

#    return[1,"小甲鱼",3.14]

#test()

##fromkeys()用于创建并返回一个新的字典:第一个参数是字典的键值,第二个参数是可选的,传入键对应的值

#dict1 = {}

#print(dict1.fromkeys((1,2,3)))##{1: None, 2: None, 3: None}

#dict2 = {}

#print(dict2.fromkeys((1,2,3),"Number"))##{1: 'Number', 2: 'Number', 3: 'Number'}

#dict3 = {}

#print(dict3.fromkeys((1,2,3),("0ne","two","three")))##{1: ('0ne', 'two', 'three'), 2: ('0ne', 'two', 'three'), 3: ('0ne', 'two', 'three')}

#访问字典的方法有keys(),values()Items()

#dict1 = {}

#dict1 = dict1.fromkeys(range(32),"")

#print(dict1.keys())##dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])

#print(dict1.values())#dict_values(['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''])

#print(dict1.items())##dict_items([(0, ''), (1, ''), (2, ''), (3, ''), (4, ''), (5, ''), (6, ''), (7, ''), (8, ''), (9, ''), (10, ''), (11, ''), (12, ''), (13, ''), (14, ''), (15, ''), (16, ''), (17, ''), (18, ''), (19, ''), (20, ''), (21, ''), (22, ''), (23, ''), (24, ''), (25, ''), (26, ''), (27, ''), (28, ''), (29, ''), (30, ''), (31, '')])

#print(dict1.get(31))##

#print(dict1.get(32))## 找不到指定的值,返回默认值None

#print(31 in dict1)#判断一个键是否在字典中,True

#print(32 in dict1)#判断一个键是否在字典中,False

#print(dict1)#{0: '', 1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', 8: '', 9: '', 10: '', 11: '', 12: '', 13: '', 14: '', 15: '', 16: '', 17: '', 18: '', 19: '', 20: '', 21: '', 22: '', 23: '', 24: '', 25: '', 26: '', 27: '', 28: '', 29: '', 30: '', 31: ''}

#dict1.clear()

#print(dict1)#清空一个字典,{}

#复制一个新的字典,用copy

#dict2 = {}

#dict2 = dict2.fromkeys(range(3),"学号")

#print(dict2)#{0: '学号', 1: '学号', 2: '学号'}

#dict3 = dict2.copy()

#print(dict3)#{0: '学号', 1: '学号', 2: '学号'}

##pop()是给定键弹出对应的值  popitem()是弹出一个项

#a = {1:"one",2:"two",3:"three"}

#print(a.pop(2))#弹出对应的键值two

#print(a.popitem())#弹出一个项(3, 'three')

#print(a)#{1: 'one'}

#setdefault()方法与get()方法相似,setdefault()在字典中找不到相应的键值时会自动添加

#a = {1:"one",2:"two",3:"three"}

#a.setdefault(4)

#print(a)#{1: 'one', 2: 'two', 3: 'three', 4: None}

#update 可以用来更新字典

#pets = {"米奇":"老鼠","汤姆":""}

#pets.update(汤姆="")

#print(pets)##{'米奇': '老鼠', '汤姆': ''}

猜你喜欢

转载自blog.csdn.net/nowfuture/article/details/78260384