{"id":1018,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u6570\u636e\u7c7b\u578b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1018.html","title":{"rendered":"Python \u6570\u636e\u7c7b\u578b"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u6570\u636e\u7c7b\u578b<\/h1>\n<\/p><\/div>\n

\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u5b58\u50a8\u4e0d\u540c\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/span>
\n Python\u4e2d\u7684\u53d8\u91cf\u6ca1\u6709\u7c7b\u578b\uff0c\u8fd9\u91cc\u7684\u6570\u636e\u7c7b\u578b\u662f\u6307\u7684\u5185\u5b58\u4e2d\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/span>
\n Python\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\uff1a\u6587\u5b57\u7c7b\u578b\u3001\u6570\u503c\u7c7b\u578b\u3001\u5e8f\u5217\u7c7b\u578b\u3001\u6620\u5c04\u7c7b\u578b\u3001\u96c6\u5408\u7c7b\u578b\u3001\u5e03\u5c14\u7c7b\u578b\u548c\u4e8c\u8fdb\u5236\u7c7b\u578b\u3002<\/span> <\/p>\n

\u6807\u51c6\u6570\u636e\u7c7b\u578b<\/h2>\n

\u6587\u5b57\u7c7b\u578b\uff1a str\uff0c\u6bd4\u5982\u201cHello World\u201d\u3002<\/span>
\n \u6570\u503c\u7c7b\u578b\uff1a int \uff0cfloat\uff0ccomplex\uff0c\u6bd4\u5982\uff1a1\u30011.2\u30011.2+1.3j\u3002<\/span>
\n \u5e8f\u5217\u7c7b\u578b\uff1a list\uff0ctuple\uff0c range\u3002\u6bd4\u5982\uff1a[a,b,c]\u3001(a,b,c)\u3002<\/span>
\n \u6620\u5c04\u7c7b\u578b\uff1a dict\uff0c\u6bd4\u5982\uff1a{\u201ca\u201d:1,\u201db\u201d:2}\u3002<\/span>
\n \u96c6\u5408\u7c7b\u578b\uff1a set\uff0cfrozenset\uff0c\u6bd4\u5982\uff1a{\u2018a\u2019,\u2019b\u2019}\u3002<\/span>
\n \u5e03\u5c14\u7c7b\u578b\uff1a bool\uff0c\u6bd4\u5982\uff0cTrue\u3001False\u3002<\/span>
\n \u4e8c\u8fdb\u5236\u7c7b\u578b\uff1a bytes\uff0cbytearray\uff0c memoryview\u3002<\/span> <\/p>\n

\n \u6ce8\u610f\uff1a<\/span>\u5176\u4e2d\u6587\u5b57\u7c7b\u578b\u3001\u6570\u503c\u7c7b\u578b\u3001\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff0c\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u662f\u53ef\u53d8\u6570\u636e\u7c7b\u578b\u3002\n <\/div>\n

\u6587\u672c\u7c7b\u578b<\/h2>\n
\n \u6587\u672c\u7c7b\u578b\u662f\u4ee5\u5355\u5f15\u53f7''\u6216\u53cc\u5f15\u53f7\"\u201c\u62ec\u8d77\u6765\u7684\u4efb\u610f\u6587\u672c\uff0c\u6bd4\u5982'abc'\uff0c\"Hello World\"\u3002\n <\/div>\n

1\u3001\u5bf9\u5f15\u53f7\u8fdb\u884c\u8f6c\u4e49<\/h3>\n
\n \u4f46\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u5f15\u53f7\u65f6\u8981\u8fdb\u884c\u8f6c\u4e49\uff0c\u5426\u5219\u4f1a\u89e3\u6790\u9519\u8bef\uff0c\u6bd4\u5982\uff1a'I'm a lidihuo'\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u4e863\u4e2a\u5355\u5f15\u53f7\uff0c\u6ca1\u6709\u5b8c\u5168\u914d\u5bf9\uff0c\u8fdb\u800c\u51fa\u73b0\u8bed\u6cd5\u9519\u8bef\u3002\n <\/div>\n
\n \u89e3\u51b3\u7684\u529e\u6cd5\u5c31\u662f\u7528\u53cd\u659c\u6760 \\ \u8fdb\u884c\u8f6c\u4e49\uff0c\u8ba9\u7f16\u8bd1\u5668\u628a\u5979\u5f53\u6210\u666e\u901a\u6587\u672c\uff0c\u6bd4\u5982\uff1a\n <\/div>\n
\n
 str = 'I\\'m a lidihuo'<\/span>
print(str<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
I'm a lidihuo<\/span><\/code><\/pre>\n<\/p><\/div>\n

2\u3001\u4e0d\u540c\u7684\u5f15\u53f7<\/h3>\n
\n \u5f53\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5f15\u53f7\u5305\u88f9\u4ed6\u4eec\u6bd4\u5982\uff1a\n <\/div>\n
\n
 str1 = \"I'm a lidihuo\"<\/span>
str2 = 'Hello \" World'<\/span>
print(str1<\/span>)
print(str2<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
I'm a lidihuo<\/span> \n
\nHello \" World<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n

3\u3001\u957f\u5b57\u7b26\u4e32\u6362\u884c<\/h3>\n
\n Python\u4e2d\u5bf9\u7a0b\u5e8f\u7684\u6362\u884c\u548c\u7f29\u8fdb\u90fd\u6709\u4e25\u683c\u7684\u8bed\u6cd5\u8981\u6c42\u3002\u5f53\u4e00\u4e2a\u8f83\u957f\u5b57\u7b26\u4e32\u9700\u8981\u6362\u884c\u65f6\u9700\u8981\u5728\u884c\u5c3e\u6dfb\u52a0\u53cd\u659c\u6760 \\ \uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u8bed\u6cd5\u62a5\u9519\uff0c\u6bd4\u5982\uff1a\n <\/div>\n
\n
 str = 'In programming, data type is an important concept,\\
Variables can store data of different types, and different types can do different things.'<\/span>
str2 = 'Hello \" World'<\/span>
print(str<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
In programming, data type is an important concept,Variables can store data of different types, and different types can do different things.<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>\u867d\u7136\u53d8\u91cf\u8d4b\u503c\u65f6\u5b57\u7b26\u4e32\u6362\u884c\u4e86\uff0c\u4f46\u8f93\u51fa\u7684\u7ed3\u679c\u5e76\u6ca1\u6709\u6362\u884c\u3002\n <\/div>\n

4\u3001\u5b57\u7b26\u4e32\u622a\u53d6<\/h3>\n
\n python\u4e2d\u5b57\u7b26\u4e32\u622a\u53d6\u7d22\u5f15\u503c0 \u4e3a\u4ece\u5934\u5f00\u59cb\uff0c-1 \u4e3a\u4ece\u672b\u5c3e\u7684\u5f00\u59cb\u3002\n <\/div>\n

Python \u6570\u636e\u7c7b\u578b_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n
 str = 'helloworld'<\/span>
print(str<\/span>) # \u8f93\u51fa\u5b57\u7b26\u4e32<\/span>
print(str[0:-1]<\/span>) # \u8f93\u51fa\u7b2c1\u4e2a\u5230\u5012\u6570\u7b2c2\u4e2a\u4e4b\u95f4\u7684\u5b57\u7b26<\/span>
print(str[0]<\/span>) # \u8f93\u51fa\u5b57\u7b26\u4e32\u7b2c1\u4e2a\u5b57\u7b26<\/span>
print(str[2:5]<\/span>) # \u8f93\u51fa\u4ece\u7b2c3\u4e2a\u5f00\u59cb\u5230\u7b2c5\u4e2a\u7684\u5b57\u7b26<\/span>
print(str[2:]<\/span>) # \u8f93\u51fa\u4ece\u7b2c3\u4e2a\u5f00\u59cb\u7684\u540e\u7684\u6240\u6709\u5b57\u7b26<\/span>
print(str * 2<\/span>) # \u8f93\u51fa\u5b57\u7b26\u4e322\u6b21\uff0c\u4e5f\u53ef\u4ee5\u5199\u6210 print (2 * str)<\/span>
print(str + \"I am lidehuo\"<\/span>) # \u8fde\u63a5\u5b57\u7b26\u4e32<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
helloworld<\/span> \n
\nhelloworl<\/span> \n
\nh<\/span> \n
\nllo<\/span> \n
\nlloworld<\/span> \n
\nhelloworldhelloworld<\/span> \n
\nhelloworldI am lidehuo<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>\u52a0\u53f7 + \u662f\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\u7b26\uff0c \u661f\u53f7 * \u8868\u793a\u590d\u5236\u5f53\u524d\u5b57\u7b26\u4e32\uff0c\u540e\u9762\u7684\u6570\u5b57\u4e3a\u590d\u5236\u7684\u6b21\u6570\u3002\n <\/div>\n

\u6570\u503c\u7c7b\u578b<\/h2>\n

1\u3001int (\u6574\u6570)<\/h3>\n
\n \u6574\u6570\u7c7b\u578b\u5c31\u662f\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6b63\u6574\u6570\u30010 \u548c\u8d1f\u6574\u6570\u3002\u540c\u65f6\u652f\u6301\u7528\u4e0b\u5212\u7ebf_\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f8b\u5982\uff1a\n <\/div>\n
\n
 a = 10<\/span>
b = 0<\/span>
c = -100<\/span>
d = 1_234_567<\/span>
print(a<\/span>)
print(b<\/span>)
print(c<\/span>)
print(d<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
10<\/span> \n
\n0<\/span> \n
\n-100<\/span> \n
\n1234567<\/span><\/code><\/pre>\n<\/p><\/div>\n

2\u3001float (\u6d6e\u70b9\u6570)<\/h3>\n
\n \u6d6e\u70b9\u6570\u5c31\u662f\u5e26\u5c0f\u6570\u7684\u6570\u5b57\uff0c\u5c0f\u6570\u6709\u4e24\u79cd\u8868\u73b0\u5f62\u5f0f\uff0c\u5206\u522b\u662f\u5341\u8fdb\u5236\u5f62\u5f0f\u548c\u6307\u6570\u5f62\u5f0f\u3002\u6bd4\u5982\uff1a\n <\/div>\n
\n
 a = 12.34<\/span>
b = 100000000000000000000.45<\/span>
print(\"a = \", a<\/span>)
print(\"b = \", b<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
a = 12.34<\/span> \n
\nb = 1e+20<\/span><\/code><\/pre>\n<\/p><\/div>\n

3\u3001complex\uff08\u590d\u6570\uff09<\/h3>\n
\n complex\u662f Python \u7684\u5185\u7f6e\u7c7b\u578b\uff0c\u7531\u5b9e\u6570\u90e8\u5206\u548c\u865a\u6570\u90e8\u5206\u6784\u6210\uff0c\u865a\u6570\u90e8\u90e8\u7528j\u6216\u8005J\u4f5c\u4e3a\u540e\u7f00\uff0c\u6bd4\u5982\uff1a\n <\/div>\n
\n
 a = 1.2 + 1.2j<\/span>
print(a = \", c1<\/span>)
print(\"a type is \", type(c1)<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
a = (1.2+1.2j)<\/span> \n
\na type is \n <\/type><\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n

\u5e8f\u5217\u7c7b\u578b<\/h2>\n

1\u3001list\uff08\u5217\u8868\uff09<\/h3>\n
\n list\uff08\u5217\u8868\uff09\u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0clist\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u4e0d\u540c\uff0c\u652f\u6301\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u548c\u5d4c\u5957\u5217\u8868\u672c\u8eab\u3002
\n
list\u7684\u5143\u7d20\u5728\u65b9\u62ec\u53f7 [] \u4e4b\u95f4\uff0c\u5e76\u7528\u9017\u53f7\u8fdb\u884c\u5206\u9694\uff0c\u683c\u5f0f\u4e3a\uff1a[\u5143\u7d20,\u5143\u7d20]\u3002
\n
list\u7684\u7d22\u5f15\u503c\u4ee5 0 \u4e3a\u5f00\u59cb\u503c\uff0c-1 \u4e3a\u4ece\u672b\u5c3e\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002
\n
list\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u8fdb\u884c\u622a\u53d6\uff0c\u88ab\u622a\u53d6\u540e\u4ecd\u7136\u662f\u4e00\u4e2a\u5217\u8868\u3002
\n
\u5982\u4e0b\u56fe\u6240\u793a\u3002\n <\/div>\n

Python \u6570\u636e\u7c7b\u578b_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n list\uff08\u5217\u8868\uff09\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 list1 = ['a','b','c','d','e']<\/span>
list2 = [123,'abcd']<\/span>
print(list1<\/span>) # \u8f93\u51fa\u5b8c\u6574\u5217\u8868<\/span>
print(list1[0]<\/span>) # \u8f93\u51fa\u5217\u8868\u7b2c1\u4e2a\u5143\u7d20<\/span>
print(list1[1:3]<\/span>) # \u4ece\u7b2c2\u4e2a\u5f00\u59cb\u8f93\u51fa\u5230\u7b2c3\u4e2a\u5143\u7d20<\/span>
print(list1[2:]<\/span>) # \u8f93\u51fa\u4ece\u7b2c3\u4e2a\u5143\u7d20\u5f00\u59cb\u7684\u6240\u6709\u5143\u7d20<\/span>
print(list2*2<\/span>) # \u8f93\u51fa2\u6b21\u5217\u8868<\/span>
print(list1+list2<\/span>) # \u8fde\u63a5\u5217\u8868<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
['a', 'b', 'c', 'd', 'e']<\/span> \n
\na<\/span> \n
\n['b', 'c']<\/span> \n
\n['c', 'd', 'e']<\/span> \n
\n[123, 'abcd', 123, 'abcd']<\/span> \n
\n['a', 'b', 'c', 'd', 'e', 123, 'abcd']<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>list\u53ef\u4ee5\u4f7f\u7528+\u64cd\u4f5c\u7b26\u8fdb\u884c\u62fc\u63a5\u3002\n <\/div>\n
\n \u540c\u65f6list\u7684\u5143\u7d20\u503c\u53ef\u4ee5\u6539\u53d8\uff0clist\uff08\u5217\u8868\uff09\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 a = ['a','b','c','d','e']<\/span>
a[0] = 'f'<\/span>
a[2:4] = ['a','b','c','d','e']<\/span>
print(a<\/span>)
a[2:4] = []<\/span>
print(a<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
['f', 'b', 1, 2, 'e']<\/span> \n
\n['f', 'b', 'e']<\/span><\/code><\/pre>\n<\/p><\/div>\n

2\u3001tuple(\u5143\u7ec4)<\/h3>\n
\n tuple\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8etuple\u7684\u5143\u7d20\u4e0d\u80fd\u4fee\u6539\u3002tuple\u5199\u5728\u5c0f\u62ec\u53f7 () \u91cc\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u9017\u53f7\u9694\u5f00\uff0ctuple\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u53ef\u4ee5\u4e0d\u540c\uff0c\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 tuple1 = ('a','b','c','d','e')<\/span>
tuple2 = (123,'abcd')<\/span>
print(tuple1<\/span>) # \u8f93\u51fa\u5b8c\u6574\u5143\u7ec4<\/span>
print(tuple1[0]<\/span>) # \u8f93\u51fa\u5143\u7ec4\u7b2c1\u4e2a\u5143\u7d20<\/span>
print(tuple1[1:3]<\/span>) # \u4ece\u7b2c2\u4e2a\u5f00\u59cb\u8f93\u51fa\u5230\u7b2c3\u4e2a\u5143\u7d20<\/span>
print(tuple1[2:]<\/span>) # \u8f93\u51fa\u4ece\u7b2c3\u4e2a\u5143\u7d20\u5f00\u59cb\u7684\u6240\u6709\u5143\u7d20<\/span>
print(tuple2*2<\/span>) # \u8f93\u51fa2\u6b21\u5143\u7ec4<\/span>
print(tuple1+tuple2<\/span>) # \u8fde\u63a5\u5143\u7ec4<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
['f', 'b', 1, 2, 'e']<\/span> \n
\n['f', 'b', 'e']<\/span> \n
\n('a', 'b', 'c', 'd', 'e')<\/span> \n
\na<\/span> \n
\n('b', 'c')<\/span> \n
\n('c', 'd', 'e')<\/span> \n
\n(123, 'abcd', 123, 'abcd')<\/span> \n
\n('a', 'b', 'c', 'd', 'e', 123, 'abcd')<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span> \u867d\u7136tuple\u7684\u5143\u7d20\u4e0d\u53ef\u6539\u53d8\uff0c\u4f46\u5b83\u53ef\u5305\u542b\u53ef\u53d8\u7684\u5bf9\u8c61\u3002tuple\u4e5f\u53ef\u4ee5\u88ab\u7d22\u5f15\u548c\u5207\u7247\uff0ctuple\u4e5f\u53ef\u4ee5\u4f7f\u7528+\u64cd\u4f5c\u7b26\u8fdb\u884c\u62fc\u63a5\u3002\n <\/div>\n

3\u3001range (\u8303\u56f4)<\/h3>\n
\n range \u7c7b\u4f3c\u4e8elist\uff0c\u662f\u8303\u56f4\u5217\u8868\uff0c\u4e0d\u8fc7\u91cc\u9762\u7684\u5143\u7d20\u53ea\u80fd\u662f\u6570\u5b57\uff0c\u5728for\u5faa\u73af\u4e2d\u5e94\u7528\u8f83\u591a\u3002\u5177\u4f53\u683c\u5f0frange(start, stop, step)\n <\/div>\n

start: \u8ba1\u6570\u4ece start \u5f00\u59cb\u3002\u9ed8\u8ba4\u662f\u4ece 0 \u5f00\u59cb\u3002<\/span>
\n stop: \u8ba1\u6570\u5230 stop \u7ed3\u675f\uff0c\u4f46\u4e0d\u5305\u62ec stop\u3002<\/span>
\n step\uff1a\u6b65\u957f\uff0c\u9ed8\u8ba4\u4e3a1\u3002<\/span> <\/p>\n

\n range(10) # \u7b49\u4ef7\u4e8erange(0,10)
\n
range(5,10) #5,6,7,8,9,10
\n
range(10,100,10) # 10\uff0c20\uff0c30...
\n
\u5177\u4f53\u5e94\u7528\n <\/div>\n
\n
 for i in range(5)<\/span>:
 print(i<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
0<\/span> \n
\n1<\/span> \n
\n2<\/span> \n
\n3<\/span> \n
\n4<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 for i in range(0,6,2)<\/span>:
 print(i<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
0<\/span> \n
\n2<\/span> \n
\n4<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 for i in range(5,0,-1)<\/span>:
 print(i<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
5<\/span> \n
\n4<\/span> \n
\n3<\/span> \n
\n2<\/span> \n
\n1<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 l = [1, \"range\", \"\u7684\", \"for\", \"\u5faa\u73af\"]<\/span>
for i in range(len(l))<\/span>:
 print(i<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
0<\/span> \n
\n1<\/span> \n
\n2<\/span> \n
\n3<\/span> \n
\n4<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>range\u4e00\u822c\u5e94\u7528\u5728for\u5faa\u73af\u4e2d\uff0c\u91cc\u7684\u5143\u7d20\u53ea\u80fd\u662f\u6570\u5b57\u3002\n <\/div>\n

\u6620\u5c04\u7c7b\u578b<\/h2>\n

dict\uff08\u5b57\u5178\uff09<\/h3>\n

dict\u662f\u65e0\u5e8f\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u5143\u7d20\u4ee5\u201c\u952e\u503c\u5bf9\uff08key-value\uff09\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u3002<\/span>
\n dict\u76f8\u5bf9\u4e8e\u5217\u8868\uff08list\uff09\u548c\u5143\u7ec4\uff08tuple\uff09\u662f\u6709\u5e8f\u7684\u5e8f\u5217\u3002<\/span>
\n dict\u4e2d\u7684\u5143\u7d20\u662f\u901a\u8fc7\u952e\u6765\u5b58\u53d6\u7684\uff0c\u5217\u8868\u662f\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u5b58\u53d6\u7684\u3002<\/span>
\n dict\u7528 { } \u6807\u8bc6\uff0c\u662f\u4e00\u4e2a\u952e(key) : \u503c(value) \u7684\u96c6\u5408\uff0ckey\u503c\u5fc5\u987b\u4fdd\u969c\u552f\u4e00\u3002<\/span>
\n dict\u683c\u5f0f\u4e3a{'key':'value1', 'key2':'value2', ..., 'keyn':valuen}<\/span> <\/p>\n

\n \u521b\u5efadict\n <\/div>\n
\n
 dict = {}<\/span>
dict['name'] = \"lidihuo\"<\/span>
dict[1] = \"Hello World\"<\/span>
dict1 = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
dict2 = {(1,2,3): 'great', 30: [1,2,3]}<\/span> # \u4f7f\u7528\u5143\u7ec4\u548c\u5217\u8868\u4f5c\u4e3akey\u548cvalue<\/span>
print(dict['name']<\/span>) # \u8f93\u51fa\u952e\u4e3a 'name' \u7684\u503c<\/span>
print(dict[1]<\/span>) # \u8f93\u51fa\u952e\u4e3a1\u7684\u503c<\/span>
print(dict1<\/span>) # \u8f93\u51fa\u5b8c\u6574\u7684\u5b57\u5178<\/span>
print(dict1.keys()<\/span>) # \u8f93\u51fa\u6240\u6709\u952e<\/span>
print(dict1.values()<\/span>) # \u8f93\u51fa\u6240\u6709\u503c<\/span>
print(dict2<\/span>) # \u8f93\u51fa\u5b8c\u6574\u7684\u5b57\u5178<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span> \n
\nHello World<\/span> \n
\n{'name': 'lidihuo', 'course': 'python', 'site': 'www.lidihuo.com'}<\/span> \n
\ndict_keys(['name', 'course', 'site'])<\/span> \n
\ndict_values(['lidihuo', 'python', 'www.lidihuo.com'])<\/span> \n
\n{(1, 2, 3): 'great', 30: [1, 2, 3]}<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n
\n \u8bbf\u95eedict\n <\/div>\n
\n dict\u662f\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002\u56e0\u4e3a\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u90fd\u4e0d\u56fa\u5b9a\uff0c\u6240\u4ee5\u5b57\u5178\u4e5f\u4e0d\u80fd\u50cf\u5217\u8868\u548c\u5143\u7ec4\u90a3\u6837\uff0c\u91c7\u7528\u4e0b\u6807\u6765\u8bbf\u95ee\u5143\u7d20\u3002
\n
dict\u5143\u7d20\u7684\u5177\u4f53\u683c\u5f0f\u4e3a\uff1adictname[key]\u3002dictname \u8868\u793adict\u53d8\u91cf\u7684\u540d\u5b57\uff0ckey \u8868\u793a\u952e\u540d\u3002\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print(dict['name']<\/span>) # \u8f93\u51fa\u952e\u5bf9\u5e94\u7684\u503c<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Python \u4e2d\u66f4\u63a8\u8350\u4f7f\u7528get() \u65b9\u6cd5\u6765\u83b7\u53d6\u6307\u5b9a\u952e\u5bf9\u5e94\u7684\u503c\u3002\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1adictname.get(key[,default])\uff0c\u5176\u4e2d\uff0cdictname \u8868\u793a\u5b57\u5178\u53d8\u91cf\u7684\u540d\u5b57\uff1bkey \u8868\u793a\u6307\u5b9a\u7684\u952e\uff1b default \u7528\u4e8e\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u952e\u4e0d\u5b58\u5728\u65f6\uff0c\u6b64\u65b9\u6cd5\u8fd4\u56de\u7684\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u4e0d\u624b\u52a8\u6307\u5b9a\uff0c\u4f1a\u8fd4\u56de None\u3002\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print(dict.get('name')<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u96c6\u5408\u7c7b\u578b<\/h2>\n

1\u3001set\u96c6\u5408<\/h3>\n
\n \u96c6\u5408\u548c\u5b57\u5178\u7c7b\u4f3c\u4e5f\u662f\u65e0\u5e8f\u7684\uff0c\u7528\u6765\u4fdd\u5b58\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0cPython\u4e2d\u7684\u96c6\u5408\u5c06\u6240\u6709\u5143\u7d20\u653e\u5728\u4e00\u5bf9\u5927\u62ec\u53f7 {} \u4e2d\uff0c\u76f8\u90bb\u5143\u7d20\u4e4b\u95f4\u7528\u201c,\u201d\u5206\u9694\uff0c\u683c\u5f0f\u4e3a\uff1a{item1,item2,...,itemn}\u3002
\n
\u96c6\u5408\u53ea\u80fd\u5b58\u50a8\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u578b\u3001\u6d6e\u70b9\u578b\u3001\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\uff0c\u65e0\u6cd5\u5b58\u50a8\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u8fd9\u4e9b\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\u3002\n <\/div>\n
\n \u521b\u5efaset\u96c6\u5408\n <\/div>\n
\n
 a = {1,'a',(1,2,3),'b',1.2}<\/span> \u7528{}\u65b9\u5f0f\u521b\u5efa<\/span> 
b = set((1,2,3,4,5))<\/span> set\u65b9\u5f0f\u521b\u5efa<\/span>
print(a<\/span>)
print(b<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{1, 1.2, (1, 2, 3), 'b', 'a'}<\/span> \n
\n{1, 2, 3, 4, 5}<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>\u521b\u5efa\u7a7a\u96c6\u5408\u53ea\u80fd\u4f7f\u7528 set() \u51fd\u6570\u5b9e\u73b0\uff0c\u4f7f\u7528\u4e00\u5bf9 {}\uff0c\u4f1a\u5c06\u89c6\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\n <\/div>\n
\n \u8bbf\u95eeset\u96c6\u5408\n <\/div>\n
\n \u8bbf\u95ee\u96c6\u5408\u5143\u7d20\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u5c06\u96c6\u5408\u4e2d\u7684\u6570\u636e\u9010\u4e00\u8bfb\u53d6\u51fa\u6765\u3002\n <\/div>\n
\n
 a = {'cat', 'dog', 'cow', 'pig'}<\/span>
print(a<\/span>)
for item in a:<\/span>
 print(item<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{'cow', 'cat', 'pig', 'dog'}<\/span> \n
\ncow<\/span> \n
\ncat<\/span> \n
\npig<\/span> \n
\ndog<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n
\n set\u96c6\u5408\u4e4b\u95f4\u8fd0\u7b97\n <\/div>\n
\n
 a = set('123456789')<\/span>
b = set('45678')<\/span>
print(a<\/span>)
print(a - b<\/span>) # a \u548c b \u7684\u5dee\u96c6<\/span>
print(a | b<\/span>) # a \u548c b \u7684\u5e76\u96c6<\/span>
print(a & b<\/span>) # a \u548c b \u7684\u4ea4\u96c6<\/span>
print(a ^ b<\/span>) # a \u548c b \u4e2d\u4e0d\u540c\u65f6\u5b58\u5728\u7684\u5143\u7d20<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{'7', '9', '3', '6', '1', '8', '5', '2', '4'}<\/span> \n
\n{'2', '9', '3', '1'}<\/span> \n
\n{'7', '9', '3', '6', '1', '8', '5', '2', '4'}<\/span> \n
\n{'7', '6', '8', '5', '4'}<\/span> \n
\n{'9', '3', '1', '2'}<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n

frozenset \u96c6\u5408<\/h3>\n
\n frozenset \u548cset\u76f8\u6bd4\u662f\u4e0d\u53ef\u53d8\u5e8f\u5217\uff0c\u6240\u6709\u5728set\u4e2d\u80fd\u6539\u53d8\u96c6\u5408\u672c\u8eab\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982 remove()\u3001discard()\u3001add() \u7b49\uff0cfrozenset \u90fd\u4e0d\u652f\u6301.
\n
\u901a\u8fc7\u547d\u4ee4dir(frozenset)\u53ef\u4ee5\u770b\u5230 frozenset \u96c6\u5408\u652f\u6301\u7684\u65b9\u6cd5\u6709\uff1a['copy', 'difference', 'intersection', 'isdisjoint', 'issubset', 'issuperset', 'symmetric_difference', 'union']
\n
\u5f53\u96c6\u5408\u7684\u5143\u7d20\u4e0d\u9700\u8981\u6539\u53d8\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 fronzenset \u66ff\u4ee3 set\uff0c\u8fd9\u6837\u66f4\u52a0\u5b89\u5168\u3002\n <\/div>\n
\n
 a = {'cat', 'dog', 'cow', 'pig'}<\/span>
b = frozenset(['lion', 'deer'])<\/span>
c = {'rabbit', 'rat'}<\/span>
# \u5411set\u96c6\u5408\u4e2d\u6dfb\u52a0frozenset<\/span>
a.add(b<\/span>)
print(b<\/span>)
# \u5411\u4e3aset\u96c6\u5408\u6dfb\u52a0\u5b50set\u96c6\u5408<\/span>
c.add(a<\/span>)
print(c<\/span>)
print(a | b<\/span>) # a \u548c b \u7684\u5e76\u96c6<\/span>
print(a & b<\/span>) # a \u548c b \u7684\u4ea4\u96c6<\/span>
print(a ^ b<\/span>) # a \u548c b \u4e2d\u4e0d\u540c\u65f6\u5b58\u5728\u7684\u5143\u7d20<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{'cow', frozenset({'deer', 'lion'}), 'dog', 'pig', 'cat'}<\/span> \n
\nTraceback (most recent call last):
File \"script.py\", line 8, in
c.add(a)
TypeError: unhashable type: 'set'
Exited with error status 1 <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>set \u96c6\u5408\u4e2d\u7684\u5143\u7d20\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c \u56e0\u4e3a set \u662f\u53ef\u53d8\u7684\uff0c\u6240\u4ee5\u4e0d\u80fd\u6dfb\u52a0\u5230set\u4e2d\uff0c\u800c\u6dfb\u52a0frozenset\u3002\u662f\u6ca1\u95ee\u9898\u7684\u3002\n <\/div>\n

\u5e03\u5c14\u7c7b\u578b<\/h2>\n

\u5e03\u5c14\u7c7b\u578b\u53ea\u6709True\u3001False\u4e24\u79cd\u503c\uff0c\u8981\u4e48\u662fTrue\uff0c\u8981\u4e48\u662fFalse\u3002<\/span>
\n \u5e03\u5c14\u503c\u53ef\u4ee5\u7528and\u3001or\u548cnot\u8fd0\u7b97\u3002<\/span>
\n \u5e03\u5c14\u503c\u7ecf\u5e38\u7528\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\u3002<\/span> <\/p>\n

\n
 >>> True or True
True
>>> True and False
False
>>> not Flase
True <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 if<\/span> 1>2:
 print('1>2'<\/span>)
elif<\/span>1==2:
 print('1>2'<\/span>)
else:<\/span>
 print('1<2'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
1<2<\/span><\/code><\/pre>\n<\/p><\/div>\n

8\u3001\u4e8c\u8fdb\u5236\u7c7b\u578b<\/h2>\n

bytes\uff08\u5b57\u8282\u4e32\uff09<\/h3>\n

bytes\u662f Python 3.x \u65b0\u589e\u7684\u7c7b\u578b\uff0c\u5b57\u7b26\u4e32\u7531\u82e5\u5e72\u4e2a\u5b57\u7b26\u7ec4\u6210\uff0c\u4ee5\u5b57\u7b26\u4e3a\u5355\u4f4d\u8fdb\u884c\u64cd\u4f5c<\/span>
\n \u5b57\u8282\u4e32\u548c\u5b57\u7b26\u4e32\u9664\u4e86\u64cd\u4f5c\u7684\u6570\u636e\u5355\u5143\u4e0d\u540c\u4e4b\u5916\uff0c\u5b83\u4eec\u652f\u6301\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u57fa\u672c\u76f8\u540c\u3002<\/span>
\n \u5b57\u8282\u4e32\u548c\u5b57\u7b26\u4e32\u90fd\u662f\u4e0d\u53ef\u53d8\u5e8f\u5217\uff0c\u4e0d\u80fd\u968f\u610f\u589e\u52a0\u548c\u5220\u9664\u6570\u636e<\/span>
\n bytes \u4ee5\u5b57\u8282\u5e8f\u5217\u7684\u5f62\u5f0f\uff08\u4e8c\u8fdb\u5236\u5f62\u5f0f\uff09\u6765\u5b58\u50a8\u6570\u636e\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u56fe\u7247\u3001\u97f3\u9891\u7b49\u3002<\/span> <\/p>\n

\n
 a = bytes()<\/span>
b = b'lidihuo'<\/span>
c = bytes('\u7acb\u5730\u8d27', encoding='UTF-8')<\/span>
d = \"\u7acb\u5730\u8d27\".encode('UTF-8')<\/span>
print(a<\/span>)
print(b<\/span>)
print(c<\/span>)
print(d<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
b''<\/span> \n
\nb'lidihuo'<\/span> \n
\nb'\\xe7\\xab\\x8b\\xe5\\x9c\\xb0\\xe8\\xb4\\xa7'<\/span> \n
\nb'\\xe7\\xab\\x8b\\xe5\\x9c\\xb0\\xe8\\xb4\\xa7'<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>\u975e ASCII \u5b57\u7b26\uff0cprint \u8f93\u51fa\u7684\u662f\u5b83\u7684\u5b57\u7b26\u7f16\u7801\u503c\uff08\u5341\u516d\u8fdb\u5236\u5f62\u5f0f\uff09\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u672c\u8eab\u3002\n <\/div>\n

\u5236\u5b9a\u6570\u636e\u7c7b\u578b<\/h2>\n
\n \u5982\u679c\u8981\u6307\u5b9a\u6570\u636e\u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6784\u9020\u51fd\u6570\uff1a\n <\/div>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u64cd\u4f5c<\/td>\n\u6570\u636e\u7c7b\u578b<\/td>\n<\/tr>\n
x = str(\"Hello World\")<\/td>\nstr<\/td>\n<\/tr>\n
x = int(20)<\/td>\nint<\/td>\n<\/tr>\n
x = float(20.5)<\/td>\nfloat<\/td>\n<\/tr>\n
x = complex(1j)<\/td>\ncomplex<\/td>\n<\/tr>\n
x = list((\"apple\", \"banana\", \"cherry\"))<\/td>\nlist<\/td>\n<\/tr>\n
x = tuple((\"apple\", \"banana\", \"cherry\"))<\/td>\ntupe<\/td>\n<\/tr>\n
x = range(6)<\/td>\nrange<\/td>\n<\/tr>\n
x = dict(name=\"John\", age=36)<\/td>\ndict<\/td>\n<\/tr>\n
x = set((\"apple\", \"banana\", \"cherry\"))<\/td>\nset<\/td>\n<\/tr>\n
x = frozenset((\"apple\", \"banana\", \"cherry\"))<\/td>\nfrozenset<\/td>\n<\/tr>\n
x = bool(5)<\/td>\nbool<\/td>\n<\/tr>\n
x = bytes(5)<\/td>\nbytes<\/td>\n<\/tr>\n
x = bytearray(5)<\/td>\nbytearray<\/td>\n<\/tr>\n
x = memoryview(bytes(5))<\/td>\nmemoryview<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n
 x = 1<\/span># int<\/span>
y = 1.2<\/span># float<\/span>
z = 1j<\/span># complex<\/span>
a = float(x)<\/span># \u8f6c\u6362\u79f0float<\/span>
b = int(y)<\/span># \u8f6c\u6362int<\/span>
c = complex(x)<\/span># \u8f6c\u6362\u79f0complex:<\/span>
print(a<\/span>)
print(b<\/span>)
print(c<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
1.0<\/span> \n
\n1<\/span> \n
\n (1+0j)<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6570\u636e\u7c7b\u578bzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1018","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1018"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1018"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1018\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}