python语言思想
python语言基础与应用
超级计算器
python语言解释器
为啥选用PYCHARM
create new project:
NANE+ 选择解释器
open ,选择打开文件或者加入project
注意对齐与缩进
注意字母大小写、空格
注意左右括号配对
错误是常见的,跟BUG和缺陷斗争得到过程
观察代码文本要求:注意上述的注意事项
对数据的抽象,对问题的处理采用的是对数据的抽象,避免处理其他杂糅的数据
何为大数据:
大数据分析处理语言
多种多样的数据类型
简单类型用来表示值
容器类型用来组织这些值
何为计算:
对现实世界处理和过程的抽象
控制流语句
定义语句:把一系列语句集合起来起一个名字 描述了一个包含一系列处理过程的计算单元 标签
基本类型:数值
整数类型:
双大小比较号 为比较判断 得到逻辑值 真或假
是很遵循数学逻辑
数的进制 所谓进制 9>0变两位加1 为10
整数的各种进制表示
0b 0o 0x
浮点数收到17位有限数字的限制
科学计数法:2.3*10^5
有效数字为2位
除了内置数学
还有math模块 面向整数浮点数
cmath 面向复数
基本类型:逻辑值
逻辑运算:双目运算 :与 and 或or ,
单目运算:非not:
优先级not最高 and ,or最低
关于优先级往往会用括号进行提示,并非读代码人员运用所掌握的优先级方法转换
not None 变为真
字符数据类型:
三个连续单引号表示多行字符串 也就是包含了换行的位
转移符号:
字符串有哪些操作?:
小于0 的反方向编号
字符串是数据的本身
名字是数据的标签
名字和字符串是“名”与“值”之间的关系
常见的字符串操作:
序列:
字符串是一种序列
给数据的命名:
名字与变量
名字像一个标签
名字和数值之间的关联称为引用
设么是变量、何为变量?
与数值关联的名字也称作变量,
python语言是动态的语言
可以随时指向任何的类型,其他常见语言都是静态的
何为赋值? 名字与数值关联的过程,称为给变量赋值,数值也称为数据对象
python 是语言大师创造的语言
上机练习时间:
要更注意错误,需要学会看错误,
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 4.2+2.1==6.3
False
>>> print(4.2+2.1)
File "<stdin>", line 1
print(4.2+2.1)
^
SyntaxError: invalid character in identifier
>>> print(4+1)
5
>>> 4.2+2.1
6.300000000000001
>>> 1+3j
(1+3j)
>>> (1+2j).real
1.0
>>> (1+2j).imag
2.0
>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> h=88
>>> m=g="dd"
>>> m
'dd'
>>> g
'dd'
>>> h
88
>>> type(h)
<class 'int'>
>>> type (m)
<class 'str'>
>>> len(m)
2
>>> n="qwertyuiop"
>>> n[1:6]
'werty'
>>> n*2
'qwertyuiopqwertyuiop'
>>> c=n*2
>>> c
'qwertyuiopqwertyuiop'
>>> c.isalpha
<built-in method isalpha of str object at 0x000001A0A09619E0>
>>> h.isdigit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'isdigit'
>>> n=" ppq"
>>> c=n*3
>>> c
' ppq ppq ppq'
>>> c.split('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator
>>> c.split(' ')
['', 'ppq', 'ppq', 'ppq']
>>> me="how are you, i an fine thankyou"
>>> me.split(" ")
['how', 'are', 'you,', 'i', 'an', 'fine', 'thankyou']
>>> me[1]
File "<stdin>", line 1
me[1]
^
IndentationError: unexpected indent
>>> me[1:]
'ow are you, i an fine thankyou'
>>> me[1;2]
File "<stdin>", line 1
me[1;2]
^
SyntaxError: invalid syntax
>>> me[1:2]
'o'
>>> me[-1:-3]
''
>>> me[1:10]
'ow are yo'
>>> me[-3:-1]
File "<stdin>", line 1
me[-3:-1]
^
SyntaxError: invalid character in identifier
>>> me[-1:-3:1]
''
>>> me[-1:-10]
''
>>> me.upper
<built-in method upper of str object at 0x000001A0A0961940>
>>> me.upper()
'HOW ARE YOU, I AN FINE THANKYOU'
>>> "+++".join(me)
'h+++o+++w+++ +++a+++r+++e+++ +++y+++o+++u+++,+++ +++i+++ +++a+++n+++ +++f+++i+++n+++e+++ +++t+++h+++a+++n+++k+++y+++o+++u'
>>> "+".join(me,c,"nihaoma")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: join() takes exactly one argument (3 given)
>>> "+".join(me,c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: join() takes exactly one argument (2 given)
>>> "+".join([me,c])
'how are you, i an fine thankyou+ ppq ppq ppq'
>>> "".join(["123456789"])
'123456789'
>>> "+".join("123456789")
'1+2+3+4+5+6+7+8+9'
>>> "+".join(["111"])
'111'
>>> me[0]
'h'
>>> me[-1]
'u'
>>> me[1]
'o'
>>> me[2]
'w'
>>> me.in("h"0
File "<stdin>", line 1
me.in("h"0
^
SyntaxError: invalid syntax
>>> me.in("h")
File "<stdin>", line 1
me.in("h")
^
SyntaxError: invalid syntax
>>> me.in"h"
File "<stdin>", line 1
me.in"h"
^
SyntaxError: invalid syntax
>>> me.in(h)
File "<stdin>", line 1
me.in(h)
^
SyntaxError: invalid syntax
>>> hex(33)
'0x21'
>>> oct(7)
'0o7'
>>> bin(7)
'0b111'
>>> 1.str()
File "<stdin>", line 1
1.str()
^
SyntaxError: invalid syntax
>>> str(1)
'1'
>>> int("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
>>> int(abc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> int(1)
1
>>> float(1)
1.0
>>> double(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'double' is not defined
>>> str(False)
'False'
>>> bool(1)
True
>>> bool(False)
False
>>> bool(false)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'false' is not defined
>>> int(sbc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sbc' is not defined
>>> 1 is None
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> 1 is None
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> me+c
'how are you, i an fine thankyou ppq ppq ppq'
>>> d=me+c
>>>
>>> d
'how are you, i an fine thankyou ppq ppq ppq'
>>> ord("A")
65
>>> chr("A")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>> chr(65)
'A'
>>> chr(64)
'@'
>>> d.in
File "<stdin>", line 1
d.in
^
SyntaxError: invalid syntax
>>> d
'how are you, i an fine thankyou ppq ppq ppq'
>>> "h" in d
True
>>> p in d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'p' is not defined
>>> "w a" in d
True
>>> "wa"in d
False
>>> ord("猪")
29482
>>> chr(29482)
'猪'
>>> ord(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 43 found
>>> ord("你好")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
>>> len("你好“)
File "<stdin>", line 1
len("你好“)
^
SyntaxError: EOL while scanning string literal
>>> len("nihao")
5
>>> len("你好")
2
>>> d[1]
'o'
>>> s[-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> d[-1]
'q'
>>> d[-1:-5]
''
>>> print(d[-1;-5])
File "<stdin>", line 1
print(d[-1;-5])
^
SyntaxError: invalid syntax
>>> d[-1:-5:]
''
>>> d[-1:]
'q'
>>> d[-2]
'p'
>>> d[::-1]
'qpp qpp qpp uoyknaht enif na i ,uoy era woh'
>>> d[-1:-5:-1]
'qpp '
>>> d.split(" ")
['how', 'are', 'you,', 'i', 'an', 'fine', 'thankyou', 'ppq', 'ppq', 'ppq']
>>> d.center
<built-in method center of str object at 0x000001A0A08D4BD0>
>>> d.center()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: center expected at least 1 argument, got 0
>>> center(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'center' is not defined
>>> d.center(30)
'how are you, i an fine thankyou ppq ppq ppq'
>>> "ppp".center(10)
' ppp '
>>> "ppp".center(100)
' ppp '
>>> d.replace("how","mememe")
'mememe are you, i an fine thankyou ppq ppq ppq'
>>>
>>>> d.isdigit(
...
...
... )
False
>>> d.isdigit()
False
>>> d.isalpha()
False
>>> d.isalpha(h)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isalpha() takes no arguments (1 given)
>>> f.isalpha("h")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'f' is not defined
>>> "123 ".isdigit()
False
数据收纳盒:
面临得需要处理的问题:如何使用? 名字[n]
列表,
元组(不可变序列)
字符串用“”
列表用[]
元组用()
列表的操作
容器,何为容器?是从简单数据类型引申出来的容器将简单类型数据组织起来
函数reversed不返回列表,而是返回一个迭代器。可使用list将返回的对象转换为列表。
>>> box["1",1,"a"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'box' is not defined
>>> box[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'box' is not defined
>>> box=["ss","1",1]
>>> box
['ss', '1', 1]
>>> len.box
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'box'
>>> box.len
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'len'
>>> me.len
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'len'
>>> len(box)
3
>>> type(box)
<class 'list'>
>>> a1= list(me)
>>> a1
['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ',', ' ', 'i', ' ', 'a', 'n', ' ', 'f', 'i', 'n', 'e', ' ', 't', 'h', 'a', 'n', 'k', 'y', 'o', 'u']
>>> print(a1)
['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ',', ' ', 'i', ' ', 'a', 'n', ' ', 'f', 'i', 'n', 'e', ' ', 't', 'h', 'a', 'n', 'k', 'y', 'o', 'u']
>>> len(a1)
31
>>> a=["s",3,a1]
>>> a
['s', 3, ['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ',', ' ', 'i', ' ', 'a', 'n', ' ', 'f', 'i', 'n', 'e', ' ', 't', 'h', 'a', 'n', 'k', 'y', 'o', 'u']]
>>> a.pop()
['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ',', ' ', 'i', ' ', 'a', 'n', ' ', 'f', 'i', 'n', 'e', ' ', 't', 'h', 'a', 'n', 'k', 'y', 'o', 'u']
>>> a.pop
<built-in method pop of list object at 0x000001A0A08C6D40>
>>> a.pop(0)
's'
>>> a
[3]
>>> a.append(a)
>>> a
[3, [...]]
>>> a
[3, [...]]
>>> a.append("aa")
>>> a
[3, [...], 'aa']
>>> a.extend(a)
>>>
>>> a
[3, [...], 'aa', 3, [...], 'aa']
>>> a.insert(2,"ppp")
>>> a
[3, [...], 'ppp', 'aa', 3, [...], 'aa']
>>> a.insert(2,a)
>>> a
[3, [...], [...], 'ppp', 'aa', 3, [...], 'aa']
>>> a.reverse
<built-in method reverse of list object at 0x000001A0A08C6D40>
>>> a.reverse()
File "<stdin>", line 1
a.reverse()
^
IndentationError: unexpected indent
>>> a.reverse()
>>> a
['aa', [...], 3, 'aa', 'ppp', [...], [...], 3]
>>> a.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'list' and 'str'
>>> a=[1,2,3]
>>> a.sort()
>>> a
[1, 2, 3]
>>> a.sort(reverse = true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> a.sort(reverse = True)
>>> a
[3, 2, 1]
>>> me
'how are you, i an fine thankyou'
>>> b1=list(me)
>>> b1
['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ',', ' ', 'i', ' ', 'a', 'n', ' ', 'f', 'i', 'n', 'e', ' ', 't', 'h', 'a', 'n', 'k', 'y', 'o', 'u']
>>> reversed(a)
<list_reverseiterator object at 0x000001A0A046F820>
>>> a
[3, 2, 1]
>>> c1=reversed(a)
>>> c1
<list_reverseiterator object at 0x000001A0A046F820>
>>> a
[3, 2, 1]
>>> a
[3, 2, 1]
>>> c1
<list_reverseiterator object at 0x000001A0A046F820>
>>> c2=list(reversed(c1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list_reverseiterator' object is not reversible
>>> c2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c2' is not defined
>>> c2=list(reversed(a))
>>> c2
[1, 2, 3]
>>> c3=list(c2)
>>> c3
[1, 2, 3]
>>> c3=list(c1)
>>> c3
[1, 2, 3]
>>> c4=list(sorted(c3))
>>> c4
[1, 2, 3]
>>> c5=list(c4.reverse())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
>>> c4.reverse()
>>> c4
[1, 2, 3]
报错:
SyntaxError: invalid syntax 语法错误,详解。
TypeError: extend() takes exactly one argument (2 given)
TypeError:ExpDug()正好有一个参数(2给出)
AttributeError: ‘str’ object has no attribute ‘sort’
AttributeError:“str”对象没有属性“sort”
>>> 10**16
10000000000000000
>>> 22/7
3.142857142857143
>>> a=[1,2,3,"sss"]
>>> a
[1, 2, 3, 'sss']
>>> len(a)
4
>>> b=list(reversed(a))
>>> b
['sss', 3, 2, 1]
>>> b.pop()
1
>>> b
['sss', 3, 2]
>>> b.pop(0)
'sss'
>>> b
[3, 2]
>>> b.append("qwer")
>>> b
[3, 2, 'qwer']
>>> b.append(a)
>>> b
[3, 2, 'qwer', [1, 2, 3, 'sss']]
>>> b.append(a,c[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined
>>> b.append(b)
>>> b
[3, 2, 'qwer', [1, 2, 3, 'sss'], [...]]
>>> len(b(
... len(b)
... b
File "<stdin>", line 3
b
^
SyntaxError: invalid syntax
>>> b
[3, 2, 'qwer', [1, 2, 3, 'sss'], [...]]
>>> len(b)
5
>>> a.extend()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (0 given)
>>> s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> a
[1, 2, 3, 'sss']
>>> a.extend(b)
>>> a
[1, 2, 3, 'sss', 3, 2, 'qwer', [...], [3, 2, 'qwer', [...], [...]]]
>>> a=("a",1,""," ")
>>> a
('a', 1, '', ' ')
>>> len(a)
4
>>> a.pop(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'pop'
>>> a.pop(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'pop'
>>> a
('a', 1, '', ' ')
>>> b
[3, 2, 'qwer', [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], [...]]
>>> b.pop(1)
2
>>> b
[3, 'qwer', [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], [...]]
>>> b.reverse()
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 'qwer', 3]
>>> b.remove([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> b.remove(3)
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 'qwer']
>>> b.remove("qwer")
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]]]
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]]]
>>> b.append("3")
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], '3']
>>> b.append(3)
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], '3', 3]
>>> b.remove("3")
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 3]
>>> b.remove("3")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> b.del()
File "<stdin>", line 1
b.del()
^
SyntaxError: invalid syntax
>>> b.del(1)
File "<stdin>", line 1
b.del(1)
^
SyntaxError: invalid syntax
>>> b.count(3)
1
>>> b.cout([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'cout'
>>> b.index(3)
2
>>> len(b)
3
>>> b.append(3,4,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (3 given)
>>> b.append(3)
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 3, 3]
>>> b.extend(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (3 given)
>>> b.append(b)
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 3, 3, [...]]
>>> b.index(3)
2
>>> b.del(2)
File "<stdin>", line 1
b.del(2)
^
SyntaxError: invalid syntax
>>> b.dle(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'dle'
>>> del.b[1]
File "<stdin>", line 1
del.b[1]
^
SyntaxError: invalid syntax
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 3, 3, [...]]
>>> del.b[2]
File "<stdin>", line 1
del.b[2]
^
SyntaxError: invalid syntax
>>> del b[2]
>>> b
[[...], [1, 2, 3, 'sss', 3, 2, 'qwer', [...], [...]], 3, [...]]
>>> del b[1]
>>> b
[[...], 3, [...]]
>>> b
[[...], 3, [...]]
>>> del b[]
File "<stdin>", line 1
del b[]
^
SyntaxError: invalid syntax
>>> del b[0]
>>> b
[3, [...]]
>>> b.append("1")
>>> b
[3, [...], '1']
>>> b.append(1)
>>> b
[3, [...], '1', 1]
>>> print (b)
[3, [...], '1', 1]
>>> b
[3, [...], '1', 1]
>>> b.remove(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> b.remove(1)
>>> b
[3, [...], '1']
>>> b.index("1")
2
>>> b.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'list' and 'int'
>>> b.extend(b)
>>> b
[3, [...], '1', 3, [...], '1']
>>> b.extend("a")
>>> b
[3, [...], '1', 3, [...], '1', 'a']
>>> b.extend(z)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
>>> b.extend(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> b.extend(b,b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (2 given)
>>> b.extend(b)
>>> b
[3, [...], '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a']
>>> len(b)
14
>>> b.del(0S)
File "<stdin>", line 1
b.del(0S)
^
SyntaxError: invalid syntax
>>> b.del(0)
File "<stdin>", line 1
b.del(0)
^
SyntaxError: invalid syntax
>>> b.del(0)
File "<stdin>", line 1
b.del(0)
^
SyntaxError: invalid syntax
>>> b.dle(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'dle'
>>> del b[1]
>>> b
[3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a']
>>> a=[1,"d"]
>>> c=s+b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> c=a+b
>>> c
[1, 'd', 3, '1', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 'a', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 'a']
>>> a+b
[1, 'd', 3, '1', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 'a', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 'a']
>>> a+b
[1, 'd', 3, '1', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 'a', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 3, [3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a'], '1', 'a']
>>> c=(1,2,"s")
>>> c+c
(1, 2, 's', 1, 2, 's')
>>> c*2
(1, 2, 's', 1, 2, 's')
>>> c*3
(1, 2, 's', 1, 2, 's', 1, 2, 's')
>>> print(c*3)
(1, 2, 's', 1, 2, 's', 1, 2, 's')
>>> a="s,1"
>>> a
's,1'
>>> a="a,1,"1","a""
File "<stdin>", line 1
a="a,1,"1","a""
^
SyntaxError: invalid syntax
>>> a="a"
>>>
>>> a
'a'
>>> a="a,"1""
File "<stdin>", line 1
a="a,"1""
^
SyntaxError: invalid syntax
>>> a="1"
>>> a
'1'
>>> type(a)
<class 'str'>
>>> a="1,\"
File "<stdin>", line 1
a="1,\"
^
SyntaxError: EOL while scanning string literal
>>> a="1,2,3"
>>> a.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'sort'
>>> b
[3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a']
>>> b.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
>>> a
'1,2,3'
>>> a[2]
'2'
>>> a[1,2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
>>> a
'1,2,3'
>>> a[1]
','
>>> a
'1,2,3'
>>> a[2]
'2'
>>> a
'1,2,3'
>>> a[1]
','
>>> a[0]
'1'
>>> a[3]
','
>>> a[4]
'3'
>>> a
'1,2,3'
>>> a
'1,2,3'
>>> a"0"
File "<stdin>", line 1
a"0"
^
SyntaxError: invalid syntax
>>> a
'1,2,3'
>>> type(a)
<class 'str'>
>>> a
'1,2,3'
>>> a=list(a)
>>> a
['1', ',', '2', ',', '3']
>>> a[1]
','
>>> a=str(a)
>>> a
"['1', ',', '2', ',', '3']"
>>> a
"['1', ',', '2', ',', '3']"
>>> type(a)
<class 'str'>
>>> a
"['1', ',', '2', ',', '3']"
>>> a=list(a)
>>> a
['[', "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '2', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '3', "'", ']']
>>> type(a)
<class 'list'>
>>> a[0]
'['
>>> a[0,1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
>>> a
['[', "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '2', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '3', "'", ']']
>>> a[1]
"'"
>>> a.remove(,)
File "<stdin>", line 1
a.remove(,)
^
SyntaxError: invalid syntax
>>> a
['[', "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '2', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '3', "'", ']']
>>> a.remove(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> a.remove(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> len(a)
25
>>> len([)
File "<stdin>", line 1
SyntaxError: closing parenthesis ')' does not match opening parenthesis '['
>>> a.remove([)
File "<stdin>", line 1
SyntaxError: closing parenthesis ')' does not match opening parenthesis '['
>>> del a[0]
>>> a
["'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '2', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", '3', "'", ']']
>>> a.reverse()
>>> a
[']', "'", '3', "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '2', "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'"]
>>> a.remove(",")
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", '2', "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'"]
>>> a.remove(')
File "<stdin>", line 1
a.remove(')
^
SyntaxError: EOL while scanning string literal
>>> a.remove(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> a.remove("2")
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'"]
>>> a.appenda(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'appenda'
>>> a.append(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
>>> a.extend(a,a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (2 given)
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'"]
>>> a.extend(a)
>>>
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'"]
>>> len(a)
44
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'"]
>>> a.append(1)
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", 1]
>>> len(a)
45
>>> p=liat(reversed(a))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'liat' is not defined
>>> p=list(reversed(a))
>>> p
[1, "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", "'", ',', ' ', "'", ',', "'", ' ', "'", '3', "'", ']', "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", "'", ',', ' ', "'", ',', "'", ' ', "'", '3', "'", ']']
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", 1]
>>> a==b
False
>>> a==p
False
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", 1]
>>> p
[1, "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", "'", ',', ' ', "'", ',', "'", ' ', "'", '3', "'", ']', "'", '1', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", "'", ',', ' ', "'", ',', "'", ' ', "'", '3', "'", ']']
>>> p=list(reversed(p))
>>> p==a
True
>>> c=3
>>> d=3
>>> c==d
True
>>> e=1+2
>>> e
3
>>> c==e
True
>>> d==e
True
>>> e==3
True
>>> "3" in a
True
>>> a.index(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 3 is not in list
>>> a.index("3")
2
>>> 3 in a
False
>>> sum(1+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> sum(c,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> c
3
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", 1]
>>> b
[3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a']
>>> sum(b,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> o=[1,2,6]
>>> o
[1, 2, 6]
>>> sum(o,2)
11
>>> max(o)
6
>>> min(o)
1
两个小数相加的和跟确切的值比较输出False
>>> c=6+3
>>> 9==c
True
>>> c==9
True
>>> 4.2+36
40.2
>>> c=3.6+2
>>> d=3+2.6
>>> c==d
True
>>> d=2.2+3.4
>>> c==d
True
>>> print(c)
5.6
>>> 5.6==c
True
>>> 4.2+2==6.2
True
>>> 4.2+2.1==6.3
False
>>> c=6.3
>>> 3.6+2.0
5.6
>>> 3.6+2==5.6
True
>>> 3.6+2.0==5.6
True
字典
创建一个字典
update用法:
>>> student = dict.fromkeys(a)
>>> student
{']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}
>>> school={}
>>> school
{}
>>> school["name:"]="pdsu"
>>> school
{'name:': 'pdsu'}
>>> school["name:"] = o
>>> school
{'name:': [1, 2, 6]}
>>> school.update(num=o)
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6]}
>>> school.update(beizhu=student)
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}}
>>> school.update(small="pp","oo",11,"11")
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> school.update(small="pp")
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}, 'small': 'pp'}
>>> school.update(big="a",big="b")
File "<stdin>", line 1
SyntaxError: keyword argument repeated
>>> school.update(big="big",big="aa")
File "<stdin>", line 1
SyntaxError: keyword argument repeated
>>> school.update(big="aa",ok="aa")
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}, 'small': 'pp', 'big': 'aa', 'ok': 'aa'}
>>> school.update(qq="l"+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> school.update(school)
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}, 'small': 'pp', 'big': 'aa', 'ok': 'aa'}
>>> a={"pokr":1,"age"=7)
File "<stdin>", line 1
a={"pokr":1,"age"=7)
^
SyntaxError: invalid syntax
>>> a={"poke"="c")
File "<stdin>", line 1
a={"poke"="c")
^
SyntaxError: invalid syntax
>>> a = {"poke":1,"a":3)
File "<stdin>", line 1
SyntaxError: closing parenthesis ')' does not match opening parenthesis '{'
>>> a
[']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", ']', "'", '3', "'", ' ', "'", ',', "'", ' ', ',', "'", "'", ' ', ',', "'", ',', "'", ' ', ',', "'", '1', "'", 1]
>>> a={"a":1,"b":"1"}
>>> a
{'a': 1, 'b': '1'}
>>> school.update(a)
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}, 'small': 'pp', 'big': 'aa', 'ok': 'aa', 'a': 1, 'b': '1'}
>>> b
[3, '1', 3, [...], '1', 'a', 3, [...], '1', 3, [...], '1', 'a']
>>> b={66.3:66}
>>> b
{66.3: 66}
>>> school.update(b)
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}, 'small': 'pp', 'big': 'aa', 'ok': 'aa', 'a': 1, 'b': '1', 66.3: 66}
print易错:
>>> print("school['num']",school["num"])
school['num'] [1, 2, 6]
>>> print("school['num']:",school["num"])
school['num']: [1, 2, 6]
>>> print("''")
''
>>> print('""')
""
>>> print("school['num']:,school["nu"]:",school["num"])
File "<stdin>", line 1
print("school['num']:,school["nu"]:",school["num"])
^
SyntaxError: invalid syntax
>>> print("school['num']:,school['s']:",school["num"])
school['num']:,school['s']: [1, 2, 6]
>>>
>>>> student
{'name': 10, 'age': 10}
>>> student
{'name': 10, 'age': 10}
>>> school
{'name:': [1, 2, 6], 'num': [1, 2, 6], 'beizhu': {']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}, 'small': 'pp', 'big': 'aa', 'ok': 'aa', 'a': 1, 'b': '1', 66.3: 66, 'type': 3, ']': None, "'": None, '3': None, ' ': None, ',': None, '1': None, 1: None}
>>> student = dict.fromkeys(("ss","ssss"),3)
>>> student = dict.fromkeys(("ss","ssss",ppp),3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ppp' is not defined
>>> student = dict.fromkeys(("ss","ssss"),3)
>>> student
{'ss': 3, 'ssss': 3}
>>> student = dict.fromkeys(("ss","ssss",ppp),3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ppp' is not defined
>>> student = dict.fromkeys(("ss","ssss","ppp"),3)
>>> student = dict,fromkeys(("aa","'ff'",3),4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fromkeys' is not defined
>>> student = dict.fromkeys(("aa","'ff'",3),4)
>>> student
{'aa': 4, "'ff'": 4, 3: 4}
fromkeys使用:元素数量为被命名数量
参数为字符串则字符串内部每个元素都被使用成KEY
参数为列表,元素数量为被命名数量
key的名称类型只能是不可变的数据类型:元组、字符串、数字类型,若所给参数不是不可变类型
>>>student=dict.fromkeys(("name","sex","age"),"none")
{'name': 'none', 'sex': 'none', 'age': 'none'}
>>> example=student.fromkeys("qwer,.?")
>>> example
{'q': None, 'w': None, 'e': None, 'r': None, ',': None, '.': None, '?': None}
>>> p="qwer,.?"
>>> len(p)
7
>>> student.get(("name"))
'wpm'
>>> student
{'sex': '男', 'age': 16, 'name': 'wpm'}
>>> student["name"]="www"
>>> student
{'sex': '男', 'age': 16, 'name': 'www'}
>>> a
('name', 1)
>>> student[a]="nihao world"
>>> student
{'sex': '男', 'age': 16, 'name': 'www', ('name', 1): 'nihao world'}
>>> "男" in student
False
>>> "男" in student.values()
True
>>>
>>>
集合
创建集合: