Python (Programming Language)
Total Page:16
File Type:pdf, Size:1020Kb
Python (programming language) Python is a widely used general-purpose, high-level pro- gramming language.[17][18][19] Its design philosophy em- phasizes code readability, and its syntax allows program- mers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.[20][21] The language provides constructs intended to enable clear programs on both a small and large scale.[22] Python supports multiple programming paradigms, in- cluding object-oriented, imperative and functional pro- gramming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[23] Python interpreters are available for installation on many operating systems, allowing Python code execution on a wide variety of systems. Using third-party tools, such as Py2exe or Pyinstaller,[24] Python code can be pack- aged into stand-alone executable programs for some of the most popular operating systems, allowing for the dis- tribution of Python-based software for use on those en- vironments without requiring the installation of a Python interpreter. CPython, the reference implementation of Python, is free and open-source software and has a community-based development model, as do nearly all of its alternative im- plementations. CPython is managed by the non-profit Python Software Foundation. Guido van Rossum, the creator of Python 1 History be closed, but I had a home computer, and not much else on my hands. I decided to write Main article: History of Python an interpreter for the new scripting language I had been thinking about lately: a descendant of Python was conceived in the late 1980s[25] and its im- ABC that would appeal to Unix/C hackers.I plementation was started in December 1989[26] by Guido chose Python as a working title for the project, van Rossum at CWI in the Netherlands as a successor to being in a slightly irreverent mood (and a big the ABC language (itself inspired by SETL)[27] capable fan of Monty Python’s Flying Circus). of exception handling and interfacing with the Amoeba operating system.[5] Van Rossum is Python’s principal au- thor, and his continuing central role in deciding the direc- Python 2.0 was released on 16 October 2000, and in- tion of Python is reflected in the title given to him by the cluded many major new features including a full garbage Python community, benevolent dictator for life (BDFL). collector and support for Unicode. With this release the development process was changed and became more About the origin of Python, Van Rossum wrote in transparent and community-backed.[29] 1996:[28] Python 3.0 (also called Python 3000 or py3k), a ma- Over six years ago, in December 1989, jor, backwards-incompatible release, was released on I was looking for a “hobby” programming 3 December 2008[30] after a long period of testing. project that would keep me occupied during the Many of its major features have been backported to the week around Christmas. My office ... would backwards-compatible Python 2.6 and 2.7.[31] 1 2 3 SYNTAX AND SEMANTICS 2 Features and philosophy at the cost of clarity.[41] When speed is important, Python programmers use PyPy, a just-in-time compiler, or move Python is a multi-paradigm programming language: time-critical functions to extension modules written in object-oriented programming and structured program- languages such as C. Cython is also available which trans- ming are fully supported, and there are a number of lates a Python script into C and makes direct C level API language features which support functional program- calls into the Python interpreter. ming and aspect-oriented programming (including by An important goal of the Python developers is making metaprogramming[32] and by magic methods).[33] Many Python fun to use. This is reflected in the origin of the other paradigms are supported using extensions, includ- name which comes from Monty Python,[42] and in an oc- ing design by contract[34][35] and logic programming.[36] casionally playful approach to tutorials and reference ma- Python uses dynamic typing and a combination of terials, for example using spam and eggs instead of the [43][44] reference counting and a cycle-detecting garbage collec- standard foo and bar. tor for memory management. An important feature of A common neologism in the Python community is Python is dynamic name resolution (late binding), which pythonic, which can have a wide range of meanings re- binds method and variable names during program execu- lated to program style. To say that code is pythonic is tion. to say that it uses Python idioms well, that it is natural The design of Python offers only limited support for or shows fluency in the language, that it conforms with functional programming in the Lisp tradition. The Python’s minimalist philosophy and emphasis on read- language has map(), reduce() and filter() functions; ability. In contrast, code that is difficult to understand or comprehensions for lists, dictionaries, and sets; as well reads like a rough transcription from another program- as generator expressions.[37] The standard library has two ming language is called unpythonic. modules (itertools and functools) that implement func- Users and admirers of Python—most especially those tional tools borrowed from Haskell and Standard ML.[38] considered knowledgeable or experienced—are often re- [45][46] The core philosophy of the language is summarized by the ferred to as Pythonists, Pythonistas, and Pythoneers. document “PEP 20 (The Zen of Python)", which includes aphorisms such as:[39] 3 Syntax and semantics • Beautiful is better than ugly Main article: Python syntax and semantics • Explicit is better than implicit • Simple is better than complex Python is intended to be a highly readable language. It is designed to have an uncluttered visual layout, frequently • Complex is better than complicated using English keywords where other languages use punc- • Readability counts tuation or keywords. Furthermore, Python has a smaller number of syntactic exceptions and special cases than C or Pascal.[47] Rather than requiring all desired functionality to be built into the language’s core, Python was designed to be highly extensible. Python can also be embedded in existing ap- 3.1 Indentation plications that need a programmable interface. This de- sign of a small core language with a large standard library Main article: Python syntax and semantics § Indentation and an easily extensible interpreter was intended by Van Rossum from the very start because of his frustrations with ABC (which espoused the opposite mindset).[25] Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks; this feature is While offering choice in coding methodology, the Python also termed the off-side rule. An increase in indentation philosophy rejects exuberant syntax, such as in Perl, in comes after certain statements; a decrease in indentation favor of a sparser, less-cluttered grammar. As Alex signifies the end of the current block.[48] Martelli put it: “To describe something as clever is not considered a compliment in the Python culture.”[40] Python’s philosophy rejects the Perl "there is more than 3.2 Statements and control flow one way to do it" approach to language design in favor of “there should be one—and preferably only one—obvious Python’s statements include (among others): way to do it”.[39] Python’s developers strive to avoid premature optimiza- • The if statement, which conditionally executes a tion, and moreover, reject patches to non-critical parts of block of code, along with else and elif (a contrac- CPython which would offer a marginal increase in speed tion of else-if). 3.3 Expressions 3 • The for statement, which iterates over an iterable ob- • Addition, subtraction, and multiplication are the ject, capturing each element to a local variable for same, but the behavior of division differs (see use by the attached block. Mathematics for details). Python also added the ** operator for exponentiation. • The while statement, which executes a block of code as long as its condition is true. • In Python, == compares by value, in contrast to Java, where it compares by reference. (Value compar- • The try statement, which allows exceptions raised in isons in Java use the equals() method.) Python’s is its attached code block to be caught and handled by operator may be used to compare object identities except clauses; it also ensures that clean-up code in (comparison by reference). Comparisons may be a finally block will always be run regardless of how chained, for example a <= b <= c. the block exits. • Python uses the words and, or, not for its boolean • The class statement, which executes a block of code operators rather than the symbolic &&, ||, ! used in and attaches its local namespace to a class, for use Java and C. in object-oriented programming. • Python has a type of expression termed a list compre- • The def statement, which defines a function or hension. Python 2.4 extended list comprehensions method. into a more general expression termed a generator expression.[37] • The with statement (from Python 2.5), which en- • closes a code block within a context manager (for Anonymous functions are implemented using example, acquiring a lock before the block of code lambda expressions; however, these are limited in is run and releasing the lock afterwards, or opening that the body can only be a single expression. a file and then closing it), allowing RAII-like behav- • Conditional expressions in Python are written as x if ior. c else y[54] (different in order of operands from the ?: operator common to many other languages). • The pass statement, which serves as a NOP.