Python 的学习脚印(1)

 python 的学习脚印
 
 (1) 整数 与 浮点数 (float)
 (2) 永远的执行整除 //
 (3) 长整型数  (末尾加个 L ), 处理比较大的数,很好
 (4) 十六进制和八进制
     >>> 0xAF
     175
     >>> 010
     8
 (5) py3.0 之后, print 升级为了函数, 如 : 你应该 print(42)  而不是 print 42
 (6) 函数input,调用时,会返回一个值(象许多其它函数一样)。  
       你不一定非要使用这个值,但象我们这种情况,我们要使用它。这样,下面这两个表达式有着很大的差别: 
       foo = input
                     bar = input()
       foo现在包含input函数本身(所以它事实上可以象foo("What is your age?")这样使用;这被称为动态函数调用)而bar包含用户键入的值。 
     说明 : 1) input()        是内建函数
            2) pow(2, 3)        内建函数
            3) abs(-10)         内建函数
            4) round(1.0/2.0) 会把浮点数四舍五入为最接近的整数值。
 (7) 导入模块
       >>> import math
       >>> math.floor(32.9)
       32.0
       或者 {
         >>> from math import sqrt
         >>> sqrt(9)
         3.0
       }
                                  使用变量来引用函数 foo=math.sqrt (略)
 (8) 拼接字符串用 + 
 (9) 字符串 (3种方法可以转化之) str, repr, 反引号。 print 与 不用 print
      str  函数会把值转换为合理形式的字符串,以便用户可以理解。
      repr 会创建一个字符串,它以合法的python表达式形式来表示值
 (10) input  与 raw_input 的区别。 (叮嘱 : 要尽可能的使用 raw_input )
 (11) 最基本的数据结构 : 序列 『6种内建序列 - *元组和列表*』 元组vs列表 元组不能更改!
      元组 做 字典的key,不能用列表!
 (12) 序列分片,乘法,成员资格
 (13) 长度,最小值,最大值  len, min, max
 (14) list函数。 其实字符串很有时候不能像列表一样被修改。>>> list('hello')
 (15) 删除元素 del arr[2]
 (16) 分片赋值 {
         >>> name = list('Perl')
         >>> name[1:] = list('ython')
         >>> name
         ['P', 'y', 't', 'h', 'o', 'n']
 (17) 列表方法 - 是方法
       1, append                 7,remove
       2, count                  8, reverse
       3, extend                 9, sort [非常好]
       4, index                 10, >>> sorted('python') 返回值是列表
       5, insert
       6, pop
 (18) >>> y = x[:]     (x 的副本赋值给 y)
 (19) 高级排序
      >>> cmp(42, 39)  
      1
      >>> 自定义比较函数 (留下以后学)
 (20) >>> 1, 2, 3
      (1, 2, 3)  自动创建了元组
      >>> 3*(40+2,)
      (42, 42, 42)
    * tuple 函数 * 功能是 :序列转化为元组
      >>> tuple([1, 2, 3])
      (1, 2, 3)
      >>> tuple('abc')
      ('a', 'b', 'c')
      >>> x = 1, 2, 3  基本元组操作
      >>> x[1]
      2
 第三章 使用字符串
    字符串的格式化
  >>> format = "Hello, %s. %s enough for ya?"  # %s  
  >>> values = ('world', 'Hot')  
  >>> print format %values  
  Hello, world. Hot enough for ya?  


>>> title = "Hello_world good"  
>>> title.find('llo')  
2  
>>>   
 *2 join 是 split 的逆方法  
>>> seq = ['1', '2', '3', '4', '5']  
>>> fu = '+'  
>>> fu.join(seq)


  (22) find 返回索引
  (23) lower 返回字符串的小写字母版  'string'.lower()
                                     'string'.replace('src', 'dst') src 被替换
       split    -->      >>> '1+2+3+4'.split('+')
                         ['1', '2', '3', '4', '5']
                         >>> 'string'.split() 默认(空格, 制表,换行)
       strip
   (24) 字典的开始
>>> phonebook = {'Alice': '2341', 'Beth': '9102', 'Ceil': '3258'}
>>> phonebook
{'Beth': '9102', 'Alice': '2341', 'Ceil': '3258'}
>>> phonebook['Beth']
'9102'
>>> items = [('name', 'Gumby'), ('age', 42)]
>>> d = dict(items)
>>> d
{'age': 42, 'name': 'Gumby'}
>>> d = dict(name='Gumby', age=42)
>>> d
{'age': 42, 'name': 'Gumby'}
   (25) dict, list, tuple, str 函数。默认都是返回空的相应类型
   字典方法 {
       1, clear 2, copy 3, fromkeys 使用给定的键建立新的字典 4, get 5, has_key 6, items 方法将所有的字典项以列表方式返回
       7, keys 将字典中的键以列表形式返回  8, pop 方法用来获得对应于给定键的值, d.pop('x') 并弹出
       9, popitem 方法类似于 list.pop, 随机弹出
       10, setdefault  11, update 利用一个字典项更新另外一个字典
       12, values() 返回字典中的值的列表
 copy() 浅拷贝与深拷贝  
>>> from copy import deepcopy             
>>> d = {}
>>> d['names'] = ['Alfred', 'Beth']
>>> c = d.copy()
>>> dc = deepcopy(d)
>>> d['names'].append('Clive')
>>> c
{'names': ['Alfred', 'Beth', 'Clive']}
>>> dc
{'names': ['Alfred', 'Beth']}
    (26) 条件,循环和其他语句
          print 1, 2, ‘abc’    # 可以打印多条语句
     (27) 序列解包
           >>> values = 1, 2, 3
           >>> values
           (1, 2, 3)
           >>> x, y = y, x 交换值
           
           
       
       
  
       
  
 
 
 


原文地址:https://www.cnblogs.com/robbychan/p/3786660.html