print ("I love python")
"I love"
>>> "I love python"+"and you"
'I love pythonand you'
>>> print("I love python" *2) // 会打印两遍
I love pythonI love python>>> "I love python" *2+" "
'I love pythonI love python '
// \n 为换行符 然而却出现不同的结果
>>> "I love python\n" *3
'I love python\nI love python\nI love python\n'
>>> print ("I love python\n" *3)
I love python
I love python
I love python
不支持 printf ("I love python");
不支持 print "I love python"
不支持
>>> print (s)
Traceback (most recent call last):
File "", line 1, in
print (s)
NameError: name 's' is not defined不支持
>>> I love python
SyntaxError: invalid syntax不支持 "I love python" + 2
>>> "I love python" + 2
Traceback (most recent call last):
File "
", line 1, in "I love python" + 2
TypeError: Can't convert 'int' object to str implicitly // 不能将 int进行隐式的str转换
不支持
>>> print("I love python" - 0)
Traceback (most recent call last):
File "", line 1, in
print("I love python" - 0)
TypeError: unsupported operand type(s) for -: 'str' and 'int'不支持
>>> print("I love python" + 3)
Traceback (most recent call last):
File "", line 1, in
print("I love python" + 3)
TypeError: Can't convert 'int' object to str implicitly不支持
>>> print("I love python" / 1)
Traceback (most recent call last):
File "", line 1, in
print("I love python" / 1)
TypeError: unsupported operand type(s) for /: 'str' and 'int'不支持
>>> 3*2+""
Traceback (most recent call last):
File "", line 1, in
3*2+""
TypeError: unsupported operand type(s) for +: 'int' and 'str'
print (3*2)
3+5
print (3+2);
(3+3);
(3+2)
3+2;
不支持 print 3*2
不支持 printf (3+3);
不支持 printf (3+3)
>>> a = [1,3,5,7,9]
>>> for i in a:
print(i)
1
3
5
7
9
>>> a = [1,'a',2,'b',3,'c']
>>> for i in a:
print(i)
1
a
2
b
3
c
break 和 continue 和java中作用相同
>>> member = []
>>> member
[]
>>> len(member)
0
>>> member.append(2)
>>> member
[2]
>>> member.extend('a')
>>> member
[2, 'a']
>>> b = [1,'b']
>>> member.append(b)
>>> member
[2, 'a', [1, 'b']]
>>> member.extend(b)
>>> member
[2, 'a', [1, 'b'], 1, 'b']
>>> member.insert(0,'插入到第一')
>>> member
['插入到第一', 2, 'a', [1, 'b'], 1, 'b']
>>> len(member)
6
调换顺序:
>>> temp = member[0]
>>> member[0]= member[2]
>>> member[2]= temp
>>> member
['a', 2, '插入到第一', [1, 'b'], 1, 'b']
>>>
删除:
>>> member.remove("a")
>>> member
[2, '插入到第一', [1, 'b'], 1, 'b']
>>> member.remove(0)
Traceback (most recent call last):
File "
member.remove(0)
ValueError: list.remove(x): x not in list
>>> del member[0]
>>> member
['插入到第一', [1, 'b'], 1, 'b']
>>> del member // 删除全部
pop方法 是出栈 列表是以栈形式存储的
>>> member
['插入到第一', [1, 'b'], 1, 'b']
>>> member.pop()
'b'
>>> member
['插入到第一', [1, 'b'], 1]
>>>
// pop方法参数 只能为 索引
>>> member
['插入到第一', [1, 'b'], 1]
>>> member.pop(1)
[1, 'b']
>>> member
['插入到第一', 1]
>>> member.pop('插入到第一')
Traceback (most recent call last):
File "
member.pop('插入到第一')
TypeError: 'str' object cannot be interpreted as an integer
// 分片取值
>>> member
['插入到第一', 1, 'a', 'b']
>>> member[1:3]
[1, 'a']
>>> member
['插入到第一', 1, 'a', 'b']
>>> member[1,2]
Traceback (most recent call last):
File "
member[1,2]
TypeError: list indices must be integers or slices, not tuple
>>> member
['插入到第一', 1, 'a', 'b']
>>> member[:2]
['插入到第一', 1]
>>> member[2:]
['a', 'b']
>>> member[:]
['插入到第一', 1, 'a', 'b']
比较: 只比较第一个元素 字符串的话 按ASSIC编码表比较 只比较第一个字符
>>> list1 = [123,456]
>>> list2 = [234,111]
>>> list3 = [123,456]
>>> list1>list2
False
>>> list1
>>> list1 = ['a','b']
>>> list2 = ['b','c']
>>> list1>list2
False
>>> list1 = ['ab','c']
>>> list2 = ['ba','c']
>>> list1>list2
False
>>> list1
加减:
>>> list4 = list1 + list2
>>> list4
['ab', 'c', 'ba', 'c']
>>> list4 = list1 * list2
Traceback (most recent call last):
File "
list4 = list1 * list2
TypeError: can't multiply sequence by non-int of type 'list'
>>> list4 = list1 - list2
Traceback (most recent call last):
File "
list4 = list1 - list2
TypeError: unsupported operand type(s) for -: 'list' and 'list'
乘:
>>> list1*3
['ab', 'c', 'ab', 'c', 'ab', 'c']
>>> list1 *= 2
>>> list1
['ab', 'c', 'ab', 'c']
>>>
成员操作符:
>>> list1
['ab', 'c', 'ab', 'c']
>>> 'a' in list1
False
>>> 'c' in list1
True
>>> 'a' not in list1
True
>>>
>>> list1
[1, 2, 4, ['a', 'b']]
>>> 'a' in list1
False
>>> 'a' in list1[3]
True
>>> list1[3][1]
'b'
count带参 返回的是 参数在列表中出现的次数
>>> list1.count(4)
1
>>> list1.count('a')
0
index参数在列表中的位置 返回索引值 第二个参数 和第三个参数 是查找的索引范围
>>> list1.index(4)
2
>>> list1.index(0,3)
Traceback (most recent call last):
File "
list1.index(0,3)
ValueError: 0 is not in list
>>> list1.index(4,0,3)
2
reverse() 反转
>>> list1.reverse()
>>> list1
[['a', 'b'], 4, 2, 1]
sort排序 不带参是默认从小到大 参数的reverse默认为False 改为True时就是倒叙 默认使用的是归并排序(貌似是可以三个参数的)
>>> list1
[['a', 'b'], 4, 2, 1]
>>> list1.sort()
Traceback (most recent call last):
File "
list1.sort()
TypeError: unorderable types: int() < list()
>>> list1 = [1,4,2,9,0,6,3]
>>> list1.sort()
>>> list1
[0, 1, 2, 3, 4, 6, 9]
>>> list1.sort(reverse=True)
>>> list1
[9, 6, 4, 3, 2, 1, 0]
不支持 str 与 int的比较
>>> list1 = ['a','c','b']
>>> list1.sort()
>>> list1
['a', 'b', 'c']
>>> list1.append(4)
>>> list1
['a', 'b', 'c', 4]
>>> list1.sort()
Traceback (most recent call last):
File "
list1.sort()
TypeError: unorderable types: int() < str()
sort 排序支持 int 与 float
>>> list1 = [1,4,2,3,9,5,3.2]
>>> list1.sort()
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]
拷贝 与 赋值的区别:
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]
>>> list2 = list1[:]
>>> list3 = list1
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]
>>> list2
[1, 2, 3, 3.2, 4, 5, 9]
>>> list3
[1, 2, 3, 3.2, 4, 5, 9]
>>> list1.sort()
>>> list1.reverse()
>>> list1
[9, 5, 4, 3.2, 3, 2, 1]
>>> list2
[1, 2, 3, 3.2, 4, 5, 9]
>>> list3
[9, 5, 4, 3.2, 3, 2, 1]
>>> list4 = list1.copy()
>>> list4
[9, 5, 4, 3.2, 3, 2, 1]
>>> list1.sort()
>>> list4
[9, 5, 4, 3.2, 3, 2, 1]
>>> list1
[1, 2, 3, 3.2, 4, 5, 9]