Python is indeed a good tool

Source: Internet
Author: User

I recently studied Python and thought that his Q & A interface is especially suitable for learning programming. Here are two examples.
1. Quick Sort Algorithm Description
>>> Def qsort (aL ):
... If aL = []: return []
... Else:
... Smaller = [x for x in aL [1:] if x <aL [0] # smaller part than aL [0]
... Bigger = [x for x in aL [1:] if x> = aL [0] # greater (or equal) than aL [0]
... Return qsort (smaller) + [aL [0] + qsort (bigger)
...
>>> Qsort ([3, 4, 5, 1])
[1, 3, 4, 5]
>>> Import random
>>> A = range (0,100)
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> Random. shuffle ()
>>>
[69, 77, 17, 97, 21, 1, 44, 20, 82, 46, 59, 63, 4, 47, 38, 25, 48, 84, 72, 2, 52, 10, 31, 3, 58, 43, 9, 96, 95, 27, 0, 71, 40, 12, 76, 8, 14, 79, 53, 80, 28, 15, 91, 22, 65, 68, 86, 18, 32, 85, 55, 36, 92, 74, 50, 11, 83, 81, 7, 57, 90, 54, 87, 24, 29, 37, 61, 35, 56, 60, 88, 75, 30, 16, 13, 39, 26, 70, 93, 49, 51, 23, 41, 98, 66, 99, 89, 5, 34, 6, 94, 64, 67, 62, 78, 73, 19, 33, 42, 45]
>>> Qsort ()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Note: although the algorithm efficiency is not high, the meaning is clear.

2. Imitate the QQ tail "Notepad tail"
The"Hook", You need to downloadPyhookModule
Import win32ui, win32con, pyHook, pythoncom
Def hookhandle (event ):
If event. KeyID = 13: # capture the Enter key (Note: QQ tail captures Ctrl + Enter and click send)
Try:
Pwin = win32ui. FindWindow ('notepad ', None) # Find the Notepad window
Textbox = win32ui. find1_wex (pwin, None, 'edit', None) # Find the editing window
Buf = '0x0' * 1024
Oldlen = textbox. sendmessage (win32con. wm_gettext, Buf) # obtain the original text in the editing window.
Textbox. sendmessage (win32con. wm_settext, Buf [0: oldlen] + '/R/nhttp: // www.csdn.net') # Add a tail.
Except t:
Pass
Hm = pyhook. hookmanager () # hook instantiation
Hm. keydown = hookhandle # specifies the callback function
Hm. hookkeyboard () # hook keyboard
Pythoncom. pumpmessages () # message loop

Of course, the QQ tail will certainly not be written in dynamic languages. I am only here to illustrate that python is indeed a good learning tool, as long as you can think of the principle,
Use it nowPythonVerify it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.