zoukankan      html  css  js  c++  java
  • 学习python第三天

    函数的三种定义方式:

    1、无参函数

    不需要接收外部传入的参数

    def foo()
       print("feom foo...")
    f00()

    2、有参函数

    需要接收外部传入的参数

    def login(user,pwd):
        print(user,pwd)
    #传参,多一不可,少一不可
    login('tank','123')
    login('tank','123''111')#错误
    login('tank')#错误

    3、比较两数大小

    def max2(x,y):
        if x > y:
            print(x)
        else:
            print(y)
    
    max2(20,10)
    空函数
    适应范围:
    遇到一些比较难实现的功能,会导致暂时无法继续编写代码,所以一般多用在生产开发中都会将所有的功能实现定义成空函数
    def fun():
        pass #pass表示什么都不做
    fun()
    函数对象:指的是函数名指向的函数地址
    def func():
        pass
    print(func)  #返回func函数指向的函数地址
    func()
    
    def func2():
        pass
    
    dict1={'1':func,'2':func2}
    choice=input("请输入功能编号:").strip()
    if choice in dict1:
        dict1[choice]()  #dict1[2]即为func2,加括号表示调用func2函数

    嵌套函数

    1、定义

    def func1():
        print('func1...')
        def func2():
            print('fun2...')
            def func3():
                print('fun3...')
            return func3
        return func2

    2、调用方式

    1)通过函数内部的函数值,调用函数

    func2=func1()
    func3=func2()
    func3()

    2)内部调用方法

    def func1():
        print('func1...')
        def func2():
            print('func2...')
            def func3():
                print('func3...')
            func3()
        func2()
    func1()
    名称空间

    分类:python解释器自带的:内置名称空间

    自定义的py文件内,顶着最左边写的:全局名称空间

    函数内部定义的:局部名称空间

    name='tank'
    def func1():
        print(name)
        def func2():
            print('func2...')
    
    func1()

    内置模块

    time模块

    import time#导入time模块
    print(time.time()) #获取时间戳
    #等待5s
    time.sleep(5)
    print(time.time())

    OS模块

    #os
    import  os
    print(os.path.exists('zmm.txt'))#判断给定路径是否存在
    print(os.path.exists('zmm1.txt'))#判断给定路径是否存在
    print(os.path.exists(r'F:软件pycharm工程3dayzmm.txt'))#判断给定路径是否存在
    
    print(os.path.dirname(__file__))#F:/软件/pycharm/工程/03day

    Sys模块

    #sys
    import  sys
    #获取python在环境变量中的文件路径
    print(sys.path)
    sys.path.append(os.path.dirname(__file__))
    print(sys.path)

    Json模块

    #json
    import json
    user_info={
        'name':'tank',
        'pwd':'123'
    }
    
    #dump :序列化
    #1、把字典转化成json数据
    #2、再把json数据转化成字符串
    res=json.dumps(user_info)#通过此种方式将字典类型转化成字符串类型,得到的字符串类型都是双引号的
    print(res)
    print(type(res))#得到字符串类型
    with open('user.json','wt',encoding="utf-8") as f:
        f.write(res)
    
    with open('user1.json','wt',encoding="utf-8") as f:
        json.dump(user_info,f)#dump自带的write功能
    
    
    
    #loads 反序列化
    json.loadS()
    #1、把json文件的数据读到内存中
    with open('user.json','r',encoding="utf-8") as f:
        res=f.read()
        user_dict=json.loads(res)
        print(user_dict)
        print(type(user_dict))#得到字典类型
    
    #load自定触发f.read
    with open('user.json','r',encoding="utf-8") as f:
        user_dict=json.load(f)
        print(user_dict)

    模块与包

    #import 模块名
    import B
    
    #会导入B模块中a的文件
    #会自动执行a文件中的代码
    from B import a
    a
    
    #__name__  为B.a

    #print('from a')
    def func1():
        print('from func1')
    
    #用于测试函数
    print(__name__)  #__main__
    if __name__=='__main__':
        func1()

    爬虫

    http协议:
    请求url:
    http://www.baidu.com/
    请求方式:
    GET
    请求头:
    Cookie:可能需要关注
    User-Agent:证明你是浏览器
    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
    Host:www.baidu.com
    import requests
    
    response=requests.get(url="http://www.baidu.com/")
    response.encoding='utf-8'
    print(response)#<response [200]>
    print(response.status_code)#200返回响应状态码
    
    print(response.text)#返回响应文本
    print(type(response.text))#<class 'str'>
    
    with open('baidu.html','w',encoding='utf-8') as f:
        f.write(response.text)
    
    res=requests.get(url='https://video.pearvideo.com/mp4/adshort/20190613/cont-1565846-14013215_adpkg-ad_hd.mp4')
    print(res.content)
    with open('视频.mp4','wb') as f:
        f.write(res.content)

    作业:从校花网中爬取一张图片

    import requests
    res=requests.get(url='https://www.dxsabc.com/api/xiaohua/upload/min_img/20190206/20190206uUbpVqqhiC.jpg')
    print(res.content)
    with open('宋仲基.jpg','wb') as f:
        f.write(res.content)

    小结:今天在tank老师的带领下学习了更多有关python的知识,其中最有趣的是关于爬虫的知识,让我觉得python的有趣性,同学们的学习氛围也很高涨,老师的教学也很风趣。

     
     
     
     
  • 相关阅读:
    【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
    【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
    【leetcode】236. Lowest Common Ancestor of a Binary Tree
    【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
    【leetcode】352. Data Stream as Disjoint Intervals
    【leetcode】897. Increasing Order Search Tree
    【leetcode】900. RLE Iterator
    BEC listen and translation exercise 26
    BEC listen and translation exercise 25
    BEC listen and translation exercise 24
  • 原文地址:https://www.cnblogs.com/zmmm/p/11018708.html
Copyright © 2011-2022 走看看