分享

python常见语句汇总

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

相关推荐:《python视频

python常用语句:

(1)、赋值:创建变量引用值

1

a,b,c='aa','bb','cc'

(2)、调用:执行函数

1

log.write('spam,name')

打印、输出:调用打印对象,print 语句

1

print ('abc')

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

1

2

if 'iplaypython' in text:

    print (text)

(4)for、else:序列迭代

1

2

for x in list:

    print x

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

1

2

while a > b :

    print 'hello'

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

1

2

while True:

    pass

(7)、break:退出循环

1

2

3

while True:

    if exit:

        break

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

1

2

3

while True:

    if skip:

        continue

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

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

1

2

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

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

(10)、return:函数返回值

1

2

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

    return a+b+c+d[1]

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

1

2

3

x = 'a'

def function():

    global x ; x = 'b'

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

1

import sys

(13)、from:属性访问

1

from sys import stdin

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

1

2

3

4

5

6

7

8

9

10

11

12

13

class 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

4

try:

    action()

except:

    print ('action error')

(16)、raise:触发异常

1

raise Endsearch (location)

(17)、assert:python 断言、检查调试

1

assert a>b ,'a roo small'

以上就是python常见语句汇总的详细内容,更多请关注php中文网其它相关文章!

本文原创发布php中文网,转载请注明出处,感谢您的尊重!
专题推荐:python 常见语句

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多