美文网首页
Python_语言基础

Python_语言基础

作者: 738bc070cd74 | 来源:发表于2016-03-21 21:54 被阅读27次

    数据类型

    a=10         # int 整数
    a=1.3        # float 浮点数
    a=True       # 真值 (True/False)
    a='Hello!'   # 字符串
    

    以上是最常用的数据类型,对于字符串来说,也可以用双引号。
    变量不需要声明,不需要删除,可以直接回收适用。

    type(): 查询数据类型
    

    sequence 序列

    sequence 序列是一组有序对象的集合,序列元素的下标从0开始

    • list
      基本引用方式

        s1[1]
      

    范围引用: 基本样式[下限:上限:步长],上限本身不包括在内
    print s1[:5] # 从开始到下标4 (下标5的元素 不包括在内)
    print s1[2:] # 从下标2到最后
    print s1[0:5:2] # 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素)
    print s1[2:0:-1] # 从下标2到下标1

    • tuple (元组)
      一旦建立,tuple的各个元素不可再变更,而list的各个元素可以再变更。
      一个序列作为另一个序列的元素
      字符串是一种特殊的元素,因此可以执行元组的相关操作。

    运算

    数学 +, -, *, /, **, %
    判断 ==, !=, >, >=, <, <=, in
    逻辑 and, or, not

    缩进

    if语句之后的冒号
    以四个空格的缩进来表示隶属关系, Python中不能随意缩进

    if  <条件1>:
        statement
    elif <条件2>:
         statement
    elif <条件3>:
         statement
    else:
        statement
    

    for 循环

    for 元素 in 序列: 
        statement
    

    函数

    def function_name(a,b,c): 
        statement return something  # return不是必须的
    

    return可以返回多个值,以逗号分隔。相当于返回一个tuple(定值表)

    面向对象

    class Bird(object): 
        have_feather = True 
        way_of_reproduction = 'egg'
        def move(self, dx, dy): #方法需要传递 self 属性
            position = [0,0]
            position[0] = position[0] + dx 
            position[1] = position[1] + dy
            return position
    

    Bird 继承自 object

    __init__()方法 ,对象创建时会自动调用,相当于构造函数,可以传递参数
    

    dir()用来查询一个类或者对象所有属性

    print dir(list)
    

    help()用来查询的说明文档

    print help(list)

    相关文章

      网友评论

          本文标题:Python_语言基础

          本文链接:https://www.haomeiwen.com/subject/rubklttx.html