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
** 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)
*** 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
** 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)
*** 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"
In [ ]: