3.2.迈向编程的第一步

3.2. First Steps Towards Programming

【原文】

Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write an initial sub-sequence of the Fibonacci series as follows:

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while a < 10:
...     print(a)
...     a, b = b, a+b
...
0
1
1
2
3
5
8

This example introduces several new features.

  • The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

  • The while loop executes as long as the condition (here: a < 10) remains true. In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are written the same as in C: < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).

  • The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

  • The print() function writes the value of the argument(s) it is given. It differs from just writing the expression you want to write (as we did earlier in the calculator examples) in the way it handles multiple arguments, floating point quantities, and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely, like this:

    >>> i = 256*256
    >>> print('The value of i is', i)
    The value of i is 65536
    

    The keyword argument end can be used to avoid the newline after the output, or end the output with a different string:

>>> a, b = 0, 1
>>> while a < 1000:
...     print(a, end=',')
...     a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

【译】

当然,我们可以用Python做比2+2更复杂的任务。比如,我们能写出波斐那切数初始的子序列。

这个例子有几个新特征。

  • 第一行包含多个赋值:变量 a和变量b同时赋了新值01。在最后一行它们又被使用。在赋值时,首先计算赋值右侧的所有表达式,右侧表达式从左到右进行计算。
  • 只要条件为真(这里的条件是a<10),while循环就会执行。在Python里,像C里一样,任何非0整数都是true0false。条件也可以是字符串或列表值,实际上任何序列都可以。序列长度非0都是true,空序列是false。在这个实例中使用的是一个简单比较运算。标准比较运算符的书写和C语言一样:<(小于)>(大于)==(等于)<=(小于等于)>=(大于等于)!= (不等于)
  • 循环体缩进:缩进是Python语句进行分组的方式。在Python解释器交互提示符下,用Tab或空格产生缩进。实践中,你会用文本编辑器完成Python程序输入。所有的像样的文本编辑器都有自动缩进的功能。当以交互方式输入一个复合结构时,复合结构后面必须有一个空行(否则语法分析程序不能猜出你何时完成键入)。注意,在同一基本块里的每一行用同样数量的缩进。

  • print()函数输出参数传递给它的值。它不同与你写出想写的(像我们在前面写的计算器的例子)表达式,它还处理复杂的参数,浮点数和字符串。print()函数字符串输出后不带引号,输出项间用空格分割,你要能很好的格式化,像这样(简单的小例子):

关键字参数end可用于避免输出后出现换行符,或者用不同的字符结束输出。

【附】

斐波那契数列

指的是这样一个数列:1123581321345589...

这个数列从第3项开始,每一项都等于前两项之和。

盆子里的菲波那切数列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值