《Python编程快速上手——让繁琐的工作自动化》读书笔记2

其实,写这个是为了督促自己看书……然后 ……其实没有然后了,人一松懈下来,就……ε=(´ο`*)))唉

第三章 函数

①def语句和参数

先举一个简单的例子:

def hello():
    print('Hello World!')

hello()
hello()
hello()
输出:
Hello World!
Hello World!
Hello World!

注意:一定要记得冒号(被无数次报错,内心崩溃,绝望)

好了,上面这种做法很类似在C++中的这种操作
#include<bits/stdc++.h>
using namespace std;
void hello()
{
    cout<<"Hello World!\n";
}
int main()
{
    hello();
    hello();
    hello();
    return 0;
}

所以,我们看到,在括号里是没有参数的。
如果仅仅可以这样,那这种语言……还不如没有……
所以,理所当然,括号里也可以出现参数,举例如下:

def hello(name):
    print('Hello '+name+'!')

hello('Alice')
hello('Bob')

输出:
Hello Alice!
Hello Bob!
------日常歪楼-------
对了,忘了提一点,在Python中,与C++不同,写在不同行的输出在运行的时候,输出的内容也会在不同行,如果要同行输出,就需要用逗号隔开。 
比如:
print('Hello ')
print('World!')

最后得到的是:
Hello
World!
这是因为print()函数自动在传入字符串末尾添加了换行符。          
但是         :
设置end关键字参数,像这样,也可以实现:Hello World!
代码如下:
print('Hello ',end='')
print('World!')

同时,注意,还有别的操作,像这样:
print('cats','dogs','mice')
#output:cats dogs mice
print('cats','dogs','mice',sep=',')
#output:cats,dogs,mice
---------言归正传-------
在上面的函数中,有一个名为name的变量当函数被调用的时候,参数就储存在这个变量中。



②返回值和return语句
 还是举例说明:
 
import random
def getAnswer(answerNumber):
    if answerNumber==1:
        return 'It is certain'
    elif answerNumber==2:
        return 'It is decidedly so'
    elif answerNumber==3:
        return 'Yes'
    elif answerNumber==4:
        return 'Reply hazy try again'
    elif answerNumber==5:
        return 'Ask again later'
    elif answerNumber==6:
        return 'Concentrate and ask again'
    elif answerNumber==7:
        return 'My reply is no'
    elif answerNumber==8:
        return 'Outlook not so good'
    elif answerNumber==9:
        return 'Very doubtful'
        
r=random.randint(1,9)
fortune=getAnswer(r)
print(fortune)

输出主要看运气……
比如,我是这种:
Concentrate and ask again

return语句包括:
return关键字;
函数应该返回的值或表达式。
在上面的函数里函数的名字是:getAnswer,参数的名字是:answerNumber。
r=random.randint(1,9)
fortune=getAnswer(r)
print(fortune)
这里也可以写成一行:
print(getAnswer(random.randint(1,9)))

如↑所示。



③None值
None值就是没有值,要注意首字母大写
None是NoneType数据类型的唯一值(在其他语言中,被称为null、nil或undefined)



④局部和全局作用域
作用域很重要,灰常重要,格外重要:
全局作用域中的代码不能使用任何局部变量;
但是局部作用域可以访问全局变量;
一个函数的局部作用域中的代码,不能使用其他局部作用域中的变量;
如果在不同的作用域中,你可以用相同的名字命名不同的变量。

1、局部变量不能在全局作用域内使用
比如:
def spam():
    eggs=31337
spam()
print(eggs)
报错信息如下:
Traceback (most recent call last):
  File "/usercode/file.py", line 4, in <module>
    print(eggs)
NameError: name 'eggs' is not defined

其实也就是说,eggs变量只属于spam()调用所创建的局部作用域。在程序执行从spam返回后,该局部作用域就被销毁了,不再有名为eggs的变量

2、局部作用域不能使用其他局部作用域内的变量

比如:
def spam():
    eggs=99
    bacon()
    print(eggs)
def bacon():
    ham=101
    eggs=10
spam()

输出:
99
可以很容易发现,在bacon中对eggs的赋值被完完全全忽略了……spam和bacon是两个局部作用域,所以bacon中的eggs与spam中的eggs的唯一关系就是名字恰好一样……


3、全局变量可以在局部变量中读取
def spam():
    print(eggs)
eggs=42
spam()
print(eggs)

输出:
42
42
在这里eggs就在全局变量中,所以局部作用域spam可以读取

4、名称相同的局部变量和全局变量
def spam():
    eggs='spam local'
    print(eggs)
def bacon():
    eggs='bacon local'
    print(eggs)
    spam()
    print(eggs)
eggs='global'
bacon()
print(eggs)
    

输出:
bacon local
spam local
bacon local
global
这样写使代码可读性降低,所以强烈建议尽量不要取相同的名字。




⑤global语句
举例如下:
def spam():
    global eggs
    eggs='spam'
eggs='global'
spam()
print(eggs)
    
输出:
spam


如何区分局部变量和全局变量
1、如果变量在全局作用域中使用(即在所有函数之外),它就总是全局变量
2、如果在一个函数中,有针对该变量的global语句,它就是全局变量
3、否则,如果该变量用于函数中的赋值语句,它就是局部变量
4、如果该变量没有用在赋值语句中,就是全局变量

举例:
def spam():
    global eggs
    eggs='spam'#this is global

def bacon():
    eggs='bacon'#this is local

def ham():
    print(eggs)#this if global
    
eggs=42#this is global
spam()
print(eggs)
    
输出:
spam




⑥异常处理
用try except语句调试,具体方法和C++里调试的方法区别不大,就是截取片段来试,酱紫。




'''
然后接下来,书里是一个很……的小程序,就不放上来了,或者哪天实在无聊敲一下。
所以就到这里啦。(✺ω✺)
'''



猜你喜欢

转载自blog.csdn.net/karen_yu_/article/details/79303752