Practical Python Programming
Practical Python Programming
In class exercises
Execute these lines one at a time, in order, to see what each one does. (Loops will execute after the entire loop is input.)
How to...
1. Input and print a line of space-delimited strings from any platform.
Straightforward:
line = raw_input() # Inputs an entire line of data as a string
line.strip() # Deletes initial and final white space (end of line, carriage
# returns, tabs, blanks)
aList = line.split() # Constructs a list with intervening white space removed
for x in aList: # Note: you do not have to index the sequence: Just use it.
print x
More Pythonic:
line = raw_input().strip().split() #Combine the string operations
for x in line:
print x
2. Input and print space-delimited numbers and their squares.
Straightforward
line = raw_input().strip().split()
for x in line:
n = eval(x)
print " %d**2 =%d'" %(n,n**2)
To make the equal signs line up:
line = raw_input().strip().split()
for x in line:
n = eval(x)
print " %25d**2 =%d" %(n,n**2) # Use a field length in the format string
More Pythonic:
line = raw_input().strip().split()
numbers = [] # Make a list of the numbers
for x in line:
numbers.append(eval(x)) # Alternatively: numbers += [eval(x)]
for n in numbers:
print " %25d**2 =%d" %(n,n**2)
Even More Pythonic:
line = raw_input().strip().split()
numbers = [eval(x) for x in line]
for n in numbers:
print " %25d**2 =%d" %(n,n**2)
3. Use a blank line as an end of file marker.
done = False
while not done:
x = raw_input().strip()
done = (len(x) == 0)
if not done:
n = eval(x)
print " %25d**2 =%d" %(n,n**2)
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- python programming books free pdf
- best python programming book
- python programming language pdf book
- free python programming books
- python programming pdf free download
- python programming tutorials
- python programming for absolute beginners
- python programming on win32 download
- basic python programming examples
- python programming examples pdf
- python programming examples source code
- introduction to python programming pdf