• 文件内光标的移动 函数基础 定义函数的三种形式 函数的返回值 调用方式


    # with open(r'a.txt', 'r', encoding='utf-8')as f:
    # data1=f.read()
    # print('>1>:',data1)
    # print(f.tell()) # 44 只有一种情况下,光标的意思是字符
    # data2=f.read()
    # print('>2>:',data2) # 第一次有结果,第二次没有,第一次读取数据后光标已经移到了文件尾

    # 只有一种情况下,光标以字符为单位:文件以rt方式打开,read()
    # with open(r'a.txt', 'rt', encoding='utf-8')as f:
    # print(f.read(8))
    # print(f.tell())
    # f.seek(0,0) # seek 字节为单位
    # print(f.read(8))
    # print(f.tell())
    # f.seek(13, 0)
    # print(f.read(3))
    # print(f.tell())

    # 只有0模式能够在t模式下使用
    # with open(r'a.txt', 'rt', encoding='utf-8')as f:
    # print(f.read(3))
    # f.seek(3,3)
    # print(f.read(3))

    # with open(r'a.txt', 'rb')as f:
    # print(f.read(6)) # b'helloxe4'
    # f.seek(2,1)
    # print(f.tell())
    # print(f.read().decode('utf_8'))

    # with open(r'a.txt', 'rb')as f:
    # f.seek(-3,2) # 光标倒着移动3个
    # print(f.tell())
    # f.seek(0,2) # 直接把光标移到末尾

    # tail -f access.log # 打开文件,读追加的内容

    # import time
    # with open(r'b.txt','rb')as f:
    # f.seek(0,2) # 光标移动到末尾
    # while True:
    # line=f.readline() # 所有的数据类型自带布尔值,一旦为空就是False
    # if line:
    # print(line,end='')
    # else:
    # time.sleep(0.05)

    # with open(r'b.txt', 'a', encoding='utf-8')as f:
    # f.write('you are my apple ')
    # f.flush() # 追加写并立即保存到硬盘

    # import time
    # with open(r'b.txt','rb')as f:
    # f.seek(0,2)
    # while True:
    # line=f.readline()
    # if line:
    # print(line) #‘end=''’bytes类型中没有换行这个意思,end=''就是普通的字符串
    # else:
    # time.sleep(0.05)

    # import time
    # with open(r'b.txt','rb')as f:
    # f.seek(0,2)
    # while True:
    # line=f.readline()
    # if line:
    # print(line.decode(),end='')
    # else:
    # time.sleep(0.05)

    # 文件截断
    # with open(r'a.txt', 'a', encoding='utf-8')as f:
    # f.truncate(3) # 只保留前三个字节

    # 为什么要有函数?没有函数带来的困扰
    # 组织结构不清晰,可读性差
    # 代码冗余
    # 可扩展性差
    # 什么是函数?
    # 具备某一个功能的工具
    # 先定义后调用
    # 函数的分类
    # 内置函数:len(),max(10,100),
    # 自定义函数:def
    # 自定义函数:
    # def 函数(参数1,参数2,...): # 不写参数可以,但是()要有
    # (缩进)'''注释''' # help(函数名) 注释必须要有,但是还是可选的
    # 函数体 # 必须有
    # return # 返回值 可选
    # ===============
    # You are my apple
    # ===============

    # def print_tag():
    # print('===============')
    # def print_msg():
    # print('You are my apple')

    # print(print_tag) # <function print_tag at 0x00000178BEC54DC8> 内存地址
    # 相当于print_tag=<function print_tag at 0x00000178BEC54DC8>

    # print_tag()
    # print_msg()
    # print_tag()
    # '''
    # ===============
    # You are my apple
    # ===============
    # '''
    # def print_tag():
    # print('*'*15)
    # def print_msg():
    # print('You are my apple')
    # print_tag()
    # print_msg()
    # print_tag()
    # '''
    # ***************
    # You are my apple
    # ***************
    # ''' # 扩展性高了

    # def auth():
    # name=input('name>>:').strip()
    # password=input('password>>:').strip()
    # if name == 'OBOS' and password == '123':
    # print('登陆成功')
    # else:
    # print('用户名或者密码错误')
    #
    # auth()

    # 定义函数阶段都发生了什么事?
    # 定义函数阶段只检测语法,不执行代码。语法没问题就定义成功了,不会报错。

    # 先调用后使用
    # def foo():
    # print('from foo')
    # bar()
    # def bar():
    # print('from bar')

    # 定义函数的三种形式
    # 第一种:无参函数 #
    # def foo():
    # def bar():
    # def print_tag():
    # def print_msg():
    # x=10
    # y=11
    # if x >= y:
    # print('x')
    # else:
    # print('y')
    # 第二种:有参函数
    # def my_max(x,y):
    # if x >= y:
    # print(x)
    # else:
    # print(y)
    # my_max(1,2)
    # def auth(name,password):
    # if name == 'OBOS' and password == '123':
    # print('登陆成功')
    # else:
    # print('用户名或者密码错误')
    #
    # def love():
    # name=input('name>>:').strip()
    # password=input('password>>:').strip()
    # auth(name,password)
    # love()
    # 函数尽可能各做各的,把一个个功能单独提取出来
    # 第三种:空函数
    # def auth():
    # '''
    # '''
    # pass
    # def put():
    # pass
    # def get():
    # pass
    # def is():
    # pass
    # 体现程序的组织结构

    # 函数的返回值 return
    # 函数内可以有多个return,但是只能执行一次return,执行return后函数立刻停止,并且把return后的值当作本次调用的结果返回
    # def foo(x,y):
    # return x+y
    #
    # res=foo(1,2)
    # print(res)
    # def my_max(x,y):
    # if x >= y:
    # return x
    # else:
    # return y
    #
    # res=my_max(1,2)
    # print(res)

    # def foo():
    # print('first')
    # return 1
    # print('second')
    # return 2
    # print('third')
    # return 3
    #
    # foo() # first 只执行一次,并且把return的值当作本次调用的结果返回
    # res=foo()
    # print(res) # 1

    # return返回的值没有限制
    # def bar():
    # print('from bar')
    # def foo():
    # return [1,2]
    #
    # print(foo()) # [1, 2]

    # def bar():
    # # print('from bar')
    # # def foo():
    # # return bar # 返回bar的内存地址
    # #
    # # print(foo()) # <function bar at 0x0000014639094DC8>
    # # print(foo() is bar) # True

    # 没有return # 默认返回None
    # def bar():
    # pass
    # print(bar()) # N/one
    # 返回一个值 # 值本身
    # 返回多个值 # 返回一个元组

    # 函数调用的三种形式

    # def my_max(x,y):
    # if x >= y:
    # return x
    # else:
    # return y
    # res1=my_max(1,2)
    # res2=my_max(1,2)*10
    # my_max(my_max(1,2),3) # 把函数调用当作另一个函数的参数 range(len[1,2,3])
    # res3=my_max(my_max(1,2),3)
    # print(res3) # 3
  • 相关阅读:
    css 背景透明 适合所有浏览器
    Razor中Html.DropDownListFor用法
    outlook2010如何初始化设置
    移除文件关联
    MVC中的Html.ActionLink的介绍(转)
    DES可逆加解密
    Razor视图下服务器代码给Javascript变量赋值
    js获取url参数值
    python 3 简单线程、数据库操、Mssql访问示例
    ps快捷键
  • 原文地址:https://www.cnblogs.com/0B0S/p/11952840.html
Copyright © 2020-2023  润新知