Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with function files in python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Python in how to deal with function files, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Document processing

# f=open ('a. Txtwriting, recording and writing') # read and write

# f=open ('a.txtwriting, recording and reading') # write and read

# f=open ('a. Txtrecording dagger') # append and read

# f=open ('a. TXT)

# f.write ('111111\ n')

# f.close ()

# context Management

# with open ('a. Txt') as with open ('a. Txt') as bauf:

# # print (f.read ())

# print ('= =)

# add

# for i in range (3):

# print (I)

# # continue

# if I = = 1:

# break

# else:

# print ('= >') # when the for loop is not interrupted by break, the else code is executed

#

# with open ('a. Txthandjiggle recording, writing, etc.) as read_f,\

# open ('aa.txt','w',encoding='utf-8') as write_f:

#

# for line in read_f:

# write_f.write (line)

# else:

# print ('write successfull')

# iTunes 0

# while i

< 5: # print(i) # i+=1 # if i == 3: # break # else: # print('------>

')

# with open ('a. Txtrecords as rb') as f:

# print (f.read (). Decode ('utf-8'))

# with open ('c. Txthammer dagger') as f:

# f.write ('ha '.encode (' utf-8'))

# f=open ('sb.jpg','r',encoding='utf-8') # text cannot read binaries

# print (f.read ())

# with open ('sb.jpg','rb') as read_f,\

# open ('sb_alex.jpg','wb') as write_f:

# data=read_f.read ()

# write_f.write (data)

# what is not commonly used, understand

# with open ('a. Txt recording, writing, etc., etc.) as f:

# print (f.read (4)) # digits refer to characters read

#

# with open ('a. Txtrecords as rb') as f:

# print (f.read (1)) # digits refer to characters read

# with open ('a. Txt recording, writing, etc., etc.) as f:

The number specified in # f.seek (3) # seek represents bytes

# print (f.tell ()) # current cursor location

# print (f.read ())

# with open ('aa.txt','r+',encoding='utf-8') as f:

# # numbers specified in f.seek (3) # seek represent bytes

# # print (f.read ())

#

# f.truncate (1)

# with open ('b.txtrecording recordings rb') as f:

# f.read ()

# f.seek (3) # by default, it starts with the start of the file and moves back 3 bytes

# f.read (1)

# print (f.tell ())

# f.seek (2Pol 1) # 1 means to move back 2 bytes starting with the current cursor.

# print (f.tell ())

# f.seek (- 1 bytes 2) # 2 Table starts with the current cursor and moves back 2 bets

# print (f.tell ())

# f.seek (0Pol 2)

# with open ('c. Txt recording, writing, etc.) as f:

# f.seek (0Pol 2)

# print ('= >', f.read ())

# tail-f access.log

Function

''

What's the problem without a function?

1. Unorganized and unstructured, code redundancy

two。 Poor readability

3. Cannot be managed uniformly and the maintenance cost is high.

''

# there are two types of functions in python: built-in functions and custom functions

# built-in functions

# sum

# max

# min

#

# a=len ('hello')

# print (a)

#

# b=max ([1pm 2pm 3])

# print (b)

# Custom function

# # *

# # *

# # *

# # hello world

# # *

# # *

# # *

Parameters of the function

# from a large point of view, there are two kinds of parameters of the function: formal parameter (variable name) and actual parameter (value)

# definition phase

# def foo (XBI y): # Xbox 1 recorder 2

# print (x)

# print (y)

# call phase

# foo (1Pol 2)

# the parameters of the distinguishing function can be divided into five types:

# position parameter, keyword parameter, default parameter, variable length parameter (* args,**kwargs), named keyword parameter

# position parameters

# def foo (xperiary z): # positional parameters: parameters that must be passed values

# print (xQuery yPiez)

#

# # foo (1, 2, 3)

# foo (1pr 2pm 3) # position real parameter: one-to-one correspondence with formal parameter

# keyword parameter: key=value

Def foo (xmeme yjinz):

Print (xmeme yjinz)

# foo (zonal 3, X, 1, and 2)

# keyword parameters need to pay attention to:

