Meet Python: little notes 2
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的更多相关文章
- Meet Python: little notes 3 - function
Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...
- Meet Python: little notes
Source: http://www.liaoxuefeng.com/ ❤ Escape character: '\' - '\n': newline; - '\t': tab; - '\\': \; ...
- Meet python: little notes 4 - high-level characteristics
Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...
- python 100day notes(2)
python 100day notes(2) str str2 = 'abc123456' print(str1.endswith('!')) # True # 将字符串以指定的宽度居中并在两侧填充指 ...
- 70个注意的Python小Notes
Python读书笔记:70个注意的小Notes 作者:白宁超 2018年7月9日10:58:18 摘要:在阅读python相关书籍中,对其进行简单的笔记纪要.旨在注意一些细节问题,在今后项目中灵活运用 ...
- [Python Study Notes]匿名函数
Python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的.在Python中,lambda作 ...
- [Python Study Notes]字符串处理技巧(持续更新)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]with的使用
在 Python 2.5 中, with 关键字被加入.它将常用的 try ... except ... finally ... 模式很方便的被复用.看一个最经典的例子: with open('fil ...
- [Python Study Notes]实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- exec
之前一直这样显示 不知道修改了什么,变成了这样. 在终端 找到这个podfile所在的目录 chmod 600 podfile 然后 就变回来了.可能是修改权限的问题.
- xcode 删除文件后编译会出现*** is missing from working copy
删除文件后 工程中会出现如图所示 如果你使用了svn管理工具 你就会看到如图所示 然后 选中 删除 就可以了 好了 多了 不多说了 最近比较忙 博客写的比较 少 等闲了 一定会 ...
- 自定义button
改变button内部label和imageView的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect - (CGRect)imag ...
- ubuntu下安装Apache + PHP + Mysql
首次登录 在本地设备中打开终端,执行ssh命令,登陆服务器. ssh root@139.196.222.22 输入根用户密码,按回车确认. 升级软件 为了确保操作系统中的默认的软件安装了最新的更新和补 ...
- iOS 学习 - 13.微信分享链接、QQ 分享图片
准备工作---原文来自这个 首先要在微信开放平台申请 AppID 和 QQ ID(我第一天晚上申请的,第二天中午就通过了),接着导入 SDK,也就是3个 .h 和一个 .a 文件,详情见这里 如果你是 ...
- iOS中的过期方法和新的替代方法
关于iOS中的过期方法和新的替代方法 1.获取某些类的UINavigationBar的统一外观并设置UINavigationbar的背景 注:方法名改了但是基本使用方法不变 + (instancety ...
- 转载:检测到有潜在危险的 Request.Form 值
转载:检测到有潜在危险的 Request.Form 值 金刚 ASP.NET Request.Form 这是一篇转载的文章,文章原始出处.点我 这种问题是因为你提交的Form中有HTML字符串,例如你 ...
- 转:查看sql语句执行时间/测试sql语句性能
原文出处:http://www.cnblogs.com/qanholas/archive/2011/05/06/2038543.html 写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务 ...
- PHP判断访问者手机移动端还是PC端的函数,亲测好用
,用手机访问PC端WWW域名的时候,自动判断跳转到移动端,用电脑访问M域名手机网站的时候,自动跳转到PC端,我们团队在开发erdaicms二代旅游CMS网站管理系统的时候(http://www.erd ...
- AEAI DP按钮权限配置说明
1 背景概述 AEAI DP3.5版本以后支持对按钮权限进行灵活的管理配置,本文对配置过程进行详细说明,为相关使用人员提供指导和参考. 2 预期读者 数通畅联技术人员 AEAI DP开发平台使用人员 ...