"Reprint" Learn to write programs in Python

Source: Internet
Author: User
Tags first string

Learn to write programs in Python

Work for many years, because the project needs, the use of the programming language quite a lot: C + +, Java, C #, assembly, VB, Objective C, Apple script. But the main use of the C + +, on the one hand is used for a long time, accustomed to. On the other hand, the way to think about the problem has been "bottom", do not want to memory, do not consider pointers, it seems to be sweating, unable to program. I am also in the interview with some children will not consciously ask some of the underlying knowledge points. On the other hand, you want the program to run more efficiently, the individual has been to write efficient (algorithm) program as the goal, and C + + is in addition to the Assembly, can write the most efficient programming language-everyone do not spray, there are different views is normal-and I am the only one to use the assembly to do the project, Also in MS writes Microsoft Excel's underlying library of functions, and also to improve the computational efficiency of Excel.

However, C + + write some "fast track approach", run once can throw the text program is very not aspects, but also consider character encoding, but also consider the text format, but also parse the string and so on. Python is designed to save programmers ' programming time, which inevitably increases program uptime, and has always wanted to learn python, just to take this opportunity to drive with specific small tasks while learning to do it. Now I feel pretty cool.

Writing a program in Python is pretty fast indeed. syntax is relatively concise, built-in tools more. For my previous tangle of operational efficiency, there are also surprises: if you run the script directly, the speed is very fast--python the default underlying implementation should be CPython, that is, many modules are supported by C, and compiled well. To run these modules is to invoke the compiled C program. But if the debugging--I use the python2.7 + eric4--running speed will be reduced very much, 10 times times slower, feel debugging time to use PVM interpreter, a lot of compiled C module can not directly run, it should be caused by the commissioning time greatly increased the reason.

In short, Python writes the program very quickly, and as long as it is written correctly, the operation is not too slow. This ape is quite satisfactory.

Attached: Because it is task-driven to learn python, no gnawing What book, the basic process is not to ask degrees Niang, degree Niang will not ask Google. Previously searched, accumulated some problems and solutions, listed below, Python danale please detour:

1. Read the file per line to remove the line break at the end of the ask price Filein = open ("FileName", "R") for lines in FileIn:line.strip () ...     2. String lookup, Delimited # find delimiter ' # ', and store the contents of ' # ' in a string, the contents after ' # ' are stored in another string in filein:sepposition = Line.strip (). Find (' # ') frontstring = Line.strip () [: sepposition] backstring = Line.strip () [Sepposition+1:]3. Appdict is a dictionary, key is the app Name,value is a property of the app, sorted by key, and output to the file Step1: Sort by key, output to list # Sort by the package name Appdictsor Ted = Sorted (Appdict.iteritems (), Key=lambda x:x[0], reverse=false) Step2: output content from appdictsorted to file for item in Appdic TSorted:fileOut.write (Item[0] + "#" + item[1] + "\ n") 4. Python's for loop tends to iterate through the elements in the container, such as: ItemList = {' A ', ' B ', ' C '} for item in ItemList do something the question is, how to repeat as in C + + Do some operations n times? A: With the range function for I in range (0, +) do something the value of Timesi starts at 0 and accumulates to 99 end 5. The collection is generated by the list, with the set constructor set () Itemset = set (itemList) 6. Find the intersection and Itemsetone of the two sets of the set and Itemsettwo: Itemjoinset = itemsetone & itemsettwo set: Itemunionset = Itemsetone | IteMsettwo difference set: Itemdiffset = Itemunionset-itemjoinset7. Find the number of elements in a dictionary? Use the Len () function appdict = Dict () ... appcount = Len (appdict) 8. How do I traverse a dictionary? A: Go through the key of the dictionary and use the keys () function to return the set of keys in the dictionary for key in Appdict.keys (): value = Appdict[key] #做爱做的事情写法上也可以不特意调用 Keys () function, but the function is the same, such as: for key in Appdict:value = Appdict[key] #做爱做的事情9. File opened to close, call the file's property function close (), such as: Filein = open (FileName, "R") ... Filein.close () 10. Initialization of tuples, lists, and dictionaries initialization with parentheses: a = (), the contents of a meta-lease object cannot be changed by the initialization of the list with brackets: a = [], the contents of the list object can change the initialization of the dictionary with curly braces: a = {}, if there is content, then: a = {key1:value1, key2 : value2, ...} 11. How to put a number in a file with a percent semicolon, such as: "80%" into a floating point 0.8 ideas: First string processing, remove the "80%" in the percent semicolon, and then reduce the number of 80 to decimal thevalue = float ("80%". Partition ('% ') [0] * The role of 0.01str.partition (' X ') is to replace the find and Replace methods in string, so that the string division and substitution method is more convenient, specifically, it is the str according to ' X ' into a ternary group (a, ' X ', b), A is str ' The previous part of X ', B is the latter part. The above statement "80%". Partition ('% ') [0] is to divide the string "80%" according to "%", divided into three parts ("80", "%", ""), ' [0] ' means taking the first part

"Reprint" Learn to write a program in Python

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.