python常见语句汇总


本文摘自php中文网,作者爱喝马黛茶的安东尼,侵删。

在我们学习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常见语句汇总的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python语言有哪两种编程方式

Python中list可以修改吗

Python的三种程序结构是什么

Python中的map怎么使用

Python用什么编译器

Python导入csv文件出现syntaxerror问题分析_Python

Python多进程的用法示例(代码)

Python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别

Python list和tuple的区别

什么是Python自动化

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...