The first step in Python3 programming
In the previous tutorial, we have learned some basic syntax knowledge of Python3. Now we try to write a Fibonacci sequence.
The example is as follows:
#!/usr/bin/python3 # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 while b < 10: print(b) a, b = b, a+b
Execute the above program, the output result is:
1 1 2 3 5 8
This example introduces several new features.
The first line contains a compound assignment: variables a and b get new values 0 and 1 at the same time. The last line uses the same method again, and you can see that the expression on the right is executed before the assignment changes. The expressions on the right are executed from left to right.
>>> i = 256*256 >>> print('i 的值为:', i) i 的值为: 65536
end keyword
The keyword end can be used to output the results to the same line, or add different characters at the end of the output. Examples are as follows:
#!/usr/bin/python3 # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 while b < 1000: print(b, end=',') a, b = b, a+b
Execute the above program, the output result is:
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,