Updated document.txt

goromlagche authored
revision 3d6c9a208a66da05ea29ef5400f404159fbeed47
document

# **Python Essential Reference**

## **Print Stuff**
1. print year, principal
2. print "%3d %0.2f" % (year, principal)
3. print format(year,"3d"),format(principal,"0.2f")
4. print "{0:3d} {1:0.2f}".format(year,principal)


## **File
###
**
-
Read File:
f = open("foo.txt") # Returns a file object
line = f.readline() # Invokes readline() method on file
while line:
print line, # trailing ',' omits newline character
print(line,end='') # Use in Python 3
line = f.readline()
f.close()
###
-
Write File:
1. f = open("out","w") # Open file for writing
while year <= numyears:
principal = principal * (1 + rate)
print >>f,"%3d %0.2f" % (year,principal)
//f.write("%3d %0.2f¶
" % (year,principal))
year += 1
f.close()
2. import sys
sys.stdout.write("Enter your name :")
name = sys.stdin.readline()
3. name = raw_input("Enter your name :")


- Triple-quoted strings are useful when the contents of a string literal span multiple lines of text

- Strings are stored as sequences of characters indexed by integers, starting at zero.

- Although str() and repr() both create strings, their output is usually slightly different.
str() produces the output that you get when you use the print statement,
whereas repr() creates a string that you type into a program to exactly represent the
value of an object. For example:
>>> x = 3.4
>>> str(x)
'3.4'
>>> repr(x)
'3.3999999999999999'

## **List** []
import sys # Load the sys module
if len(sys.argv) != 2 # Check number of command line arguments :
print "Please supply a filename"
raise SystemExit(1)
f = open(sys.argv[1]) # Filename on the command line
lines = f.readlines() # Read all lines into a list
f.close()
# Convert all of the input values from strings to floats
fvalues = [float(line) for line in lines]
# Print min and max values
print "The minimum value is ", min(fvalues)
print "The maximum value is ", max(fvalues)

- List-comprehension
fvalues = [float(line) for line in open(sys.argv[1])]
- min max
min(fvalues)
max(fvalues)


## **Tuples** ()
- Python often recognizes that a tuple is intended even if the parentheses are missing:
stock = ('GOOG', 100, 490.10)
stock = 'GOOG', 100, 490.10

- Some programmers are inclined to ignore tuples altogether and simply use lists because they seem to be more flexible. Although this works, it wastes memory if your program is going to create a large number of small lists (that is, each containing fewer than a dozen items).This is because lists slightly overallocate memory to optimize the performance of operations that add new items. Because tuples are immutable, they use a more compact representation where there is no extra space.

- Use List and Tuple together:
filename = "portfolio.csv"
portfolio = []
for line in open(filename):
fields = line.split(",") # Split each line into a list
name = fields[0] # Extract and convert individual fields
shares = int(fields[1])
price = float(fields[2])
stock = (name,shares,price) # Create a tuple (name, shares, price)
portfolio.append(stock) # Append to list of records


## **Sets** set()
- Unlike lists and tuples, sets are unordered and cannot be indexed by numbers.Moreover, the elements of a set are never duplicated.
>>> t = set("Hello")
>>> t
set(['H', 'e', 'l', 'o'])
- Sets support a standard collection of operations
a = t | s # Union of t and s
b = t & s # Intersection of t and s
c = t – s # Set difference (items in t, but not in s)
d = t ^ s # Symmetric difference (items in t or s, but not both)
- Add Update Remove
t.add('x') # Add a single item
s.update([10,37,42]) # Adds multiple items to s
t.remove('H') # Removes single items from t

## **Dictionaries** {}