函数
def func1():定义函数
'''testing1'''#函数的说明
print("in the func1")#定义过程
return 0 #得到函数的执行结果。还是程序的结束
过程就是没有返回值的'函数'

def func2():
'''testing2'''
print('in the func2')
x=func1()
y=func2()

print('from func1 return is %s'%x)
print('from func2 return is %s'%y)

import time
def logger():
time_format='%Y-%m-%d %X'# 定义时间格式年 月 日 时秒
time_current=time.strftime(time_format)#引用上面时间格式
with open('a.txt','a+') as f:#打开一个文件,以二进制追加的方式获取文件描述符f
f.write('%s end action\n' %time_current) #给文件里写一段字符串

def test1():
print('in the test1')

logger()

def test2():
print('in the test2')

logger()

def test3():
print('in the test3')

logger()
test1()
test2()
test3()

'''函数的好处1、重复利用,减少重复代码
2、保持一致性
3、可扩展性'''

# def test1():
print('in the test1')

def test2():
print('in the test2')
return 0 #return 0返回值得意义要根据返回值的结果做出不同的操作
def test4():
print('in the test4')
return 1
def test3():
print('in the test3')
return 1,'hello',['电脑','打印机'],{'天气':'晴天'}#可以返回任何东西
def test5():
print('in the test5')
return test2

x=test1()
y=test2()
z=test3()
o=test4()
p=test5()
print(x)#返回的是none
print(y)#返回的是0
print(z)#返回的是元祖
print(o)#返回的是1
print(p)#返回的是test2函数的内存地址

def test(x,y):#x,y是实参
print(x)
print(y)
test(1,7)# 位置调用
# =test(x=1,y=7)#与实参对应1、2是形参 定义两个值1传给x,2传给y

#默认参数
def test(x,y=1)
print(x)
print(y)
test(1,3)#默认参数特点:调用函数时,默认参数非必须传递
#用途1 默认安装至
# 2、连接数据库默认端口号

#参数组
def test(*args):#*args接受N个位置参数,把他转换成元祖的形式
print(args)
test(1,2,3,4,5,6)
test(*[1,2,4,6,8,])#args=tuple([1,2,4,6,8])

def test1(x,*args):
print(x)
print(args)

test1(1,2,3,4,5,6,7)

#**kwargs:接收关键字参数,把N个关键字参数,转换成字典的方式
def test2(**kwargs):
print(kwargs)
test2(name='dream',age=18,)
test2(**{'name':'dream','age':18})

def test2(name,age,**kwargs):#参数组一定要放在**kwargs之前
print(name)
print(age)
print(kwargs)
test2(name='dream',age=18,)
test2(**{'name':'dream','age':18})

def test4(name,age=18,**kwargs):#参数组一定要放在**kwargs之前
print(name)
print(age)
print(kwargs)
test4('dream',33,hobby='tesla',gender='m')

#作用域,字符串,数字不能局部该全局

school='oldboy edu.'#变量用于全局
def change_name(name):
#global school #复杂程序不能使用
school='Mage.linux'#
print('before change',name,school)
name='Dream zhao'#变量仅用于这个函数
age=28
print("after change",name,school)
print('school',school)
name="alex"
change_name(name)
print(name,school)

name=['Tom','Jack','Rain']#列表、字典、集合变量用于全局
def change_name():
name[0]='Jim'#列表、字典、集合可以在局部改全局
name.append("dream")#列表、字典、集合可以追加
print('inside func',name)

change_name()
print(name)

最新文章

  1. 在TableView上添加悬浮按钮
  2. The constructor BASE64Encoder() is not accessible due to restriction on required library
  3. a new blog from MarsEdit
  4. WCF服务配置问题
  5. Oracle 客户端免安装数据库连接
  6. TestLink学习八:TestLink1.9.13与Mantis1.2.19集成
  7. nginx地址重写
  8. Google Map API 代码示例
  9. 舍伍德(Sherwood)算法学习笔记
  10. 安装mono和jexus,运行asp.net程序
  11. angular2 组件交互
  12. Tornado 协程
  13. Spring Cloud Alibaba基础教程:Sentinel使用Nacos存储规则
  14. Oracle 查询重复索引列
  15. provisional headers are shown 知多少
  16. P1122 最大子树和
  17. 从零开始学 Web 之 CSS3(四)边框图片,过渡
  18. OptionParser命令参数介绍及使用
  19. ifconfig 命令,改变主机名,改DNS hosts、关闭selinux firewalld netfilter 、防火墙iptables规则
  20. 签名Cookie

热门文章

  1. 关于web.xml的welcome-file-list 配置与tomcat的关系:
  2. maven课程 项目管理利器-maven 1-1课程概述
  3. JS的函数参数传递为值传递
  4. 电路中GND和GROUND、VCC,VDD,VEE,VSS
  5. 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume
  6. gradle方式集成融云sdk dlopen failed: library "libsqlite.so" not found
  7. check_mk检测插件编写
  8. ASP.NET与json对象互转
  9. April 24 2017 Week 17 Monday
  10. Arduino-舵机控制Servo