Python 学习笔记

一. 组织行

        #!/usr/bin/python

    告诉 Linux 系统,当执行该程序时,应该运行哪个解释器。


二. 字符串

1. 单引号和双引号完全相同

2. 三引号,在三引号中可以自由的使用单引号和双引号

3. 自然字符串:给字符串加上 r 或者 R 前缀,则字符串中不再转义,在处理正则表达式时非常方便。


三. 变量

1. 变量的第一个字符必须是字符或者下划线,所以 2b 不是一个合法的变量

2. 大小写敏感


四. 控制流

1. if:

    elif:

    else:

2. while True:

     else:

3. for i in range(1, 5):

     else:


五. 函数

1. def func(a, b=5, c=12):

        print 'a is', a, ‘b is’, b, 'c is', c

2. global

    函数内的局部变量默认会覆盖同名的全局变量,使用 global 关键字表明使用的是全局变量

3. DocStrings:函数第一个逻辑行中的字符串,同样适用于模块和类

    惯例是一个多行字符串,首行以大写字母开始,句号结尾,第二行是空行,从第三行开始是详细描述。

    使用函数的 __doc__ 属性即可展示该函数的 DocString


六. 模块

1. import sys

    sys.argv

2. 每个 python 模块都有它的 __name__,如果它是 __main__,说明这个模块被用户单独运行

3. dir(sys)


七. 数据结构

1. 列表

    ['one', 'two', 'three', 'four']

    列表是可变的

2. 元组

    ('one', 'two', 'three', 'four')

    元组不可变,通过数组下标访问。最常用的用法是用在打印语句中:

        name='test‘

        age=22

        print '%s is %d years old' % (name, age)

3. 字典

    ab = { 'key1': 'value1',

               'key2': 'value2',

             }

    print ab['key1']

    for name, address in ab.items()

        print 'Contact %s at %s' % (name, address)

4. 序列

    shoplist = ['apple', 'mango', 'carrot', 'banana']

    索引操作符:

    print 'Item 0 is', shoplist[0]

    print 'Item 1 is', shoplist[1]

    print 'Item 2 is', shoplist[2]

    print 'Item 3 is', shoplist[3]

    print 'Item -1 is', shoplist[-1]    # 索引同样可以是负数,位置是从序列尾开始计算的,-1 表示最后一个元素

    print 'Item -2 is', shoplist[-2]

    切片操作符

    print 'Item 1 to 3 is', shoplist[1:3]

    print 'Item 2 to end is', shoplist[2:]

    print 'Item 1 to -1 is', shoplist[1:-1]

    print 'Item start to end is', shoplist[:] # 返回整个序列的拷贝

5. 对象和参考

    列表的赋值语句不创建拷贝:

    shoplist = ['apple', 'mango', 'carrot', 'banana']

    mylist = shoplist        # mylist is just another name pointing to the same object!  如果 shoplist 发生变法,则 mylist 同样变化

    使用切片操作符来建立序列的拷贝:

    mylist = shoplist[:]     # make a copy by doing a full slice


八. 面向对象


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值