Use two basic ways to modify the function of the file

Both editing mode file

Open with # ( 'a.txt', MODE = '+ R & lt T', encoding = 'UTF-. 8') AS F: 
# f.seek (9,0)
# f.write ( '<M women's director>' )

# files that were modified in two ways
# one way: text editor is used in this way
# realization of ideas: the contents of the file is read into memory all at once issued, and then modify the original file is completed and then written back cover in memory
# advantages : in the process of amending the document only one copy of the same data
# disadvantages: will take up too much memory
# with Open ( 'c.txt', the MODE = 'RT', encoding = 'UTF-8') AS f:
# RES reached, f.read = ()
# = res.replace Data ( 'Alex', 'DSB')
# Print (Data)
# with Open ( 'C.txt', MODE = 'wt', encoding = 'UTF-. 8') f1 AS:
# f1.write (the Data)

# Second way:
# Import os
# realization of ideas: to open the original file read, write way to open a temporary file, line by line to read the contents of the original document, written after modification ... into a temporary file, delete the original file, the temporary file is renamed original file name
# advantages: do not take up too much memory
# disadvantages:In the file modification process the same data stored duplicate
# with open ( 'c.txt', mode = 'rt', encoding = 'utf-8') as f, \
# open('.c.txt.swap', mode='wt', encoding='utf-8') as f1:
# for line in f:
# f1.write(line.replace('alex', 'dsb'))
# os.remove('c.txt')
# os.rename('.c.txt.swap', 'c.txt')

# f = open('a.txt')
# res = f.read()
# print(res)
函数的基本使用
"" " 
1, what is the function of
a function is equivalent tool has a function of
using the function must follow a principle:
first definition
After calling
2. Why do we use function
1, the organizational structure is not clear, poor readability
2, the code redundancy More than
3, maintainability, scalability poor

3, how to use the function
to define
three definitions of the way
after calling
three kinds of call mode

the return value of
three kinds of return value in the form of
"" "
# first, define
#define syntax
'' '
DEF function name (parameter 1, parameter 2, ...):
"" "document describes" ""
function body
return value
'' '

# form a: no function parameter
# DEF FUNC ():
# X #
# # Print (
# print ( 'ha')
# print ( 'ha')
# Print ( 'ha ha ha')

thing happened defined functions #
# 1, the application memory space to save the body function code is
# 2, the above-mentioned memory address binding function name
# 3, defined functions will not execute the function body code, but the body will detect function syntax

thing happened # call the function
# 1, by function were found function memory address
# 2, then add slogan is performing the trigger function thereof code
# Print (FUNC)
# FUNC ()

# exemplary. 1
# DEF bar (): # bar = memory address of the function
# print ( 'from bar ')
#
# DEF foo ():
# # Print (bar)
# bar ()
# Print (' from foo ')
#
# foo ()

# model 2
# DEF foo ():
# # Print (bar)
# bar ()
# Print ( 'from foo')
#
# DEF bar (): # bar = function of the memory address
# Print ( 'from bar')
#
# foo ()

# exemplary. 3
# DEF foo ():
# # Print ( bar)
# bar ()
# Print ( 'from foo')

# foo ()
#
# DEF bar (): # bar = memory address of the function
# Print ( 'from bar')

# form II: with a function parameter
# def func (x, y) : X = Y =. 1 # 2
# Print (X, Y)
# FUNC (1,2)

# three forms: empty function code function body Pass
# DEF FUNC (X, Y):
# Pass

# three definitions of various ways where used in
# 1, no function parameter scenario
# DEF Interactive ():
# name = INPUT ( '>> username:')
# Age = INPUT ( '>> Age:')
# = Gender INPUT ( 'Gender >>: ')
# MSG =' name: Age {}: {gender} '.format (name, Age, gender)
# Print (MSG)

# Interactive ()
# Interactive ()
# Interactive ()
# Interactive ()

# 2, has a function parameter scenario
# def add (x, y) :# Parameters - the "raw material
# # x = 20
# # y=30
# res=x + y
# # print(res)
# return res # 返回值-》产品

# # add(10,2)
# res=add(20,30)
# print(res)

# 3、空函数的应用场景
# def auth_user():
# """user authentication function"""
# pass

# def download_file():
# """download file function"""
# pass

# def upload_file():
# """upload file function"""
# pass

# def ls():
# """list contents function"""
# pass

# def cd():
# """change directory"""# 2, the expression in the form:# the Add (1,2)# Interactive ()# 1, the statement of the form: bracketed only call the function# two, call the function
Pass #







# Def add (x, y) : # Parameters - "raw
# RES = X + Y
# # RES return Return Value -" Product
# AssignExpression
# RES = the Add (1,2)
# Print (RES)
# Mathematical Expression formula
# RES = the Add (1,2) * 10
# Print (RES)

#. 3, the function call can be used as the parameter
# RES = the Add (the Add (1,2), 10)
# Print (RES)

# Third, the return value of the function
# return is a function of the end of the flag, i.e., once the code is running function body will immediately return to
# termination functions of running and will return a value obtained as a result of this operation returns:
# 1, returns None: no return in vivo function
# return
# return None

# 2, returns a value: return value
# DEF FUNC ():
# return 10

# RES = FUNC ()
# Print (RES)

#. 3, a plurality of return values: a plurality of values separated by commas, will be returned to the return tuple
# DEF FUNC ():
# return 10, 'AA', [. 1, 2]

# RES = FUNC ()
# print(res, type(res))

Guess you like

Origin www.cnblogs.com/0B0S/p/12511689.html