From this blog I will turn to Markdown for original writing.

Source: http://www.liaoxuefeng.com/

list

  • a list could be accessed using positive number (start from 0) in sequence or negative number in reverse sequence. Note that square brackets should be used here;
  • listname.append('...'): add ... as the last element in the list;
  • listname.insert(index, '...'): insert ... as the indexed element of the list;
  • listname.pop(): delete the last element in the list;
  • listname.pop(index): delete the last element in the list;
  • listname[inedx] = '...': replace the indexed element to ...;

    Note: The Python type list is like cell in Matlab, that the elements within one list are not necessarily the same type. The element in a list can even be another list.
  1. >>> example = ['a', 'b', ['c', 'd'], 'e'];
  2. >>> example[2][1]
  3. d
  4. # Defind a null list
  5. >>> L = [];
  6. >>> len(L)
  7. 0

tuple

  • a list whose elements cannot be changed once initialised. But pay attention that when defining a tuple, you should use round brackets (list: square brackets);
  1. # Defing a null tuple
  2. >>> t = ()
  3. # Defining a tuple with only one element
  4. >>> t = (1,)
  5. # If you defining like this:
  6. >>> t = (1)
  7. 1 # t is not a tuple, but an integer: 1
  • just like list, the elements in a tuple can be of different type, so that we could use list to construct an 'alterable tuple'.
  1. >>> t = ('a', 'b', ['A', 'B']);
  2. >>> t[2][0] = 'X';
  3. >>> t[2][1] = 'Y';
  4. >>> t
  5. ('a', 'b', ['X', 'Y'])

♥ if-else

  1. # - pay attention to the colon at the end of each judegmeng sentence;
  2. # - unlike Matlab, there is no *end* at the end of the structure.
  3. if <judgement 1>:
  4. <action 1>
  5. elif <judgement 2>:
  6. <action 2>
  7. elif <judgement 3>:
  8. <action 3>
  9. else:
  10. <action 4>

input

  • When using input(), be cautious about the data type obtain from input(), which is originally str. If number is needed, int() privides a way to convert string to integer.

♥ Loop

  • for...in
  1. >>> sum = 0
  2. >>> for x in range(5): # list(range(5)): [0, 1, 2, 3, 4]
  3. sum = sum + x
  4. >>> print(sum)
  5. 10
  • while

    End when the condition is not satisfied.
  1. >>> sum = 0
  2. >>> n = 99
  3. >>> while n > 0:
  4. sum = sum + n
  5. s = n - 2
  6. >>> print(sum)
  7. 2500

♥ dict

  • Abbreviation of dictionary, realising correspondance between multiple lists. With 'key-value' structure, dict could speed up the searching process;

  • operation examples:

  1. # Using following dict to replace the following two lists in one time:
  2. # names = ['Mary', 'Edith', 'Sybil']
  3. # birth_order = [1, 2, 3]
  4. >>> downton = {'Mary': 1, 'Edith': 2, 'Sybil': 3} # Using brace here
  5. >>> downton['Mary']
  6. 1
  7. # value assignment and obtainment
  8. >>> downton['Edith'] = 2
  9. >>> downton.get('Edith')
  10. 2
  11. >>> downton.get('Carson')
  12. # no return value if the key does not exist in the dict
  13. >>> downton.get('Cora', -1) # return -1 if 'Cora' is not found
  14. -1
  15. >>> downton.get('Edith', -1) # while if 'Edith' already exists, will
  16. # return the true value no matter what is
  17. # assigned
  18. 2
  19. #check if certain elements is in the dict
  20. >>> 'Mary' in d
  21. True
  22. # Value deletion
  23. >>> downton.pop('Sybil')
  24. 3
  25. >>> print(downton)
  26. {'Edith': 2, 'Mary': 1}
  • Note

    1 dict consumes a lot of RAM;

    2 keys in dict should be unchanged objects: string, integer are OK while list cannot be a key;

    3 The keys' storing order of dict is different from keys' assignment order;
  1. >>> downton = {'Mary': 1, 'Edith': 2, 'Sybil': 3} # Using brace here
  2. >>> downton['Mary']
  3. 1
  4. >>> print(downton)
  5. {'Edith': 2, 'Sybil': 3, 'Mary': 1}

