Getting Started with Python
Written by Alex Armstrong   
Wednesday, 08 February 2012
Article Index
Getting Started with Python
Control structures
Functions
Inheritance

Control structures

You need to know what sort of control structures are available in Python before moving on to consider the object-oriented nature of the language. Indeed, discovering how to form a conditional and various loops is part of learning any language.

As you would expect there are for and while loops and a full block structured if statement. The only slight surprise is that the for loop enumerates through a list rather than numeric values.

So, for example:

for z in x :

steps through each value stored in the list x.

To create a numeric for loop you can make use of the “range(n)” function which returns a list which runs from 0 to n-1. For example:

for z in range(10) :

repeats for values of z from 0 to 9.

The for loop is a compound statement header and this brings us to the subject of multi-line statements and blocks.

You need to pay special attention here because this is something that Python does differently from most other languages.  Python executes everything as you type it in and pressing return ends the line and Python gets on with obeying what you have typed. When you type something like a for loop which needs to work with other instructions you have to end the command with a colon which switches the interpreter into multi-line mode as indicated by the prompt ">>>" vanishing.

In this mode you can enter additional statements which will be repeated by the for loop. Notice that indenting is automatic and pressing Enter on a line containing nothing but spaces at the end  runs the loop.

For example –

>>>x=["spam","brian","grail"]  
>>> for z in x:
        print z

spam
brian
grail

What you might not realise at first is that, not only is indenting automatic, but it actually defines which block a statement belongs to.

In Python layout can change the meaning of a program.

If you manually enter indents using tabs or spaces then you determine the block that a statement belongs to. For example

>>> if x==1:
 print "block 1"
  y=2
  if y==2:
 print "block2"
print "block 1"

 

This way of entering blocks of code seems strange at first but you very quickly get used to it and it has many advantages.

Notice that the if statement has now been introduced and the symbol for a logical test of equality is “==”.

Which “if” an “else” statement pairs with is controlled by indenting. For example:

if x==1:
print "block 1"
y=2
if y==2:
print "block 2"
else:
print "block 3"
print "block 1"

In this case the else pairs with the inner if statement which should be compared to:

 

if x==1:
print "block 1"
y=2
if y==2:
print "block 2"
else:
print "block 3"
print "block 3"

In the above snippet the else pairs with the outer if and both print statements are in the else block.

Always remember that it is the number of tab characters that define which block a line belongs to. Any set of consecutive lines with the same number of indents forms a single block.

Another difference between Python and other languages is that the “else” can be paired with “for” and “while” as well as “if”. In this case the “else” is executed if the loop is exited normally, i.e. a break isn’t used to end it.

There is more to learn about Python control structures but this brief look is enough to get you started and certainly enough to let you read the rest of the code examples.

Everything is an object - references

At the very start of this introduction to Python I said that it was an object-oriented language and you might well be wondering when the objects are going to make their appearance.

Well in a sense they already have. Python calls just about everything an object and this reflects the way the Python system treats everything equally.

This everything is an object idea has one very big consequence that it is worth getting sorted out at as early as possible.

In most programming languages you store values in variables.

In Python variables store references to objects.

You can think of a reference as a pointer to an object which exists independently of the variable.

For example, consider:

 x=1
 y=x
 y=2

In this case you might read this as store 1 in x, copy what is stored in x to y, now store 2 in y. At the end we have 1 in x and 2 in y.

So far everything works as you would expect, but this is not what is happening.

The correct interpretation is that x is set to reference the object 1, then y is set to reference the same object, and finally y is set to reference the new object 2.

In this case the result is the same, but this isn't always the case. Consider the same process but now with a list:

 x=[1,2,3]
 y=x
 y[0]="spam"

If you read this as the list [1,2,3] is stored in x, then the contents of x are stored in y and the first element of the list in y is changed to "spam" then you have to conclude the x[0] is still 1.

This is not the case as x is set to reference the object [1,2,3], then y is set to reference the same object, i.e. no copy is made, and finally y[0] is changed to "spam".

I hope now you can see that the first element of the object that x and y reference has been changed.

In Python variables hold references to objects.

<ASIN:193518220X>

<ASIN:0982106017>

<ASIN:0596158068>



Last Updated ( Wednesday, 08 February 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.