Python入门之走向编程第一步一个例子开始(python简单项目实例,关键词优化)

时间:2024-05-03 10:45:38 作者 : 石家庄SEO 分类 : 关键词优化
  • TAG :

    Python%E5%85%A5%E9%97%A8%E4%B9%8B%E8%B5%B0%E5%90%91%E7%BC%96%E7%A8%8B%E7%AC%AC%E4%B8%80%E6%AD%A5%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90%E5%BC%80%E5%A7%8B

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当然,我们可以使用Python来完成比把两个和两个加在一起更复杂的任务。例如,我们可以写出斐波那契级数的初始子序列如下

>>> # Fibonacci series:

... # the sum of two elements defines the next

... a, b = 0, 1

>>> while b < 10:

... print(b)

... a, b = b, a+b

...

1

1

2

3

5

8

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 righ

第一行包含多个赋值:变量A和B同时获得新值0和1。在最后一行中,再次使用,表明在任何赋值发生之前首先对右边的表达式进行求值。右边表达式由左到右进行计算。

The while loop executes as long as the condition (here: b < 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).只要条件(这里:b<10)成立,while循环就执行。在Python中,像在C中一样,任何非零整数值都是true;0是false。条件也可以是字符串或列表值,实际上是任何序列;具有非零长度的任何东西都是真的,空序列是错误的。在示例中使用的测试是一个简单的比较。标准比较运算符的编写与C:<(小于)、>(大于)、=(等于)、<=(小于或等于)、> =(大于或等于)和!=(不等于)。

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.循环体是缩进的:缩进是Python对语句进行分组的方式。在交互式提示符上,您必须为每个缩进线键入一个制表符或空格。在实践中,您将为Python编写一个更复杂的文本编辑器,所有的文本编辑器都有一个自动缩进工具。当一个复合语句交互输入时,必须跟随一条空白行来表示完成(因为解析器不能猜出您输入了最后一行)。请注意,基本块中的每一行必须以相同的数量缩进。

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:Print()函数写入给定的参数的值。它不同于只写表达式(正如我们前面在计算器示例中所写的),它处理多个参数、浮点量和字符串的方式不同。字符串不带引号打印,在项目之间插入一个空格,这样就可以很好地格式化,像这样:

>>> 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 b < 1000:

... print(b, end=',')

... a, b = b, a+b

...

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

Footnotes:注意:

Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2.

由于**比-A具有更高的优先级,-*** 2将被解释为-(3**2),从而导致-9。为了避免这个问题,得到9,你可以使用(-3)** 2。

Unlike other languages, special characters such as \n have the same meaning with both single ('...') and double ("...") quotes. The only difference between the two is that within single quotes you don’t need to escape " (but you have to escape \') and vice versa.不像其他语言,特殊字符如\与双单引号(‘...’)和双引号(“....”)有相同的作用,唯一的不同是在双单引号时您不需要避免双引号,但是要避免\',反之亦然。

本文:Python入门之走向编程第一步一个例子开始的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:深圳投资控股有限公司18亿收购B2B供应链巨头怡亚通下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18