set

  • Store keys in a non-repeat way(without values);
  1. # A list should be provided as input to initialise a set
  2. >>> s = set([1, 1, 2, 2, 3, 4])
  3. >>> s
  4. {1, 2, 3} # non-repeat keys
  • set_name.add(...): add ... into a set;

  • set_name.remove(...):remove certain key from a sey;

  • Note

    1 like dict, keys in set should be unchanged objects;

    2 advantage of set: good for set operation for its out-of-order and non-repeat nature.

Meet Python: little notes 2的更多相关文章

  1. Meet Python: little notes 3 - function

    Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...

  2. Meet Python: little notes

    Source: http://www.liaoxuefeng.com/ ❤ Escape character: '\' - '\n': newline; - '\t': tab; - '\\': \; ...

  3. Meet python: little notes 4 - high-level characteristics

    Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...

  4. python 100day notes(2)

    python 100day notes(2) str str2 = 'abc123456' print(str1.endswith('!')) # True # 将字符串以指定的宽度居中并在两侧填充指 ...

  5. 70个注意的Python小Notes

    Python读书笔记:70个注意的小Notes 作者:白宁超 2018年7月9日10:58:18 摘要:在阅读python相关书籍中,对其进行简单的笔记纪要.旨在注意一些细节问题,在今后项目中灵活运用 ...

  6. [Python Study Notes]匿名函数

    Python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的.在Python中,lambda作 ...

  7. [Python Study Notes]字符串处理技巧(持续更新)

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  8. [Python Study Notes]with的使用

    在 Python 2.5 中, with 关键字被加入.它将常用的 try ... except ... finally ... 模式很方便的被复用.看一个最经典的例子: with open('fil ...

  9. [Python Study Notes]实现对键盘控制与监控

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

随机推荐

  1. iOS中的过期方法和新的替代方法

    关于iOS中的过期方法和新的替代方法 1.获取某些类的UINavigationBar的统一外观并设置UINavigationbar的背景 注:方法名改了但是基本使用方法不变 + (instancety ...

  2. [转]Json转换神器之Google Gson的使用

    这几天,因为项目的需要,接触了Google的Gson库,发现这个东西很好用,遂记下简单的笔记,供以后参考.至于Gson是干什么的,有什么优点,请各位同学自行百度.话不多说,切入正题: 1. 下载Gso ...

  3. post请求报文

    POST /02_WEB_HTTP/index.html HTTP/1.1 Accept: application/x-ms-application, image/jpeg, application/ ...

  4. 【转】OpenStack和Docker、ServerLess能不能决定云计算胜负吗?

    还记得在十多年前,SaaS鼻祖SalesForce喊出的口号『No Software』吗?SalesForce在这个口号声中开创了SaaS行业,并成为当今市值460亿美元的SaaS之王.今天谈谈『No ...

  5. javascript中,对于this指向的浅见

    # this的指向在函数创建的时候确定不了.只有在执行的时候,才可以确定. ## 1 . 这里的this指向window window.fn(); 所以this.user是undefined func ...

  6. yii2 GridView 下拉搜索实现案例教程

    作者:白狼 出处:http://www.manks.top/article/yii2_gridview_dropdown_search本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章 ...

  7. 联通烽火hg220桥接tplink路由器

    转载请注明:from http://www.cnblogs.com/juandx/p/5544517.html 1.普通账户:user(7fnmmnml,默认是bdettbr0),猫背后可见 2.工程 ...

  8. Asp.net MVC验证哪些事(2)-- 验证规则总结以及使用

    上篇文章Asp.net MVC验证那些事(1)-- 介绍和验证规则使用中,介绍了Asp.net MVC中的验证功能以及如何使用.这里将对MVC中内置的验证规则进行总结. 一,查找所有验证规则 上篇文章 ...

  9. java开发中的23中设计模式详解--大话设计模式

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  10. Redis基本配置

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...