Python 学习系列(三)
这篇文章主要学习Python 3.1中的条件和循环语句,同时,对一些基本的内建函数作些介绍,下面是我做的一个sample:'''
Created on 2010-4-1
@author: Jamson Huang
'''
import sys
#Control 语句=>If/While/for
if __name__ == '__main__':
#IF语句
#对于else语句来说,不仅仅只有if可以与之结合,while/for都可以与之结合使用
a = 5
count = 0
tempStr = ['homework','something', 'python','jamson']
tempZip = ['Children', 'do', 'Male', 'user']
myDicts = {('China','Hubei'):('yourName','Jamson')}
tempTable = {'china':86,'singpore':65}
globalStr = 'python is loved language!'
if a < 3:
print('a is less than 3!')
elif a < 5 :
print('a is less than 5!')
else:
print('a is large than or equal with 5!')
user = ''
if user == tempStr:
print('user:%s' %user)
elif user == tempStr:
print('user:%s' %user)
else:
user = tempStr
print('user:%s' %user)
#=>
if user in tempStr:
print('user:%s' %user)
else:
print('invalid string!')
#while
while count < 4:
print('count is %d' %count)
count +=1
#for
for str in ['homework','something', 'python']:
print('str:%s ' %str)
#pay attention to function range() and len()
for i in range(4):
print('i:%d ' %i)
for i in range(len(tempStr)):
print(tempStr,' i:(%s)' %i)
for value in range(2, 23, 3):
print('value:%i ' %value)
for value in range(2,5):
print('value:%i ' %value)
#enumerate()/reversed() function,return iterator
for i, eachItem inenumerate(tempStr):
print('%d %s Item ' %(i+1, eachItem))
for i in reversed(tempStr):
print("reversed(): %s" %i)
#sorted()/Zip() function,return table
for j in sorted(tempStr):
print('sorted(): %s' %j)
for j, k in zip(tempStr,tempZip):
print('zip(): %s %s' %(j, k))
#continue/break
#break会跳出整个循环,而continue是会跳出本次循环,并且不执行后面的程序。
def whileFunc(i, j, k):
while True:
i += 1
if i == 3:
print('break i:', i)
break
else:
print('continue j:', j)
continue
j += 1
else:
k += 1
print('k: %d ' %k)
#call parameter function
whileFunc(0,0,0)
#pass function:be used in debug process
#当调试的时候,确信某一个部分功能无误的情况,可以用pass函数
def passFunc():
iPass = 0
if iPass == 2:
pass
else:
iPass += 1
print('passFunc(): %d ' %iPass)
#call no paramter function passFunc
passFunc()
#iter() function/itertools module/any(),all() function
#seqence use
print(help('next'))
def iterSeqFunc():
myIter = iter(tempStr)
while True:
try:
print(next(myIter))
except StopIteration:
print('StopIteration:', sys.stderr)
break
finally:
print('iterSeqFunc() finally')
iterSeqFunc()
#dictionary use
def iterDictFunc():
print(myDicts.items())
for eachDict in myDicts.keys():
# for eachDict in myDicts:
print('Country:%s\tProvince:%s\t' %eachDict)
print('Name:%s\t %s' %myDicts)
# if myDicts.values() == 'Jamson':
print('values: %s' %myDicts.values())
iterDictFunc()
#File use write()/close()/read()/writelines()
#pay attention to close and writelines function
#List comps 列表解析
# print(help('split'))
print('sys.stdout:', sys.stdout)
print('sys.stdin:', sys.stdin)
def iterFileFunc():
myFile = open('C:/python/mylog.txt','r').read()
for eachLine in myFile:
print('eachLine:%s ' %eachLine)
# myFile.close()
iterFileFunc()
# 生成器表达式
run Python,Console输出的结果如下:
a is large than or equal with 5!
user:python
user:python
count is 0
count is 1
count is 2
count is 3
str:homework
str:something
str:python
i:0
i:1
i:2
i:3
homeworki:(0)
somethingi:(1)
pythoni:(2)
jamsoni:(3)
value:2
value:5
value:8
value:11
value:14
value:17
value:20
value:2
value:3
value:4
1 homework Item
2 something Item
3 python Item
4 jamson Item
reversed(): jamson
reversed(): python
reversed(): something
reversed(): homework
sorted(): homework
sorted(): jamson
sorted(): python
sorted(): something
zip(): homework Children
zip(): something do
zip(): python Male
zip(): jamson user
continue j: 0
continue j: 0
break i: 3
passFunc(): 1
Help on built-in function next in module builtins:
next(...)
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator
is exhausted, it is returned instead of raising StopIteration.
None
homework
iterSeqFunc() finally
something
iterSeqFunc() finally
python
iterSeqFunc() finally
jamson
iterSeqFunc() finally
StopIteration: <_io.TextIOWrapper name='<stderr>' encoding='UTF-8'>
iterSeqFunc() finally
dict_items([(('China', 'Hubei'), ('yourName', 'Jamson'))])
Country:ChinaProvince:Hubei
Name:yourName Jamson
values: dict_values([('yourName', 'Jamson')])
sys.stdout: <_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>
sys.stdin: <_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>
eachLine:I
eachLine:f
eachLine:
eachLine:y
eachLine:o
eachLine:u
eachLine:
eachLine:d
eachLine:o
eachLine:
eachLine:m
eachLine:u
eachLine:c
eachLine:h
eachLine:
eachLine:w
eachLine:o
eachLine:r
eachLine:k
eachLine:
eachLine:o
eachLine:n
eachLine:
eachLine:c
eachLine:o
eachLine:m
eachLine:p
eachLine:u
eachLine:t
eachLine:e
eachLine:r
eachLine:s
eachLine:,
eachLine:
eachLine:e
eachLine:v
eachLine:e
eachLine:n
eachLine:t
eachLine:u
eachLine:a
eachLine:l
eachLine:l
eachLine:y
eachLine:
eachLine:y
eachLine:o
eachLine:u
eachLine:
eachLine:f
eachLine:i
eachLine:n
eachLine:d
eachLine:
eachLine:t
eachLine:h
eachLine:a
eachLine:t
eachLine:
eachLine:t
eachLine:h
eachLine:e
eachLine:r
eachLine:e
eachLine:’
eachLine:
eachLine:s
eachLine:
eachLine:s
eachLine:o
eachLine:m
eachLine:e
eachLine:
eachLine:t
eachLine:a
eachLine:s
eachLine:k
eachLine:
eachLine:y
eachLine:o
eachLine:u
eachLine:’
eachLine:d
eachLine:
eachLine:l
eachLine:i
eachLine:k
eachLine:e
eachLine:
eachLine:t
eachLine:o
eachLine:
eachLine:a
eachLine:u
eachLine:t
eachLine:o
eachLine:m
eachLine:a
eachLine:t
eachLine:e
eachLine:.
eachLine:
eachLine:F
eachLine:o
eachLine:r
eachLine:
eachLine:e
eachLine:x
eachLine:a
eachLine:m
eachLine:p
eachLine:l
eachLine:e
eachLine:,
eachLine:
eachLine:y
eachLine:o
eachLine:u
eachLine:
eachLine:m
eachLine:a
eachLine:y
eachLine:
eachLine:w
eachLine:i
eachLine:s
eachLine:h
eachLine:.
页:
[1]