PythonÈëÃŵÄ36¸öÀý×Ó Ö® 34
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
raiseÓÐÁ½¸ö²ÎÊý£¬µÚÒ»¸öÊÇÓÉÎÒÃÇ×Ô¼º¶¨ÒåµÄÒì³£ÀàÐÍ£¬µÚ¶þ¸öÊǹØÓÚ´ËÒì³£µÄÉÙÁ¿ËµÃ÷ÐÅÏ¢¡£
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# end of def
print getAge()
output:
ÒÔÏÂÊÇÁ½´ÎÔËÐеĽṹ
>>>
Input your age:22
22
>>>
Input your age:911
Traceback (most recent call last):
File "C:/Documents and Settings/ning/×ÀÃæ/Python Intro/038_RaiseAnError.py", line 11, in <module>
print getAge()
File "C:/Documents and Settings/ning/×ÀÃæ/Python Intro/038_RaiseAnError.py", line 6, in getAge
raise 'BadAgeError', 'It is impossible!!!!!'
TypeError: exceptions must be classes or instances, not str
>>>
Ïà¹ØÎĵµ£º
# 004
# ÀûÓÃÈýÒýºÅ(''' or """)¿ÉÒÔָʾ¶àÐÐ×Ö·û´®
print '''line1
line2
line3'''
# ÁíÍ⣬Ä㻹¿ÉÒÔÔÚÈýÒýºÅÖÐÈÎÒâʹÓõ¥ÒýºÅºÍË«ÒýºÅ
print ''' "What's up? ," he replied.'''
# ·ñÔò£¬ÄãºÃʹÓÃתÒå·ûÀ´ÊµÏÖͬÑùµÄЧ¹û
# »¹ÊÇʹÓÃÈýÒýºÅºÃ£¬²»È»¾ÍÆÆ»µÁËÊÓ¾õÃÀÁË
print ' \"Wha ......
# 005
# ÔÚPythonÖиø±äÁ¿¸³ÖµÊ±²»ÐèÒªÉùÃ÷Êý¾ÝÀàÐÍ
i = 33
print i
# ¿ÉÒÔÕâÑù×öµÄÔÒòÊÇPython°Ñ³ÌÐòÖÐÓöµ½µÄÈκζ«Î÷¶¼¿´³ÉÊǶÔÏó(Á¬intÒ²²»ÀýÍâ)
# ÕâÑù,ÔÚʹÓöÔÏóʱ,±àÒëÆ÷»á¸ù¾ÝÉÏÏÂÎĵĻ·¾³À´µ÷ÓöÔÏó×ÔÉíµÄ·½·¨Íê³ÉÒþʽµÄת»»
# ÄãÉõÖÁ¿ÉÒ԰ѳÌÐòд³ÉÕâÑù
print 3 * 'haha '
# µ«Èôд³ÉÕâÑù±àÒëÆ÷¾Í»á±¨´í ......
¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# Ò²¿ÉÒÔʹÓÃÕûÐÍ×÷ΪΨһµÄ±àºÅ
dict2 = {
5064001:'Mememe',
506400 ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 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 ......