下面是我做的几个用列:

 #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. 移动端 css 禁止长按屏幕选中

    *{ -webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select:none; -moz-user-select:n ...

  2. No form of payment has been added yet.

    You may select a form of payment after your account balance reaches $10.00. Learn more 显然是说达到10美元以后才 ...

  3. 关于鼠标不敏感导致自以为ubuntu很怪的问题

    你要相信自己拥有的确实是一个垃圾鼠标,而不要以为复制和粘贴有感觉控制不住.

  4. >=< 在set和dict中的不同

    两个dict比较的算法是,长度>键>值,由于dict无序,所以比较的时候会自动将键对齐比较,我们不用担心这个. >>> d1 = dict(x=1, y=2) >& ...

  5. BZOJ - 3295 动态逆序对 (树状数组套treap)

    题目链接 思路和bzoj2141差不多,不过这道题的数据更强一些,线段树套treapT了,树状数组套treap卡过~~ #include<bits/stdc++.h> using name ...

  6. Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn【报错】

    自己的项目没有测通  可能是自己项目原因——因为自己项目中级联关系的类涉及太多 自己的项目[这样的配置报错] @OneToMany(fetch=FetchType.LAZY,cascade = { C ...

  7. BZOJ1510 POI2006 Kra-The Disks 【模拟】

    BZOJ1510 POI2006 Kra-The Disks LINK 还是粘题面吧,但是图就算了 Description Johnny 在生日时收到了一件特殊的礼物,这件礼物由一个奇形怪状的管子和一 ...

  8. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  9. CentOS 6.6下安装OpenOffice4.0

    最近由于项目需要,要在公司服务器上安装Openoffice,网上搜了一些资料后成功安装,现分享给大家. 1.首先先下载好需要的rpm包:Apache_OpenOffice_4.0.0_Linux_x8 ...

  10. linux的mysql

    mysql https://www.cnblogs.com/cnblogsfans/archive/2009/09/21/1570942.html https://blog.csdn.net/Smal ...