Py4Inf-04-Functions-PrintPy4Inf-04-Functions-Print
-
资源ID:23993783
资源大小:161.97KB
全文页数:6页
- 资源格式: PDF
下载积分:5金贝
快捷下载
账号登录下载
微信登录下载
微信扫一扫登录
1、金锄头文库是“C2C”交易模式,即卖家上传的文档直接由买家下载,本站只是中间服务平台,本站所有文档下载所得的收益全部归上传人(卖家)所有,作为网络服务商,若您的权利被侵害请及时联系右侧客服;
2、如你看到网页展示的文档有jinchutou.com水印,是因预览和防盗链等技术需要对部份页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有jinchutou.com水印标识,下载后原文更清晰;
3、所有的PPT和DOC文档都被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;下载前须认真查看,确认无误后再购买;
4、文档大部份都是可以预览的,金锄头文库作为内容存储提供商,无法对各卖家所售文档的真实性、完整性、准确性以及专业性等问题提供审核和保证,请慎重购买;
5、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据;
6、如果您还有什么不清楚的或需要我们协助,可以点击右侧栏的客服。
|
下载须知 | 常见问题汇总
|
Py4Inf-04-Functions-PrintPy4Inf-04-Functions-Print
FunctionsChapter 4Python for Informatics: Exploring Informationww.pythonlearn.comUnless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License.htp:/creativecommons.org/licenses/by/3.0/.Copyright 2010- Charles R. SeveranceStored (and reused) StepsOutput:HelloFunZipHelloFunProgram:def hello():print 'Hello'print 'Fun'hello()print 'Zip'hello()defprint 'Hello'print 'Fun'hello()print “Zip”We cal these reusable pieces of code “functions”.hello():hello()Python FunctionsThere are two kinds of functions in Python.Built-in functions that are provided as part of Python - raw_input(), type(), float(), int() .Functions that we define ourselves and then useWe treat the of the built-in function names as "new" reserved words (i.e. we avoid them as variable names)Function DefinitionIn Python a function is some reusable code that takes arguments(s) as input does some computation and then returns a result or resultsWe define a function using the def reserved wordWe cal/invoke the function by using the function name, parenthesis and arguments in an expression >>> big = max('Hello world')>>> print bigw>>> tiny = min('Hello world')>>> print tiny>>>big = max('Hello world')Argument'w'ResultAssignmentMax Function>>> big = max('Hello world')>>> print big'w'max()function“Hello world” (a string)w(a string)A function is some stored code that we use. A function takes some input and produces an output.Guido wrote this codeMax Function>>> big = max('Hello world')>>> print big'w'def max(inp):blahblahfor x in y:blahblah“Hello world” (a string)w(a string)A function is some stored code that we use. A function takes some input and produces an output.Guido wrote this codeType ConversionsWhen you put an integer and floating point in an expression the integer is implicitly converted to a floatYou can control this with the built in functions int() and float()>>> print float(99) / 1000.99>>> i = 42>>> type(i)>>> f = float(i)>>> print f42.0>>> type(f)>>> print 1 + 2 * float(3) / 4 - 5-2.5>>> String ConversionsYou can also use int() and float() to convert between strings and integersYou wil get an error if the string does not contain numeric characters>>> sval = '123'>>> type(sval)>>> print sval + 1Traceback (most recent cal last):File "", line 1, in TypeError: cannot concatenate 'str' and 'int'>>> ival = int(sval)>>> type(ival)>>> print ival + 1124>>> nsv = 'hello bob'>>> niv = int(nsv)Traceback (most recent cal last):File "", line 1, in ValueError: invalid literal for int() Building our Own FunctionsWe create a new function using the def keyword folowed by optional parameters in parenthesis.We indent the body of the functionThis defines the function but does not execute the body of the functiondef print_lyrics():print "I'm a lumberjack, and I'm okay."print 'I sleep al night and I work al day.'x = 5print 'Hello'def print_lyrics():print "I'm a lumberjack, and I'm okay."print 'I sleep al night and I work al day.'print 'Yo'x = x + 2print xHelloYo7print "I'm a lumberjack, and I'm okay."print 'I sleep al night and I work al day.'print_lyrics():Definitions and UsesOnce we have defined a function, we can cal (or invoke) it as many times as we likeThis is the store and reuse patternx = 5print 'Hello'def print_lyrics():print "I'm a lumberjack, and I'm okay."print 'I sleep al night and I work al day.'print 'Yo'print_lyrics()x = x + 2print xHelloYoI'm a lumberjack, and I'm okay.I sleep al night and I work al day.7ArgumentsAn argument is a value we pas into the function as its input when we cal the functionWe use arguments so we can direct the function to do different kinds of work when we cal it at different timesWe put the arguments in parenthesis after the name of the functionbig = max('Hello world')ArgumentParametersA parameter is a variable which we use in the function definition that is a “handle” that alows the code in the function to access the arguments for a particular function invocation.>>> def greet(lang):. if lang = 'es':. print 'Hola'. elif lang = 'fr':. print 'Bonjour'. else:. print 'Hello'. >>> greet('en')Hello>>> greet('es')Hola>>> greet('fr')Bonjour>>> Return ValuesOften a function wil take its arguments, do some computation and return a value to be used as the value of the function cal in the caling expression. The return keyword is used for this.def greet():return "Hello"print greet(), "Glenn"print greet(), "Sally"Hello GlennHello SallyReturn ValueA “fruitful” function is one that produces a result (or return value)The return statement ends the function execution and “sends back” the result of the function>>> def greet(lang):. if lang = 'es':. return 'Hola'. elif lang = 'fr':. return 'Bonjour'. else:. return 'Hello'. >>> print greet('en'),'Glenn'Hello Glenn>>> print greet('es'),'Saly'Hola Saly>>>