#20100901
'''
Python 語句的簡介
Python 程序可以分解成模塊、語句、表達式以及對象?
1、程序有模塊構成
2、模塊包含語句
3、語句包含表達式
4、表達式建立並處理對象
'''
#Python的語法實質上是由語句和表達式組成的。表達式處理對象並嵌套在語句中。
'''
Python語句
語句 角色 例子
賦值 創建引用 a,b,c = 'good','bad','eggr'
調用 執行函數 log.write("spam",ham/n")
print 打印對象 print "hello world"
if/elif/else 選擇動作 if "python" in text:
print text
for/else 順序迭代 for x in mylist:
print x
while/else 一般循環 while X > Y:
print 'hello'
pass 空佔位符 while True:
pass
break,continue 循環跳躍 while True:
if not line:break
try/except/finally 捕捉異常 try:
action()
exctpt:
print 'action erro'
raise 觸發異常 raise endSearch,location
import,from 模塊讀取 import sys
from sys import stdin
def,return,yield 創建函數 def f(a,b,c=1,*d)
return a+b+c+d[0]
def gen(n):
for i in n,yield i*2
class 創建對象 class subclass(Superclass):
staticData = []
global 命名空間空間 def function():
global x,y
x = 'new'
del 刪除引用 def data[k]
def data[i:j]
def obj.attr
def variable
exec 執行代碼字符串 exec "import " + modName
exec code in gdict,ldict
assert 調試檢查 assert X > Y
with/as 環境管理 with open('data') as myfile:
process(myfile)
'''
'''
指定判斷
while True:
reply = raw_input('Enter Guess:')
if reply == 'stop':
break
elif not reply.isdigit():
print 'Bad!' * 5
else:
print int(reply) ** 2
print 'Bye'
#isdigit方法:檢查字符串內容
S = '123'
T = 'spam'
print S.isdigit(),T.isdigit()
#try 異常錯誤捕獲
while True:
reply = raw_input('Enter Guess:')
if reply == 'stop':
break
try:
num = int(reply)
except:
print 'Bad!' * 10
else:
print int(reply) ** 2
print 'Bye'
#嵌套代碼三層
while True:
reply = raw_input('Enter Guess:')
if reply == 'stop':
break
elif not reply.isdigit():
print 'Bad!' * 8
else:
num = int(reply)
if num < 20:
print 'low'
else:
print num ** 2
print 'Bye!!!'
'''
#賦值語句、表達式、打印
'''
複製語句建立對象引用值
變量名在首次賦值時會被創建
變量名在引用前必須賦值
隱式賦值語句:import from def class for
賦值語句的形式:
運算 解釋
spam = 'Spam' 基本形式
spam,ham = 'yun','YUM' 元祖賦值運算(位置性)
[spam,ham] = ['yum','YUM'] 列表賦值運算(位置性)
a,b,c,d = 'spam' 序列賦值運算,通用性
spam = ham = 'lunch' 多目標賦值運算
spams += 42 曾強賦值運算(相當於spams = spams + 42)
'''
'''
#序列賦值
bob = 12
num = 13
A,B = bob,num
print A,B
[C,D] = [bob,num]
print C,D
[a,b,c] = (1,2,3)
print a,c
(a,b,c) = "456"
print a,c
#高級序列賦值語句模式
string = 'SPAM'
a,b,c,d = string
print a,b
#注意:雖然可以再“=”符號兩側混合和匹配的序列類型,右邊元素的數目還是要跟左邊的變量的數目相同,不然會產生錯誤
#eg:a,b,c = string 會報錯的
#如果想通用的話,就需要使用分片了,這裏有幾種方式使用分片運算,可以使最後的情況正常工作
a,b,c = string[0],string[1],string[2:]
print a,b,c
a,b,c = list(string[:2]) + [string[2:]]
print a,b,c
a,b = string[:2]
c = string[2:]
print a,b,c
(a,b),c = string[:2],string[2:]
print a,b,c
((a,b),c) = ('SP','AM')
print a,b,c
red,green,blue = range(3)
print red,blue
print range(3)
#range一般用於循環中:
L = [1,2,3,4,5,6,7]
while L:
front,L = L[0],L[1:]
print front,L
#多目標賦值
a = b = c = 'spam'
print a,b,c
c = 'nihao'
b = c
a = b
print a
#多目標賦值以及共享引用
#修改b只會b發生修改,因爲數字不支持在原處的修改。
a = b = 0
b = b + 1
print b
#a b 引用共同對象,通過b附加值上去,而我們通過a也會看見所有效果
a = b = []
b.append(42)
print a
print a,b
#增強賦值以及共享引用
#增強賦值語句的三個優點:
#1、程序員輸入減少。
#2、左側只需要算一次。
#3、優化技術會自動選擇。
L = [1,2]
M = L
L = L + [3,4]
print L,M
L = [1,2]
M = L
L += [3,4]
print L,M
'''
#表達式語句
#Python常見的表達式語句
'''
運算 解釋
spam(eggs,ham) 函數調用
spam.ham(eggs) 方法調用
spam 在交互式模式解釋器內打印變量
spam < ham and ham != eggs 符合表達式
spam < ham < eggs 測試範圍
'''
L = [1,2]
L.append(3)
print L
#對列表調用append、srot或reverse這類在原處的修改的運算,一定是針對列表做原處修改。
import sys
sys.stdout.write('Hello World/n')
print 'X'
#等價於
sys.stdout.write(str('X') + '/n')
#print >> file 擴展
#通過賦值sys.stdout而將打印文字重定向的技巧,在實際應用中非常常用。
#標準輸出
import sys
temp = sys.stdout
sys.stdout = open('text.txt','w')
print 'spam'
print 1,2,3,4,5
sys.stdout.close()
sys.stdout = temp
print open('text.txt').read()
log = open('test1.txt','w')
print >> log,1,2,3
print >> log,3,4,5,6,7,8,9,9
log.close()
print open('test1.txt').read()
#打印標準錯誤流
#sys.stderr.write(('Bad!' * 5) + '/n')
#print >> sys.stderr, 'Bad!' * 10
Python入門8
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.