python 學習筆記

title - python 學習筆記

0.參考資料
[1].http://man.chinaunix.net/develop/python/python2.3tut/tut/
[2].http://woodpecker.org.cn/abyteofpython_cn/chinese/index.html
[3].http://woodpecker.org.cn/diveintopython/
[4].http://www.ibm.com/developerworks/cn/linux/theme/python/index.html

1. python環境
$sudo apt-get install python

2. 快速入門
2.1. hellopython.py源代碼 ([1])

#!/usr/bin/env python

#
# Welcome
#
print "Hello Python!"

print "=== Control Flow ==="
#
# if
#
print "*** if ***"
#x = int(raw_input("Please enter an integer:"))
x = -1
if x < 0:
    x = 0
    print 'Negative changed to zero'
elif x == 0:
    print 'Zero'
elif x == 1:
    print 'Single'
else:
    print 'More'

#
# for
#
print "*** for ***"
a = ['cat', 'window', 'defenestrate']
for x in a:
    print x, len(x)

for x in a[:]:   #make a slice copy of the entire list
    if len(x) > 6: a.insert(0, x)

for i in range(len(a)):
    print i, a[i]

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print n, 'equals', x, '*', n/x
            break
    else:   # not break
        print n, 'is a prime number'


#
# while
#
print "*** while ***"
a, b = 0, 1
while b < 100:
    print b,   # "," not change line
    a, b = b, a+b

print
n = 2
while (n < 10):
    x = 2
    while x < n:
        if n % x == 0:
            print n, 'equals', x, '*', n / x
            break
        x += 1
    else: #not break
        print n, 'is a prime number'
    n = n + 1

#
# function
#
print "*** function ***"
def fib(n):
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a + b

fib(200)
print
print fib(0)

def fib2(n):
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a + b
    return result
f100 = fib2(100)
print f100

print "*** lambda ***"
def make_incrementor(n):
    return lambda x: x + n

f = make_incrementor(32)
print f(0)
print f(1)

#
# Data Structures
#
print "=== Data Structures ==="
print "*** list ***"
a = [66, 333, 333, 1, 22]
print a.count(333), a.count(66), a.count('x')
print 'a=', a
a.insert(2, -1)
print 'a.insert(2, -1), a=', a
a.append(333)
print 'a.append(333), a=', a
print 'a.index(333)=', a.index(333)
a.remove(333)
print 'a.remove(333), a= ', a

# use list as stack
stack = [3, 4, 5]
print "stack=", stack
stack.append(6)
print "stack=", stack
stack.append(7)
print "stack=", stack
print "stack.pop()=", stack.pop()
print "stack=", stack

print "*** Tuple ***"
t=123,456,"abc"
print t
u=t,(1,2,3,4,5)
print u

print "*** Dictrionariy ***"
tel={'a':111,'b':222,'c':333}
print tel
tel['d']=444
print tel
del tel['c']
print tel
print tel.keys()
print tel.values()
for k, v in tel.items():
    print k,v

#
# format
#
print "=== Output format ==="
for x in range(1, 11):
    print '%2d %3d %4d' % (x, x*x, x*x*x)

import string
print string.zfill('12',5)


#
# Read and Write Files
#
print "=== Read and Write Files ==="
fout=open('./workfile','w')
fout.write('aaaaa/n')
fout.close()
fin=open('workfile', 'r')
data=fin.read()
print data

#
# Modules
#
print "=== Modules ==="
from fib import fib,fib2
fib(100)
print fib2(100)

2.2. fib.py 源代碼([1])
def fib(n):
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

def fib2(n):
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

2.3. 運行代碼
將hellopython.py,fib.py存放在同一目錄中
$chmod +x hellopython.py
$./hellopython.py

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章