py基础-2

2020-07-27  本文已影响0人  JSFnull

每日一分享

1.数据类型
(1)数字


image.png
>>> 11
11
>>> 0o11   ---1* 8的一次方 + 1* 8的0次方
9
>>> 0x11   ---1* 16的一次方 + 1* 16的0次方
17
>>> 0b11
3
>>> oct(20)  -转8进制
'0o24'
>>> hex(20)  -转16进制
'0x14'
>>> bin(20)  -转2进制
'0b10100'
10000秒化为时分秒
>>> divmod(10000,60)
(166, 40)
>>> divmod(166,60)
(2, 46)

(2)字符串


image.png
>>> words = """
... xixi
... xiix
... xixixi
... """

>>> print(words)
xixi
xiix
xixixi
>>> words  
'\nxixi\nxiix\nxixixi\n'
在python解释器中,直接写变量,将会输出内部存储的样式

(3) 字符串切片

>>> py_str = 'python'
>>> py_str[0]
'p'
>>> py_str[-1]
'n'
>>> py_str[-2]
'o'
>>> py_str[4]
'o'
>>> py_str[2:4]
'th'
>>> py_str[2:] ----或者[2:2000000]比下标范围大即可
'thon'
>>> py_str[:4]
'pyth'
>>> py_str[::2]-----步长值为2  偶数 ---开头默认为0
'pto'
>>> py_str[1::2]
'yhn'
>>> len(py_str) ---算长度
6
>>> 'py' in py_str
True
>>> 'to' in py_str---需要连续
False

(4) 字符串拼接

>>> c = 'is me'
>>> py_str + '' + c 
'pythonis me'
>>> py_str + ' ' + c 
'python is me'
>>> '*' * 10
'**********'
>>> py_str * 3
'pythonpythonpython'

(5) 列表


image.png
>>> alist = [10 , 20 , 'tom' , 'yyf' , [1,2]]
>>> len(alist)
5
>>> 20 in alist
True
>>> alist[-1]
[1, 2]
>>> alist[2:4]
['tom', 'yyf']
>>> alist + [100]
[10, 20, 'tom', 'yyf', [1, 2], 100]
>>> alist * 2
[10, 20, 'tom', 'yyf', [1, 2], 10, 20, 'tom', 'yyf', [1, 2]]
>>> alist[-1]=50
>>> alist
[10, 20, 'tom', 'yyf', 50] ---修改内容
>>> c = alist + [100]
>>> c
[10, 20, 'tom', 'yyf', 50, 100]
>>> alist.append('shachao')  ----增加列表数值
>>> alist
[10, 20, 'tom', 'yyf', 50, 'shachao']
>>> alist.
alist.append(   alist.count(    alist.insert(   alist.reverse(  
alist.clear(    alist.extend(   alist.pop(      alist.sort(     
alist.copy(     alist.index(    alist.remove(
alist[1:4:2]----->步长

6.浅copy深copy----对应软连接和硬链接
浅copy只拷贝表面一层,不会拷贝深层的东西,像下面的列表套列表,只会拷贝第一层
深copy则是完全是新的一份数据,独自占用一份内存
当浅copy的时候,[ [] ] 列表套列表 ,里面的列表不是一份真数据,只是一个指向地址,指向数据所在的内存地址。
---应用在相同的卡设置联卡, 设置初始值的时候用

>>> person = ["xiaohong",["a",200]]
>>> person
['xiaohong', ['a', 200]]
>>> p1 = copy.copy(person)
>>> p2 = list(person)
>>> p3 = person[:]
>>> p1[0] = "aya"
>>> p2[0] = "aoa"
>>> p3[0] = "apa"
>>> print(p1, p2, p3)
['aya', ['a', 200]] ['aoa', ['a', 200]] ['apa', ['a', 200]]
>>> person
['xiaohong', ['a', 200]]
>>> person[1][1] = 100
>>> print(p1, p2, p3)
['aya', ['a', 100]] ['aoa', ['a', 100]] ['apa', ['a', 100]]
>>>

>>> import random
>>> alist = [random.randint(1,100) for i in range(5)]
>>> alist
[3, 68, 37, 95, 99]
>>> alist.append([1,2])
>>> alist
[3, 68, 37, 95, 99, [1, 2]]
>>> alist[5][0] = 3
>>> alist
[3, 68, 37, 95, 99, [3, 2]]
>>> blist = alist.copy()
>>> blist
[3, 68, 37, 95, 99, [3, 2]]
>>> import copy
>>> clist = copy.deepcopy(alist)
>>> clist
[3, 68, 37, 95, 99, [3, 2]]
>>> alist[2] = 4
>>> alist
[3, 68, 4, 95, 99, [3, 2]]
>>> dlist = alist.copy()
>>> dlist
[3, 68, 4, 95, 99, [3, 2]]
>>> alist[2] = 5
>>> dlist
[3, 68, 4, 95, 99, [3, 2]]
>>> alist[5][0] = 4
>>> dlist
[3, 68, 4, 95, 99, [4, 2]]
>>> clist
[3, 68, 37, 95, 99, [3, 2]]

>>> elist = alist[:]
>>> elist
[3, 68, 5, 95, 99, [4, 2]]
>>> flist = copy.copy(alist)
>>> flist
[3, 68, 5, 95, 99, [4, 2]]
>>> glist = list(alist)
>>> glist
[3, 68, 5, 95, 99, [4, 2]]

上一篇下一篇

猜你喜欢

热点阅读