Python 3 Programming First Steps

First Steps in Python 3 Programming
In the previous tutorial, we’ve learned some basic Python 3 syntax. Now, let’s try writing a Fibonacci sequence.
Example (Python 3.0+)

#!/usr/bin/python3

# Fibonacci series: Fibonacci numbers
# The sum of two elements determines the next number
a, b = 0, 1
while b < 10:
print(b)
a, b = b, a+b

The code a, b = b, a+b evaluates the expression on the right first and then assigns it to the expression on the left, which is equivalent to:

n=b
m=a+b
a=n
b=m

Executing the above program produces the following output:

1
1
2
3
5
8

This example introduces several new features.

The first line contains a compound assignment: the variables a and b are given the new values 0 and 1 simultaneously. The last line uses the same technique again. You can see that the expression on the right is executed before the assignment changes the value. The order of execution for expressions on the right is from left to right.

Output variable value:

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

end keyword

The end keyword can be used to output results on the same line or to add different characters to the end of the output. Examples are as follows:
Example (Python 3.0+)

</pre>
<p>Executing the above program, the output is:</p>
<pre><code class="language-python line-numbers">1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

Leave a Reply

Your email address will not be published. Required fields are marked *