Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

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


Ïà¹ØÎĵµ£º

PythonÈëÃŵÄ36¸öÀý×Ó——04 ÓÅÑŵÄ×Ö·û´®

# 004
# ÀûÓÃÈýÒýºÅ(''' or """)¿ÉÒÔָʾ¶àÐÐ×Ö·û´®
print '''line1
line2
line3'''
# ÁíÍ⣬Ä㻹¿ÉÒÔÔÚÈýÒýºÅÖÐÈÎÒâʹÓõ¥ÒýºÅºÍË«ÒýºÅ
print ''' "What's up? ," he replied.'''
# ·ñÔò£¬ÄãºÃʹÓÃתÒå·ûÀ´ÊµÏÖͬÑùµÄЧ¹û
# »¹ÊÇʹÓÃÈýÒýºÅºÃ£¬²»È»¾ÍÆÆ»µÁËÊÓ¾õÃÀÁË
print ' \"Wha ......

PythonÈëÃŵÄ36¸öÀý×Ó——05 ´ÏÃ÷µÄ±äÁ¿

# 005
# ÔÚPythonÖиø±äÁ¿¸³ÖµÊ±²»ÐèÒªÉùÃ÷Êý¾ÝÀàÐÍ
i = 33
print i
# ¿ÉÒÔÕâÑù×öµÄÔ­ÒòÊÇPython°Ñ³ÌÐòÖÐÓöµ½µÄÈκζ«Î÷¶¼¿´³ÉÊǶÔÏó(Á¬intÒ²²»ÀýÍâ)
# ÕâÑù,ÔÚʹÓöÔÏóʱ,±àÒëÆ÷»á¸ù¾ÝÉÏÏÂÎĵĻ·¾³À´µ÷ÓöÔÏó×ÔÉíµÄ·½·¨Íê³ÉÒþʽµÄת»»
# ÄãÉõÖÁ¿ÉÒ԰ѳÌÐòд³ÉÕâÑù
print 3 * 'haha '
# µ«Èôд³ÉÕâÑù±àÒëÆ÷¾Í»á±¨´í ......

PythonÄ£¿éѧϰ

¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......

PythonÈëÃŵÄ36¸öÀý×Ó Ö® 21

Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# Ò²¿ÉÒÔʹÓÃÕûÐÍ×÷ΪΨһµÄ±àºÅ
dict2 = {
5064001:'Mememe',
506400 ......

PythonÈëÃŵÄ36¸öÀý×Ó Ö® 28

Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 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 ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