对于任何一门语言的学习,学语法是最枯燥无味的,但又不得不学,基础概念较繁琐,本文将不多涉及概念解释,用例子进行相关解析,适当与C语言对比,避免陷入语法的苦海。我认为初学者学习语法的目标是学会使用即可,关于对概念的深入理解,剖析,没有一定的知识积累是很难做到的。
学习Python,基本语法不是特别难,有了C的基本知识,理解比较容易。本文的主要内容是Python基础语法,学完后,能熟练使用就好。(开发环境依然是Python2.7,简单使用)
1 >>> x=12
2 >>> y=13
3 >>> z=x+y 4 >>> print z 5 25
注意:尽管变量不需要预先定义,但是要使用的时候,必须赋值,否则报错:
1 >>> le 2 Traceback (most recent call last): 3 File "<pyshell#8>", line 1, in <module>
4 le 5 NameError: name 'le' is not defined
2,查看变量的类型函数type():
1 >>> type(x) 2 <type 'int'>
1 >>> x=12
2 >>> y=13
3 >>> z=x+y 4 >>> m=12
5 >>> print 'id(x)=',id(x) 6 id(x)= 30687684
7 >>> print 'id(m)=',id(m) 8 id(m)= 30687684
9 >>> print 'id(z)=',id(z) 10 id(z)= 30687528
11 >>> x=1.30
12 >>> print 'id(x)=',id(x) 13 id(x)= 43407128
从上述结果可以发现:变量的指向变,地址不变,换句话说,整数12的地址值始终不变,变化的是变量的指向(如x的地址变化);
1 >>> x='day'
2 >>> y=13.4
3 >>> print x,type(x) 4 day <type 'str'>
5 >>> print y,type(y) 6 13.4 <type 'float'>
1 >>> print 'x=',12
2 x= 12
格式化控制符:%f浮点数;%s字符串;%d双精度浮点数(这和C的输出是一致的)。
1 >>> x=12
2 >>> y=13.0004
3 >>> z='Python'
4 >>> print "output %d %f %s"%(x,y,s) 5 output 12 13.000400 Python
5,输入函数raw_input():
1 >>> raw_input("input an int:") 2 input an int:12
3 '12'
1 >>> help(id) 2 Help on built-in function id in module __builtin__: 3
4 id(...) 5 id(object) -> integer 6
7 Return the identity of an object. This is guaranteed to be unique among 8 simultaneously existing objects. (Hint: it's the object's memory address.) 9
1 #define function:add (函数说明)
2 def add(x,y): #函数头部,注意冒号,形参x,y
3 z=x+y #函数体
4 return z #返回值
5 #define main function
6 def main(): 7 a=12
8 b=13
9 c=add(a,b) #函数调用,实参a,b
10 print c 11 main() #无参函数调用
12 print 'End1!'
注意:这部分与C的存在的异同在于:
1 def test(n1,n2): 2 print n1, 3 print n2 4 n=n1+n2 5 m=n1*n2 6 p=n1-n2 7 e=n1**n2 8 return n,m,p,e 9 print 'Entry programme1'
10 sum,multi,plus,powl=test(2,10) #这个是C语言所没有的赋值方式
11 print 'sum=',sum 12 print 'multi=',multi 13 print 'plus=',plus 14 print 'powl=',powl 15 re=test(2,10) 16 print re #数据类型为:'tuple'
17 print re[0],re[1],re[2],re[3] 18 print 'End1!\n'
运行结果:
1 Entry programme 2 2 10
3 sum= 12
4 multi= 20
5 plus= -8
6 powl= 1024
7 2 10
8 (12, 20, -8, 1024) 9 12 20 -8 1024
10 End!
2,局部变量:
1 def f1(): 2 x=12 #局部变量
3 print x 4 def f2(): 5 y=13 #局部变量
6 print y 7 def f3(): 8 print x #错误:没有定义变量x,这与“不需要预先定义数据类型”不矛盾
9 print y 10 def main(): 11 f1() 12 f2() 13 #f3()#变量报错
14 main() 15 print 'End2!'
1 def modifyGlobal(): 2 global x #全局变量定义
3 print 'write x =-1'
4 x=-1
5 def main(): 6 # printLocalx()
7 # printLocaly()
8 # readGlobal()
9 modifyGlobal() 10
11 x=200
12 #y=100
13 print 'before modified global x=', 14 print x 15 main() 16 print 'after modified global x=', 17 print x
1 >>>
2 before modified global x= 200
3 write x =-1
4 after modified global x= -1
三、表达式与分支语句
1 >>> sex="male"
2 >>> if sex=='male': 3 print 'Man!'
4 #此处有两次回车键
5 Man! 6 >>>
2)形式二:(if <condition>: else (if <condition>:))
1 sex=raw_input('Please input your sex:') 2 if sex=='m' or sex=='male': 3 print 'Man!'
4 else: 5 print 'Woman!'
运行结果:
1 >>>
2 Please input your sex:male 3 Man!
3)形式三:(if <condition>: elif <condition>: else ))(这是Python有而C没有的形式)
1 count=int(raw_input('Please input your score:')) 2 if count>=90: 3 print'优秀!'
4 elif count>=80: 5 print '优良!'
6 elif count>=70: 7 print '合格!'
8 elif count>=60: 9 print '及格!'
10 else: 11 print '不及格!'
运行结果:
1 >>>
2 Please input your score:90
3 优秀!
注意:Python没有switch语句。
1 i=1
2 while i<5: 3 print 'Welcome you!'
4 i=i+1
2)while……else……形式下:
1 i=1
2 while i<5: 3 print 'Welcome you!'
4 i=i+1
5 else: 6 print "While over!" #循环正常结束
注意:如果while非正常状态结束(即不按循环条件结束),则else语句不执行。如下:
1 i=1
2 while i<5: 3 print 'Welcome you!'
4 i=i+1
5 if i==2: 6 print 'While……'
7 break
8 else: 9 print "While over!"
运行结果:
1 >>>
2 Welcome you! 3 While……
1 i=1
2 while i<=5: 3 if i==2 or i==4: 4 print 'While……continue'
5 i=i+1
6 continue
7 print 'Welcome you!'
8 i=i+1
9 else: 10 print "While over!"
运行结果:
1 >>>
2 Welcome you! 3 While……continue
4 Welcome you! 5 While……continue
6 Welcome you! 7 While over!
2,for语句:(见后续文章)
五,小结
本文介绍了Python的变量,输入输出函数,表达式,基本语句(分支和循环)等知识的相关使用,通过练习,应该对Python有一个初步的认识。