设为首页 收藏本站
查看: 1100|回复: 0

[经验分享] Python 学习系列(三)

[复制链接]

尚未签到

发表于 2017-4-24 12:06:05 | 显示全部楼层 |阅读模式
这篇文章主要学习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[0]:
print('user:%s' %user)
elif user == tempStr[1]:
print('user:%s' %user)
else:
user = tempStr[2]
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 in  enumerate(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[eachDict])
#            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
homework  i:(0)
something  i:(1)
python  i:(2)
jamson  i:(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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-368643-1-1.html 上篇帖子: Python基础:Python可变对象和不可变对象 下篇帖子: Python重要模块安装包
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表