python基本语句大全_python常见语句汇总

在我们学习python的过程中,会遇到一些我们经常遇到的语句,下面我将这些语句盘点一下,方便大家记忆。

194674644_1_20200705105803552.jpg

python常用语句:

(1)、赋值:创建变量引用值1a,b,c='aa','bb','cc'

(2)、调用:执行函数1log.write('spam,name')

打印、输出:调用打印对象,print 语句1print ('abc')

(3)if、elif、else:选择条件语句,if语句、else与elif语句1

2if 'iplaypython' in text:

print (text)

(4)for、else:序列迭代1

2for x in list:

print x

(5)、while、else:一般循环语句1

2while a > b :

print 'hello'

(6)、pass:占位符(空)1

2while True:

pass

(7)、break:退出循环1

2

3while True:

if exit:

break

(8)、continue:跳过当前循环,循环继续1

2

3while True:

if skip:

continue

下面的语句是相对比较大的程序中会用到,有关函数、类、异常以及模块,同样只是简单介绍方法用途和格式。

(9)、def:定义函数和方法1

2def info(a,b,c=3,*d):

print (a+b+c+d[1])

(10)、return:函数返回值1

2def info(a,b,c=3,*d):

return a+b+c+d[1]

(11)、python global:命名空间、作用域1

2

3x ='a'

deffunction():

global x ; x ='b'

(12)、import:访问、导入模块1import sys

(13)、from:属性访问1from sys import stdin

(14)、class:创建对象,类class定义方法1

2

3

4

5

6

7

8

9

10

11

12

13class Person:

def setName(self,name):

self.name = name

def getName(self):

return self.name

def greet(self):

print 'hello,%s' % self.nameclass Person:

def setName(self,name):

self.name = name

def getName(self):

return self.name

def greet(self):

print 'hello,%s' % self.names

(15)、try、except、finally:python 捕获异常1

2

3

4try:

action()

except:

print ('action error')

(16)、raise:触发异常1raise Endsearch (location)

(17)、assert:python 断言、检查调试1assert a>b ,'a roo small'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值