\u7b2c4.1\u8bb2-python\u51fd\u6570.pptx - Python \u8bed\u6cd5\u57fa\u7840 \u51fd\u6570 \u6559\u5e08\uff1a\u4e1b\u57f9\u76db [email protected] \u6253\u5370\u4e00\u9897\u6811 print print print print print print

第4.1讲-python函数.pptx - Python 语法基础 函数...

This preview shows page 1 - 10 out of 23 pages.

[email protected] Python 语法基础 函数 教师:丛培盛
Image of page 1
2 打印一颗树 print(' *') print(' ***') print(' *****') print('*******') print(' *') print(' ***') print(' *****') print('*******') print(' #') print(' #') print(' #') 代码重复 换个符号?不 灵活
Image of page 2
3 为何要用函数 from math import * ka=pow(10,-4.56) while True: a=float(input("a=")) b=float(input("b=")) fa = a*a*a+ka*a*a-(ka*0.01+1e-14)*a-ka*1e-14 fb = b*b*b + ka*b*b-(ka*0.01+1e-14)*b-ka*1e-14 if( fa* fb <0): break …. m = (a+b) /2.0 fm = m*m*m+ka*m*m-(ka*0.01+1e-14)*m-ka*1e-14 例子: 求 y=x 3 + 10 -4.56 * x 2 - (10 -4.56 * 0.01 + 10 -14 ) * x – 10 -4.56 * 10 -14 3 次使用, 极容易出错
Image of page 3
4 先看树:树的组成 print(' *') print(' ***') print(' *****') print('*******') print(' *') print(' ***') print(' *****') print('*******') print(' #') print(' #') print(' #') 树的头部 树干
Image of page 4
5 格式: def 函数名 ( 参数表 ) : 函数体 return 语句 函数的定义 函数由函数头 函数体组成 return 返回结果 return 可省略
Image of page 5
6 树的组成 函数定义 def treeTop(): print(' *') print(' ***') print(' *****') print('*******') def treeTrunk(): print(' #') print(' #') print(' #') 树的头部 树干 def tree: treeTop() treeTop() treeTrunk() tree() 主函数调用
Image of page 6
7 继续画树 画树使用的字符—参数 def treeTop(s): print(' ',s) print(' ',3*s) print(' ',5*s) print(' ',7*s) def treeTrunk(s): print(' ',s) print(' ',s) print(' ',s) 树的头部 树干 def tree(s): treeTop(s) treeTop(s) treeTrunk(s) Tree(‘@’)
Image of page 7
8 函数的定义 -- 斐波那契数列 斐波那契函数的定义 1 2 项为 0 1 后续的每项是前两项的和 求前 n 传递参数 n ,形式参数 返回 :一个 列表,后续会展开, 格式 [1,2,3 ] 格式: def 函数名 ( 参数表 ) : 函数体 return 语句
Image of page 8
9 x=fibs(10) print (x) k=5 y=fibs(k) print (y) 函数的定义 -- 斐波那契数列
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 23 pages?

  • Spring '09
  • FAREWELL

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes