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.
>>> example = ['a', 'b', ['c', 'd'], 'e'];
>>> example[2][1]
d # Defind a null list
>>> L = [];
>>> len(L)
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);
# Defing a null tuple
>>> t = () # Defining a tuple with only one element
>>> t = (1,) # If you defining like this:
>>> t = (1)
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'.
>>> t = ('a', 'b', ['A', 'B']);
>>> t[2][0] = 'X';
>>> t[2][1] = 'Y';
>>> t
('a', 'b', ['X', 'Y'])

♥ if-else

  # - pay attention to the colon at the end of each judegmeng sentence;
# - unlike Matlab, there is no *end* at the end of the structure.
if <judgement 1>:
<action 1>
elif <judgement 2>:
<action 2>
elif <judgement 3>:
<action 3>
else:
<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
>>> sum = 0
>>> for x in range(5): # list(range(5)): [0, 1, 2, 3, 4]
sum = sum + x
>>> print(sum)
10
  • while

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

♥ dict

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

  • operation examples:

# Using following dict to replace the following two lists in one time:
# names = ['Mary', 'Edith', 'Sybil']
# birth_order = [1, 2, 3]
>>> downton = {'Mary': 1, 'Edith': 2, 'Sybil': 3} # Using brace here
>>> downton['Mary']
1 # value assignment and obtainment
>>> downton['Edith'] = 2
>>> downton.get('Edith')
2
>>> downton.get('Carson') # no return value if the key does not exist in the dict
>>> downton.get('Cora', -1) # return -1 if 'Cora' is not found
-1
>>> downton.get('Edith', -1) # while if 'Edith' already exists, will
# return the true value no matter what is
# assigned
2 #check if certain elements is in the dict
>>> 'Mary' in d
True
# Value deletion
>>> downton.pop('Sybil')
3
>>> print(downton)
{'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;
>>> downton = {'Mary': 1, 'Edith': 2, 'Sybil': 3}   # Using brace here
>>> downton['Mary']
1
>>> print(downton)
{'Edith': 2, 'Sybil': 3, 'Mary': 1}

set

  • Store keys in a non-repeat way(without values);
# A list should be provided as input to initialise a set
>>> s = set([1, 1, 2, 2, 3, 4])
>>> s
{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. Android 使用SoundPool播放音效

    在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高.延迟时间较长.不支持多个音频同时播放等.这些缺点决定了MediaP ...

  2. redis配置文件参数说明及命令操作

    redis下载地址:https://github.com/MSOpenTech/redis/releases. Redis 的配置文件位于 Redis 安装目录下,文件名为redis.windows. ...

  3. Mac下修改Hosts文件工具——Gas Mask

    这段时间在做公司APP的项目,看到公司开发IOS的同事和我这边联调程序时,经常需要手动修改hosts文件,比较麻烦. 在公司忙,没有来及找,给同事推荐了我当时知道的一个切换hosts文件的工具:sma ...

  4. Linux Shell 网络层监控脚本(监控包括:连接数、句柄数及根据监控反馈结果分析)

    脚本监控: 获取最大句柄数的进程: 链接分析: 脚本片段: case "$handle" in 2) echo "The handle of the process : ...

  5. 2.1 CMMI2级——7个PA简述

    摘要: 阶段式的CMMI没有1级,最开始的级别就是2级.一个处于“无序化”生产的软件公司,要进行过程改进,首要是改进什么呢?2级告诉你,我们需要从计划.计划跟踪.需求管理.采购.度量.配置管理.质量保 ...

  6. EMC Documentum DQL整理(一)

    1.Get user SELECT * FROM dm_user WHERE r_is_group = 0   2.Get Group SELECT * FROM dm_group WHERE gro ...

  7. 网络编程4--毕向东java基础教程视频学习笔记

    Day24 06 自定义浏览器-Tomcat服务端07 自定义图形界面浏览器-Tomcat服务端08 URL-URLConnection09 小知识点10 域名解析 06 自定义浏览器-Tomcat服 ...

  8. 【英文版本】Android开源项目分类汇总

    Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassActionBar v7 appcompat library ...

  9. C#编写抽奖问题

    输入每个人的中奖号码,进行滚动显示    //清屏  //随即一个索引   // 根据索引打印  //等待0.1秒            Console.Write("请输入参与者人数:&q ...

  10. java 读写word java 动态写入 模板文件

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...