python常见语句汇总

python教程评论684 views阅读模式

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

相关推荐:《python视频》

python常见语句汇总

python常用语句:

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

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

(2)、调用:执行函数

log.write('spam,name')

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

print ('abc')

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

if 'iplaypython' in text:
    print (text)

(4)for、else:序列迭代

for x in list:
    print x

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

while a > b :
    print 'hello'

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

while True:
    pass

(7)、break:退出循环

while True:
    if exit:
        break

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

while True:
    if skip:
        continue

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

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

def info(a,b,c=3,*d):
    print (a+b+c+d[1])

(10)、return:函数返回值

def info(a,b,c=3,*d):
    return a+b+c+d[1]

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

x = 'a'
def function():
    global x ; x = 'b'

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

import sys

(13)、from:属性访问

from sys import stdin

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

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 捕获异常

try:
    action()
except:
    print ('action error')

(16)、raise:触发异常

raise Endsearch (location)

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

assert a>b ,'a roo small'

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

企鹅博客
  • 本文由 发表于 2019年9月11日 11:53:55
  • 转载请务必保留本文链接:https://www.qieseo.com/350691.html

发表评论