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]实现对键盘控制与监控
		
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
 
随机推荐
- Android 在C代码中调用logcat
			
本文给<Android java传递int类型数组给C>中添加C代码中调用logcat的功能 Android.mk文件增加以下内容 LOCAL_LDLIBS += -llog C代码中增加 ...
 - 【代码笔记】iOS-等待动画
			
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
 - 【代码笔记】iOS-带输入框的UIAlertView
			
一,效果图. 二,代码. //点击任何处,弹出输入框 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UIAlertV ...
 - 【读书笔记】iOS网络-优化请求性能
			
一,度量网络性能 1,网络带宽 用于描述无线网络性能的最常见度量指标就是带宽.在数字无线通信中,网络带宽可以描述为两个端点之间的通信通道每秒钟可以传输的位数.现代无线网络所能提供的理论带宽是很高的.不 ...
 - GitHub 上有哪些完整的 iOS-App 源码值得参考?
			
1. Coding iOS 客户端 Coding官方客户端. 笔者强烈推荐的值得学习的完整APP.GitHub - Coding/Coding-iOS: Coding iOS 客户端源代码 2. OS ...
 - webpack入门和实战(一):webpack配置及技巧
			
一.全面理解webpack 1.什么是 webpack? webpack是近期最火的一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffee.样式(含less/sass).图片等都 ...
 - 测试驱动开发(Test-Driven Development,简称TDD)--单元测试-->提高代码质量
			
!!! 1.估算和做项目计划时要算上单元测试时间 2.开发之前写单元测试代码 盖房子的时候,工人师傅砌墙,会先用桩子拉上线,以使砖能够垒的笔直,因为垒砖的时候都是以这根线为基准的.TDD就像这样,先写 ...
 - 十五天精通WCF——第八天  对“绑定”的最后一点理解
			
转眼已经中断10几天没有写博客了,也不是工作太忙,正好碰到了端午节,然后最近看天津台的爱情保卫战入迷了...太好看了,一直都是回味无穷...而且 涂磊老师话说的真是tmd的经典,然后就这样耽搁了,好了 ...
 - SQL Server调优系列基础篇(并行运算总结篇二)
			
前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...
 - 记录Sqlserver2012附加Sqlserver2008的数据库出错的解决方案
			
一.摘要 最近在实验里面用台式编写好了一个软件,想移植到家里的笔记本上.在附加数据的时候却出现了错误,具体也没有提示什么错误,反正就是附加失败了. 二.解决方案 在网上看了一些资料,有的说[低版本不能 ...