One, the file operation
f
=
open
(
"file.txt"
,
‘r‘
)
print
(
len
(f.read()))
#文件总长度
print
(f.tell())
#读完文件,文件指针位置
f.seek(
0
,
0
)
#偏移回文件头
print
(f.readline())
#打印出文件中一行(第一行)
print
f.tell()
#显示文件指针现在的位置
f.seek(
2
,
1
)
#从当前文件指针(向后)偏移2个字节
print
(f.readline())
#再打印一行(应该少2个字节)
print
(f.tell())
#显示 现在 的指针位置
f.seek(
-
7
,
1
)
#从当前位置向前偏移7个字节
print
(f.readline())
#打印下一行
print
(f.tell())
#显示 现在 的指针位置
f.seek(
-
9
,
2
)
#从尾部向前偏移9个字符
print
(f.tell())
#显示 现在 的指针位置
print
(f.readline())
#打印出内容
f.seek(
0
)
#==>默认是0 等等seek(0,0)
f.seek(
80
)
#等同上面的seek(-9,2) 从尾部向前取
function Shape parametric the memory unit is allocated only when it is called, and the allocated memory unit is freed immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when a function is returned, the parametric argument can be used as a constant, variable, expression, function, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter. Therefore, you should use the assignment, input and other methods to get the parameters to determine the value def stu_register (name, age, country, course): print ("----Enrolled Student information------") Print ("Name:", name) print ("Age:", age) print ("Nationality:", country) Print ("Course:", course) stu_register ("Wang Shanbao", "CN", "Python_devops") stu_register ("Zhang Jiaochun") , +, "cn", "Linux") Stu_register ("Liu Lao Gen", "("), "cn", "Linux") non-fixed parameters If your function is undefined, you can use a non-fixed parameter def If you are not sure how many parameters the user wants to pass in Stu_ Register (name, age, *args): # *args will turn multiple incoming parameters into a tuple form print (name, age, args) stu_register ("Alex" Stu_register ("Jack", +, "CN", "Python") can also have a **kwargsdef stu_register (name, age, *args, **kwargs): # * Kwargs will turn multiple incoming parameters into a dict form print (name, age, args, Kwargs) stu_register ("Alex") stu_ Register ("Jack", +, "CN", "Python"), sex= "Male", province= "Shandong")
1. Normal call
As with function calls in other languages, in Python, when you call a function, you need to give the same number of arguments as the parameter and correspond in order one by one.
def
fun(name, age, gender)
Xxxx
fun(
‘Jack‘
,
Call 20
: ,
‘man‘
)
2. Calling functions using keyword parameters
Functions can also be called by keyword=value keyword parameters, because we explicitly point out the correspondence, so the order of the parameters is irrelevant.
def
fun(name, age, gender):
XXXXX
调用:fun(gender
=
‘man‘
, name
=
‘Jack‘
, age
=
20
)
3. Call the function with default argument
A function in Python can also specify a default value for one or more parameters so that it can be selectively omitted when called:
def
fun(a, b, c
=100
):
print
(a,
b,
c)
fun(
1
,
2
)
fun(
1
,
2
,
3
)
Note: Typically, the default value is computed only once, but if the default value is a Mutable object when it differs, such as when a list, dictionary, or most class object is used. For example, the following function accumulates parameter values in subsequent calls:
def
fun(a, L
=
[]):
L.append(a)
print
(L)
fun(
1
)
# 输出[1]
fun(
2
)
# 输出[1, 2]
fun(
3
)
# 输出[1, 2, 3]
4. Call variable parameter function
Specify that a function can receive any number of arguments by adding an asterisk (*) or two asterisk (* *) before the parameter.
def
fun(
*
args):
print
(
type
(args))
print
(args)
fun(
1
,
2
,
3
,
4
,
5
,
6
)
# 输出:
# <class ‘tuple‘>
# (1, 2, 3, 4, 5, 6)
def
fun(
*
*
args):
print
(
type
(args))
print
(args)
fun(a
=
1
,b
=
2
,c
=
3
,d
=
4
,e
=
5
)
# 输出:
# <class ‘dict‘>
# {‘d‘: 4, ‘e‘: 5, ‘b‘: 2, ‘c‘: 3, ‘a‘: 1}
As can be seen from the output of two examples: when a parameter is shaped like *args, any actual attendees passed to the function are wrapped in a tuple (tuple) by position, and any key=value that are passed to the function are wrapped into a dictionary (dict) when the parameter is shaped like **args.
5. Calling functions by unpacking parameters
The last point is that when you pass any number of arguments, they are packaged into a tuple or dictionary, and of course there is an unpacking (unpacking) package. Unpack list, tuple, and dictionary by single and double star:
def
fun(a
=
1
, b
=
2
, c
=
3
):
print
(a
+
b
+
c)
fun()
# 正常调用
list1
=
[
11
,
22
,
33
]
dict1
=
{
‘a‘
:
40
,
‘b‘
:
50
,
‘c‘
:
60
}
fun(
*
list1)
# 解包列表
fun(
*
*
dict1)
# 解包字典
# 输出:
# 6
# 66
# 150
Iii.. Return value to get the execution result of the function, you can return the result to the note using the return statement:
- The function stops executing and returns the result as soon as it encounters a return statement during execution
- If return is not specified in the function, the return value of this function is None
Iv.. Built-in functions
Python3 the third day