[原创] Learning in Python:Chapter 4 Introducing Python Object Types

Chapter 4: Introducing Python Object Types

 
 
1. Build-in Types:
 

        
 
2. Numbers:
  • Python support big numbers
  • Modules: math, random
3. String:
  • String is a Sequence
  • Sequence operation:
    • len(s)
    • s[1], s[-1]
    • s[1:3], s[:3], s[1:], s[:-1]
    • s + 'abc'
    • s*8
  • Type-Specific methods:
    • s.find('abc')
    • s.replace('abc','bbb')
    • s.split(',')
    • s.upper()
    • s.isalpha(), s.isdigit()
    • s.rstrip()
    • Formating:
      • '%s hello, %s world' % ('I', 'python')
      • '{0} hello, {1} world'.format('I','python')
      • '{0:o}, {1:x}, {2:b}'.format(64, 64, 64)
  • String is Immutable (not changing the original string, but creating new one), but can be used to make new strings.
  • numbers, strings, and tuples are immutable; lists and dictionaries are not
  • \0 does not teminates a string
  • """  """ for mutiple-line string. r'' for raw string
  • pattern matching:
    • module:  re
3. Getting help
  • dir(s), help(s.replace)
 
4. List
  • Mutable, Sequence
  • Tpye-Specific Operations:
    • L.append()
    • L.pop(2)    # pop out the second last item
    • L.insert()
    • L.remove()
    • L.sort()
    • L.reverse()
  • Comprehension:
    • [row[1] + 1 for row in M]
    • [row[1] + 1 for row in M if row[1]%2 == 0]
    • {ord(x) for x in 'spaam'}
    • {x:ord(x) for x in 'spaam'}
  • Generator
    • G = ( sum(row) for row in M )
    • next(G)
5. Dictinary
  • Mapping (no order), mutable
  • Type-specific operations:
    • D.keys()
    • sorted(D)  The Dictionary can be ordered.
  • Iteration Protocol: a physically stored sequence in memory.
  • Time optimization module: time,  timeit, profile
  • Missing Key:
    • if a in D
    • get: value = D.get('x', 0)
    • if/else expression: value = D['x'] if 'x' in D else 0
6. Tuples
  • A list that cannot be changed: immutable
  • Type-specific methods:
    • T.index(4)   return the index of 4
    • T.count(4)   return the exsit times of 4
7. Files
  • file = open("abc.txt","w")
    • w: clear/create a file for writing
  • file.write()
  • Read:
    • file.read(), read the entire file
    • file.readline(), read one line at a time
  • file.seek()
8.set
  • X = set('spam')
  • Y = {'h', 'a', 'm'}
  • >>> X, Y
  • >>> X & Y # Intersection
  • >>> X | Y # Union
  • >>> X – Y # Difference
  • >>> {x ** 2 for x in [1, 2, 3, 4]} # Set comprehensions in 3.0
9.  type build-in function
  • type()    return the type of a object
  • isinstance(L, list)

转载于:https://www.cnblogs.com/icemoon1987/archive/2012/09/29/2708916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值