1.print和import
1.1 print 略
1.2 import
(1)impore somemodule
(2)from somemodule import somefunction
(3)from somemodule import somefunction,anotherfunction
(4)from somemodule import*
(5)import somemodule as somename #為整個模塊提供別名
(6)from somemodule import somefunction as somename #為某個模塊的某個函數提供別名
2.賦值
2.1 序列解包:將多個值的序列解開,然后放到變量的序列中。
2.2 鏈式賦值:將同一個值賦給多個變量。
2.3 增量賦值:eg:x+=1
3.語句塊:條件為真時執行或執行多次的一組語句,由代碼前放置空格縮進語句創建。
4.條件語句
4.1 布爾變量 python中的假:False None 0 “” () [] {}
4.2 if語句
4.3 else語句
4.4 elif語句
4.5 嵌套代碼塊
4.6 條件
(1)比較運算符
(2)相等運算符 ==
(3)同一性運算符 is
(4)成員資格運算符 in
(5)字符串和序列比較
(6)布爾運算符
4.7 斷言: assert 放入檢查點,確保某個條件一定為真才能讓程序正常工作
5. 循環
5.1 while循環
5.2 for循環
5.3 循環遍歷字典元素
5.4 迭代工具
(1) 並行迭代:同時迭代兩個序列
(2)按索引迭代
(3)翻轉和排序迭代
5.5 跳出循環
(1) break
(2) continue
(3) while True/break
5.6 循環中的else子句
6.列表推導式-輕量級循環
7.pass,del,exec
本章相關代碼:
# -*- coding: utf-8 -*-
#1.print 與import
#1.1 print 使用逗號輸出
print 'Age:',42 #Age: 42
name='xiaming'
age='42'
print name,age #xiaming 42
#2.賦值語句
#2.1.序列解包:將多個值的序列解開,然后放到變量的序列中
x,y,z=1,2,3
print x,y,z #1 2 3
x,y=y,x #2 1 3
print x,y,z
v=1,2,3
x,y,z=v
print x #1
#2.2.鏈式賦值 將同一個值賦給多個變量的捷徑
x=y=4
print x,y
#2.3.增量賦值
x=2
x+=1
x*=2
a='bo'
a+='x'
a*=2
print x,a #6,boxbox
#3:語句塊:縮排
#4.條件與條件語句
#4.1.布爾變量的作用
#4.2 if elif else語句
name=raw_input('what is your name?')
if name.endswith('ming'):
print 'hello,ming'
elif name.endswith('hong'):
print 'hello,hong'
else:
print 'hello'
#4.3 嵌套語句
num=raw_input('input a number')
if num>10:
if num<20:
print '10<num<20'
else:
print 'num>=20'
else:
print 'num<=10'
#5.循環
#5.1.while循環
x=1
while x<=10:
print x
x+=1
name=''
while not name:
name=raw_input('enter your name:')
print 'Hello,%s!'%name #Hello,ming!
#5.2.for 循環
names=['ming','hong','qiang','qing']
for n in names:
print n
print range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] rang(0,10)
for num in range(1,10):
print num
#5.3.循環遍歷字典元素
d={'x':1,'y':2,'z':3}
for key in d:
print key,d[key] #y 2 x 1 z 3
#5.4.一些迭代工具
#5.4.1 並行迭代
names=['ming','hong','qiang','qing']
ages=[10,11,12,13]
for i in range(len(names)):
print names[i],'is',ages[i],'years old' #ming is 10 years old...
print zip(names,ages) #[('ming', 10), ('hong', 11), ('qiang', 12), ('qing', 13)] 將兩個序列壓縮,返回一個元組的列表
for n,a in zip(names,ages):
print n, 'is',a, 'years old' #ming is 10 years old...
#5.4.2按索引迭代
#5.4.3翻轉和排序迭代
#5.5.跳出循環
#5.5.1break
from math import sqrt
for n in range(99,0,-1):
print n
root=sqrt(n)
if root==int(root):
print "the biggst is %s"%n
break
#5.5.2 continue
#5.5.3 while True/break
while True:
word=raw_input('Please enter a word:')
if not word:break
print 'The word was '+word
#5.6 循環體中的else子句
from math import sqrt
for n in range(99,80,-1):
print n
root=sqrt(n)
if root==int(root):
print "the biggst is %s"%n
break
else:
print "didn't find it"
#6 列表推導式-輕量級循環:利用其它列表創建新列表
print [x*x for x in range(10)] #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print [x*x for x in range(10) if x%3==0] #[0, 9, 36, 81]
print [(x,y,z) for x in range(2) for y in range(2) for z in range(2)]
#[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
#7 三個語句:pass,del,exec
#7.1 pass :
#7.2 del:刪除不再使用的對象
#7.3 exec:執行一系列Py語句 exel:計算Py表達式,並且返回結果值