函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计。函数就是面向过程的程序设计的基本单元。传入函数函数的本身也可以作为参数。Python内建的mapreduce的函数。(来源于谷歌的,后来被道格这家伙开源了,成为当今处理大数据最火热的hadoop中的计算模型---MapReduce)我们先看map。map()函数接收两个参数,一个是函数...
定义函数 格式:def 函数名(参数): 函数体函数名其实就是指向一个函数对象的引用,完全可以把函数名赋值给一个变量,相当于给这个函数起了一个别名def Pname(): #当前函数不放参数print(‘大家好我是杰大哥!‘)
Pname() #调用函数 执行了函数里面的代码
pri = Pname #将函数名赋值给另一个变量,相当于给当前函数取一个别名
pri() #pri()的作用等于Pname()函数参数...
1、函数执行流程:def foo1(a, a1=1):print("foo1 called", a, a1)def foo2(b):foo3(b)print("foo2 called", b)def foo3(c):print("foo3 called", c)def main():print("main called")foo1(100, 101)foo2(200)print("main ending")main()# 函数执行流程:
1.全局帧中生成 foo1、foo2、foo3、main 函数对象;
2.main 函数调用;
3.main 中查找内建函数 print 压栈,将常量字符串压栈,调用函数,弹出栈顶;
4.main 中全局查找函数 fo...
1#“hello %s”与%name之间不能加逗号2def hi(name):
3print("hello %s" %name)
45#“hello”与name之间加逗号6def hi(name):
7print("hello " ,name) 原文:https://www.cnblogs.com/zmuyez/p/9500916.html
1def ssh_login(ip, user="root", passwd=None, prompt="]#", port="22", log_file=None, raise_exception=True):2""" 3 Login remote host with ssh4 return pexpect.spawn5""" 6 cmd = "ssh %s@%s -p %s" % (user, ip, port)7 p = pexpect.spawn(cmd)8 p.logfile = open_log_file(log_file)910 Flag = True
11 index = p.expect(["[Pp]assword:", "(yes/no)", prompt, pexpect.EOF])
12if ind...
def shifou_space(args):ret = Truefor a in args:if a.isspace():ret = Falsebreakreturn retresult = shifou_space("123 12312")
print("有空格",result) 原文:http://www.cnblogs.com/zgyc/p/6229703.html
enumerate 函数用于遍历序列中的元素以及它们的下标:>>> for i,j in enumerate((‘a‘,‘b‘,‘c‘)): print i,j 0 a1 b2 c>>> for i,j in enumerate([1,2,3]): print i,j 0 11 22 3>>> for i,j in enumerate({‘a‘:1,‘b‘:2}): #注意字典,只返回KEY值!! print i,j 0 a1 b>>> for i,j in enumerate(‘abc‘): print i,j 0 a1 b2 c 原文:http://www.cnblogs.com/itfat/p/7392371.html
#位置实参def func(a,b):print(‘a=‘+a)print(‘b=‘+b)
func(‘a‘,‘b‘)
func(‘b‘,‘a‘)#关键字实参def func1(a,b):print(‘a=‘ + a)print(‘b=‘ + b)
func1(a=‘a‘,b=‘b‘)#参数具有默认值def func1(a,b=‘fei‘):print(‘a=‘ + a)print(‘b=‘ + b)
func1(a=‘a‘)#参数可选
#可选参数一定要在末尾,否则errordef printFullName(first,last,middle=‘‘):#if middle:Error不对if middle==‘‘:print(first+last)else...
def intadd(a,b):return a+b注意:不要忘记冒号注意格式要对齐;>>> def intadd(a,b):return a+b>>> intadd(3,2)
5 原文:http://www.cnblogs.com/quant-lee/p/5343075.html
英文文档:chr(i)Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string ‘a‘, while chr(8364) returns the string ‘€‘. This is the inverse of ord(). The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range说明: 1. 函数返回整形参数值所对应的...
Python常用功能函数汇总1.按行写字符串到文件中import sys, os, time, json
def saveContext(filename,*name):format = ‘^‘context = name[0]for i in name[1:]:context = context + format + str(i)context = str(context).replace(‘(‘,‘(‘).replace(‘)‘,‘)‘).replace(‘,‘,‘,‘).replace(‘:‘,‘:‘)#去除首位空格filename = filename.strip()#读取目录名称path = os.path.dirname(filename)#如果目录不存在则创...
函数就是一个通用的程序结构部件:在程序中主要扮演两个角色一、最大化的代码重用和最小化代码冗余。函数允许整合以及通用化代码,以便能够以后多次使用代码。二、流程的分解。函数也提供了一种将一个系统分割为定义完好的不同部分的工具。一般来说,函数讲的是流程:告诉你怎样去做某事,而不是让你使用它去做的事。 编写函数def是可执行的代码--函数并不存在,直到Python运行了def后才存在。事实上,在if语句、while循环甚至是其...
将68个内置函数按照其功能分为了10类,分别是: 数学运算(7个)
abs() divmod() max() min() pow() round() sum() 类型转换(24个)
bool() int() float() complex() str() bytearray() bytes() memoryview() ord() chr() bin() oct() hex() tuple() list() dict() set() frozenset( ) enumerate() range() iter(...
目录文件修改的两种方式两种文件的修改方式方式一方式二:函数的基本使用什么是函数为何要用函数如何用函数定义函数发生的事情调用函数发生的事情定义的语法形式一:无参函数形式二:有参函数形式三:空函数,函数体代码为pass三种定义方式的应用无参函数的应用场景有参函数的应用场景空函数的应用场景调用函数语句的形式表达式形式:函数调用可以当做参数函数返回值返回None返回一个值返回多个值文件修改的两种方式a.txt的样例with...
函数: 函数的概念:就是一段代码;一段操作流程。 优点:代码量少、简洁。 维护起来方便 -- 在函数的定义进行修改 函数的定义:1 - def 函数名(): 函数内容 2 - 函数的定义是不会执行函数内容的代码的 3 - 案例:def func(): print(‘step1...