Python语法基础

1.注释
单行注释 #需要注释的内容
块注释 ”’需要注释的内容”’

2.变量
变量名的首字母必须是字母或下划线,首字符以外的字符可以由字母,数字或下划线组成。
python为解释执行,不需要严格的类型声明,所有变量在使用时进行赋值,语法上不允许使用未赋值的变量,变量生命周期类似Java的垃圾回收机制,即不被引用时python虚拟机自动回收。
存在局部变量和全局变量,在函数内使用grobal关键字引用。
对于一次赋多值可使用如下语法:v = (‘a’, ‘b’, ‘e’);(x, y, z) = v 按顺序赋值。

3.三目运算符
python不存在三目运算符,但由于语法的灵活性可如下方式实现三目运算:

1
2
def findMax(x,y):
    return x if x>=y else y

类似Java中:

1
2
3
int findMax(int x,int y){
    return x>=y?x:y
}

4.分号
Python也支持分号,同样用于一条语句的结束标识。但在Python中分号的作用已经不像C、Java中那么重要了,Python中的分号可以省略,主要通过换行(缩进)来识别语句的结束,严格的缩进能自动区分物理块和逻辑块。换行的方式是python推荐的。
当一行语句进行多个变量的声明时必须使用分号分隔,例如:

a = 1; b = 2; c = 3

多行写一条语句,Python使用“\”作为换行符,例如:

a = \
1;b=2

————–更新于2012-1-31 demo如下:—————————–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
name = "fanpt"
age = 23
size = 0
def findMax(x,y):
    return x if x>=y else y   #eq return x>=y?x:y
def testList():
    mylists = ["apple","milk","flowers","orange"]
    mylists.append("rice");
    for i in mylists:
        print i;
    mylists.sort();
    del mylists[0]
    for i in mylists:
        print i;

def testDic():
    ab={'Swaroop':'swaroopch@byteofpython.info','Larry':'larry@wall.org','Matsumoto':'matz@ruby-lang.org','Spammer':'spammer@hotmail.com'}
    print "Swaroop's address is %s" % ab['Swaroop']
    ab['Guido'] = 'guido@python.org' #add key-value
    del ab['Spammer']                #del key-value
    print 'There are %d contacts in the address-book' % len(ab)
    for name, address in ab.items():
        print 'Contact %s at %s' % (name, address)
    if 'Guido' in ab:
        print "Guido's address is %s"% ab['Guido']

def testSys():
    print "a lens is",len(a)
    print sys.path

def testWhile():
    flag = True;
    while flag:
        size = int(raw_input('Enter an integer : '))
        if size > 50:
            print "size > 50";
            flag = False;
        elif size < 50:
            print "size < 50";
        else :
            print "size = 50";

def testbreak():
    while True:
        s = raw_input('Enter something :')
        if s == 'quit':
            break
        print 'Still in loop,continued?exit with quit'
    print 'Done'

def testloop():
    for i in range(1,6):
        print i
    else:
        print "loop is over"

def testReplace():
    print '%s is %d years old' % (name, age)
    print 'Why is %s playing with that python?' % name

def testObject():
    print 'Simple Assignment'
    shoplist = ['apple', 'mango', 'carrot', 'banana']
    mylist = shoplist # mylist is just another name pointing to the same object!
    del shoplist[0]
    print 'shoplist is', shoplist
    print 'mylist is', mylist
    # notice that both shoplist and mylist both print the same list without
    # the 'apple' confirming that they point to the same object
    print 'Copy by making a full slice'
    mylist = shoplist[:] # make a copy by doing a full slice
    del mylist[0] # remove first item
    print 'shoplist is', shoplist
    print 'mylist is', mylist
    # notice that now the two lists are different
  1. 没有评论

  1. 没有通告