20190814 Python 练习记录

>>> d={'michael':95,'bob':75,'Tracy':85}
>>> d
{'michael': 95, 'bob': 75, 'Tracy': 85}
>>> d['bob']
75
>>> d['bob']=77
>>> d
{'michael': 95, 'bob': 77, 'Tracy': 85}
>>> jac in d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'jac' is not defined
>>> 'jc' in d
False
>>> d.get('jc',9)
9
>>> s=set([24,2,3,4,1,5,6,78,3,4])
>>> s
{1, 2, 3, 4, 5, 6, 78, 24}
>>> s.add(2)
>>> s
{1, 2, 3, 4, 5, 6, 78, 24}
>>> s.add('r')
>>> s
{1, 2, 3, 4, 5, 6, 'r', 78, 24}
>>> s.add('ui')
>>> s
{1, 2, 3, 4, 5, 6, 'r', 78, 'ui', 24}
>>> s.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 7
>>> s.remove(78)
>>> s
{1, 2, 3, 4, 5, 6, 'r', 'ui', 24}
>>> s2=set([2,4,5,34,3,4])
>>> s2
{2, 3, 34, 4, 5}
>>> s and s2
{2, 3, 34, 4, 5}
>>> s &s2
{2, 3, 4, 5}
>>> a = [33,5,54,34,7,64,34,2576,]
>>> a.sort()
>>> a
[5, 7, 33, 34, 34, 54, 64, 2576]
>>> a='abc'
>>> a.replace('a','A')
'Abc'
>>> a
'abc'
>>> aa=a.replace('a','A')
>>> aa
'Abc'
>>> aa
'Abc'
>>> a
'abc'
>>> def enroll(name,gender):
... print('name:',name)
  File "<stdin>", line 2
    print('name:',name)
        ^
IndentationError: expected an indented block
>>> def enroll(name,gender):
...     print('nmae:',name)
...     print('gender:',gender)
...
>>> enroll('jk','f')
nmae: jk
gender: f
>>> def add_end(L=[])
  File "<stdin>", line 1
    def add_end(L=[])
                    ^
SyntaxError: invalid syntax
>>> def add_end(L=[]):
...     L.append('end')
...     return L
...
>>> add_end()
['end']
>>> add_end()
['end', 'end']
>>> add_end()
['end', 'end', 'end']
>>> def add_end(L=[]):
...     if L is None
  File "<stdin>", line 2
    if L is None
               ^
SyntaxError: invalid syntax
>>> def add_end(L=[]):
...     if L is None:
...             L =[]
...     L.append('end')
...     return L
...
>>> add_end()
['end']
>>> add_end()
['end', 'end']
>>> >>> def add_end(L=[]):
  File "<stdin>", line 1
    >>> def add_end(L=[]):
     ^
SyntaxError: invalid syntax
>>> ...     if L is None:
  File "<stdin>", line 1
    ...     if L is None:
                        ^
SyntaxError: invalid syntax
>>> ...             L =[]
  File "<stdin>", line 1
    ...             L =[]
                    ^
SyntaxError: invalid syntax
>>> ...     L.append('end')
  File "<stdin>", line 1
    ...     L.append('end')
            ^
SyntaxError: invalid syntax
>>> ...     return L
  File "<stdin>", line 1
    ...     return L
                 ^
SyntaxError: invalid syntax
>>>
>>>  def add_end(L=[]):
  File "<stdin>", line 1
    def add_end(L=[]):
    ^
