PythonÈëÃŵÄ36¸öÀý×Ó Ö® 31
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 035
class Person:
population = 0 #Õâ¸ö±äÁ¿ÊÇÊôÓÚÕû¸öÀàµÄ
def __init__(self, name):
self.name = name
print '³õʼ»¯ %s' % self.name
Person.population += 1
# end of def
def __del__(self):
print '%s says bye.' % self.name
Person.population -= 1
if Person.population == 0:
print 'ÎÒÊÇ×îºóÒ»¸öÈËÁË£¡'
else:
print '»¹ÓÐ%d¸öÈË¡£' % Person.population
# end of if
# end of def
def sayHi(self):
print 'Hi, my name is %s' % self.name
# end of def
def howMany(self):
print Person.population
# end of def
# enf of class
ning = Person('Ning')
ning.sayHi()
ning.howMany()
#zhang = Person('Zhang')
#zhang.sayHi()
#zhang.howMany()
#ning.sayHi()
#ning.howMany()
output:
×¢Ò⣡ÏÂÃæÊÇÁ½´ÎÔËÐеĽá¹û£¬¿ÉÒÔ¿´³ödel£¨Îö¹¹º¯Êý£©µÄÔËÐÐʱ¼äÊDz»È·¶¨µÄ¡£Õâ¸öÏÖÏóµÄÔÒòÔÚÓÚPythonµÄÀ¬»ø»ØÊÕ»úÖƵÄ×Ô¶¯»ØÊÕʱ¼äÊDz»È·¶¨µÄ¡£
³õʼ»¯ Ning
Hi, my name is Ning
1
>>>
³õʼ»¯ Ning
Ning says bye.
ÎÒÊÇ×îºóÒ»¸öÈËÁË£¡
Hi, my name is Ning
0
>>>
Ïà¹ØÎĵµ£º
# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......
¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......
# 027
toolName = 'Google'
if toolName.startswith('Go'):
print 'The tool\'s name starts with \"Go\".'
if 'oo' in toolName:
print 'The tool\'s name contains the stirng "oo".'
print toolName.find('gl') # ·µ»ØÊ״γöÏÖ“gl”×Ö·û´®µÄË÷Òý
if toolName.find('Baidu ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 028
consoleInput = raw_input('ÇëÊäÈëµãʲô°É£º')
aFile = file(r'C:\out.txt', 'w')
aFile.write(consoleInput + '\n')
aFile.write('ÕâÀïÊǵڶþÐÐ')
aFile.close()
output£º
>>>
ÇëÊäÈëµãʲô°É£ºhaha
>>>
......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 032
# ÆäʵcPickleÕâ¸öÄ£¿éÆðµ½µÄ×÷ÓÿÉÒÔÓÓÍêÃÀµØе÷ÁËÎļþÖеÄÄÚÈÝ£¨¶ÔÏ󣩺ʹúÂëÖеÄÒýÓÔÀ´ÐÎÈÝ
import cPickle as p # ÕâÌõÓï¾ä¸øcPickleÆðÁ˸öСÃûp
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close ......