区间访问:[from:to:step]
- step默认是1;from表示起始索引(包括),to表示结束索引(不包括)
- step如果有符号,表示方向从右到左;
- from,to有符号,表示从倒数开始算,如-3表示倒数第三个
- string是特殊的类型,且是元组,不能修改;
- 对list添加元素,不能像JS一样,要使用append(item)函数
# [from:to:step] from include,to not include,step default is 1. ( Nagetive sign refer than the direction is from right to left.)
s1 = (2, 1.3, 'love', 5.6, 9, 12, False) s2 = [True, 5, 'smile']
print s1==s1[:] #default is the list print s1[:5] # 前5个 print s1[2:] # 从第3开始
print s1[0:5:2] #每两个
print s1[2:0:-1] # not include s1[0] ,because 0 is the TO.
# 倒序引用
print s2[-1] print s2[1] == s2[-2]
# String is a tuple ,can't modify the char. str = 'abcdef' print str[2:4],type(str) #str[0] = 'z' will throw
# 添加元素,需要调用 append s2.append('pzdndd') print s2[-1] == 'pzdndd' |
数学 +, -, *, /, **, %
判断 ==, !=, >, >=, <, <=, in
逻辑 and, or, not
print 3**4 # 乘方 81
print 5 in [1,3,5] #
print True and False print True or False print not True |