Skip to main content

Python Programming Language | Part 2

Welcome to Python Programming Language Part 2. In this article we are briefing you all about the python programming language basics . The output from the chaos program may not look very exciting, but it illustrates a very interesting phenomenon known to physicists and mathematicians. Let’s take a look at this program line by line and see what it does. Don’t worry about understanding every detail right away; we will be returning to all of these ideas in the next chapter. The first two lines of the program start with the # character: # File: chaos.py   # A simple program illustrating chaotic behavior. These lines are called comments. They are intended for human readers of the program and are ignored by Python. The Python interpreter always skips any text from the pound sign (#) through the end of a line. The next line of the program begins the definition of a function called main. def main(): Strictly speaking, it would not be necessary to create a main funct...

Python Programming Language | Part 2


Welcome to Python Programming Language Part 2. In this article we are briefing you all about the python programming language basics.

The output from the chaos program may not look very exciting, but it illustrates a very interesting phenomenon known to physicists and mathematicians. Let’s take a look at this program line by line and see what it does. Don’t worry about understanding every detail right away; we will be returning to all of these ideas in the next chapter. The first two lines of the program start with the # character:

# File: chaos.py
 # A simple program illustrating chaotic behavior.

These lines are called comments. They are intended for human readers of the program and are ignored by Python. The Python interpreter always skips any text from the pound sign (#) through the end of a line. The next line of the program begins the definition of a function called main.

def main():

Strictly speaking, it would not be necessary to create a main function. Since the lines of a module are executed as they are loaded, we could have written our program without this definition. That is, the module could have looked like this:

# File: chaos.py
 # A simple program illustrating chaotic behavior.

print "This program illustrates a chaotic function"
x = input("Enter a number between 0 and 1: ")
 for i in range(10):
 x = 3.9 * x * (1 - x)
 print x

This version is a bit shorter, but it is customary to place the instructions that comprise a program inside of a function called main. One immediate benefit of this approach was illustrated above; it allows us to (re)run the program by simply invoking chaos.main(). We don’t have to reload the module from the file in order to run it again, which would be necessary in the main-less case. The first line inside of main is really the beginning of our program.

print "This program illustrates a chaotic function"

This line causes Python to print a message introducing the program when it runs. Take a look at the next line of the program.

x = input("Enter a number between 0 and 1: ")

Here x is an example of a variable. A variable is used to give a name to a value so that we can refer to it at other points in the program. The entire line is an input statement. When Python gets to this statement, it displays the quoted message Enter a number between 0 and 1: and then pauses, waiting for the user to type something on the keyboard and press the <Enter> key. The value that the user types is then stored as the variable x. In the first example shown above, the user entered .25, which becomes the value of x. The next statement is an example of a loop.

for i in range(10):

A loop is a device that tells Python to do the same thing over and over again. This particular loop says to do something 10 times. The lines indented underneath the loop heading are the statements that are done 10 times. These form the body of the loop.

x = 3.9 * x * (1 - x)
print x
The effect of the loop is exactly the same as if we had written the body of the loop 10 times:
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x
x = 3.9 * x * (1 - x)
print x

Obviously using the loop instead saves the programmer a lot of trouble. But what exactly do these statements do? The first one performs a calculation.

x = 3.9 * x * (1 - x)

This is called an assignment statement. The part on the right side of the = is a mathematical expression. Python uses the * character to indicate multiplication. Recall that the value of x is 0.25 (from the input statement). The computed value is 3.9(0.25) (1-0.25) or 73125. Once the value on the right hand side is computed, it is stored back (or assigned) into the variable that appears on the left hand side of the =, in this case x. The new value of x (0.73125) replaces the old value (0.25).

 The second line in the loop body is a type of statement we have encountered before, a print statement.

print x

When Python executes this statement the current value of x is displayed on the screen. So, the first number of output is 0.73125.

 Remember the loop executes 10 times. After printing the value of x, the two statements of the loop are executed again.

x = 3.9 * x * (1 - x)

print x

Of course, now x has the value 0.73125, so the formula computes a new value of x as 3.9(0.73125) (1-0.73125), which is 0.76644140625.

Can you see how the current value of x is used to compute a new value each time around the loop? That’s where the numbers in the example run came from. You might try working through the steps of the program yourself for a different input value (say 0.5). Then run the program using Python and see how well you did impersonating a computer.


Comments

Popular posts from this blog

Python Programming Language | Part 1

In last article we saw all the technical details, it’s time to start having fun with Python. In this article we will learn about python programming language basics . The ultimate goal is to make the computer do our bidding. To this end, we will write programs that control the computational processes inside the machine. You have already seen that there is no magic in this process, but in some ways programming feels like magic. The computational processes inside the computer are like magical spirits that we can harness for our work. Unfortunately, those spirits only understand a very arcane language that we do not know. What we need is a friendly Genie that can direct the spirits to fulfill our wishes. Our Genie is a Python interpreter. We can give instructions to the Python interpreter, and it directs the underlying spirits to carry out our demands. We communicate with the Genie through a special language of spells and incantations (i.e., Python). The best way to start lear...