python开发_python中的函数定义
下面是我做的几个用列:
#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中的函数定义的更多相关文章
- python开发_python中的range()函数
python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...
- python开发_python中str.format()
格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...
- python开发_python中的module
在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...
- python开发_python中的变量:全局变量和局部变量
如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...
- python开发_python中字符串string操作
在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...
- python开发_python中的Boolean运算和真假值
python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...
- python开发_python中for循环操作
如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...
- python开发_python中的list操作
对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...
- python开发_python关键字
python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...
随机推荐
- 博客迁移至“零一积流|it-refer.com”
虽然在博客园写了没几篇文章,考虑到个人发展,自己还是建立一个独立的站点:零一积流. 以后直接在自己网站上写东西了,此处只用做文章收藏.
- vim简单的文本编辑命令
<blockquote>:e! enter</blockquote>消除所有这次编辑的,回到原来文件的样子.不过此文件还是打开状态.<blockquote>:q! ...
- Servlet实现数字字母验证码图片(二)
Servlet实现数字字母验证码图片(二): 生成验证码图片主要用到了一个BufferedImage类,如下:
- 《selenium2 python 自动化测试实战》(15)——调用js控制滚动条等操作
看代码: # coding=utf-8 from time import sleepfrom selenium import webdriver driver = webdriver.Firefox( ...
- Robot Framework-断言函数
测试用例的目的是要验证一些操作否符合我们的预期结果,所以在测试用例中,断言函数是必不可少的一项.我们做的每一步操作都会有预期的结果,为了保证操作得到的结果符合预期,我们需要在测试用例中添加断言,来保证 ...
- 完美解决github访问速度慢[转]
1. 修改本地hosts文件 windows系统的hosts文件的位置如下:C:\Windows\System32\drivers\etc\hosts mac/linux系统的hosts文件的位置如下 ...
- PEP
用python , PEP难以绕过. PEP是什么? Python Enhancement Proposals , 它集合了python的改进提案. 它不是版本递进的, 有些PEP是应该去读一 ...
- lapis docker 运行说明
1. lapis docker 镜像制作 因为openresty 新版本一个json 库的问题,我们使用的是 openresty:1.11.2.1 基础镜像 FROM openresty/openre ...
- drill 数据源配置补充
1. mongodb { "type":"mongo", "connection":"mongodb://user:passwor ...
- 使用ILMerge将源DLL合并到目标EXE(.NET4.6.2)
本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 本文主要是使用微软的ILMerge工具将源DLL合并到目标EXE,因此,需要下载以下工具: https://www.micro ...