Lecture 3

A. while loop

1. • Need a loop variable

    – Initialized outside loop

    – Changes within loop

    – Test for termination depends on variable

   • Useful to think about a decrementing function

   – Maps set of program variables into an integer

   – When loop is entered, value is non-negative

   – When value is <= 0, loop terminates, and

   – Value is decreased every time through loop

   eg: abs(x) – ans**3 (smaller the solution, nearer we will done.)

2. different between branching program and looping program:

  • Branching structures (conditionals) let us jump to different pieces of code based on a test

  – Programs are constant time

  • Looping structures (e.g., while) let us repeat pieces of code until a condition is satisfied

  – Programs now take time that depends on values of variables, as well as length of program

3. some command

a.

what does these stand for

b. If the command break is executed within a loop, it halts evaluation of the loop at that point and passes control to the next expression.

c. stop infinite loop : control C

B. for loop:    for <identifier> in <sequence>:

                           <code block>

1. some logics:

• Identifier bound to first value in sequence

• Code block executed

• Identifier bound to next value

• Code block executed

• Continues until sequence exhausted or a break statement is executed

• To generate a sequence of integers, use

– range(n) = [0, 1, 2, 3, …, n-1]

– range(m,n) = [m, m+1, …, n-1]

PS: spaces in strings as characters too!

2. range function:


some examples

more info: https://docs.python.org/3/library/functions.html#range

3. in range, 'num' variable will assign new value. (In python you can assign new values to variables. In this case the for loop will iterate in the list [0, 1, 2, 3, 4], each time the for loop iterates from the list it will assign num a new value.)

4. 5%2=1 5/2=2 5.0/2=2.5

5. if no indent, continue execute following if 


6. Approximation Methods

• Suppose we now want to find the square root of any non-negative number?

• Can’t guarantee exact answer, but just look for something close enough

• Start with exhaustive enumeration

– Take small steps to generate guesses in order

– Check to see if close enough

•general method:

-Exhaustive enumeration

– Bisection search

– Newton-Raphson (for root finding)

7. Print on the same line: use comma

# 习题中出现的问题:

a. 多次重复的部分可以命名成变量

b. 尽量避免在循环判断当中有重复。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,741评论 5 6
  • 人生的旅途是一次又一次选择的叠加,就像十字路口一样,有多个抉择,任一选择都没有客观上的好与坏,只是适合不适合自己而...
    樱花梦落阅读 283评论 0 0
  • 我终于到了成都。 不知道什么时候开始喜欢瑜伽的,一开始只是单纯的练,后来喜欢上了练瑜伽的人,再后来就是自己喜欢上了...
    二月周阅读 230评论 0 0
  • 从事人力资源接近18年,喜欢学习,喜欢整理 ,喜欢分享,一直致力于个人成长突破方向!很开心通过两年的专业教练学习,...
    夏翚Shari阅读 407评论 0 4
  • 今天来谈谈我的一个同事。她今年二十八九岁,两个孩子的妈妈,在我们公司做总务的工作。 工作上是个比较粗心且随便的人。...
    玲夏ling阅读 185评论 0 0

友情链接更多精彩内容