2019.8.8 learning content and quizzes notes

Advanced application file

Front learned r: read (read), w: write (write), a: append (additionally) three uses

  1. r + can be read there may write (try not to use), r + is added in the back
with open('text.py','r+',encoding='utf-8') as f:
     data = f.read()
     #print(f.readable())
     f.write('x = 10')
     print(data)

Note: Try not to use this method

  1. w + can be written readable (Note: w will clear the file, not to use w +), empty the file and then write
with open('text.py','w+',encoding='utf-8') as f:
    data = f.readable()
    #print(data) (可读)
    f.write('333444')
    print(data)
  1. a + have additional functions, a pointer to the end of useless, features a +
with open('text.py','a+',encoding='utf-8') as f:
    # data = f.readable()
    # print(data)
    # f.write('111222')
    # print(data)
    # f.seek(3,0) # 指针在末尾,seek(x,0),x代表跳过它来读取后面的数据(按字节位移)
    # data = f.read()
    # print(data) 
    f.write('x=10')
    f.flush()

Built-in method files

seek (seeking) (byte displacement)

with open('text.py','rb') as f:
#     #f.seek(1,0) # 1表示位移1位,0表示从头开始
#     #print(f.read())
#     #.seek(2,1) # 2表示位移2位,1表示从当前位置开始
#     f.seek(0,2) # 0 表示位移0位,2表示从文件末开始,把指针移到文件末
#     print(f.read())

tell (tell), byte displacement

tell(告诉) #字节
with open('text.py', 'r', encoding='utf-8') as f:
    f.seek(2, 0)
    print(f.tell())

read (n) only in the mode r

with open('text.py', 'r', encoding='utf-8') as f:
    # print(f.read(2))

truncate

with open('text.py', 'a', encoding='utf-8') as f:
    data = f.truncate(7)# 把2个字符后面的东西清空
    print(data)

Note: The above four methods can be used, but not necessary

Files modified in two ways

First of all, do not modify a file that only cover a say

import os
# with open('text.py','r',encoding='utf-8') as fr,\
#     open('test.txt','w',encoding='utf-8') as fw:
#     data = fr.read()
#     data = data.replace('1444555','111222')
#     fw. write(data)
# os.remove('text.py')
# os.rename('test.txt','text.py')
import os
with open('text.py','r',encoding='utf-8') as fr, \
    open('test.py','w',encoding='utf-8') as fw:
    for i in fr:
        i = i.replace('111222','chen handsome')
        fw.write(i)
os.remove('text.py')
os.rename('test.py','text.py')

Defined functions

Function is more an idea, he is not a technology

def register():
"""注册函数"""
    username = input('请输入你的用户名:')
    pwd = input('请输入你的密码:')

    with open('user_info.txt', 'a', encoding='utf8') as fa:
        fa.write(f'{username}:{pwd}|')
register()
def login():
    """登录函数"""
    username = input('请输入你的用户名:')
    pwd = input('请输入你的密码:')

    with open('user_info.txt', 'r', encoding='utf8') as fr:
        data = fr.read()
        user_list = data.split('|')
        print(user_list)
        user_info = f'{username}:{pwd}'
        if user_info in user_list:
            print('登录成功')
        else:
            print('傻逼,密码都忘了')
login()

Define how the function

"""
def 函数名() #定义阶段(相当于造车轮阶段)
    """函数注释写在这里""" #函数相当于工具,注释相当于工具的说明书
    <代码块>
    
 #使用 #调用阶段(相当于开车阶段)
 函数名()

"""

Keep in mind : the definition phase does not execute the function body code that only detect syntax errors ***

def func()
   """func函数的注释撒地方撒地方撒大方"""
    # todo:未来写一个函数
    pass
print(login.())

Commonly used built-in method:

String: split

List: append

Dictionary: get

Collection: add

read(), write()

Three definitions way function

Parameters: is unknown variables mean

No reference function # tool may be used alone

def add():
    """无参函数"""
    x = input('num1')
    y = input('num2')
    print(int(x)+int(y))
add()    

There are function parameters

def add(x,y):  # 这个不能单独使用,必须加上配件才能使用
   """有参函数"""
    print(int(x)+int(y))
print(1)   
x = input('num1')
y = input('num2')
add(x,y)

Empty function : the definition of the function, and consequently no

def func():   #只知道工具叫什么名字,但不知道如何造出这个工具
    pass

Call functions

What the book calls the function: is the name of the function () that is calling the function, the function will be executed

Body codes, known or encountered executing the function body return after the end of all code

Function runs out code, if the function body do not write return, it will return None

Why call the function: Call function is to use the function function

E.g:

def add(x,y):
    reeturn x + y
    res = add(10,20)
    print(res*10)

Function's return value

What is the return value: returns the result value is the internal code obtained through a series of logic

def func():
    name = 'nick'
    return name
name = func()
print(name)
def add(x,y):
    #逻辑
    print(x+y)
    return x + y  # 函数的返回值,终止函数(跳出函数)
res = add(10,20)
print(res)
def add(x,y):
    # (x,y,x+y) # return可以返回任意数据类型
    return x,y,x+y  # 不加括号返回多个值时,默认用元组的形式返回

x,y,z = add(1,2)
print(x,y,z)

Function parameters

def add(num1,num2): #表示形参
    """有参函数"""
    print(int(num1)+int(num2))
add(1,2) # 表示实参

Parameter: the parameter definition phase only, in the form of parameters, use had nothing, accounts for only a position, with descriptive sense

Arguments: call phase only argument, the actual parameters have specific values

Location parameter: one is a write parameter

def add(num1,num2): #形参
    """有参函数"""
    print(num1)
    print(num2)          # 这样一个一个写形参,就是位置形参
    print(int(num1)+int(num2))
 

Location argument: a one write in the past, it is the location of argument

add(1,2) #实参

Note: two positions parameter, it is necessary to have two positions corresponding to arguments, from left to right correspond

Default parameter: no transmission parameters, a default value may be used; if you pass a reference value used to pass, the default parameter must be placed behind the position parameter

def shopping(name='nick'): # 形参经常会引用同一个值
    goods_dict = {1:'特斯拉',2:'奔驰',3:'nick'}
    print(f'恭喜{name},得到{goods_dict[1]}一个')
shopping()

Keyword argument: parameter name given in accordance with specific values, you can break location parameter must have one correspondence rule

def shopping(x,name = 'nick'):
    goods_dict = {1:'特斯拉',2:'奔驰',3:'nick'}
    print(f'恭喜{name},得到{goods_dict[x]}')
shopping(1,name = 'kuiming')

Note: The function of the parameters try not to over three

Keyword parameter: time needed by the general position of the position parameter argument +

Guess you like

Origin www.cnblogs.com/chmily/p/11348650.html