`

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[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],' 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:China	Province: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
0
分享到:
评论

相关推荐

    Python学习资料整理

    这个"Python学习资料整理"压缩包包含了一系列的学习资源,旨在帮助初学者和进阶者提升Python编程技能。 首先,Python的基础知识是理解其语法结构,包括变量、数据类型(如整型、浮点型、字符串、布尔型、列表、元组...

    Python学习资源

    "零基础学python"这个压缩包很可能是针对初学者设计的一系列学习资料,旨在帮助没有编程背景的人逐步掌握Python编程。 在学习Python的过程中,以下几个关键知识点是必不可少的: 1. **基础语法**:Python的基础...

    Python-IPythonNotebooks学Python一系列ipynb来学习基础的Python编程知识

    4. **模块和包**:学习导入和使用Python标准库中的模块,如math、os、sys等,以及第三方包如numpy、pandas、matplotlib等。 5. **列表、元组和字典**:深入理解这些数据结构,包括它们的区别、操作方法和应用场景。...

    PYTHON学习资料包.zip

    【标题】"PYTHON学习资料包.zip"所包含的是一个综合性的Python学习资源集合,适合不同层次的学习者。从初学者到有一定基础的开发者,都可以在这个资料包中找到适合自己的学习材料。 【描述】"python学习资料包合集...

    Python学习手册 第5版(英文原版).zip

    《Python学习手册 第5版》是一本深入浅出的Python编程指南,专为那些希望掌握Python编程基础乃至进阶知识的读者设计。本书由资深Python开发者撰写,详细讲解了Python语言的核心概念、语法结构和常用库,是学习Python...

    python 学习视频

    ##### 1.4 入门学习系列 - **知识点概述**:分为三个阶段,覆盖了从基本语法到高级特性的全面内容。 - **关键内容**: - 阶段一:变量、数据类型、运算符等; - 阶段二:函数、模块、异常处理等; - 阶段三:文件...

    Python学习路径以及学习视频

    "Python学习路径"意味着从初学者到精通者所应遵循的一系列学习步骤和资源。"黑马程序员全套视频课程"提供了全面的学习材料,涵盖从基础到高级的Python知识,包括Linux操作系统的基础、前端开发、网络爬虫技术以及Web...

    python学习小项目

    "Python学习小项目"是专为刚刚接触编程或Python语言的人设计的一系列实践练习,旨在帮助他们巩固基础知识,提升编程技能。这个小项目可能涵盖诸如文件操作、数据处理、网页爬虫、图形用户界面(GUI)等方面的内容,...

    python学习经典书籍

    第一本,《Python学习手册》(Python Learning Handbook),这本书是Python学习的经典之作。作者深入浅出地介绍了Python的基础语法和核心概念,包括变量、数据类型、控制结构、函数、模块、面向对象编程等。此外,书...

    Python学习文档资料

    这份“Python学习文档资料”包含了两本极具价值的学习资源,旨在帮助初学者快速入门并深入理解Python语言。 第一本书是《笨办法学 Python(第四版)》。这本书以“做中学”的理念,引导读者通过一系列精心设计的练习...

    python学习源码和开发应用技术

    ### Python学习源码和开发应用技术 #### Python简介与特性 Python是一种高级编程语言,以其简洁易读的语法和强大的功能而闻名。Python支持多种编程范式,包括面向对象、命令式、函数式以及过程式编程。由于其设计...

    Python学习视频,36节课程.docx

    本"Python学习视频"课程由36节精心设计的课程组成,旨在帮助初学者系统地掌握Python编程技能。 课程可能涵盖以下几个核心知识点: 1. **Python基础**:首先会介绍Python的历史、特点以及安装方法,确保学员拥有...

    Python 学习资料下载地址大全

    本文将基于提供的几个链接以及其他相关资源,为大家总结归纳一系列值得收藏的Python学习资料。 #### 一、Python基础知识学习资源 1. **《Python基础教程》** - **下载地址**:...

    Python学习_pdf

    ### Python学习知识点详解 #### 一、Python简介与特点 **Python** 是一种高级编程语言,因其简单易学、功能强大而在全球范围内受到广泛的欢迎。它不仅是一种完全面向对象的语言,而且支持多种编程范式,如过程式...

Global site tag (gtag.js) - Google Analytics