day3 python 函数
常犯的错误:
IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行...
函数是指一组语句的集合通过一个名字(函数名)封装起来,执行这个函数,调用这个函数名即可。
特性:
减少代码重复
使程序变得可扩展性
使程序易维护
#定义函数
def sayhi(): #函数名
    print ('hello world')
sayhi()#调用函数
f=open('yesterdate.txt','a')
f.truncate(44) #截取长度
#文件的修改
f=open('../dang.txt','r') #源文件
p=open('yesterdat.txt','w') #没有文件创建文件,修改的内容写入新文件里
#replace修改文件内容
for line in f:
if "i like code but really you" in line:
line=line.replace("i like code but really you","i like code but fulimei you")
p.write(line)
f.close()
p.close()
#这个用sed就可以了:
#sed -i 's/version=.*/version=0/' config.ini
#如果有多个ini文件:
#sed -i 's/version=.*/version=0/' *.ini
#位置参数
def calc(x,y):
print(x)
print(y)
calc(1,2)
#关键字参数
def test(x, y):
print(x)
print(y)
test(y=3,x=4)
def ff(x,y):
print(x)
print(y)
a=9
b=8
ff(y=a, x=b)
位置参数与形参一一对应
关键字参数与形参顺序无关
关键字参数不能写在位置参数前面
*args 元组 接受位置参数 ,不能接收关键字参数
**kwargs 字典
def test1() :
print('in the test1')
def test2() :
print ('in the test2')
return 0
def test3() :
print ('in the test3')
return 0 ,'hello',['zhang','xin'],{'tt':'aaa'} x=test1()
y=test2()
z=test3()
print (x)
print (y)
print (z)
----------------------------
注释很多行#,CTRL+A 然后再ctrl +/
TRL+A 然后 ctrl+d 复制下所有内容
name=['zba','dex','ggg']
def calc(name):
name[0]="女王"
print(name)
calc(name)
print(name)
预期结果
['女王', 'dex', 'ggg']
['女王', 'dex', 'ggg']
#递归
def cu(n):
print(n)
if int(n/2) >0:
return cu(n/2)
print(n)
cu(10)
# python 或 批处理 替换文件中的内容
# 有一个配置文件 config.ini 其中有一段内容 "version=x" 此处x为0、1、2、3、4等数字,但不确定是什么数字
# 如何将这段内容替换为“version=0” 要是用批处理实现是最好的,应该会用到通配符,
# 用批处理实现起来有难度。
import re
f1 = r'd:\config.ini'
f2 = r'd:\config.ini.1'
with open(f2, 'w') as ff2:
with open(f1, 'r') as ff1:
for x in ff1:
if 'version=' in x:
x = re.sub(re.search('version=(\d+)', x).group(1), '0', x)
ff2.write(x)
day3 python 函数的更多相关文章
- Day3 - Python基础3 函数、递归、内置函数
		Python之路,Day3 - Python基础3 本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8. ... 
- python 函数之day3
		一 函数的语法及特性 什么是函数? 定义:函数是一个功能通过一组语句的集合,由名字(函数名)将其封装起来的代码块,要想执行这个函数,只要调用其函数名即可. 特性: 减少重复代码 使程序变的可扩展 使程 ... 
- day3之函数的初始及进阶
		函数初始 函数的定义与调用 ''' def 函数名 (参数): 函数体 函数名:设定与变量相同 执行函数: 函数名() ''' 函数的返回值 # 函数返回值 return ''' 1.遇到return ... 
- Python函数作用域的查找顺序
		函数作用域的LEGB顺序 1.什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 2.它们 ... 
- Python函数讲解
		Python函数 
- Python函数信息
		Python函数func的信息可以通过func.func_*和func.func_code来获取 一.先看看它们的应用吧: 1.获取原函数名称: 1 >>> def yes():pa ... 
- Python函数参数默认值的陷阱和原理深究"
		本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ... 
- Python开发【第四章】:Python函数剖析
		一.Python函数剖析 1.函数的调用顺序 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian #函数错误的调用方式 def fun ... 
- Python函数解析
		对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ... 
随机推荐
- duplicate命令创建physical standby数据库报RMAN-03015 ORA-17628
			The following error is reported trying to create a Physical Standby database using "duplicate f ... 
- GTA项目 一, 包装外部WebService
			外部WebService返回的是xml太重了. 而JSON是web的新标准.所以要包装一下. 使用NewtonSoft.JSON的dll里面的JsonConvert.SerializeXmlNode方 ... 
- PostgreSQL index types and index bloating
			warehouse_db=# create table item (item_id integer not null,item_name text,item_price numeric,item_da ... 
- 模拟退火法(吊打XXX)Bzoj3680
			3680: 吊打XXX Time Limit: 10 Sec Memory Limit: 128 MBSec Special Judge Submit: 308 Solved: 94 [Subm ... 
- Ruby与Python开发的环境IDE配置(附软件的百度云链接)
			Ruby开发环境配置 1.Aptana_RadRails(提示功能不好,开发Ruby不推荐) 链接:http://pan.baidu.com/s/1i5q96K1 密码:yt04 2.Aptana S ... 
- webDriver中的alert
			driver.switchTo().alert();这句可以得到alert\confirm\prompt对话框的对象,然后运用其方法对它进行操作.对话框操作的主要方法有: getText() ... 
- 从一个例子讲解拷贝构造函数与return
			#include "iostream" using namespace std; class Location { public: Location(, ) { X = xx; Y ... 
- JSP数据交互习题错误总结
			1:如果注册完页面有中文字符需要在提交后的页面显示注册信息,切记先把接受到的request的编码方式改为中文:request.setCharacterEncoding("utf-8" ... 
- windows系统调用  进程快照
			#include "windows.h" #include "tlhelp32.h" #include "iostream" using n ... 
- cmd进入某个目录
			love you my pig java枚举类 cmd进入某个目录 2011-04-06 15:49:38| 分类: 小知识 | 标签: |字号大中小 订阅 1.开始->运行->C ... 
