Talk about my learning path from C# to Python

一、缘由

I first heard that Python went to a Beijing manufacturer (which makes car driving simulators) to see the equipment at the end of January this year, and listened to their CTO's introduction that the script control of the simulation software uses Python. (At the time, I called Python "Feisen" "Instead of "Pai Sen" 囧). I heard of this language for the first time, but I didn't care too much, because my instructor kept telling me that it was enough to use one language well. From college to now, I have used C, Basic, C++, and ASP (at first glance, it is not a computer major. Most of the computer majors I know in university are Pascal-Delphi-Java). In the past few years, I have mainly used C#. I think C# is not bad. I can also do local programs and ASP.NET programs, so I don't want to learn other languages.

During the winter vacation, I made a calculation program for traffic network analysis. This program has some foundation in the early stage, and the core algorithm uses the QuickGraph library. QuickGraph provides most of the algorithms in classic graph theory, such as shortest path, maximum flow, traversal, support tree, etc. It is a good thing, but there is no algorithm for complex network analysis. At the beginning I used QuickGraph's data structure, and then wrote complex network analysis algorithms myself. But it was very tiring to write, and it just happened to be the New Year, so I temporarily let go of this job.

During the Chinese New Year, I was idle and fine. I searched the Internet for a library that directly provides complex network analysis algorithms. I really found two: igraph and networkx. (See http://igraph.sourceforge.net/ and http://networkx.lanl.gov/), where igraph is written in C, but provides a Python interface; networkx is purely a Python implementation Library. I downloaded their documents and found that it was exactly what I wanted: mature and complex network analysis algorithms, a large number of sample programs. If you want to use these two libraries directly, you must learn the Python language. I think all kinds of languages ​​are similar anyway, just learn it.

二、语法

To learn a language, you must first start with basic grammar. The syntax of Python is indeed very simple, because there is a basic C# programming, learning it without any difficulties. Compared with C#, Python has fewer curly braces and completely relies on indentation to solve structural problems. At first, I was not used to it, especially when I wrote function definitions, loop control and other statements, I always lost the colon after it. It’s much better now, and the habit will slowly develop (now it’s always a colon when writing C# programs, and then the debugger starts to greet me endlessly). In addition, Python does not have a switch, because I do scientific calculations, which involve a lot of conditional control. At first, using ifelifelif... I found it very annoying, but later I found that using dict is very convenient, simpler and more beautiful than switch.

Speaking of dict, by the way, let’s talk about the list, tuple, and dict types built into Python. This is indeed very convenient. To do the same thing in C#, you need to reference Collections, but Python is much simpler, and it does simplify a lot of work for scientific calculations. However, Python types are not defined in advance. At first, I was not used to them. They were always int a and str b. Later, I discovered that this is not generic in C#! It's a good thing! And Python's functions are also very powerful, with anonymous functions, multiple return values, list connotations, keyword parameters, etc., which greatly simplifies programming. When I first saw these features in STL, Boost and C#, I was shocked. After learning Python, I realized that this thing is not that mysterious-the basic method.

In addition, Python has a nice function that is exec, execfile and eval. For me, this is very practical, because some functions need to be imported at runtime when doing calculations. For example, y = a + func(b), the form of func() is not known in advance, and the input is based on user needs at runtime. It used to take a lot of effort to implement this function in other languages ​​(using expression parsers, etc.), but now Python has it built-in directly. In other words, Python can be used as its own scripting language for Python programs! Python is too powerful! As the famous saying in the Python community said: "battery included"! Let me say that Python not only comes with a battery, but also a charger with TMD!

三、库及跨平台

There are many third-party libraries for Python, especially for computing. Python is richer than C# libraries, and it is used by more people. I have used igraph and networkx (graph theory and complex networks), numpy and scipy (numerical calculations), and matplotlib and cairo (graphs and visualization). Python (x, y) projects have integrated more scientific computing libraries. . Of course, this is related to the fact that most foreigners engaged in scientific computing use non-windows platforms. Most of the people I have contacted use unix/linux platforms (I am embarrassed to send emails with word attachments). Although there are Mono support C# under these platforms, But there are still few people using it. There are many scientific computing libraries for C and Fortran, but most of them provide Python packages or alternatives written in Python, so there is no need to reinvent the wheel yourself. I have completely given up on the idea of ​​expanding the QuickGraph, because I will not be able to keep up with igraph and networkx after two years of work, and the point is that no one uses what I am doing...

In addition, there are many GUI libraries available for Python: Tkinter, wxPython, PyQt, PyGTK, PyWin, and so on. Except for PyWin, most GUIs are cross-platform. This is very good. I don’t have to worry about MS getting stuck (even now I still use the D version, I will continue to do this under Linux if the restrictions are strict), my knowledge and technology can be accumulated and continued without starting from scratch Start. Of course, it is impossible to use all of the GUI libraries, and learning one should be enough. After trying the above libraries, I chose PyQt4 because I saw that the things made in its Demo are more beautiful, and the signals/slots mechanism of Qt is also easier to understand than the callback mechanism. In addition, a very important point for beginners is that Qt has more Chinese books than wxPython. Note that it is not PyQt, PyQt has relatively few Chinese books. wxPython I only found a very thick "wxPython In Action". The book is good, but I am more afraid of reading thick books, because time is always limited. It is best to use brochures such as In Nutshell. If you read it before going to bed the day before, you can write what you want the next day. .

四、Web开发

My main purpose of learning Python is to do some small numerical calculation programs, because doing network analysis always needs to analyze and calculate a large amount of data. Python's concise syntax and rich third-party libraries can greatly improve my work efficiency. But the research is just for interest, and to support the family, I still have to do some short and quick projects (miserable!). In the past two years, I have done a lot of web applications combined with engineering (because my instructor's direction is GIS and traffic design informatization). I have been doing it with ASP.Net in the past, from 1.0 to 2.0 to 3.5, I have been familiar with ASP.Net all the way. Because I learned Python, I also paid special attention to Python's web development functions.

