Python Workbook 0-9by White Shinhuata (http://blog.csdn.net/whiterbear) reprint need to indicate the source, thank you.
This time at home to do a little Python workbook in the title, the problem is not difficult, but it will take some time, suitable for familiar with the use of Python. Here is the first 10 questions of the answer ideas and code.
The No. 0000 question: Add your QQ head (or Weibo avatar) to the top right corner with a red number, similar to the amount of unread information that prompt effect.
Similar to the effect in the diagram
idea: using Python's PiL module, PIL module provides image generation, image drawing and other methods. The subject needs to use PIL in the Imagefont module, the call will be reported the _imagingft C module is not installed wrong, you can refer to this blog to solve.
question No. 0001: as an independent developer of the Apple Store App, do you want to make a limited-time promotion, generate an Activation code (or coupon) for your app, and use Python to generate 200 activation codes (or coupons)?
idea: first understand what the coupon is, coupon is the event to send users a unique can verify the identification number. It is not difficult to generate an identifier that is not duplicated, but you may want to refer to this blog if you want to generate identification numbers that are easy to identify and not easily forge.
question No. 0002 : Save the 200 activation codes (or coupons) generated by 0001 questions to the MySQL relational database.
idea: install MySQL, learn a few common Python operation MySQL code can. Mac installation using MySQL seems to be more cumbersome than other platforms, please refer to this blog to install MySQL configuration.
question No. 0003: Save the 200 activation codes (or coupons) generated by 0001 questions to the Redis non-relational database.
idea: First contact with NoSQL, the first to search the features of Redis and several other non-relational databases, and then go to the official website to download Redis, after the installation of Redis and then to install the Redis module under Python.
Below are the simple Redis installation uses and the installation of Python's Redis modules.
Download:
Go to the official website to download the latest version of Redis, mine is redis-3.0.2
Installation:
Enter the redis-3.0.2 directory, enter make
it, and then enter it make test
.
To start the Redis service:
Under Redis directory, enter: src/redis-server
To turn off the Redis service:
jhbdeMacBook-Pro:redis-3.0.2 jhb$ src/redis-cli
127.0.0.1:6379> shutdown
not connected>
Redis also has a lot of other commands and related configuration edits, specifically Google.
Using Redis under Python requires:
sudo pip install redis
Reference:
import redis
Connection:
r = redis.Redis(host=‘localhost‘, port=6379, db=0)
For other uses, refer to the API documentation
question No. 0004: any plain text file in English, counting the number of occurrences of the word.
idea: traverse text, use split for word segmentation, and store it in a dictionary.
question No. 0005: You have a directory, loaded with a lot of photos, their size is not larger than the size of iPhone5 resolution.
idea: Another problem that needs PIL to solve. The PIL image module provides the resize function to resave the picture based on the given size. Determine if the image size is larger than the size of the iPhone5, then scale the picture proportionally to the larger width gap, then save.
question No. 0006: You have a directory, put your diary for one months, are txt, in order to avoid the problem of word segmentation, if the content is English, please count the most important words you think of each diary.
idea: The text is still the word statistics to find the most frequent occurrence of several words representing the characteristics of the whole diary. It is important to note that some words appear very frequently, but they cannot be significant words, such as a, the, is, etc., they are stop words and need to be filtered out. Of course, even if it is just a rough way to find out the important words, in-depth analysis also needs to calculate the weight of each word (TF-IDF?), which is not discussed in depth.
question No. 0007: There is a directory, which is the program you wrote yourself, to count how many lines of code you have written. Include blank lines and comments, but are listed separately.
idea: to get all the files under the directory, the extracted files are. py or. C or. cpp or. java, etc. end. Separate processing is required for different types of program files. I only implemented the. py file statistics. Open the file in turn, traverse each line, use the regular to determine whether it is a comment, if not, determine whether the bank contains non-whitespace characters, there is code, none is a blank line. It is important to note that the file saved by Utf-8 in Notepad under Windows will have a BOM header, which will cause the first line comment to fail.
question No. 0008: An HTML file to find the text inside.
idea: using the BeautifulSoup module, however, some HTML text will still output some JS code when using the BeautifulSoup Text property. It is advisable to use other methods such as regular matching.
question No. 0009: An HTML file to find the link inside.
idea: use requests to get the page and extract all the links with the regular.
Code link
- Github:show-me-the-code
- CSDN Download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Practice book 0-9