Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

Python Programming Tutorial-Page4

Refer following pages to learn complete Python language tutorial.
Python tutorial page1
Python tutorial page2
Python tutorial page3
Python tutorial page4
Python tutorial page5
Python tutorial page6

Formatted Output

When working with data, you often want to produce structured output (tables, etc.). For example table having Name,Shares and Price as columns.
Formatting operator (%)
>>> "The value is %d" % 3
'The value is 3'
Requires single item or a tuple on right.Commonly used with print and Format codes are same as with C printf().
%d Decimal integer
%u Unsigned integer
%x Hexadecimal integer
%f Float
%s String
%c Character

Formatting with fields in a dictionary
>>> stock = {
... 'name' : 'GOOG',
... 'price' : 490.10,
... 'shares' : 100
}
>>> print "%(name)8s %(shares)10d %(price)10.2f" % stock
GOOG 100 490.10
>>>

Working with sequences

Python has three sequence datatypes as mentioned below:
a = "Hello" # String
b = [1,4,5] # List
c = ('GOOG',100,490.10) # Tuple

Sequences are ordered : s[n]
a[0] 'H'

Sequences have a length : len(s)
len(a) 5

Sequences can be replicated : s * n
>>> a = 'Hello'
>>> a * 3
'HelloHelloHello'

Similar sequences can be concatenated : s + t
>>> a = (1,2,3)
>>> b = (4,5)
>>> a + b
(1,2,3,4,5)

Slicing operator : s[start:end]
a = [0,1,2,3,4,5,6,7,8]
a[2:5] [2,3,4]

Indices must be integers
Slices do not include end value
If indices are omitted, they default to the beginning or end of the list.

Extended slicing: s[start:end:step]
a = [0,1,2,3,4,5,6,7,8]
a[:5] [0,1,2,3,4]

Sequence Reductions

sum(s)
>>> s = [1, 2, 3, 4]
>>> sum(s)
10
min(s), max(s)
>>> min(s)
1
>>> max(s)
4

Iterating over a Sequence

The for-loop iterates over sequence data
>>> s = [1, 4, 9, 16]
>>> for i in s:
... print i
...
1
4
9
16
>>>
On each iteration of the loop, you get new item of data to work with.

Iteration Variables

Each time through the loop, a new value is placed into an iteration variable
for x in s:
statements:
Overwrites the previous value (if any)
After the loop finishes, the variable has the value from the last iteration of the loop
x = 42
for x in s: # Overwrites any previous x
statements
print x # Prints value from last iteration

break and continue

Breaking out of a loop (exiting)
for name in namelist:
if name == username: break

Jumping to the start of the next iteration
for line in lines:
if not line: continue
# More statements
...

These statements only apply to the innermost loop that is active

Looping over integers

If you simply need to count, use xrange()
xrange([start,] end [,step])
for i in xrange(100):
# i = 0,1,...,99
range([start,] end [,step])
range() creates a list of integers

enumerate() Function

Provides a loop counter value
names = ["Elwood","Jake","Curtis"]
for i,name in enumerate(names):
# Loops with i = 0, name = 'Elwood'
# i = 1, name = 'Jake'
# i = 2, name = 'Curtis'
...

Example: Keeping a line number
for linenumber,line in enumerate(open(filename)):
...

enumerate() is a nice shortcut
for i,x in enumerate(s):
statements
Compare to:
i = 0
for x in s:
statements
i += 1
Less typing and enumerate() runs slightly faster

for and tuples

Looping with multiple iteration variables
points = [
(1,4),(10,40),(23,14),(5,6),(7,8)
]

for x,y in points:
# Loops with x = 1, y = 4
# x = 10, y = 40
# x = 23, y = 14
# ...
Here, each tuple is unpacked into a set of iteration variables.

zip() Function

Combines multiple sequences into tuples
a = [1,4,9]
b = ['Jake','Elwood','Curtis']
x = zip(a,b) # x = [(1,'Jake'),(4,'Elwood'), ...]

zip() always stops with shortest sequence
a = [1,2,3,4,5,6]
b = ['Jake','Elwood']
x = zip(a,b) # x = [(1,'Jake'),(2,'Elwood')]

List Sorting

Lists can be sorted "in-place" (sort method)
s = [10,1,7,3]
s.sort() # s = [1,3,7,10]


Sorting in reverse order
s = [10,1,7,3]
s.sort(reverse=True) # s = [10,7,3,1]
Sorting works with any ordered type
s = ["foo","bar","spam"]
s.sort() # s = ["bar","foo","spam"]

Sometimes you need to perform extra processing while sorting
Example: Case-insensitive string sort
>>> s = ["hello","WORLD","test"]
>>> s.sort()
>>> s
['WORLD','hello','test']

Sorting with a key function:
>>> def tolower(x):
... return x.lower()
...
>>> s = ["hello","WORLD","test"]
>>> s.sort(key=tolower)
>>> s
['hello','test','WORLD']

Sequence Sorting

sorted() function
Turns any sequence into a sorted list
>>> sorted("Python")
['P', 'h', 'n', 'o', 't', 'y']
>>> sorted((5,1,9,2))
[1, 2, 5, 9]


               


Share this page

Translate this page