内容简介:python基础5-函数、类和文件操作
1 def name(para)
def myabs(x):
if x>0:
return x
else:
return -x
2 lambda表达式
用于声明匿名函数,既没有名字的小函数
f = lambda x,y,z:x+y+z print(f(1,2,3)) #6 L = [(lambda x:x**2),(lambda x:x**3)] print(L[0](3),L[1](3)) #(9, 27)
3 类
class Car:
def infor(self):
print("This is a car")
car = Car()
car.infor() #This is a car
4 私有成员与公有成员
两个下划线“__”开头为私有属性,其他为public
5 向文本文件中写入内容
s = "hello world"
with open('sample.txt','a+') as f:
f.write(s)
6 读取文本文件内容
f = open('sample.txt','r')
print(fp.read(5)) #读取前5字节
f = open('sample.txt','r')
while True:
line = f.readline()
if line=='':
break
print line,
f.close()
read()一次性读取全部,适用于小文件。
read(size) 每次读取size 个大小,适用于文件大小未知。
readlines() 每次读取一行,可以来读配置文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Spring in Action
Craig Walls / Manning Publications / 2011-6-29 / USD 49.99
Spring in Action, Third Edition has been completely revised to reflect the latest features, tools, practices Spring offers to java developers. It begins by introducing the core concepts of Spring and......一起来看看 《Spring in Action》 这本书的介绍吧!
HTML 压缩/解压工具
在线压缩/解压 HTML 代码
CSS 压缩/解压工具
在线压缩/解压 CSS 代码