PYTHON之路

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PYTHON之路相关的知识,希望对你有一定的参考价值。


python类型

 

 

技术分享


技术分享
其中pypy这种类型的比上面几种要快,因为解释器在编译的时候,是编译成字节码,紧接着编译成机器码。所以运行时直接运行机器码。它就是编译要慢,运行快。
上面几种呢,解释器在编译的时候,是编译成字节码。然后运行时运行字节码文件,虚拟机编译成机器码,再交由CPU执行。编译快,运行慢。


编码
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCII码最多只能表示 256 个符号。
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...

 

技术分享






接收传入参数
python file.py parameters

由sys模块处理, sys.argv表示所有参数
#cat test.py
import sys
print(sys.argv)
print ("the first parameter is the file name: %s, the second parameter is %s" %(sys.argv[0],sys.argv[1]))

#python test.py 1
[‘test.py‘,‘1‘]
the first parameter is the file name: test.py, the second parameter is 1


字符串拼接 万恶的+号
newString=‘alex‘
[‘a‘,‘l‘,‘e‘,‘x‘]
[‘a‘,‘l‘,‘e‘,‘x‘]+‘y‘ ==> [‘a‘,‘l‘,‘e‘,‘x‘,‘y‘] , 原来的[‘a‘,‘l‘,‘e‘,‘x‘]还存在。


元组
元组的元素不可更改, 元组的元素的元素可以更改
>>> a=(1,2,{‘key‘:‘value‘})
>>> a[2][‘key‘]=‘ok‘
>>> print a
(1, 2, {‘key‘: ‘ok‘})
>>> a[2]=3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘tuple‘ object does not support item assignment
>>>



字典
键 keys
值 values
键值对 items
>>> a={‘key1‘:‘value1‘,‘key2‘:‘value2‘}
>>> a.keys()
[‘key2‘, ‘key1‘]
>>> a.values()
[‘value2‘, ‘value1‘]
>>> a.items()
[(‘key2‘, ‘value2‘), (‘key1‘, ‘value1‘)]
>>>



__name__

技术分享



 

技术分享


object:

 

技术分享



int常用内置功能

技术分享   技术分享    技术分享  



>>> age=-18
>>> age.bit_length()
5
>>> age.__abs__()
18
>>> abs(age)
18
>>> age.__add__(100)
82
>>> age.__divmod__(4)
(-5, 2)
>>> age.__abs__().__divmod__(4)
(4, 2)

python3才有__eq__()
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> age=18
>>> age.__eq__(19)
False

python3运算
>>> 5/6
0.8333333333333334
>>> 5//6
0
>>> re = 5.__floordiv__(6)
File "<stdin>", line 1
re = 5.__floordiv__(6)
^
SyntaxError: invalid syntax
>>> result=age.__floordiv__(8)
>>> print(result)
2
>>> result=5.__floordiv__(8)
File "<stdin>", line 1
result=5.__floordiv__(8)
^
SyntaxError: invalid syntax
>>> a=5
>>> result=a.__floordiv__(8)
>>> print(result)
0


>>> print type(age.__float__())
<type ‘float‘>
>>>






str
__contains__(‘string‘)
capitalize()
casefold()
center(width,fillchar=None)
ljust(width,fillchar=None)
rjust(width,fillchar=None)
count(sub,start=None,end=None)
encode
endswith(suffix,start=None,end=None)

>>> name=‘newbility‘
>>> name.__contains__(‘go‘)
False
>>> name.__contains__(‘eo‘)
False
>>> name.__contains__(‘e‘)
True
>>> name.capitalize()
‘Newbility‘
>>> name.capitalize().casefold()
‘newbility‘
>>> name.center(20,‘*‘)
‘*****newbility******‘
>>> name.count(‘i‘)
2
>>> name.count(‘i‘,0,5)
1
>>> name.endswith(‘y‘)
True
>>> name.endswith(‘x‘)
False
>>> name.endswith(‘w‘,0,2)
False
>>> name.endswith(‘w‘,0,3)
True
>>> name.encode(‘gbk‘)
b‘newbility‘
>>> name.encode(‘utf-8‘)
b‘newbility‘
>>> name=‘牛‘
>>> print(name)

