Python basis of summarizing the tenth day began [further in-depth look at the function, re-learn what] (novice can supervise each other)

Thank you for your attention recently, I hope my study notes to help everyone! Also thanks for your comments and recommendations, please exhibitions.

        Before rediscovering the function, we look at two functions. One is that we often use in the previous notes print (); the other is the input ().

print () function: for printing out, the most common of a function. We also have in front of almost every small cases used functions.

Let's look at the parameters of the function print (), look at the code to achieve:

In particular: sep meaning = '' and end = '' parameter.

. 1  Print ( ' A ' , ' B ' ,. 1, ' GG ' )                 # a plurality of output objects. A plurality of output objects, need, separated. 
2  Print ( ' A ' , ' B ' ,. 1, ' GG ' , On Sep = ' * ' )        # spaced plurality of objects, the default value is an asterisk. 
. 3  Print ( ' A ' , ' B ' ,. 1, ' GG ' ,0000 ' )     # set to what end, can be replaced with the character you need.

How, for print output print () function is not the use of it? Next we look at another input () function:

input () which accepts a standard input data, return type string, the string is. That input () function default result is a string, whether you enter an integer / fractional, will default quotation marks, into a string. We look at the code:

. 1 B = INPUT ()     # interacts with the user, the input data in the console 
2  Print (B)       # we enter the digital object type int 
. 3  
. 4  Print (type (B))   # we use type () function, see variables b data types, string types found oh

          en ...... I believe you learn, but also understand the meaning I want to express, you may ask, we need to return a digital object of type int how to do, we only need to cast the result as an int on it.

Here I will not speak about that, behind the scenes notes used to give everyone a look.

          Then see print () function and input () function usage, we rediscover our function.

The notes written before the concept a little scope, and now we look at the scope of variables:

Scope of variables:

x = 2                 #全局变量
def add():            #定义函数
    x = 10            #局部变量
    print('这里打印的数字是:',x)

add()        #调用函数
print('******************************')
print('这里打印的数字是:',x)

 

         我们在代码中看到两个变量名为:X,但是第一个函数里面的print()函数,输出的是内部的X变量;第三个print()函数,输出的是 外面的X变量。两个变量没有任何瓜葛的哦,虽然都是叫X,但是他们都是在自己的领域起到作用,函数里面的变量叫做:局部变量;函数外面的变量叫做:全局变量。记住哦,影响的范围不一样的。

       如果我们想在函数体内引用外面的全局变量,怎么办?我们看下代码

 

 1 x = 2                 #全局变量
 2 def add():            
 3     global x          #申明我们要在函数里面使用全局变量 x
 4     y = 10            #局部变量
 5 
 6     print('x + y 等于 :',x+y)            #我们在函数体内计算x+y=?
 7 
 8 add()        #调用函数
 9 print('******************************')
10 print('这里打印的数字是:',x)

在函数内多了 一个关键字:global   ,我们看到的:global  x  这行代码就是在声明 X是全局变量,也就是说,这个X和外面的X是同一个X。所以

2  + 10=12.     如果有不理解的小伙伴留言哦~~~

接下来我们看下什么是形参,什么是实参:写函数的时候,def关键字后面函数名括号中的参数是形参,而调用函数时指定的参数是实参。

1 def add(a,b = 3):  #定义函数时候的参数a和b=3 ,他们是形参
2     print(a+b)
3 
4 add(a = 2,b = 5)   #调用函数的时候:a = 2 ,b = 5 ,他们是实参

看到上面的函数执行后的结果等于 7 吗?说明形参即使默认等于3,但是调用的时候有实参b=5,实参中有自己的值,在执行中,使用实参的值,而不是形参的默认值。如果不指定实参,则使用的就是形参默认的值:

1 def add(a,b = 3):  #定义函数时候的参数a和b=3 ,他们是形参
2     print(a+b)
3 
4 add(a = 2)   

 

 缺省参数的规则:

带有默认值的缺省参数,必须从右至左依次存在,下面的案例中,如果变量a在最右的位置,执行会报错的哦!

1 def add(a,b=5,c=1):  #带有默认值的缺省参数必须从右至做依次存在。
2     print(a+b+c)
3 
4 add(2)

接下来我们看下可变参数是怎么回事:

定义可变参数,只需要在参数名前面加一个*号,调用的时候python解释器会创建一个tuple元组,把传入的参数放可变参数的元组中。

1 def add(a,*b):  #可变参数格式,参数名前面加*一个号
2     print(a)
3     print(b)   #我们看下B是不是一个空的元组
4 
5 add(2)        #我们只传入一个参数给a

 

我们多放几个参数进去,试试看看怎么样。

1 def add(a,*b):  #可变参数格式,参数名前面加*一个号
2     print(a)
3     print(b)   #在我们不确定传入的参数有多少个的时候,可变参数真的很好用,b指向了这个元组
4 add(2,3,'abc',[1,2,'acc'])        #我们传入多个参数

 如果我们传入的参数对象在列表或元组中存放,那么我们可以这样传入*参数名:

1 nums = [1,2,3] #也可以是元组哦
2 
3 def add(a,*b):
4     print(a)   #会将
5     print(b)
6 
7 add(*nums)  #函数调用时,传入*参数,会对传入的列表或元组参数进行一个解包功能,每一个元素自动作为函数的参数。

 

最后我们再看下关键字参数,关键字参数是:  **kargs  参数名称前两个*号哦!!也就是通常说的字典参数。我们看下代码的实现:

1 dict_1 = {'city':'beijing','job':'IT'}
2 
3 def add(name,age,**kargs):      #它**kargs,就是关键字可变参数
4     print(name,age,kargs)
5 
6 add('小红','25',**dict_1)   #它**dict_1是代表传入一个字典

 

 你学会了吗?如果有不懂的地方敲十代码,还是不懂就留言吧

总结:

1、当*和**符号同时出现在函数定义的参数中时,都表示参数列表可接受任意数量的参数,均是可变参数;

2、*参数,表示的是任意多个或者0个参数,说白了就是有数据的元组,或者空元组,即使一个元素,也是元组;

3、**参数,表示任意多个参数,说白了就是字典参数;

4、如果定义的函数中,两个同时存在,一定将*参数,放在**参数前面,看示例:

1 def add(name,*age,**kargs):      #元组参数一定放在在字典参数前面!!!

5、传入的参数是*参数或者**参数,这个动作就是解包;

6、最后参数大集合:记住在函数括号中的优先级哈~~~~

1 def add(name,job='IT'*age,**kargs):      #普通参数,缺省参数,元组参数,字典参数。记住顺序优先级哈

7、什么是缺省参数:声明的函数中,某个参数有一个默认的值。调用函数的时候,实参中有自己的值,在执行中,使用实参的值,而不使用形参的默认值。如果不指定实参,则使用的就是形参默认的值。

 

今天对函数有了新的认识,要 反复使用,消化~~~~~

                                             

 

Guess you like

Origin www.cnblogs.com/woshidaliua/p/11285583.html