基础一

Python 语言参考


一、语法分析

(一)line 结构

1.logical lines

2.physical lines

3.comments

注释一般都是以#开头

4.encoding declarations

注释一般都是以下这种结构

# -*- coding: <encoding-name>-*-

5.explicit line joining

通常是以‘\’这个字符结尾

if 1900 < year < 2100 and 1 <= month <= 12 \
   and 1 <= day <= 31 and 0 <= hour < 24 \
   and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
        return 1

6.implicit line joining

一般都是方括号或圆括号,例如:

month_names = ['Januari', 'Februari', 'Maart',      # These are the
               'April',   'Mei',      'Juni',       # Dutch names
               'Juli',    'Augustus', 'September',  # for the months
               'Oktober', 'November', 'December']   # of the year

7.blank lines

8.indentation

1)每个逻辑行开始的空格键(spaces和tab)被用于计算行的压缩层次,进而被用于确定每组statements

2)注意跨平台最好不要两个混用

9.whitespace between tokens


(二)identifiers and keywords

identifiers的定义形式如下所示:

identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"

1.keywords

and       del       from      not       while
as        elif      global    or        with
assert    else      if        pass      yield
break     except    import    print
class     exec      in        raise
continue  finally   is        return
def       for       lambda    try


2.保留标识符

1)_* : 用于交互式解释器存储上次计算的结果,他存入到__buildin__module中

2)__*__

3)__*

(三) literal 文字

1.String literals

2.String literals 串联

3.numeric literals

4.integer and long integer literals

5.floating point literals

6.imaginary literay 虚数

例如: 3.14j


(四)操作符

+       -       *       **      /       //      %
<<      >>      &       |       ^       ~
<       >       <=      >=      ==      !=      <>

(五)delimiters 分隔符

(       )       [       ]       {       }      @
,       :       .       `       =       ;
+=      -=      *=      /=      //=     %=
&=      |=      ^=      >>=     <<=     **=



'makeTextFile.py -- create text file'

import os
ls = os.linesep


#get filename
fname='test.txt'
while True:


    if os.path.exists(fname):
        print ("error: '%s' already exists" % fname)
    else:
        break

# get file content lines
all = []

print ("\n enter 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!')

fobj.writelines(['%s%s' % (x,ls) for x in all])

将内存中的内容逐行写入文件,每个文件都需要一个行结束符(或文件结束字符)。该行结构称为列表解析,他进行以下的工作:对我们文件的每一行,根据程序运行平台添加一个合适的行结束符。‘%s%s'为每一行添加行结束符,(x,ls)表示每一行及其行结束符,对Unix平台,是‘\n’,对DOS或win32平台,则是'\r\n'。通过os.lineseq,我们不必关心程序运行在什么平台,也不必要根据不同的平台决定使用哪个行结束符。文件对象的writelines()方法接受包含行结束符的结果列表,并将它写入文件。


#!/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()


日志系统:

五级日志级别:紧急、错误、警告、信息和调试


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值