python基础[1]

来源:互联网 发布:mac修改用户名无法登录 编辑:程序博客网 时间:2024/06/05 17:52

#用于注释

使用import导入模块:

    >>> import math

    可以使用math中的定义的类及函数,如

    >>> math.sqrt(10)

    >>> from math import sqrt

    从math中引入sqrt,于是可以直接使用sqrt:

    >>> sqrt(10

    >>> from math import *   --> 引入math中的所有内容

    为引入的函数提供别名

    >>> from module1 import f as f1

    >>> from module2 import f as f2


    >>> print 'cheng'    ->    cheng

    >>> print 'cheng' "yu"   ->    chengyu (即直接将两个字符串拼接)

    >>> print 'cheng' 10     ->    出错,要拼接字符串与数值可用如下方法:

    >>> print 'cheng' `10`   ->   cheng10, 其中10被反引号`括起来,``将其括起来的式子求值并返回成字符串类型

    >>> a = 10

    >>> print 'cheng' `a`    ->    cheng10

repr函数返回对象的标准字符串形式(representation),``作用和其一样

raw_input函数从标准输入中读取一个字符串,去除尾部换行符,EOF(unix击Ctrl+D,Windows下击Ctrl-Z+Return),产生EOFError错误

input函数相当于eval(raw_input([prompt])),所以它的输入应该是合法的python表达式


列表(list):

    >>> a = ['cheng', 10, ['in', 'side', 'a', 'list'],999]   -> 列表

    >>> a[0]    ->    'cheng'

    >>> a[-1]    ->    999

    分片操作

    >>> a[n1:n2]  -> 从n1索引的元素到n2索引的元素的前一个元素构成的表, n1被省略表示从索引为0的元素开始,n2索引的元素在n1索引的元素前面则返回[],空表

    >>> a[n1:n2:step]    ->   step表示步长

    >>> a[-1:1]    ->  [999, 'cheng']

    >>> list1 + list2  ->   返回将list1和list2拼接的表

    >>> [None]*10   ->   返回包含长为10的空表


   >>> 10 in a    ->    True, 因为a含有成员10

   >>> list('holy')  ->   ['h', 'o', 'l', 'y']  字符串类型转换为列表类型

   >>> ','.join[a_list]   ->  a_list是一个字符串组成的列表(或字符串),该式子的结果是一个字符串---由列表各成员(或字符串中的每个字符号)通过','连接而成

   >>> a[1] = 'what ever'  -> 对a[1]进行赋值

   >>> del a[1]  ->  删除a[1]

   分片赋值, 如a = [0, 1, 2, 3, 4, 5, 6]

    >>> a[1:3] = [2]  -> a = [0,2,4,5,6]

列表方法

   append, extend, index, count, insert, pop, remove, reverse, sort

   sort有三个参数,均有自己的默认值sort(cmp=None, Key=None, reverse=False)

          其中cmp(x,y)返回1表示x‘大于’y,即表示排序后,x在y的右边


元组(tuple):

    >>> a = (1, 2, 3)   ->   返回元组,它与list类似,但组成元素不能被赋值,list的一些方法它没有


字符串:

    >>> print 'C:\nowhere'    ->    其中\n被当作换行符,若想要C:\nowhere(如作为windows下路径名时),则需要在\n前面在加一个转义符\, 也可以:

    >>> print r'C:\nowhere'   ->   在字符串前面加r,表示raw(原始未加工的)

    >>> u'Helloworld'   ->  字符串前加u,表示该字符串使用Unicode编码,python中普通字符串以8位ASCII码存储,Unicode以16位存储

字符串格式化

   >>> print 'dadada %d' % 10   ->   dadada 10

   >>> a = '%s world, %d'

   >>> print a % ('hello', 999)    ->   hello world, 999

字符串方法:

    find:查找子串

    join, split,

    lower

    replace, translate

    strip


字典(dict):

    >>> items = {'name1':age1, 'name2':age2}

    >>> items['name1]    ->   age1

    del items['name2']删除键值为'name2'的项

    >>> data = {'name1':'dada', 'age1':100}

字典在格式化字符串中的应用

    >>> print """the age of %(name1)s is %{age1}d"""    ->    the age of dada is 100

dict的方法:

    clear, has_key, items, iteritems, key, iterkeys, value, itervalues, pop, popitem, setdefault, update

    copy: 浅复制(shallow copy) 如b = a.copy(), 若a中包含一个列表,则b中的并没有得到一个该列表的副本,而是直接指向与a中相同的列表

    >>> a = {'l1': [1, 2, 3], 'l2': [6, 7], 'n1': 1, 'n2': 2}

    >>> b = a.copy()

    >>> a['l1'].remove(2)    ->  a = b = {'l1': [1, 3], 'l2': [6, 7], 'n1': 1, 'n2': 2}

    与之对应的在copy包中包含一个deep copy   (from copy import deepcopy)

    fromkeys:用给定的键建立新的字典,每个键默认对应的值为None

    get:查询字典中key对应的value, key不存在返回None(可通过可选的第二个参数修改这个默认值)


比较运算符:

    <, > , <=, >=, ==, !=

    x is y : x和y是同一个对象, x is not y: x和y不是同一个对象

    x in y: x是y容器的成员    ,  x not in y: x不是容器的成员

    >>> x = y = [1, 2, 3]

    >>> z = [1, 2, 3]

     >>> x == y    ->    True

     >>> x == z    ->    True

     >>> x is y      ->   True

     >>> x is z      ->   False


    pass: 什么都不做,用来占位

    del :  移除对一个对象的引用和该名字本身

    >>> x = y = [1,2,4]

    >>> del x

    移除了x这个名字和它对该list的引用,由于y对该列表的引用仍然存在,所以该list不会被python的垃圾收集所回收

   exec: 将所给的字符串参数当作python语句执行(并不返回对象)

   >>> scope = {}

   >>> exec 'sqrt = 1' in scope    ->   字典scope作为命名空间用,sqrt=1这个赋值保存在字典内

    eval: 执行一条python表达式,将返回该表达式的值

0 0
原创粉丝点击