复习Py基础知识

作者: KangSmit的算法那些事儿 | 来源:发表于2019-10-03 22:43 被阅读0次

今天补补基础知识

>>> (100+125j).real
100.0
>>> (100+125j).imag
125.0
>>> complex(100,124)
(100+124j)
>>> name = "我爱你,中国!"
>>> print(name)
我爱你,中国!
>>> type(name)
<class 'str'>
>>> "I"+"LOVE"+"YOU"
'ILOVEYOU'
>>> "I"+ "LOVE" +"YOU"
'ILOVEYOU'
>>> "I"+" LOVE"+" YOU"
'I LOVE YOU'
>>> ("I"+" LOVE"+" YOU")*3
'I LOVE YOUI LOVE YOUI LOVE YOU'
>>> (" I"+" LOVE"+" YOU")*3
' I LOVE YOU I LOVE YOU I LOVE YOU'
>>> (" I"+" LOVE"+" YOU")*10
' I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU I LOVE YOU'
>>> s = "Facing the sea, with spring blossoms! "
>>> print(s[0:16])
Facing the sea, 
>>> print(s[0:30])
Facing the sea, with spring bl
>>> s[:]
'Facing the sea, with spring blossoms! '
>>> s[0:16:1]
'Facing the sea, '
>>> s[0:16:2]
'Fcn h e,'
>>> s[0:16:3]
'Fi ee '
>>> len(s)
38
>>> ord("s")
115
>>> ord("a")
97
>>> ord("A")
65
>>> chr(65)
'A'
>>> chr(97)
'a'
>>> chr(96)
'`'
>>> chr(95)
'_'
>>> chr(64)
'@'
>>> s.title()
'Facing The Sea, With Spring Blossoms! '
>>> s = "facing the sea, with spring blossoms! "
>>> s.title()
'Facing The Sea, With Spring Blossoms! '
>>> s.upper()
'FACING THE SEA, WITH SPRING BLOSSOMS! '
>>> s.lower()
'facing the sea, with spring blossoms! '
>>> s.rstrip()
'facing the sea, with spring blossoms!'
>>> #rstrip删除字符串末尾空白处
>>> #lstrip删除字符串开头空白处
>>> s.lstrip()
'facing the sea, with spring blossoms! '
>>> s = "  facing the sea, with spring blossoms!  "
>>> s.rstrip()
'  facing the sea, with spring blossoms!'
>>> s.lstrip()
'facing the sea, with spring blossoms!  '
>>> s.strip()
'facing the sea, with spring blossoms!'
>>> x = 2016
>>> y = 2
>>> x//y
1008
>>> z = 100
>>> z//y
50
>>> x//z
20
>>> x%z
16
>>> y**3
8
>>> y**2
4
>>> name = input("请输入您的账号:")
请输入您的账号:google
>>> print(name)
google
>>> a= eval('2 + 10')
>>> a
12
>>> s = "facing the sea, with spring blossoms! "
>>> list(s)
['f', 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ']
>>> s
'facing the sea, with spring blossoms! '
>>> y = list(s)
>>> y
['f', 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ']
>>> y.append(2020)
>>> y
['f', 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ', 2020]
>>> y[0]
'f'
>>> y[1]
'a'
>>> len(y)
39
>>> y[39]
Traceback (most recent call last):
  File "<pyshell#104>", line 1, in <module>
    y[39]
IndexError: list index out of range
>>> y[38]
2020
>>> for i in y:
    print(i)

    
f
a
c
i
n
g
 
t
h
e
 
s
e
a
,
 
w
i
t
h
 
s
p
r
i
n
g
 
b
l
o
s
s
o
m
s
!
 
2020
>>> y.insert(1,1314)
>>> y
['f', 1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ', 2020]
>>> y.remove("2020")
Traceback (most recent call last):
  File "<pyshell#111>", line 1, in <module>
    y.remove("2020")
ValueError: list.remove(x): x not in list
>>> y
['f', 1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ', 2020]
>>> y.remove('2020')
Traceback (most recent call last):
  File "<pyshell#113>", line 1, in <module>
    y.remove('2020')
ValueError: list.remove(x): x not in list
>>> for i in y:
    print(i,end=" ")

    
f 1314 a c i n g   t h e   s e a ,   w i t h   s p r i n g   b l o s s o m s !   2020 
>>> y
['f', 1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ', 2020]
>>> y.remove('f')
>>> y
[1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ', 2020]
>>> y.remove(2020)
>>> y
[1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!', ' ']
>>> pop(y)
Traceback (most recent call last):
  File "<pyshell#121>", line 1, in <module>
    pop(y)
NameError: name 'pop' is not defined
>>> y.pop()
' '
>>> y
[1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's', '!']
>>> y.pop()
'!'
>>> y
[1314, 'a', 'c', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's']
>>> y.pop(2)
'c'
>>> y
[1314, 'a', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 's', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's']
>>> del y[20]
>>> y
[1314, 'a', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's']
>>> del y[1]
>>> y
[1314, 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's']
>>> y.count(s)
0
>>> y.count(36)
0
>>> y
[1314, 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'e', 'a', ',', ' ', 'w', 'i', 't', 'h', ' ', 'p', 'r', 'i', 'n', 'g', ' ', 'b', 'l', 'o', 's', 's', 'o', 'm', 's']
>>> len(y)
33
>>> y.count(32)
0
>>> y.count(1)
0
>>> a = [1,2,3,4,5,6,5,4,3,2,1,2,3,4]
>>> a.count(2)
3
>>> b = [0,9,8,7,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 2, 3, 4, 0, 9, 8, 7, 6]
>>> a[3]
4
>>> 21,
(21,)
>>> 21,25
(21, 25)
>>> 1,1,1,2,2,3,3,34,4,54,5,6,7,7,8,8,9,0
(1, 1, 1, 2, 2, 3, 3, 34, 4, 54, 5, 6, 7, 7, 8, 8, 9, 0)
>>> lis = [1,34,5,6,8,9,9,3]
>>> tuple(lis)
(1, 34, 5, 6, 8, 9, 9, 3)
>>> a = tuple(lis)
>>> a
(1, 34, 5, 6, 8, 9, 9, 3)
>>> a
(1, 34, 5, 6, 8, 9, 9, 3)
>>> max(a)
34
>>> min(a)
1
>>> len(a)
8
>>> dict = {"白菜":3.5,"豌豆":14,"香菜":4,"小葱":3,"猪肉":18}
>>> dict[5] = 99
>>> dict
{'白菜': 3.5, '豌豆': 14, '香菜': 4, '小葱': 3, '猪肉': 18, 5: 99}
>>> dict["鸡肉"] = 23
>>> dict
{'白菜': 3.5, '豌豆': 14, '香菜': 4, '小葱': 3, '猪肉': 18, 5: 99, '鸡肉': 23}
>>> del dict["5"]
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    del dict["5"]
KeyError: '5'
>>> del dict[5]
>>> dict
{'白菜': 3.5, '豌豆': 14, '香菜': 4, '小葱': 3, '猪肉': 18, '鸡肉': 23}

>>> a = dict
>>> a
{'白菜': 3.5, '豌豆': 14, '香菜': 4, '小葱': 3, '猪肉': 18, '鸡肉': 23}
>>> for k,v in a:
    print("\nkey:" + k)
    print("value:" + v)

    

key:白
value:菜

key:豌
value:豆

key:香
value:菜

key:小
value:葱

key:猪
value:肉

key:鸡
value:肉
>>> dict.items()
dict_items([('白菜', 3.5), ('豌豆', 14), ('香菜', 4), ('小葱', 3), ('猪肉', 18), ('鸡肉', 23)])
>>> dict.keys()
dict_keys(['白菜', '豌豆', '香菜', '小葱', '猪肉', '鸡肉'])
>>> dict.values()
dict_values([3.5, 14, 4, 3, 18, 23])
>>> dict.clear()
>>> dict
{}
>>> a
{}
>>> a = {'白菜': 3.5, '豌豆': 14, '香菜': 4, '小葱': 3, '猪肉': 18, '鸡肉': 23}
>>> a.get("白菜")
3.5
#集合
>>> dict.keys()
dict_keys([])
>>> a.keys()
dict_keys(['白菜', '豌豆', '香菜', '小葱', '猪肉', '鸡肉'])
>>> set(a)
{'香菜', '豌豆', '鸡肉', '白菜', '猪肉', '小葱'}
>>> b = set(a)
>>> b
{'香菜', '豌豆', '鸡肉', '白菜', '猪肉', '小葱'}
>>> c = set(a.values())
>>> c
{3, 3.5, 4, 14, 18, 23}
>>> b = set(a.keys())
>>> b
{'香菜', '豌豆', '白菜', '猪肉', '小葱', '鸡肉'}
>>> set = {(2,4),(7,1),(0,4),(6,8)}
>>> set
{(0, 4), (6, 8), (2, 4), (7, 1)}


>>> for i in set:
    print(i,end=',')

    
(0, 4),(6, 8),(2, 4),(7, 1),
>>> for i in set:
    print(i)

    
(0, 4)
(6, 8)
(2, 4)
(7, 1)

>>> set1
{(0, 4), (6, 8), (2, 4), (7, 1)}
>>> set1 - set2
{(7, 1), (2, 4), (6, 8), (0, 4)}
>>> set1
{(0, 4), (6, 8), (2, 4), (7, 1)}
>>> set2
{(-2, 3), (7, 5), (9, -1), (4, 5)}
>>> set1&set2
set()
>>> set2 = {(2, -3),(-7, 5),(-4, 5),(9, -1)}
>>> #交集
>>> set2&set3
Traceback (most recent call last):
  File "<pyshell#85>", line 1, in <module>
    set2&set3
NameError: name 'set3' is not defined
>>> set3 = {(2, -3),(-7, 5),(-4, 5),(9, -1)}
>>> set2 = {(-2, 3),(7, 5),(4, 5),(9, -1)}
>>> set1 = set
>>> set1&set2
set()
>>> set1&set3
set()
>>> set2&set3
{(9, -1)}
>>> #非共同元素,类似补集
>>> set1^set2
{(6, 8), (7, 1), (-2, 3), (4, 5), (7, 5), (9, -1), (0, 4), (2, 4)}
>>> set1|set2
{(6, 8), (7, 1), (-2, 3), (4, 5), (7, 5), (9, -1), (0, 4), (2, 4)}
>>> set1|set2|set3#并集
{(-4, 5), (2, -3), (6, 8), (7, 1), (-2, 3), (4, 5), (-7, 5), (7, 5), (9, -1), (0, 4), (2, 4)}
>>> s = set1
>>> t = set2
>>> s.union(t)
{(6, 8), (7, 1), (-2, 3), (4, 5), (7, 5), (9, -1), (0, 4), (2, 4)}
>>> #并集s.union(t)

相关文章

网友评论

    本文标题:复习Py基础知识

    本文链接:https://www.haomeiwen.com/subject/fygayctx.html