python 3.0Óëpython 2.xµÄÇø±ð
ÕýÔÚÔĶÁ×îаæµÄ¡¶A byte of Python¡·¡£·¢ÏÖPython3.0ÔÚijЩµØ·½»¹ÊÇÓÐЩ¸Ä±äµÄ¡£×¼±¸ÂýÂýµÄÌå»á£¬ÓëÀÏ°æ±¾µÄ¡¶A byte of Python¡·×ö¶Ô±È£¬×îºóÔÙÈ¥²éÔĹٷ½ÍøÕ¾µÄÎĵµ¡£
1.
Èç¹ûÄãÏÂÔصÄÊÇ×îаæµÄPython£¬¾Í»á·¢ÏÖËùÓÐÊéÖеÄHello WorldÀý×Ó½«²»ÔÙÕýÈ·¡£
Old:
print "Hello World!" #´òÓ¡×Ö·û´®
New:
print("Hello World!")
½«×Ö·û´®·Åµ½À¨ºÅÖÐprint³öÀ´£¬ÕâÖÖд·¨¶ÔÓÚÎÒÕâÖÖѧϰJava³öÉíµÄÈËÀ´Ëµ£¬ºÜÊÇÇ×Çа¡£º£©
2.
Old:
guess = int(raw_input('Enter an integer : ')) #¶ÁÈ¡¼üÅÌÊäÈëµÄ·½·¨
New:
guess = int(input('Enter an integer : '))
·½·¨Ãû±äµÃ¸ü¼ÓÈÝÒ׼ǣ¡
3.
¼ÓÈëÁËÒ»¸öеÄnonlocal statement£¬·Ç¾Ö²¿±äÁ¿£¬ËüµÄ·¶Î§½éÓÚglobalºÍlocalÖ®¼ä£¬Ö÷ÒªÓÃÓÚº¯ÊýǶÌ×£¬Ó÷¨ÈçÏ£º
#!/usr/bin/python
# Filename: func_nonlocal.py
def func_outer():
x = 2
print('x is', x)
def func_inner():
nonlocal x
x = 5
func_inner()
print('Changed local x to', x)
func_outer()
4.
VarArgs parameters£¬²»ÖªµÀÕâ¸ö·Òë³Éʲô±È½ÏÍ×µ±£¿ÏÈ¿´Àý×Ó£º
#!/usr/bin/python
# Filename: total.py
def total(initial=5, *numbers, **keywords):
count = initial
for number in numbers:
count += number
for key in keywords:
count += keywords[key]
return count
print(total(10, 1, 2, 3, vegetables=50, fruits=100))
µ±ÔÚ²ÎÊýÇ°ÃæʹÓÃ*±êʶµÄʱºò£¬ËùÓеÄλÖòÎÊý£¨1£¬2£¬3£©×÷Ϊһ¸ölist´«µÝ¡£
µ±ÔÚ²ÎÊýÇ°ÃæʹÓÃ**±êʶµÄʱºò£¬ËùÓеĹؼü²ÎÊý£¨vegetables=50, fruits=100£©×÷Ϊһ¸ödictionary´«µÝ¡£
5.
¹ØÓÚPackagesµÄ»°Ì⣬ÎÒû¿´¶®¡£¡£¡£ÄÄλ´óϺ°ïæ½²½âÏ£¿
6.
ÔÚÊý¾Ý½á¹¹ÖУ¬¶àÁËÒ»ÖÖÀàÐÍ:set
SetÊÇÒ»ÖÖÎÞÐòµÄ¼òµ¥¶ÔÏóµÄ¼¯ºÏ£¬µ±ÎÒÃǹØÐÄÒ»¸ö¶ÔÏóÊÇ·ñÔÚÒ»¸ö¼¯ºÏÖдæÔÚ£¬¶ø˳ÐòºÍ³öÏֵĴÎÊýÊÇ´ÎÒªµÄʱºò£¬¿ÉÒÔʹÓÃset¡£
7.
¹ØÓÚos.sep·½·¨£¬£¨setÊÇseparator£¬·Ö¸ô·ûµ
Ïà¹ØÎĵµ£º
4¡¢Tuples Ôª×é
Ôª×éºÍListsÏàËÆ£¬µ«ËüÊÇimmutable,³õʼ»¯ºó²»ÄܸıäÆäÄÚÈÝ£¬ÕâÔÚ³ÌÐòÖÐÓÐʱºòºÜÓÐÓ㬿ÉÒÔÓÃÀ´·ÀÖ¹¶¨ÒåµÄ±äÁ¿ÄÚÈݱ»ÒâÍâ¸Ä±ä¡£
5¡¢Files Îļþ
Îļþ²Ù×÷ºÍcÓïÑԱȽϽӽü£¬ÏÂÃæֻͨ¹ý´úÂëÑÝʾ£º
>>> f = open('data.txt','w')
>>> ......
ǰһƪ½²Á˼òµ¥µÄC/C++µ÷ÓÃPython½Å±¾Ä£¿é£¨.py£©¡£¼ÈÈ»ÊÇÓÃÓÚÖî¶àÓÎÏ·³ÌÐòµÄ½Å±¾ÓïÑÔ£¬Äǿ϶¨ÊÇȱ²»ÁË»¥µ÷£¨ÀñÉÐÍùÀ´£©¡£Òò´Ë£¬±¾Æª½²Ò»¸ö¼òµ¥µÄpythonµ÷ÓÃC/C++дµÄDLLÄ£¿é£¬¶ÔPython½øÐй¦ÄÜÀ©Õ¹¡£ÕâÀïдһ¸ö¼òµ¥µÄÀý×Ó£¬Ö÷Òª¾ÍΪÁËÁ˽âÏÂÕâôÓÃPythonÀ´µ÷ÓÃC/C++дµÄDLL¿â¡£ºÃÁË£¬ÇÐÈëÕýÌ⣺
Ê×ÏÈ£¬ÎÒÊÇÓÃVS2003 ......
Ó¦¸Ã¾¡Á¿±ÜÃâʹÓÃÈ«¾Ö±äÁ¿¡£²»Í¬µÄÄ£¿é¶¼¿ÉÒÔ×ÔÓɵķÃÎÊÈ«¾Ö±äÁ¿£¬¿ÉÄܻᵼÖÂÈ«¾Ö±äÁ¿µÄ²»¿ÉÔ¤ÖªÐÔ¡£¶ÔÈ«¾Ö±äÁ¿£¬Èç¹û³ÌÐòÔ±¼×ÐÞ¸ÄÁË_aµÄÖµ£¬³ÌÐòÔ±ÒÒͬʱҲҪʹÓÃ_a£¬Õâʱ¿ÉÄܵ¼Ö³ÌÐòÖеĴíÎó¡£ÕâÖÖ´íÎóÊǺÜÄÑ·¢Ïֺ͸üÕýµÄ¡£
È«¾Ö±äÁ¿½µµÍÁ˺¯Êý»òÄ£¿éÖ®¼äµÄͨÓÃÐÔ£¬²»Í¬µÄº¯Êý»òÄ£¿é¶¼ÒªÒÀÀµÓÚÈ«¾Ö±äÁ¿¡£Í¬Ñù£¬È« ......
Python»ù±¾°²×°£º
* http://www.python.org/ ¹Ù·½±ê×¼Python¿ª·¢°üºÍÖ§³Ö»·¾³£¬Í¬Ê±Ò²ÊÇPythonµÄ¹Ù·½ÍøÕ¾£»
* http://www.activestate.com/ ¼¯³É¶à¸öÓÐÓòå¼þµÄÇ¿´ó·Ç¹Ù·½°æ±¾£¬ÌرðÊÇÕë¶ÔWindows»·¾³Óв»ÉٸĽø£»
PythonÎĵµ£º
* http://www.pyth ......