电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

Py4Inf-04-Functions-PrintPy4Inf-04-Functions-Print

6页
  • 卖家[上传人]:海天
  • 文档编号:23993783
  • 上传时间:2017-10-22
  • 文档格式:PDF
  • 文档大小:161.97KB
  • / 6 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、FunctionsChapter 4Python for Informatics: Exploring IUnless 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 Helloprint Funhello()print Ziphello()defprint Helloprint Funhello()print “Zip”We cal these reusable pieces of code “functions”.hello():hello()Python FunctionsThere are two

      2、 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 functi

      3、on by using the function name, parenthesis and arguments in an expression big = max(Hello world) print bigw tiny = min(Hello world) print tinybig = max(Hello world)ArgumentwResultAssignmentMax Function big = max(Hello world) print bigwmax()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 bigwdef max(inp):blahblahfor x in y:blahblah“Hello world” (

      4、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 be

      5、tween 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 pa

      6、renthesis.We indent the body of the functionThis defines the function but does not execute the body of the functiondef print_lyrics():print Im a lumberjack, and Im okay.print I sleep al night and I work al day.x = 5print Hellodef print_lyrics():print Im a lumberjack, and Im okay.print I sleep al night and I work al day.print Yox = x + 2print xHelloYo7print Im a lumberjack, and Im okay.print I sleep al night and I work al day.print_lyrics():Definitions and UsesOnce we have defined a function, we

      7、can cal (or invoke) it as many times as we likeThis is the store and reuse patternx = 5print Hellodef print_lyrics():print Im a lumberjack, and Im okay.print I sleep al night and I work al day.print Yoprint_lyrics()x = x + 2print xHelloYoIm a lumberjack, and Im 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 differen

      8、t 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

      9、 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 Helloprint greet(), Glennprint greet(), SallyHello 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),GlennHello Glenn print greet(es),SalyHola Saly

      《Py4Inf-04-Functions-PrintPy4Inf-04-Functions-Print》由会员海天分享,可在线阅读,更多相关《Py4Inf-04-Functions-PrintPy4Inf-04-Functions-Print》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2 2022年小学体育教师学期工作总结
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.