1python基础知识
1.1data type:
float - real number
int - integer number
str - string,text
bool - True,False
int
, or integer: a number without a fractional part. savings
, with the value 100, is an example of an integer.
float
, or floating point: a number that has both an integer and fractional part, separated by a point. growth_multiplier
, with the value 1.1, is an example of a float.
str
, or string: a type to represent text. You can use single or double quotes to build a string.
bool
, or boolean: a type to represent logical values. Can only be True or False (the capitalization is important!).
type(a) # 得类型
1.2different type = different behavior : +
2 + 3 -> 5
‘type’ + ‘tyep’ -> ‘typetype’
字符串不能和数值变量混合计算
1.3type conversion类型转变
int() float() str() bool()
variable assignment 变量分配: x = 5
access information 获取信息
syntax 语法
2Python list
- name a collection集合 of values
- contain any type
- contain different type :
fam =['liz', 1.2, 'hong', 3.2, 'zhang', 4]
fam2 = [ ['liz', 1.2], ['hong', 3.2], ['zhang', 4] ] # a list of lists
2.1子集列表![列表索引](https://i-blog.csdnimg.cn/blog_migrate/d44f15bb8ff43721a0e216ff8ec6eef7.png)
2.2list slicing
2.3subsetting lists of lists
x = [["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"]]
x[2][0]
x[2][:2]
x[:2][2] #为何不行
2.4manipu lating lists 操作列表
AAAAA
x = ['a','b','c']
y = x
y[1] = 'z'
y # y 为 ['a','z','c']
x # x 为 ['a','z','c']
BBBBB
x = ['a','b','c']
y = x[:] # 或者 y = list(x)
y[1] = 'z'
y # y 为 ['a','z','c']
x # x 为 ['a','b,'c'] 没有变
# x[:] or list[x] 防止y的修改影响x
列表操作
#change list of elements
x = ["a", "b", "c", "d"]
x[1] = "r"
x[2:] = ["s", "t"]
#extend a list + delete list elements
x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
z = x
del(x[1]) # x = ["a", "c", "d"] y = ["a", "b", "c", "d","e", "f"] z = ["a", "c", "d"]