下面是我做的几个用列:

 #python中的函数定义,使用和传参
def_str = '''\
python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]):
执行语句 如: def helloWorld():
print('hello') if __name__ == '_main__':
helloWorld() 输出:hello
'''
print(def_str) #下面进行举例说明 def helloWorld():
print('输出:hello') if __name__ == '__main__':
helloWorld() print('''\
################################################ 函数可以带参数和返回值,参数将按从左到右的匹配,
参数可设置默认值,当使用函数时没给相应的参数时,
会按照默认值进行赋值 ################################################
''') #定义一个方法:x的y次方
def myMethod(x,y):
return x**y def fib(n):
a , b = 0 , 1
while a < n:
print(a, end=' ')
a , b = b , a + b
print() #获取一个新的数组
#@param oldList 原数组
#@param length 要添加的长度
def getList(oldList,length):
if length > 0:
for i in range(0,length):
oldList.append(i)
return oldList
else:
return '你输入的长度小于0' def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise IOError('refusenik user')
print(complaint) if __name__ == '__main__':
x = 3
y = 4
n = 2000
print(x , '的' , y , '次方(' ,x ,'**' , y ,') = ' , myMethod(x,y))
print('函数fib(n),当n =' ,n)
fib(n)
print(getList(['begin'],-10))
ask_ok('y')

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]):
执行语句 如: def helloWorld():
print('hello') if __name__ == '_main__':
helloWorld() 输出:hello 输出:hello
################################################ 函数可以带参数和返回值,参数将按从左到右的匹配,
参数可设置默认值,当使用函数时没给相应的参数时,
会按照默认值进行赋值 ################################################ 3 的 4 次方( 3 ** 4 ) = 81
函数fib(n),当n = 2000
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
你输入的长度小于0
y输出:hello
Yes or no, please!
y
Yes or no, please!
y
Yes or no, please!
y
Yes or no, please!
y
Traceback (most recent call last):
File "E:/Python33/python_workspace/test_function.py", line 80, in <module>
ask_ok('y')
File "E:/Python33/python_workspace/test_function.py", line 69, in ask_ok
raise IOError('refusenik user')
OSError: refusenik user
>>>

python开发_python中的函数定义的更多相关文章

  1. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  2. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  3. python开发_python中的module

    在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...

  4. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  5. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  6. python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...

  7. python开发_python中for循环操作

    如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...

  8. python开发_python中的list操作

    对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...

  9. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

随机推荐

  1. 使用json path设置关联

    与正则表达式相比,这种方法会更简单一些: json path是在返回的是K-V的格式中根据key进行的关联,如果压的接口返回的是json的话,使用json path比较方便,如果不是json的话,就使 ...

  2. socket创建TCP服务端和客户端

    看情况选择相对应的套接字*面向连接的传输--tcp协议--可靠的--流式套接字(SOCK_STREAM)*面向无连接的传输--udp协议--不可靠的--数据报套接字(SOCK_DGRAM) 在liun ...

  3. 使用PE工具箱 ughost 恢复系统导致被捆绑一堆软件的问题

    很多的pe系统,都有这么一个东西, 没啥技术含量,就是给ghost 加一个UI 而已 , 但是使用这玩意恢复系统,会被感染一些捆绑软件 ,开机之后就会卡顿, 并下载一堆软件 包括  爱奇艺 , 百度搜 ...

  4. 菜鸟夜谈android反编译

    工具: apktool:https://code.google.com/p/android-apktool/ dex2jar: https://code.google.com/p/dex2jar/ j ...

  5. flask第十九篇——模板【3】

    请关注微信公众号:自动化测试实战 今天我们继续模板的知识,现在我们增加字典的复杂度,这个时候在render_template第二个参数可以传**title,以后我们会用**context代替原来的ti ...

  6. SpringMVC传递数据的流线图

    流程含义解释:(1)HTTP请求到达web服务器,web服务器将其封装成一个httpServletRequest对象(2)springMVC框架截获这个httpServletRequest对象(3)s ...

  7. 解决Vsphere Client 60天过期问题

  8. MySQL 8 连接时出现 1251 和 2059 错误

    MySQL 8 连接时出现 1251 和 2059 错误 原因是MySQL 8 改了密码加密算法.1 原来是:mysql_native_password MySQL8 改成了 caching_sha2 ...

  9. CSS基本样式简单介绍

    具体详情内容请查阅<css参考手册> 一.基本结构样式 width 宽度 height 高度 background 背景 border 边框 padding 内边距 margin 外边距 ...

  10. ngnix+uwsgi+django 部署mezzanine

    以下是我用ngnix+uwsgi+django 部署mezzanine全过程,其中ngnix+uwsgi这块是看了虫师大神的博客(http://www.cnblogs.com/fnng/p/52686 ...