首页 > 编程语言 >python爬虫基本学习——函数(2.16博客补)

python爬虫基本学习——函数(2.16博客补)

时间:2023-02-17 10:23:05浏览次数:39  
标签:test1 test2 python 局部变量 爬虫 print 2.16 全局变量 def

函数

概念:编写程序时,需要某块代码多次,为了提高编写效率和代码的重用,把具有独立功能的代码块组织为一个小模块,即函数。

代码练习

'''
#函数的定义
def printinfo():
    print("------------------")
    print("人生苦短,我用python")
    print("------------------")

#函数的调用
printinfo()
printinfo()
'''
#带参数的函数
'''
def add(a,b):
    c = a + b
    print(c)

add(11111,56789)
'''
#带返回值的函数
'''
def add(a,b):
    return a + b

result = add(111,222)
print(result)
'''
#带多个返回值的函数
'''
def divid(a,b):
    shang = a//b
    yu = a%b
    return shang,yu

shang,yu = divid(5,2)           #需要使用多个值来保存返回内容
print(shang,yu)
'''
#课堂练习
'''
def printinfo():
    print("-------------")

def printxuan(num):
    i = 0
    while i < num:
        printinfo()
        i += 1

printxuan(1)
def addTh(a,b,c):
    return a + b + c

def average(a,b,c):
    return addTh(a,b,c)/3

print(average(10,20,30))
'''

#全局变量和局部变量
'''
def test1():
    a = 300     #局部变量
    print("修改前,a的值为%d"%a)
    a = 100
    print("修改后,a的值为%d" % a)

def test2():
    a = 500
    print("a的值为%d"% a)


test1()
test2()
'''
'''
a = 1000        #全局变量
def test():
    print(a)
test()
'''
'''
a = 1000  #局部变量和全局变量相同名字
def test1():
    a = 300     #局部变量优先使用
    print("修改前,a的值为%d"%a)
    a = 100
    print("修改后,a的值为%d" % a)

def test2():
    print("a的值为%d"% a)  #没有局部变量,默认使用全局变量


test1()
test2()
'''
a = 1000  #局部变量和全局变量相同名字
def test1():
    global a        #声明全局变量的标识符
    print("修改前,a的值为%d"%a)
    a = 100
    print("修改后,a的值为%d" % a)

def test2():
    print("a的值为%d"% a)  #没有局部变量,默认使用全局变量

test1()
test2()

标签:test1,test2,python,局部变量,爬虫,print,2.16,全局变量,def
From: https://www.cnblogs.com/he-cheng/p/17126754.html

相关文章

  • Python——for循环
    1.for循环for确定循环次数while不确定循环次数格式:for临时变量in可迭代对象:重复执行的代码1重复执行的代码2……例:foriinstr_data:#从左......
  • python zipfile 排除指定文件类型后的文件夹压缩
    """压缩指定文件夹排除指定格式的文件"""importzipfilefrompathlibimportPathpath='./aaa.zip'#压缩文件路径path=Path(path)xya_file=Path('./te......
  • python urlencode()改为quote_plus()
    使用对拼接的字符串,以字典的格式传入加密fromurllib.parseimporturlencodebase_url="https://m.weibo.cn/api/container/getIndex?"params1={"value":"english......
  • Python Flask 使用蓝图(Blueprint)
    HTRegister.pyfromflaskimportBlueprintfromcontrollers.registerDAOimportRegisterregister=Blueprint("register",__name__,url_prefix="/register")#......
  • Python 进制转换
    内置函数:int(x,base=10)print(int('0101',2))#二进制转换为十进制print(int('27',8))#八进制转换为十进制print(int('A0',16))#十六进制转换为十进制 将十进制......
  • 2.16 背包学习
    11.背包问题求方案数思路求最优方案数可以分成两步第一步求出最优方案,也就是最大价值第二部求最大价值下的方案数具体有多少种而求出当前i,j下最大价值,然后求出相应......
  • 使用Python读取Excel中的数据并进行相关性分析
    在进行数据相关分析的时候,往往面对的是复杂所庞大的数据集,这个时候,Python所完成的脚本能够帮助你方便且快捷地整理很多数据!1.你所需要的第三方库在本次实验中,你所需要的......
  • Python 爬虫方法总结
    实现爬虫的套路准备URL准备start_urlurl地址规律不明显,总数不确定通过代码提取下一页的url通过xpath提取寻找url地址,部分参数在当前的响应中(比如当前页码数和总......
  • 2.16流程控制
      流程控制分支结构:分支结构就是根据条件判断的真假去执行不同分支对应的子代码注意事项: 1.根据条件的成立与否,决定是否执行if代码块 2.我们通过缩进代......
  • python接口自动化13-API流量回放/锲约测试/自动化测试
    PPL-Tester简介http工具集,通过代理获取到API的请求与响应信息,将这些请求信息进行流量回放/锲约测试或快速生成用例,亦可通过人工进行修改参数化提取、变量引用、断言......