It seems that Django is very hot now (I don't know how to pronounce this, but it happens that I have a friend named "Di Jianguo", so I pronounced this sound when I first saw Django :).
Baidu Encyclopedia introduced that "Django's main purpose is to develop database-driven websites simply and quickly. It emphasizes code reuse, multiple components can easily serve the entire framework in the form of "plugins". Django has many powerful functions. With third-party plug-ins, you can even easily develop your own toolkits. This makes Django highly extensible. It also emphasizes rapid development and the principle of DRY (Do Not Repeat Yourself)." It sounds good. I found the
"Django Step by Step" tutorial by Python master Limodou on the Internet. I didn’t get started after a rough look. For the time being, I’ll use my ASP.Net first, because I feel that ASP.Net currently has more resources available. It's more DRY for me. In the future, I will have some free time and then slowly discuss with "Di Jianguo".

五、体量和效率

Because of scientific computing, I attach great importance to the "lightweight" of the language and development environment. What is "lightweight"? For me, it means that I can run happily on my old 256M memory... My bad machine is a Lenovo Zhaoyang V80 that was eliminated by the unit. The CPU is P4 1.6. It is indeed difficult for him to run .Net 3.5! So on this machine I mainly use Dev C++ and SharpDevelop (Visual Studio is very difficult to start on this machine) for numerical calculation programs. My other laptop is a small black X61. The performance is good. I use it to surf the Internet, write papers, play games, etc. (However, the graphics card of the X61 is very weak. When doing OpenGL programs, I get it on the workstation of the unit. Of course, I play live. And FIFA also have to go to that workstation :). Because large-scale network analysis will take a long time (a few hours to a few days), the dirty work of these calculations is thrown at the V80. Now I use Python, IDE I chose PythonWin and Spyder, I feel that these two IDEs are relatively lightweight, very suitable for my hardware environment.

I think the running efficiency of Python is similar to that of C#, because both use virtual machines, but Python is still a bit worse than C/C++. I compared igraph (written in C) and networkx (written in Python) two libraries to generate a random graph of the same scale. The running time of igraph is an order of magnitude shorter than that of networkx (the comparison result by Drew Conway is similar, see here). In other words, for large-scale network analysis problems, there is a difference between waiting a few hours and waiting a few days. Fortunately, most of the old scientific computing libraries are written in C and Fortran, and provide a Python interface, so don't worry too much. We can use Python as a "glue"[1]: for the parts that require high efficiency, call the C/C++ library or write a C/C++ module by yourself. This is also a main consideration for my choice of the Python language.
I hope that everyone can choose their own language and learn something. Sharing codewords is not easy, I hope visitors can like and collect~ Or you can share your own language learning path under comments. You can also join a group to communicate and learn with the test leaders. Click the link to join the group chat [Python automated testing exchange group] Those who want python automated learning materials can also join the group to receive it for free (Note: csdn666 is convenient for quick review)

Guess you like

Origin blog.csdn.net/waitingwww/article/details/108783218