怎么用python语法1234_Python基本语法

参考:http://www.jb51.net/article/50759.htm

1.注释:单行用#注释

多行用’‘’注释

2.

if  :

elif :

else :

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

局部变量:

deff1():

x=12 #局部变量

printxdeff2():

y=13 #局部变量

printydeff3():print x #错误:没有定义变量x,这与“不需要预先定义数据类型”不矛盾

printydefmain():

f1()

f2()#f3()#变量报错

main()print 'End2!'

修改全局变量的值

defmodifyGlobal():global x #全局变量定义

print 'write x =-1'x=-1

defmain():#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(12j+1)

复数 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

defmain():

a=12b=13c=add(a,b) #函数调用,实参a,b

printc

main()#无参函数调用

print 'End1!'

注意:这部分与C的存在的异同在于:

1,形参与实参的用法,无参函数,有参函数,默认参数等规则一致。

如def add(x,y=2),调用可以是add(3)也可以是add(3,4),add(x,y=34)

2,C的形参需要指定数据类型,而Python不需要。

3,Python的返回值允许有多个。如:

deftest(n1,n2):printn1,printn2

n=n1+n2

m=n1*n2

p=n1-n2

e=n1**n2returnn,m,p,eprint 'Entry programme1'sum,multi,plus,powl=test(2,10) #这个是C语言所没有的赋值方式

print 'sum=',sumprint 'multi=',multiprint 'plus=',plusprint '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/pythona =10b =20c =0if(a andb ):print"Line 1 - a and b are true"else:print"Line 1 - Either a is not true or b is not true"if(a orb ):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 =0if(a andb ):print"Line 3 - a and b are true"else:print"Line 3 - Either a is not true or b is not true"if(a orb ):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"ifnot(a andb ):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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值