# 1: keyword arguments must come after location arguments

# 2: cannot repeatedly pass a value to a shape parameter

# foo (1 Magi Zhe 3 Ji Yue 2) # correct

# foo (Xero1Phone2pence3) # error

# foo (1century Xerox, 1jinx, 1pyrrine, 2fuzen3)

# default parameters

# def register (name,age,sex='male'): # parameters: default parameters

# print (name,age,sex)

#

# register ('asb',age=40)

# register ('a1sbaked Magi 39)

# register ('a2sbaked journal 30)

# register ('a3sbaked journal 29)

#

# register ('steel egg', 20pm female')

# register ('steel egg', sex='female',age=19)

# problems to be paid attention to in default parameters:

# 1: the default parameter must follow the non-default parameter

# def register (sex='male',name,age): # errors are reported at the definition stage

# print (name,age,sex)

# (understanding) 2: default parameters are already assigned in the definition stage, and only once in the definition phase

# asides 100000000

# def foo (xQuery yellowa):

# print (XBI y)

# axi0

# foo (1)

# III: the values of default parameters are usually defined as immutable types

# variable length parameter

Def foo: # * receives all overflow arguments defined by location and assigns values to args as tuples

Print (XBI y)

Print (args)

#

# foo (1, 2, 3, 4, 5)

# def add (* args):

# res=0

# for i in args:

# res+=i

# return res

# print (add (1, 2, 3, 4))

# print (add (1pm 2))

# def foo (x, y, * * kwargs): # * * all the arguments defined by keywords are received and assigned to kwargs in the form of a dictionary

# print (x, y)

# print (kwargs)

# foo (1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 5, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 5, 5, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 5

# def foo (name,age,**kwargs):

# print (name,age)

# if 'sex' in kwargs:

# print (kwargs ['sex'])

# if 'height' in kwargs:

# print (kwargs ['height'])

#

# foo ('egon',18,sex='male',height='185')

# foo ('egon',18,sex='male')

# naming keyword parameters (understand)

# def foo (name,age,*,sex='male',height):

# print (name,age)

# print (sex)

# print (height)

# # * the parameters defined later are named keyword parameters, which must be passed values and must be passed in the form of keyword arguments

# foo ('egon',17,height='185')

# def foo (name,age=10,*args,sex='male',height,**kwargs):

# def foo (name,age=10,*args,sex='male',height,**kwargs):

# print (name)

# print (age)

# print (args)

# print (sex)

# print (height)

# print (kwargs)

#

# foo ('alex',1,2,3,4,5,sex='female',height='150',a=1,b=2,c=3)

# def foo (* args):

# print (args)

# foo (1, 2, 3, 4) # 1, 2, 3, 4 * (1, 2, 3, 4)

# * ['Achievement pageantry, Bandlemagery, Candlemagne, D'], = >' Atrium, pageantry, cinema, cinnamon, cinnamon,

# foo (*]) # foo (*]) # foo ('Achilles pommel, Bacheng, Cynthia, D')

# foo (['Achilles Magnum, Bronx Magnum, Cunninghamia pundits, D']) #

# def foo (xQuery yjinz):

# print (xQuery yPiez)

#

# # foo (* [1mem2pr 3]) # foo (1pr 2pr 3)

# foo (* [1pr 2]) # foo (1pr 2)

# def foo (* * kwargs):

# print (kwargs)

#

# # Xero1Yee 2 * * {'Yee: 2,' x: 1}

# # foo (Xero1Ji Yao2)

#

# foo (* * {'Yee: 2,' x: 1) # foo (* * {'Yee: 2,' x: 1)

# def foo (xQuery yjinz):

# print (xQuery yPiez)

#

# # foo (* * {'zippermatil3recractionaliziza1)) # foo (ximper1recoveryzymor2)

# foo (* * {'zipper1)) # foo (* * {' zipper3)

# def foo (xQuery yjinz):

# print ('from foo',x,y,z)

#

# def wrapper (* args,**kwargs):

# print (args)

# print (kwargs)

#

#

# wrapper (1, 2, 3, 3, 7, 1, 5, 2, 1, 5, 2)

#

# def foo (xQuery yjinz):

# print ('from foo',x,y,z)

# def wrapper (* args,**kwargs):

# print (args) # args= (1meme 2jin3)

# print (kwargs) # kwargs= {'axiaqizuzhuo 1jinghuanghuo 2}

# foo (* args,**kwargs) # foo (* (1pyrrine 2), * * {* {'afiuuvajiaqizhuo 1pyrrine 2}) # foo (1mai 2meme 3recovery2mema1)

# # wrapper (1, 2, 3, 1, 2, 3, 3, 1, 1, and 2)

# wrapper (1Jing Zhe 2JI Yue 3)

# def foo (xQuery yjinz):

# print ('from foo',x,y,z)

# def wrapper (* args,**kwargs):

# # print (args) # args= (1,)

# # print (kwargs) # kwargs= {'Yizhuvu 3 # Zizhuizhuang 2}

# foo (* args,**kwargs) # foo (* (1,), * * {'Yizhuo 3) # foo (1)

# # wrapper (1, 2, 3, 1, 2, 3, 3, 1, 1, and 2)

# wrapper (1Jing Zhe 2JI Yue 3)

#

# add: what exactly is done in the function definition phase: only the syntax of the function body is detected and will not be executed

# def bar ():

# x

# if 1 > 2:

# print ('= =)

#

# bar ()

The return value of the function

# def foo ():

# print ('from foo')

# return None

# res=foo ()

# print (res)

''

The return value is None in all three cases:

No return.

Return doesn't write anything.

Return None

''

# def foo ():

# print ('from foo')

# xroom1

# return x

# res=foo ()

# print (res)

# return this is the result returned by a value function call

Def foo ():

Print ('from foo')

Xero1

Return 1, [2Jing 3], (4Jing 5), {}

# res=foo ()

# print (res) # print result: (1, [2, 3], (4, 5), {})

# a recorder, brecrum, c, recorder, Foo ()

# print (d)

# return value 1, value 2, value 3. Return result: (value 1, value 2, value 3.)

# t = (1pm 2pm 3)

# a ~

# print (a)

# t = (1, 2, 3, 4, 5, 6, 7, 8, 9)

# a, 5, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4

# print (a)

# print (c)

Call function

Def foo ():

Print ('from foo')

Def bar (name):

Print ('bar=== >', name)

# function calls can be divided into two types with and without parameters

Foo () # is defined with no parameters, and there is no need to pass in parameters when calling

Bar ('egon') # is defined with parameters and must be called with parameters

# according to the calling form and location of the function, there are three types

Statement form of foo () # calling function

Def my_max (XBI y):

Res=x if x > y else y

Return res

# res=my_max (1J2) * 10000000 # the expression form of the calling function

# print (res)

Res=my_max (my_max (1010) 20), 30) # call the parameters of another function in the function

Print (res)

Custom function

# Why define functions? Define first and then use it. If you use it directly without definition, it is tantamount to referencing a variable name that does not exist.

# foo ()

# def foo ():

# print ('from foo')

# print (foo)

The use of the # function includes two stages: the definition phase and the usage phase

# Syntax

# def function name (parameter 1, parameter 2, etc.):

# "" document comments ""

# function body

# return value

# x=len ('hello')

# print (x)

# define three forms of function

# 1: no-parameter function: if the function of a function is only to perform some operations, it is defined as a no-parameter function, which usually has a return value

Def print_star ():

Print ('# * 6)

# 2: define a parametric function: the execution of the function depends on the external parameters, and the parameterized function usually has a return value

# def my_max (XBI y):

# res=x if x > y else y

# return res

# Ternary expression

Xerox 10

Yellow2

# if x > y:

# print (x)

# else:

# print (y)

#

# res=x if x > y else y

# print (res)

# 3: empty function

# def auth ():

# "" Authentication function ""

# auth ()

Def insert ():

"" insert function ""

Def select ():

"" query function ""

Def delete ():

"" remove feature ""

Def update ():

"" Update feature ""

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report