IndentationError: unexpected indent
>>> def add_end(L=Null):
...     if L is Null:
...             L =[]
...     L.append('end')
...     return L
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Null' is not defined
>>> add_end()
['end', 'end', 'end']
>>> add_end()
['end', 'end', 'end', 'end']
>>> add_end()
['end', 'end', 'end', 'end', 'end']
>>> add_end()
['end', 'end', 'end', 'end', 'end', 'end']
>>> add_end()
['end', 'end', 'end', 'end', 'end', 'end', 'end']
>>> def add_en(L=Null):
...     if L is Null:
...             L = []
...     L.append('end')
...     return L
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Null' is not defined
>>> def add_en(L=None):
...     if L is None:
...             L =[]
...     L.append('END')
...     return L
...
>>> add_en
<function add_en at 0x0000026482CDC1E0>
>>> add_en
<function add_en at 0x0000026482CDC1E0>
>>> add_en
<function add_en at 0x0000026482CDC1E0>
>>> add_en()
['END']
>>> add_en()
['END']
>>> add_en()
['END']
>>> add_en()
['END']
>>> add_en(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in add_en
AttributeError: 'int' object has no attribute 'append'
>>> add_en('23')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in add_en
AttributeError: 'str' object has no attribute 'append'
>>> add_en('2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in add_en
AttributeError: 'str' object has no attribute 'append'
>>> add_en([1,'dagjlsa'])
[1, 'dagjlsa', 'END']
>>> add_en([1,'dagjlsa'])
[1, 'dagjlsa', 'END']
>>> def calc(*number)
  File "<stdin>", line 1
    def calc(*number)
                    ^
SyntaxError: invalid syntax
>>> def calc(*number):
...     sum = 0
...     for n in numbers:
...             sum =sum +n*n
...     return sum
...
>>> calc([1,2,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in calc
NameError: name 'numbers' is not defined
>>> calc(1,2,3,45)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in calc
NameError: name 'numbers' is not defined
>>> calc(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in calc
NameError: name 'numbers' is not defined
>>> def calc(*number):
...     sum = 0
...     for n in number:
...             sum =sum +n*n
...     return sum
...
>>> calc([1,3,4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in calc
TypeError: can't multiply sequence by non-int of type 'list'
>>> calc(1,3,4)
26
>>> calc({1,3,4})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in calc
TypeError: unsupported operand type(s) for *: 'set' and 'set'
>>> x=[2,3,4]
>>> calc(x[0],x[1],x[2])
29
>>> x={3,1,2.5}
>>> calc(x[0],x[1],x[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> calc(x[0],x[1],x[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> xx={1,2,3}
>>> calc(xx[0],xx[1],xx[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> def person(name,age,**kw):
...     print('name:',name,'age:',age,'other:',kw)
...
>>> person('bob',30)
name: bob age: 30 other: {}
>>> person('bxd',340,xoh)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xoh' is not defined
>>> person('bxd',340,'xoh')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: person() takes 2 positional arguments but 3 were given
>>> person('bxd',340,x=1)
name: bxd age: 340 other: {'x': 1}
>>> person('bxd',340,ci='xdin',hds=89)
name: bxd age: 340 other: {'ci': 'xdin', 'hds': 89}
>>> def person(n,a,*,c,j):
...     print(n,a,c,j)
...
>>> person('jk',24,city='1',j='34r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: person() got an unexpected keyword argument 'city'
>>> person('jk',24,c='1',j='34r')
jk 24 1 34r
>>> person('jk',24,c='1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: person() missing 1 required keyword-only argument: 'j'
>>> def fact(n):
...     if n = 1:
  File "<stdin>", line 2
    if n = 1:
         ^
SyntaxError: invalid syntax
>>> def fact(n):
...     if n=1:
  File "<stdin>", line 2
    if n=1:
        ^
SyntaxError: invalid syntax
>>> def fact(n):
...     if n==1:
...             return 1
...     else:
...             return n*fact(n-1)
...
>>> fact(5)
120
>>> fact(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in fact
  File "<stdin>", line 5, in fact
  File "<stdin>", line 5, in fact
  [Previous line repeated 994 more times]
  File "<stdin>", line 2, in fact
RecursionError: maximum recursion depth exceeded in comparison
>>> fact(20)
2432902008176640000
>>> fact(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
>>> fact(1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in fact
  File "<stdin>", line 5, in fact
  File "<stdin>", line 5, in fact
  [Previous line repeated 994 more times]
  File "<stdin>", line 2, in fact


>>> ll=['a',3,4,5,6,78,89]
>>> ll[0:2]
['a', 3]
>>> ll
['a', 3, 4, 5, 6, 78, 89]
>>> ll[4:5]
[6]
>>> ll[3:5]
[5, 6]
>>> ll[-5:-1]
[4, 5, 6, 78]
>>> ll[-1]
89
>>> ll[-3]
6
>>> ll[-2:-1]
[78]
>>> ls=range(100)
>>> ls
range(0, 100)
>>> ls=lis(range(100))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'lis' is not defined
>>> ls=list(range(100))
>>> ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> ls[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> ll[-2]
78
>>> ll[-2:]
[78, 89]
>>> ls[:50:3]
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]
>>> ls[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
>>> lx='xiousdalgusadflj'
>>> lx[:5]
'xious'
>>> lx[:5:2]
'xos'
>>> d ={'a':1,'b':2,'c':3,'d':4}
>>> for i in d:
...     print(key)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'key' is not defined
>>> for i in d:
...     print(i)
RecursionError: maximum recursion depth exceeded in comparison

d = {'bob':23,'jk':78,'xy':90,'yu':45}
#for i in d:
for i in d.values():
    print(i)
for i ,v in d.items():
    print(i,v)
for i,v in enumerate(['x',[23,4],'567']):
    print(i,v)


>>> ll=['a',3,4,5,6,78,89]
>>> ll[0:2]
['a', 3]
>>> ll
['a', 3, 4, 5, 6, 78, 89]
>>> ll[4:5]
[6]
>>> ll[3:5]
[5, 6]
>>> ll[-5:-1]
[4, 5, 6, 78]
>>> ll[-1]
89
>>> ll[-3]
6
>>> ll[-2:-1]
[78]
>>> ls=range(100)
>>> ls
range(0, 100)
>>> ls=lis(range(100))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'lis' is not defined
>>> ls=list(range(100))
>>> ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> ls[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> ll[-2]
78
>>> ll[-2:]
[78, 89]
>>> ls[:50:3]
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]
>>> ls[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
>>> lx='xiousdalgusadflj'
>>> lx[:5]
'xious'
>>> lx[:5:2]
'xos'
>>> d ={'a':1,'b':2,'c':3,'d':4}
>>> for i in d:
...     print(key)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'key' is not defined
>>> for i in d:
...     print(i)
...
a
b
c
d
>>> for i,value  in enmerate(d):
...     print(i,value)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'enmerate' is not defined
>>> for i,value  in enumerate(d):
...     print(i,value)
...
0 a
1 b
2 c
3 d
>>> for i,v in d.items():
...     print(i,v)
...
a 1
b 2
c 3
d 4
>>> l=[]
>>> for i in range(1,100):
...     l.append(i*i)
...
>>> l
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
>>> [x*x for x in range(1,100)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
>>> [x*x for x in range(1,100) if x%2==0]
[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 2304, 2500, 2704, 2916, 3136, 3364, 3600, 3844, 4096, 4356, 4624, 4900, 5184, 5476, 5776, 6084, 6400, 6724, 7056, 7396, 7744, 8100, 8464, 8836, 9216, 9604]
>>> improt os
  File "<stdin>", line 1
    improt os
            ^
SyntaxError: invalid syntax
>>> import os
>>> [d for d in os.listdir('.')]
['.idea', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll', 'venv']
>>> L=['HSdsafasgsgDSLK','saHIUDSIH','dahfldsde']
>>> [s.lower() for s in L]
['hsdsafasgsgdslk', 'sahiudsih', 'dahfldsde']
>>> L
['HSdsafasgsgDSLK', 'saHIUDSIH', 'dahfldsde']
>>> x=[s.lower() for s in L]
>>> x
['hsdsafasgsgdslk', 'sahiudsih', 'dahfldsde']
>>> g=(x*x for x in range(1,100))
>>> next(g)
1
>>> next(g)
4
>>> next(g)
9
>>> next(g)
16
>>> next(g)
25
>>> next(g)
36
>>> next(g)
49
>>> next(g)
64
>>> next(g)
81
>>> next(g)
100
>>> next(g)
121
>>> for n in g:
...     print(n)
...
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729
784
841
900
961
1024
1089
1156
1225
1296
1369
1444
1521
1600
1681
1764
1849
1936
2025
2116
2209
2304
2401
2500
2601
2704
2809
2916
3025
3136
3249
3364
3481
3600
3721
3844
3969
4096
4225
4356
4489
4624
4761
4900
5041
5184
5329
5476
5625
5776
5929
6084
6241
6400
6561
6724
6889
7056
7225
7396
7569
7744
7921
8100
8281
8464
8649
8836
9025
9216
9409
9604
9801
>>> for n in g:
...     n.append()
...
>>> n
9801
>>> n=[]
>>> for n in g:
...     n.append()
...
>>> n
[]
>>>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值