note of code in python

1. different of assignment variable and list
    for variable: just copy the content of the other
    like below:

点击(此处)折叠或打开

  1. >>> var1 = 'hello'
  2. >>> var2 = var1
  3. >>> var1 = 'world'
  4. >>> var1
  5. 'world'
  6. >>> var2
  7. 'hello'
    for list: copy the address of the other
    like below:

点击(此处)折叠或打开

  1. >>> list1 = []
  2. >>> nested = [list1, list1, list1]
  3. >>> nested
  4. [[], [], []]
  5. >>> nested[1].append('Hello ')
  6. >>> nested
  7. [['Hello '], ['Hello '], ['Hello ']]
  8. >>> list1.append('world!!')
  9. >>> nested
  10. [['Hello ', 'world!!'], ['Hello ', 'world!!'], ['Hello ', 'world!!']]
2. contrast from string\list\pair
    the difference like the code below:

点击(此处)折叠或打开

  1. strings = 'I am a good student!!'
  2. >>> lines = ['zhang', 'pei', 'hua', 'hello', 'world']
  3. >>> pairs = (6, 'dog', 'cat')
  4. >>> strings[2], lines[2], pairs[2]
  5. ('a', 'hua', 'cat')
  6. >>> len(strings), len(lines), len(pairs)
  7. (21, 5, 3)
3. the defence style function
    prevent unreasonable input and suggest reasonable solution. like below function the argument  must be string,if not return warn.
    

点击(此处)折叠或打开

  1. >>> def tag(word):
  2. ... assert isinstance(word, basestring), "argument \
  3. ... to tag() must be a string"
  4. ... if word in ['a', 'the', 'all']:
  5. ... return 'det'
  6. ... else:
  7. ... return 'noun'
  8. ...
  9. >>> tag('hello')
  10. 'noun'
  11. >>> tag(['hello'])
  12. Traceback (most recent call last):
  13.   File "<stdin>", line 1, in <module>
  14.   File "<stdin>", line 3, in tag
  15. AssertionError: argument     to tag() must be a string
4. dictionary in python like hash pair in other programming language
    the character reflected in below code:

点击(此处)折叠或打开

  1. >>> pos = {'colorless':'ADJ', 'ideas':'N', 'sleep':'V'}
  2. >>> pos
  3. {'sleep': 'V', 'ideas': 'N', 'colorless': 'ADJ'}
  4. >>> pos2 = dict(colorless='ADJ', ideas='N', sleep='V')
  5. >>> pos2
  6. {'sleep': 'V', 'ideas': 'N', 'colorless': 'ADJ'}
  7. >>> pos['sleep']
  8. 'V'
  9. >>> pos['sleep'] = ['N','V']
  10. >>> pos
  11. {'sleep': ['N', 'V'], 'ideas': 'N', 'colorless': 'ADJ'}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值