Python学习笔记《Python核心编程》第3章Python基础

语句和语法

     井号(#)表示之后的字符为python注释;

     换行(\n)是标准的行分隔符

     反斜杠(\)继续上一行

     分号(;)将两个语句连接在一行中

     冒号(:)将代码块的头和体分开

     语句(代码块)用缩进块的方式体现

     不同缩进深度分隔不同的代码块

     python文件以模块的形式组织。

赋值操作符  

     等号赋值: x = 5

     增量赋值:  x = x + 1 ; x+=1;

     多重赋值:x = y = z = 1

     “多元”赋值:x,y,z = 1,2,'string'  或者   (x,y,z)=(1,2,'string')

     多元赋值可以完成变量交换:x,y = 1,2; x,y = y,x    # x=2 y=1

标识符:

     关键字:关键字模块中同时包含了以下关键字列表和一个iskeyword()函数

      and    as    assert    break    class    continue    def    del    dlif    else    except    exec    finally    for    form    global    if    import    in    is

      lambda    not    or    pass    print    raise    return    try    while    with    yield    None

      文档:python通过__doc__来获得文档字串:在模块、类申明、或函数声明中第一个没有赋值的字符串可以用属性obj.__doc__来进行访问

模块结构和布局     

      /usr/bin/env python       # 起始行      

     "this is a test module"    # 模块文档:简要介绍模块功能及重要全局变量的含义,可通过module.__doc__访问

     import sys                 # 模块导入: 导入所需要的模块

     debug = True               # 变量定义:此处定义为全局变量

     class FooClass(object):    # -----------|

          "Foo class"           #            |      # 类定义:当模块被导入时类就会被定义。类的文档变量时class.__doc__            

          pass                  # -----------|

     def test():                # 函数定义:此处定义的函数通过module.function()访问,函数文档变量是function.__doc__

         "test function"

         foo = FooClass()

         if debug:

               print 'ran test()'

     if __name__ == '__main__':        #主程序

         test()

 核心笔记:__name__系统变量,如果模块式被导入的,__name__的值为模块名字,如果模块是被直接执行,__name__的值为‘__main__’。

 内存管理:

     变量无须事先申明;

     变量无须指定类型;            #python中对象类型和内存占用都是运行时确定的

     程序员不用关心内存管理;   #引用计数管理内存

     变量名会被“回收”;

     del 语句能够够直接释放资源;     #del obj1

第一个python程序    

     创建文件:createFile.py

#!/usr/bin/env python

'makeTextFile.py -- create text file'

import os
ls = os.linesep

#get filename
while True:
    fname = raw_input("输入文件名:")
    if os.path.exists(fname):
        print "ERROR: '%s' already exists" % fname
    else:
        break

#get file content (text) lines
all = []
print "\nEnter lines ('.' by itself to quit).\n"

#loop until user terminates input
while True:
    entry = raw_input('>')
    if entry == '.':
        break
    else:
        all.append(entry)

# write lines to file with proper line-ending
fobj = open(fname,'w')
fobj.writelines(['%s%s' % (x,ls) for x in all])
fobj.close()
print 'DONE!'

     读取文件 readFile.py

#!/usr/bin/env python

'readTextFile.py -- read and display text file'

#get filename
fname = raw_input('Enter filename: ')
print

#attempt to open file for reading
try:
    fobj = open(fname,'r')
except IOError,e:
    print "*** file open error:",e
else:
    # display contents to the screen
    for eachLine in fobj:
        print eachLine,
    fobj.close()

 

转载于:https://www.cnblogs.com/NNUF/archive/2013/01/13/2858908.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值