>>> 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)