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 ...
随机推荐
- pkcs#5和pkcs#7填充的区别
最近做到了关于加密和解密的部分. 使用算法AES的时候,涉及到数据填充的部分,数据的填充有很多种方案,用的比较多的有pkcs#5,pkcs#7, 下面的都是从网上转来的.结论就是在AES 的使用中,p ...
- 用sublime server 启动本地服务器(手机访问电脑页面)
安装sublime server 插件包 1.Ctrl + shift + p install package ...
- debian 8.1 安装idempiere 2.1 X64 笔记
接上文.当虚拟服务器和虚拟机搭建完成后.登陆debian 8.1 X64. 进入虚拟服务器控制台.打开虚拟机.root登陆.(留好初始状态系统快照.以便系统恢复.) 由于之前debian8.1X64默 ...
- HDU - 6214:Smallest Minimum Cut(最小割边最小割)
Consider a network G=(V,E) G=(V,E) with source s s and sink t t . An s-t cut is a partition of nodes ...
- CentOS 7.4中firewall防火墙详解和配置以及切换为iptables防火墙
转载:https://blog.csdn.net/xlgen157387/article/details/52672988 一.firewall介绍 CentOS 7中防火墙是一个非常的强大的功能,在 ...
- BZOJ1113 Poi2008 海报PLA【单调栈】【水】
BZOJ1113 Poi2008 海报PLA Description N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. Input 第一行给出数字N,代表有N个矩形.N在[1,250 ...
- hadoop下远程调试方法
JPDA 简介Sun Microsystem 的 Java Platform Debugger Architecture (JPDA) 技术是一个多层架构,使您能够在各种环境中轻松调试 Java 应用 ...
- Python学习-购物车程序
程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ...
- 修改selinux出现setsebool: SELinux is disabled.的解决方法
1.vi /etc/vsftpd/vsftpd.conf # You may specify an explicit list of local users to chroot() to their ...
- Centos7.x破解密码
Centos7.x破解密码 centos7 破解密码 重置Centos 7 Root密码的方式和Centos 6完全不同.让我来展示一下到底如何操作. 1 .在启动grub菜单,选择编辑选项启动 14 ...