In [1]:
import sys
import platform

sysop = sys.version_info
print "^" * 5 + "+" * 5
print(sysop)
print "+" * 5 + "^" * 5

print("\n== Python version ==> {0}.{1}.{2}".format(sysop[:3][0],
                                            sysop[:3][1],sysop[:3][2]))

print("\n== Running Python {0}.{1}".format(  
    sys.version_info[:2][0],sys.version_info[:2][1]))

print "\n== Python built for " + sys.platform + " platform"

print "\n== Installation details : "
print sys.executable
print sys.prefix


^^^^^+++++
sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)
+++++^^^^^

== Python version ==> 2.7.11

== Running Python 2.7

== Python built for linux2 platform

== Installation details : 
/home/ec2-user/anaconda2/bin/python
/home/ec2-user/anaconda2

Example to showcase how to and how not to use in-place modification of mutable sequence.

*Scenario 1 :

** The phrase, 'rise to vote sir', is being handled via for-in iteration.

** Iteration, by default, operates on the sequence from Left to Right.

** pop() method used in the code below operates from Right to Left (Last-in First-out)

What is not obvious to the beginner ?


*** When the counter has reached '6', list iteration is exhaused and there is no more elements to pop.


In [1]:
palPhrase = ['r', 'i', 's', 'e', 't', 'o', 'v', 'o', 't', 'e', 's', 'i', 'r']
newLoopCnt = 0
print "==" * 2 + "direct (in-place modification) operations on the list" + "==" * 2
print "Length of the list : %d" %(len(palPhrase))
for ee in palPhrase:
    print "Counter {0}".format(newLoopCnt)
    ff = palPhrase.pop()
    print "Popped element : ", ff
    newLoopCnt = newLoopCnt + 1
print "Elements in the original list : ", palPhrase


====direct (in-place modification) operations on the list====
Length of the list : 13
Counter 0
Popped element :  r
Counter 1
Popped element :  i
Counter 2
Popped element :  s
Counter 3
Popped element :  e
Counter 4
Popped element :  t
Counter 5
Popped element :  o
Counter 6
Popped element :  v
Elements in the original list :  ['r', 'i', 's', 'e', 't', 'o']

*Scenario 2 :

** The phrase, 'rise to vote sir', is being handled via for-in iteration.

** Iteration, by default, operates on the sequence from Left to Right.

** pop() method used in the code below operates from Right to Left (Last-in First-out)

What is different here?


*** for-in iteration is working on a copy of the sequence which is done using a slice operator on the original sequence. Remember, slicing on a sequence without start and stop markers makes a copy of the original sequence.


In [2]:
palPhrase = ['r', 'i', 's', 'e', 't', 'o', 'v', 'o', 't', 'e', 's', 'i', 'r']
loopCnt = 0
print "==" * 2 + "operations using a copy of the list" + "==" * 2
print "Length of the list : %d" %(len(palPhrase))
for cc in palPhrase[:]:
    print "Counter {0}".format(loopCnt)
    dd = palPhrase.pop()
    print "Popped element : %s" %(dd)
    loopCnt = loopCnt + 1
print "Elements in the original list : ", palPhrase
print "Find out why the original list is empty"


====operations using a copy of the list====
Length of the list : 13
Counter 0
Popped element : r
Counter 1
Popped element : i
Counter 2
Popped element : s
Counter 3
Popped element : e
Counter 4
Popped element : t
Counter 5
Popped element : o
Counter 6
Popped element : v
Counter 7
Popped element : o
Counter 8
Popped element : t
Counter 9
Popped element : e
Counter 10
Popped element : s
Counter 11
Popped element : i
Counter 12
Popped element : r
Elements in the original list :  []
Find out why the original list is empty

In [ ]: