# 输出每个字母出现的次数
s1 = ' aaaabbbbcccddddaa '
s2 = ''
for i in range(len(s1)):
if s1[i] in s2:
continue
count = 0
for j in range(i,len(s1)):
if s1[i] == s1[j]:
count += 1
s2 += s1[i] + str(count)
print(s2)
def additem(listP):
listP + = [1]
mylist = [1,2,3,4]
additem(mylist)
k = len(mylist)
print(mylist) # [1,2,3,4,1]
print(k) #5
#=========================================================#
counter = 0
def doThings():
global counter
for i in (1,2,3):
counter += 1
doThings()
print(counter)# 6
print('==========+++++++++++++===========')
class Person:
def __init__(self,id):
self.id = id
jack = Person(1)
jack.__dict__['age']=22
oo = jack.age + len(jack.__dict__)
print(jack.__dict__)# {'id':1,'age':22}
print(oo)# 22+2
# 魔法函数 __doc__属性
def foo():
"This is cool simple function that return 1"
return 1
print(foo.__doc__[-8:-2])
#===================================================#
class A:
def __init__(self,a,b,c):
self.x = a+b+c
a = A(1,2,3)
b = getattr(a,'x')
print(b) # 6
setattr(a,'x',b+1)
print(a.x)# 7
python相关代码面试题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...