1.简介
- Dynamic Typing
动态类型,自动匹配觉得数据类型;
- Strong Typing
一旦决定,就不能轻易改变;not casual
- 不支持java那样integer+String,python会报错
- Numbers
-int,long,float,complex
- Strings,Unicode:immutable
- Lists and dictionaries:containers
- Other types for e.g. binary data,regular expressions,introspection
- Extension modules can define new "build-in" data types
- 其他类型,如二进制数据,正则表达式,反射
- 可以扩展build-in 数据类型?
2.Numeric Types(1)
Integers
C longs
注意:7/2=3
Long integers
l或L结尾
可以任意长
Floating point numbers
Examples:0.,1.0,1e10,3.14e-2,6.99E4
C doubles
八进制数
O开始
十进制数
0x开始
Complex numbers:
以j或者J结尾,比如:
1+2j
3+3J
3.Operations on Numbers
基本的代数运算
四则运算
幂 a**b
其他基本函数在NumPy和SciPy里面找到
比较运算
!=
二元运算符
Bitwise or : a|b
Bitwise exclusiv or: a^b#或非 5^3=6
Bitwise and: a & b
shift : <<; >>
顺序
PEMDAS
()** * / +-
python支持混合计算,以最复杂的那个类型为准
4.Strings(1)
4.1.单引号或者双引号包围起来
enclosed in single or double quotation marks
双引号允许多行扩展字符串without backslashes, which usually signal the continuation of an expression
4.2.Concatenation and repetition
4.2.1.+字符串相连
'abc'+'def'
4.2.2.*字符串重复使用
'abc'*3
5.Indexing and Slicing(1)
下标和切断
0开始,以字符串s为例,从0到len(s)-1
s[i,j]
i包括在里面,j不包括在里面
s='string'
s[1:4]
'tri'
s[:4]
'stri'
s[2:]
'ring'
6.Indexing and Slicing(2)
6.1.继续i,j,k
s='string selected'
s[0:10]
'string sel'
s[0:10:2]
'srn e'
6.2.negative index
表示从尾部开始
s='string'
s[-1]
'g'
s[-2]
'n'
python的下标系统和Fortran,Matlab和Mathematica不一样,这后面三个都是从1开始。
7.Lists
[]
中括号
flexible arrays,可以包含数字,字符串,嵌入的sublists,或者nothing
lists are mutable:个别成员可以被重新赋值。更甚,他们可以生长或者缩小。
8.lists的操作符
Reversal
L4.reverse()
Shrinking
del L4[2]
Index and slice assignment:
L1[1]
L1[2:4]
Making a list of integer:
range(4)
[0,1,2,3]
range(1,5)
[1,2,3,4]
8.tuples
()
就是immutable list
如果没有nesting tuples,就可以省略括号
虽然说不可变,但是可以和lists互相转换
9.Arrays(1)
不是build-in python types
第三方包NumPy
from numpy import *
vec=array([1,2,3])
#3*3 array
mat=array([[1,23],[4,5,6],[7,8,9]])
zeros((m,n,'typecode')
typecode可以使用int,float,double
10.Array(2)
和lists很类似
arrays不能用+和*来做concatenation和repetition
+的意义不一样
ar1=array([2,4,6]);ar2=array([1,2,3])
ar1+ary=[3,6,9]
ar1*ar2
[2,8,18]
ar1/ar2
[2,2,2]
ar2**2
[1,4,9]