python中的函数与文件操作
Posted 郭大侠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中的函数与文件操作相关的知识,希望对你有一定的参考价值。
【1】系统函数、自定义函数
def test_converter(C):
expression_1=C*9/5 + 32
return str(expression_1)+‘F‘
print(test_converter(30))
86.0F
def test():
num=1+1
print(num)
print(test())
2
None
练习:重量转换器k换成kg
def weight_switch(k):
return k/1000
print(weight_switch(1500))
1.5
练习:求直角三角形的斜边长
from math import *;
print(pow(10,2),end=‘ ‘);
def lenth_1(long,high):
return (pow(long,2)+pow(high,2))**0.5
print(lenth_1(3,4))
100.0 5.0
【1.1】关键字参数
在使用函数的时候,我们一般情况是位置参数,入参与定义参数的位置一一对应。
但关键字参数可以切换位置,用参数名来赋值的方式,这种方式就叫关键字参数
from math import *;
print(pow(10,2))
def lenth_1(long,high):
return (pow(long,2)+pow(high,2))**0.5
print(lenth_1(long=3,high=4))
100.0
5.0
def area(base_up,base_down,height=2):
return (base_up+base_down)*height/2
print(area(4,4,5),area(4,4))
20.0 8.0
练习:构造函数获取value对应的key
def getkey_byvalue(dict_var,value_var):
return ([key for key,value in dict_var.items() if value==value_var])
dict_var1={
‘a‘:100,
‘b‘:200,
‘c‘:300
}
print(getkey_byvalue(dict_var1,200))
[‘b‘]
【1.2】参数的收集
*args ,收集所有未知参数,这里的单词并非固定 主要是 一个 * 和 ** 的区别
**kwargs 收集我们的关键字参数,什么是关键字参数?比如key value的字典类型参数
def test(name,age,*args,**kargs):
print(*args)
#print(‘aaaa~~~{}‘.format(*args))
#print(**kargs)
name1=‘abc‘
name2=‘李四‘
age1=123
age2=18
desc_var1=‘abc是个百岁老人‘
desc_var2=‘李四是一个年轻小伙‘
dict_var1={
‘name‘:‘abc‘,
‘age‘:123,
‘desc‘:‘abc是一个百岁老人‘
}
test(name1,age1,name2,age2,desc_var1,desc_var2,dict_var1)
李四 18 abc是个百岁老人 李四是一个年轻小伙 {‘name‘: ‘abc‘, ‘age‘: 123, ‘desc‘: ‘abc是一个百岁老人‘}
【2】文件操作
file=open(‘d://test.txt‘,‘w‘)
file.write(‘hello,world‘)
file.close()
高级文件操作,新建文件
def text_create(name,msg):
file_dir=‘d://‘
full_path=file_dir+name+‘.txt‘
file=open(full_path,‘w‘)
file.write(msg)
file.close()
print(‘Done‘)
text_create(‘test1‘,‘create a test1 file‘);
Done
print(‘hello
runoob‘)
print(r‘hello
runoob‘)
print(‘hello \n runoob‘)
hello
runoob
hello
runoob
hello
runoob
input(‘hello,your city:‘)
hello,your city:hanzhou
‘hanzhou‘
import sys
print(‘================Python import mode==========================‘)
print (‘命令行参数为:‘)
for i in sys.argv:
print (i)
print (‘
python 路径为‘,sys.path)
================Python import mode==========================
命令行参数为:
E:My_ProgramPythonAnaconda_Pythonlibsite-packagesipykernel_launcher.py
-f
C:UsersguochaoqunAppDataRoamingjupyter
untimekernel-5784a8b5-e752-4514-bef8-e510a4e706cb.json
python 路径为 [‘C:\Users\guochaoqun\1_Python_learn‘, ‘E:\My_Program\Python\Anaconda_Python\python37.zip‘, ‘E:\My_Program\Python\Anaconda_Python\DLLs‘, ‘E:\My_Program\Python\Anaconda_Python\lib‘, ‘E:\My_Program\Python\Anaconda_Python‘, ‘‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\win32‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\win32\lib‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\Pythonwin‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\IPython\extensions‘, ‘C:\Users\guochaoqun\.ipython‘]
from sys import argv,path # 导入特定的成员
print(‘================python from import===================================‘)
print(‘path:‘,path) # 因为已经导入path成员,所以此处引用时不需要加sys.path
================python from import===================================
path: [‘C:\Users\guochaoqun\1_Python_learn‘, ‘E:\My_Program\Python\Anaconda_Python\python37.zip‘, ‘E:\My_Program\Python\Anaconda_Python\DLLs‘, ‘E:\My_Program\Python\Anaconda_Python\lib‘, ‘E:\My_Program\Python\Anaconda_Python‘, ‘‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\win32‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\win32\lib‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\Pythonwin‘, ‘E:\My_Program\Python\Anaconda_Python\lib\site-packages\IPython\extensions‘, ‘C:\Users\guochaoqun\.ipython‘]
help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=‘ ‘, end=‘
‘, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
以上是关于python中的函数与文件操作的主要内容,如果未能解决你的问题,请参考以下文章