>>> name.encode(‘utf-8‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘nam‘ is not defined
>>> name.encode(‘utf-8‘)
b‘\\xc5\\xa3‘
>>> name.encode(‘gbk‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\\u0163‘ in position 0: illegal multibyte sequence
>>> name.encode(‘unicode‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>> name.encode(‘ascii‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: ‘ascii‘ codec can‘t encode character ‘\\u0163‘ in position 0: ordinal not in range(128)
>>> name
‘牛‘
>>> name.ljust(20,‘*‘)
‘牛*******************‘
>>> name.ljust(2,‘*‘)
‘牛*‘
>>> name.center(20,‘*‘)
‘*********牛**********‘
>>> name.rjust(2,‘*‘)
‘*牛‘
>>>



str
expandtabs(tabsize=8)
find(sub,start=None,end=None)
rfind(sub,start=None,end=None)
index(sub,start=None,end=None)
rindex
format(string)

 

技术分享

join(iterable)
lower()
lstrip(chars=None)
rstrip(chars=None)
partition(sep)
rpartition
replace(old,new,count=None)
split(sep=None,maxsplit=-1)
rsplit
startswith(prefix,start=None,end=None)
swapcase()
title()
upper()
zfill(width)




>>> name=‘new\\tbility‘
>>> name
‘new\\tbility‘
>>> name.expandtabs()
‘new bility‘
>>> name.expandtabs(2)
‘new bility‘
>>> name=‘newbility‘
>>> name.find(‘ew‘)
1
>>> name.find(‘o‘)
-1
>>> name.rfind(‘ew‘)
1
>>> name.rfind(‘w‘)
2
>>> name.rfind(‘i‘)
6
>>> name.index(‘ew‘)
1
>>> name.index(‘o‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> name.rindex(‘i‘)
6
>>> "_".join(name)
‘n_e_w_b_i_l_i_t_y‘
>>> name.lower()
‘newbility‘
>>> name.upper()
‘NEWBILITY‘
>>> name.swapcase()
‘NEWBILITY‘
>>> name.strip(‘i‘)
‘newbility‘
>>> "_".join(name).strip(‘_‘)
‘n_e_w_b_i_l_i_t_y‘
>>> "_".join(name).strip(_)
‘‘
>>> name.strip(‘y‘)
‘newbilit‘
>>> name.rstrip(‘y‘)
‘newbilit‘
>>> name.rstrip(‘i‘)
‘newbility‘
>>> name.lstrip(‘i‘)
‘newbility‘
>>> name.lstrip(‘n‘)
‘ewbility‘
>>> name.lstrip(‘e‘)
‘newbility‘
>>> name.partition(‘i‘)
(‘newb‘, ‘i‘, ‘lity‘)
>>> name.rpartition(‘i‘)
(‘newbil‘, ‘i‘, ‘ty‘)
>>> name.replace(‘i‘,‘x‘,1)
‘newbxlity‘
>>> name.replace(‘i‘,‘x‘,2)
‘newbxlxty‘
>>> name.split(‘i‘)
[‘newb‘, ‘l‘, ‘ty‘]
>>> name.rsplit(‘i‘)
[‘newb‘, ‘l‘, ‘ty‘]
>>> name.startswith(
...
KeyboardInterrupt
>>> name.startswith(‘e‘)
False
>>> name.startswith(‘e‘,1)
True
>>> name.startswith(‘e‘,2)
False
>>> name.title()
‘Newbility‘
>>> name.zfill()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: zfill() takes exactly 1 argument (0 given)
>>> name.zfill(10)
‘0newbility‘
>>> name.zfill(1)
‘newbility‘
>>> name.zfill(20)
‘00000000000newbility‘
>>> name.zfill(20).isalnum()
True
>>>





技术分享 技术分享

 

技术分享 





str中的maketrans and translate



技术分享



list

 


技术分享

pop返回从列表里拿出去的值,列表也更改了


>>> a
[‘1‘, ‘1‘]
>>> li = list((‘aa‘,‘bb‘,‘cc‘,8,9))
>>> li
[‘aa‘, ‘bb‘, ‘cc‘, 8, 9]
>>> li.append(a)
>>> li
[‘aa‘, ‘bb‘, ‘cc‘, 8, 9, [‘1‘, ‘1‘]]
>>> a.clear()
>>> a
[]
>>> b=a
>>> b
[]
>>> li.count(‘1‘)
0
>>> li.count(‘aa‘)
1
>>> li.append(‘aa‘)
>>> li.count(‘aa‘)
2
>>> li.extend(li)
>>> li
[‘aa‘, ‘bb‘, ‘cc‘, 8, 9, [], ‘aa‘, ‘aa‘, ‘bb‘, ‘cc‘, 8, 9, [], ‘aa‘]
>>> li.index(‘aa‘)
0
>>> li.insert(‘bb‘,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘str‘ object cannot be interpreted as an integer
>>> li.insert(1,‘bb‘)
>>> li
[‘aa‘, ‘bb‘, ‘bb‘, ‘cc‘, 8, 9, [], ‘aa‘, ‘aa‘, ‘bb‘, ‘cc‘, 8, 9, [], ‘aa‘]
>>> li.pop()
‘aa‘
>>> li.pop()
[]
>>> c=li.pop()
>>> c
9
>>> li
[‘aa‘, ‘bb‘, ‘bb‘, ‘cc‘, 8, 9, [], ‘aa‘, ‘aa‘, ‘bb‘, ‘cc‘, 8]
>>> li.remove([])
>>> li
[‘aa‘, ‘bb‘, ‘bb‘, ‘cc‘, 8, 9, ‘aa‘, ‘aa‘, ‘bb‘, ‘cc‘, 8]
>>> li.reverse()
>>> li
[8, ‘cc‘, ‘bb‘, ‘aa‘, ‘aa‘, 9, 8, ‘cc‘, ‘bb‘, ‘bb‘, ‘aa‘]
>>> li.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int() #python3 里字符串和数字放一起排不了序了。
>>>


>>> list(1,2,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (3 given)
>>> list((1,2,4))
[1, 2, 4]
>>> a=list((1,2,4))
>>> a
[1, 2, 4]
>>> a=list([1,2,3])
>>> a
[1, 2, 3]
>>> a=list(‘1‘)
>>> a
[‘1‘]
>>> a=list(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘int‘ object is not iterable
>>>




tuple 注意count and index方法



dict
clear()
copy()
fromkeys(seq)
get(k,d=None)
items()
keys()
values()
pop(k,d=None)
popitem()
setdefault(k,d=None)
update(E=None)


>>> dic
{‘k1‘: [77, 88, 99], ‘k2‘: [99]}
>>> dic1=dic.copy()
>>> dic1
{‘k1‘: [77, 88, 99], ‘k2‘: [99]}
>>> dic.clear()
>>> dic
{}
>>> dic1
{‘k1‘: [77, 88, 99], ‘k2‘: [99]}
>>> dic1.get(‘k1‘)
[77, 88, 99]
>>> dic1.get(‘k3‘)
>>> print (dic1.get(‘k3‘))
None
>>> dic1.keys()
dict_keys([‘k1‘, ‘k2‘])
>>> dic1.items()
dict_items([(‘k1‘, [77, 88, 99]), (‘k2‘, [99])])
>>> dic1.values()
dict_values([[77, 88, 99], [99]])
>>> dic1.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pop expected at least 1 arguments, got 0
>>> dic1.pop(‘key3‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: ‘key3‘
>>> dic1.pop(‘k2‘)
[99]
>>> dic1
{‘k1‘: [77, 88, 99]}
>>> dic1.setdefault(‘k3‘,‘new‘)
‘new‘
>>> dic1
{‘k1‘: [77, 88, 99], ‘k3‘: ‘new‘}
>>> dic1.update(dic)
>>> dic1
{‘k1‘: [77, 88, 99], ‘k3‘: ‘new‘}
>>> dic1.update([1,23])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
>>> dic1.update((1,2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
>>> dic1.update({1:1,2:2})
>>> dic1
{1: 1, 2: 2, ‘k1‘: [77, 88, 99], ‘k3‘: ‘new‘}

>>> a
{‘bb‘: ‘bbb‘, ‘aa‘: ‘aaa‘}
>>> a.fromkeys([‘bb‘,‘aa‘])
{‘bb‘: None, ‘aa‘: None}
>>> a.fromkeys([‘bb‘,‘aa‘],‘iii‘)
{‘bb‘: ‘iii‘, ‘aa‘: ‘iii‘}
>>> a.fromkeys([‘bb‘,‘aa‘,‘cc‘],‘iii‘)
{‘cc‘: ‘iii‘, ‘bb‘: ‘iii‘, ‘aa‘: ‘iii‘}
>>> a.fromkeys([‘cc‘],‘iii‘)
{‘cc‘: ‘iii‘}
>>>







以上是关于PYTHON之路的主要内容,如果未能解决你的问题,请参考以下文章

Python 都有哪些好的 Web 框架

刚入门学习Python有哪些比较好的学习资源吗?

刚入门学习Python有哪些比较好的学习资源吗?

在Python环境下比较好的工作流引擎是啥?

有哪些比较好的个人知识管理工具,推荐下

Python 都有哪些好的 Web 框架