As an object-oriented, literal translation computer programming language, Python has been mature and stable for more than a decade. This article will take you to the world of Python, assuming that you do not have any knowledge of the Python programming language, but should have at least some basic programming knowledge, we will focus on comparing Python and PHP.
You have spent a lot of time on PHP and it is imperative to switch to another language. You also know that if you do not move it, it means passive attacks. In fact, learning a new language is like traveling abroad: you will get in touch with new things, taste new foods, enjoy different cultures, talk to different people, and learn about all new things, then return to your home to experience the original environment.
The goal of this article is quite simple: briefly introduce the basic knowledge of Python and lay the foundation for readers to perform in-depth search. Fortunately, you will realize that Python is actually no different from the language you used earlier. Taking tourism as an example, you don't need to go too far. You just need to go to a neighboring country with the same language.
What is Python?
Python is defined as a general high-level programming language ". It is known for its simplicity and ease of use, and is one of the few languages that require spaces and indentation. Guido Van rosum, the main author of Python, is still very active in the community and is dubbed a benevolent dictatorship.
Python's flexibility and closeness are commendable. It supports object-oriented programming, structured programming, Aspect-Oriented Programming, and function programming. Python uses a small kernel design, but has a large number of extension libraries, thus ensuring the language's closeness and flexibility.
From a syntax perspective, you will find that Python's conciseness is exceptionally prominent-almost a pure realm. PHP developers will either be fascinated by the syntax of this method or discover its limitations. This depends on your own opinions. The attitude of the Python community to promote this aesthetic is very clear. They pay more attention to aesthetics and conciseness, rather than smart skills. PHP developers who have formed the Perl tradition "you can implement it in multiple ways") like myself) will face a completely opposite philosophy "there should be only one way to implement it ").
In fact, this community defines a unique term for code styles, namely, Python-based pythonic ). You can say that your code is made of Python, which is a good use of Python terms and shows the natural features of the language. This article is not intended to be Pythonista or Pythoneer), but if you want to continue with Python, you must never miss this article. Just as PHP has its own programming style and Perl has its own conceptual methods, it is necessary to start to use this language to learn Python.
Another key point: when writing this article, the latest version of Python is V3.0, but this article focuses on Python V2.6. Python V3.0 is not backward compatible with earlier versions, and V2.6 is the most widely used version. Of course, you can use your preferred version as needed.
What is the difference between Python and PHP?
In general, PHP is a Web development language. Yes, it provides a command line interface and can even be used to develop embedded applications, but it is mainly used for Web development. On the contrary, Python is a scripting language and can also be used for Web development. In this regard-I know I will say this-it is closer to Perl than PHP. Of course, there is no practical difference between them in other aspects. Let's continue .)
PHP syntax is filled with dollar signs $) and braces {}), while Python is more concise and clean. PHP supports the switch and do... while structures, while Python does not. PHP uses the ternary operator foo? Bar: baz) and a lengthy list of function names, and naming conventions are even more confusing. On the contrary, you will find that Python is much more concise. PHP array types support both simple list and dictionary or hash, but Python separates them. Python uses both variability and immutability: for example, tuple is an immutable list. You can create a tuple, but it cannot be modified after it is created. This concept may take some time to get familiar with, but it is extremely effective to avoid errors. Of course, the only way to change tuple is to copy it. Therefore, if you find that a large number of changes have been made to immutable objects, you should reconsider your methods.
As mentioned earlier, indentation in Python has a meaning: it is very difficult for you to adapt to this language at the beginning. You can also create functions and methods that use keywords as parameters-different from standard location parameters in PHP. Object-oriented followers will be excited about the real object-oriented ideas in Python, including its "first-level" classes and functions. If you use a non-English language, you will love Python's powerful internationalization and Unicode support. You will also like the multi-threading feature of Python, which is also one of the features that I was fascinated by at first.
To sum up, PHP and Python are similar in many aspects. You can easily create variables, loops, conditions, and functions. You can even easily create reusable modules. The user communities in both languages are full of vigor and passion. PHP has a larger user base, but this is mainly due to its advantages and popularity in hosting servers and Web attention.