当先锋百科网

首页 1 2 3 4 5 6 7

#20100901
'''
Python 语句的简介
Python 程序可以分解成模块、语句、表达式以及对象?
1、程序有模块构成
2、模块包含语句
3、语句包含表达式
4、表达式建立并处理对象
'''
#Python的语法实质上是由语句和表达式组成的。表达式处理对象并嵌套在语句中。

'''
Python语句
    语句                    角色                                例子
  赋值                    创建引用                            a,b,c = 'good','bad','eggr'
  调用                    执行函数                            log.write("spam",ham/n")
  print                打印对象                            print "hello world"
  if/elif/else        选择动作                            if "python" in text:
                                                          print text
  for/else            顺序迭代                            for x in mylist:
                                                          print x
  while/else          一般循环                            while X > Y:
                                                          print 'hello'
  pass                空占位符                            while True:
                                                          pass
  break,continue      循环跳跃                            while True:
                                                          if not line:break
  try/except/finally  捕捉异常                            try:
                                                          action()
                                                       exctpt:
                                                          print 'action erro'
  raise                触发异常                            raise endSearch,location
  import,from          模块读取                            import sys
                                                          from sys import stdin
  def,return,yield     创建函数                            def f(a,b,c=1,*d)
                                                            return a+b+c+d[0]
                                                      def gen(n):
                                                            for i in n,yield i*2
 class                创建对象                             class subclass(Superclass):
                                                      staticData = []
 global                命名空间空间                        def function():
                                                             global x,y
                                                             x = 'new'
 del                    删除引用                            def data[k]
                                                       def data[i:j]
                                                       def obj.attr
                                                       def variable
 exec                 执行代码字符串                         exec "import " + modName
                                                       exec code in gdict,ldict
 assert                调试检查                             assert X > Y
 with/as               环境管理                             with open('data') as myfile:
                                                             process(myfile)
                                                      
'''

'''
指定判断
while True:
    reply = raw_input('Enter Guess:')
    if reply == 'stop':
        break
    elif not reply.isdigit():
        print 'Bad!' * 5
    else:
        print int(reply) ** 2
print 'Bye'

#isdigit方法:检查字符串内容
S = '123'
T = 'spam'
print S.isdigit(),T.isdigit()

#try 异常错误捕获
while True:
    reply = raw_input('Enter Guess:')
    if reply == 'stop':
        break
    try:
        num = int(reply)
    except:
        print 'Bad!' * 10
    else:
        print int(reply) ** 2
print 'Bye'

#嵌套代码三层
while True:
    reply = raw_input('Enter Guess:')
    if reply == 'stop':
        break
    elif not reply.isdigit():
        print 'Bad!' * 8
    else:
        num = int(reply)
        if num < 20:
            print 'low'
        else:
            print num ** 2
print 'Bye!!!'
'''
#赋值语句、表达式、打印
'''
复制语句建立对象引用值
变量名在首次赋值时会被创建
变量名在引用前必须赋值
隐式赋值语句:import from def class for

赋值语句的形式:

    运算                                  解释
  spam = 'Spam'                         基本形式
  spam,ham = 'yun','YUM'                元祖赋值运算(位置性)
  [spam,ham] = ['yum','YUM']            列表赋值运算(位置性)
  a,b,c,d = 'spam'                      序列赋值运算,通用性
  spam = ham = 'lunch'                  多目标赋值运算
  spams += 42                           曾强赋值运算(相当于spams = spams + 42)
'''
'''
#序列赋值
bob = 12
num = 13
A,B = bob,num
print A,B
[C,D] = [bob,num]
print C,D
[a,b,c] = (1,2,3)
print a,c
(a,b,c) = "456"
print a,c

#高级序列赋值语句模式
string = 'SPAM'
a,b,c,d = string
print a,b
#注意:虽然可以再“=”符号两侧混合和匹配的序列类型,右边元素的数目还是要跟左边的变量的数目相同,不然会产生错误
#eg:a,b,c = string   会报错的
#如果想通用的话,就需要使用分片了,这里有几种方式使用分片运算,可以使最后的情况正常工作
a,b,c = string[0],string[1],string[2:]
print a,b,c
a,b,c = list(string[:2]) + [string[2:]]
print a,b,c
a,b = string[:2]
c = string[2:]
print a,b,c
(a,b),c = string[:2],string[2:]
print a,b,c
((a,b),c) = ('SP','AM')
print a,b,c

red,green,blue = range(3)
print red,blue
print range(3)

#range一般用于循环中:
L = [1,2,3,4,5,6,7]
while L:
    front,L = L[0],L[1:]
    print front,L
#多目标赋值
a = b = c = 'spam'
print a,b,c
c = 'nihao'
b = c
a = b
print a

#多目标赋值以及共享引用
#修改b只会b发生修改,因为数字不支持在原处的修改。
a = b = 0
b = b + 1
print b

#a b 引用共同对象,通过b附加值上去,而我们通过a也会看见所有效果
a = b = []
b.append(42)
print a
print a,b

#增强赋值以及共享引用
#增强赋值语句的三个优点:
#1、程序员输入减少。
#2、左侧只需要算一次。
#3、优化技术会自动选择。
L = [1,2]
M = L
L = L + [3,4]
print L,M

L = [1,2]
M = L
L += [3,4]
print L,M
'''

#表达式语句
#Python常见的表达式语句
'''
    运算                      解释
  spam(eggs,ham)            函数调用
  spam.ham(eggs)            方法调用
  spam                      在交互式模式解释器内打印变量
  spam < ham and ham != eggs 符合表达式
  spam < ham < eggs         测试范围
'''

L = [1,2]
L.append(3)
print L

#对列表调用append、srot或reverse这类在原处的修改的运算,一定是针对列表做原处修改。

import sys
sys.stdout.write('Hello World/n')

print 'X'
#等价于
sys.stdout.write(str('X') + '/n')


#print >> file 扩展
#通过赋值sys.stdout而将打印文字重定向的技巧,在实际应用中非常常用。
#标准输出
import sys
temp = sys.stdout
sys.stdout = open('text.txt','w')
print 'spam'
print 1,2,3,4,5
sys.stdout.close()
sys.stdout = temp
print open('text.txt').read()

log = open('test1.txt','w')
print >> log,1,2,3
print >> log,3,4,5,6,7,8,9,9
log.close()
print open('test1.txt').read()

#打印标准错误流
#sys.stderr.write(('Bad!' * 5) + '/n')
#print >> sys.stderr, 'Bad!' * 10