Updated document.txt

goromlagche authored
revision c499ebb172055ad267b1fc935c865132e402df2d
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'