您的位置:首页 > 编程语言 > Python开发

Python基本语法

2015-11-16 20:02 761 查看
参考:http://www.jb51.net/article/50759.htm


1.注释:单行用#注释

多行用’‘’注释

2.

if :

elif :

else :

3.尽管变量不需要预先定义,但是要使用的时候,必须赋值,否则报错

  局部变量:

def f1():
x=12     #局部变量
print x
def f2():
y=13      #局部变量
print y
def f3():
print x       #错误:没有定义变量x,这与“不需要预先定义数据类型”不矛盾
print y
def main():
f1()
f2()
#f3()#变量报错
main()
print 'End2!'


  修改全局变量的值

def modifyGlobal():
global x              #全局变量定义
print 'write x =-1'
x=-1
def main():
# printLocalx()
# printLocaly()
# readGlobal()
modifyGlobal()

x=200
#y=100
print 'before modified global x=',x
main()
print 'after modified global x=',
print x


  循环语句:与C在表达上有区别,c有while与do……while形式;Python下:while与while……else……形式

for i in range(50, 100 + 1):
print i


i=1
while i<5:
print 'Welcome you!'
i=i+1


i=1
while i<5:
print 'Welcome you!'
i=i+1
else:
print "While over!"  #循环正常结束


注意:如果while非正常状态结束(即不按循环条件结束),则else语句不执行。

4.查看变量的类型函数type():

查看变量的内存地址函数id():

>>> type(1)
<type 'int'>
>>> type(12j+1)
<type 'complex'>


复数 complex

5.逗号运算符(,):可以实现连接字符串和数字型数据。

6.格式化控制符:%f浮点数;%s字符串;%d双精度浮点数(这和C的输出是一致的)。

>>> 'john %d %s %10s %.2f %-8s %lf' % (10,'asd','xsf',0.1234,'asdff',0
'john 10 asd        xsf 0.12 asdff    0.120000'
>>>


八进制与十六进制

>>> print '%d %x %X'% (10,10,10)
10 a A


7.输入函数raw_input(),raw_input()输入的均是字符型。

8.函数定义及其调用:

#define function:add (函数说明)
def add(x,y):  #函数头部,注意冒号,形参x,y
z=x+y           #函数体
return z        #返回值
#define main function
def main():
a=12
b=13
c=add(a,b)   #函数调用,实参a,b
print c
main()             #无参函数调用
print 'End1!'


注意:这部分与C的存在的异同在于:
  1,形参与实参的用法,无参函数,有参函数,默认参数等规则一致。
    如def add(x,y=2),调用可以是add(3)也可以是add(3,4),add(x,y=34)
  2,C的形参需要指定数据类型,而Python不需要。
  3,Python的返回值允许有多个。如:

def test(n1,n2):
print n1,
print n2
n=n1+n2
m=n1*n2
p=n1-n2
e=n1**n2
return n,m,p,e
print 'Entry programme1'
sum,multi,plus,powl=test(2,10)   #这个是C语言所没有的赋值方式
print 'sum=',sum
print 'multi=',multi
print 'plus=',plus
print 'powl=',powl
re=test(2,10)
print re                                #数据类型为:'tuple'
print re[0],re[1],re[2],re[3]
print 'End1!\n'


9.逻辑运算符

运算符描述例子
and所谓逻辑与运算符。如果两个操作数都为真,则条件为真。(a and b) 为 true.
or所谓逻辑OR运算符。如果有两个操作数都为非零,则条件变为真。(a or b) 为 true.
not所谓逻辑非运算符。用反转操作数的逻辑状态。如果条件为true,则逻辑非运算符将为false。not(a and b) 为 false.

示例:

试试下面的例子就明白了所有的Python编程语言提供了逻辑运算符:

#!/usr/bin/python

a = 10
b = 20
c = 0

if ( a and b ):
print "Line 1 - a and b are true"
else:
print "Line 1 - Either a is not true or b is not true"

if ( a or b ):
print "Line 2 - Either a is true or b is true or both are true"
else:
print "Line 2 - Neither a is true nor b is true"

a = 0
if ( a and b ):
print "Line 3 - a and b are true"
else:
print "Line 3 - Either a is not true or b is not true"

if ( a or b ):
print "Line 4 - Either a is true or b is true or both are true"
else:
print "Line 4 - Neither a is true nor b is true"

if not( a and b ):
print "Line 5 - Either a is not true or b is not true"
else:
print "Line 5 - a and b are true"

当执行上面的程序它会产生以下结果:

Line 1 - a and b are true
Line 2 - Either a is true or b is true or both are true
Line 3 - Either a is not true or b is not true
Line 4 - Either a is true or b is true or both are true
Line 5 - Either a is not true or b is not true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: