text
stringlengths
0
7.06k
Explicit is better than implicit
Simple is better than complex
Complex is better than complicated
<unk> counts
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 applications that need a programmable interface . This design of a small core language with a large standard library and an easily extensible interpreter was intended by Van Rossum from the start because of his frustrations with ABC , which espoused the opposite mindset .
While offering choice in coding methodology , the Python philosophy rejects exuberant syntax , such as in Perl , in favor of a sparser , less @-@ cluttered grammar . As Alex Martelli put it : " To describe something as clever is not considered a compliment in the Python culture . " Python 's philosophy rejects the Perl " there is more than one way to do it " approach to language design in favor of " there should be one — and preferably only one — obvious way to do it " .
Python 's developers strive to avoid premature optimization , and moreover , reject patches to non @-@ critical parts of CPython that would offer a marginal increase in speed at the cost of clarity . When speed is important , a Python programmer can move time @-@ critical functions to extension modules written in languages such as C , or try using PyPy , a just @-@ in @-@ time compiler . <unk> is also available , which translates a Python script into C and makes direct C @-@ level API calls into the Python interpreter .
An important goal of Python 's developers is making it fun to use . This is reflected in the origin of the name , which comes from Monty Python , and in an occasionally playful approach to tutorials and reference materials , such as using examples that refer to spam and eggs instead of the standard foo and bar .
A common neologism in the Python community is <unk> , which can have a wide range of meanings related to program style . To say that code is <unk> is to say that it uses Python idioms well , that it is natural or shows fluency in the language , that it conforms with Python 's minimalist philosophy and emphasis on readability . In contrast , code that is difficult to understand or reads like a rough transcription from another programming language is called <unk> .
Users and admirers of Python , especially those considered knowledgeable or experienced , are often referred to as <unk> , <unk> , and <unk> .
= = Syntax and semantics = =
Python is intended to be a highly readable language . It is designed to have an uncluttered visual layout , often using English keywords where other languages use punctuation . Further , Python has fewer syntactic exceptions and special cases than C or Pascal .
= = = <unk> = = =
Python uses whitespace indentation , rather than curly braces or keywords , to delimit blocks ; this feature is also termed the off @-@ side rule . An increase in indentation comes after certain statements ; a decrease in indentation signifies the end of the current block .
= = = Statements and control flow = = =
Python 's statements include ( among others ) :
The assignment statement ( token ' = ' , the equals sign ) . This operates differently than in traditional imperative programming languages , and this fundamental mechanism ( including the nature of Python 's version of variables ) illuminates many other features of the language . Assignment in C , e.g. , x
= 2 , translates to " typed variable name x receives a copy of numeric value 2 " . The ( right @-@ hand ) value is copied into an allocated storage location for which the ( left @-@ hand ) variable name is the symbolic address . The memory allocated to the variable is large enough ( potentially quite large ) for the declared type . In the simplest case of Python assignment , using the same example , x =
2 , translates to " ( generic ) name x receives a reference to a separate , dynamically allocated object of numeric ( int ) type of value 2 . " This is termed binding the name to the object . Since the name 's storage location doesn 't contain the indicated value , it is improper to call it a variable . Names may be subsequently rebound at any time to objects of greatly varying types , including strings , procedures , complex objects with data and methods , etc . Successive assignments of a common value to multiple names , e.g. , x
= 2 ; y =
2 ; z = 2 result in allocating storage to ( at most ) three names and one numeric object , to which all three names are bound . Since a name is a generic reference holder it is unreasonable to associate a fixed data type with it . However at a given time a name will be bound to some object , which will have a type ; thus there is dynamic typing .
The if statement , which conditionally executes a block of code , along with else and <unk> ( a contraction of else @-@ if ) .
The for statement , which iterates over an <unk> object , capturing each element to a local variable for use by the attached block .
The while statement , which executes a block of code as long as its condition is true .
The try statement , which allows exceptions raised in its attached code block to be caught and handled by except clauses ; it also ensures that clean @-@ up code in a finally block will always be run regardless of how the block exits .
The class statement , which executes a block of code and attaches its local namespace to a class , for use in object @-@ oriented programming .
The def statement , which defines a function or method .
The with statement ( from Python 2 @.@ 5 ) , which encloses a code block within a context manager ( for example , acquiring a lock before the block of code is run and releasing the lock afterwards , or opening a file and then closing it ) , allowing Resource Acquisition Is <unk> ( <unk> ) -like behavior .
The pass statement , which serves as a <unk> . It is syntactically needed to create an empty code block .
The assert statement , used during debugging to check for conditions that ought to apply .
The yield statement , which returns a value from a generator function . From Python 2 @.@ 5 , yield is also an operator . This form is used to implement <unk> .
The import statement , which is used to import modules whose functions or variables can be used in the current program .
The print statement was changed to the print ( ) function in Python 3 .
Python does not support tail call optimization or first @-@ class continuations , and , according to Guido van Rossum , it never will . However , better support for <unk> @-@ like functionality is provided in 2 @.@ 5 , by extending Python 's generators . Before 2 @.@ 5 , generators were lazy <unk> ; information was passed unidirectionally out of the generator . As of Python 2 @.@ 5 , it is possible to pass information back into a generator function , and as of Python 3 @.@ 3 , the information can be passed through multiple stack levels .
= = = Expressions = = =
Some Python expressions are similar to languages such as C and Java , while some are not :
Addition , subtraction , and multiplication are the same , but the behavior of division differs ( see Mathematics for details ) . Python also added the * * operator for exponentiation .
As of Python 3 @.@ 5 , it supports matrix multiplication directly with the @ operator , versus C and Java , which implement these as library functions . Earlier versions of Python also used methods instead of an infix operator .
In Python , = = compares by value , versus Java , which compares <unk> by value and objects by reference . ( Value comparisons in Java on objects can be performed with the equals ( ) method . ) Python 's is operator may be used to compare object identities ( comparison by reference ) . In Python , comparisons may be chained , for example a < = b <
= c .
Python uses the words and , or , not for its boolean operators rather than the symbolic & & , | | , ! used in Java and C.
Python has a type of expression termed a list comprehension . Python 2 @.@ 4 extended list comprehensions into a more general expression termed a generator expression .
Anonymous functions are implemented using lambda expressions ; however , these are limited in that the body can only be one expression .
Conditional expressions in Python are written as x if c else y ( different in order of operands from the c ? x : y operator common to many other languages ) .
Python makes a distinction between lists and tuples . Lists are written as [ 1 , 2 , 3 ] , are mutable , and cannot be used as the keys of dictionaries ( dictionary keys must be immutable in Python ) . <unk> are written as ( 1 , 2 , 3 ) , are immutable and thus can be used as the keys of dictionaries , provided all elements of the tuple are immutable . The parentheses around the tuple are optional in some contexts . <unk> can appear on the left side of an equal sign ; hence a statement like x , y =
y , x can be used to swap two variables .
Python has a " string format " operator % . This functions analogous to <unk> format strings in C , e.g. " spam = % s eggs = % d " % ( " blah " , 2 ) evaluates to " spam = blah eggs = 2 " . In Python 3 and 2 @.@ 6 + , this was supplemented by the format ( ) method of the <unk> class , e.g. " spam = { 0 } eggs = { 1 } " <unk> ( " blah " , 2 ) .
Python has various kinds of string literals :
Strings delimited by single or double quote marks . Unlike in Unix shells , Perl and Perl @-@ influenced languages , single quote marks and double quote marks function identically . Both kinds of string use the backslash ( \ ) as an escape character and there is no implicit string interpolation such as " $ spam " .
Triple @-@ quoted strings , which begin and end with a series of three single or double quote marks . They may span multiple lines and function like here documents in shells , Perl and Ruby .
Raw string varieties , denoted by prefixing the string literal with an r . No escape sequences are interpreted ; hence raw strings are useful where literal <unk> are common , such as regular expressions and Windows @-@ style paths . Compare " @ <unk> " in C # .
Python has array index and array slicing expressions on lists , denoted as a [ key ] , a [ start : stop ] or a [ start : stop : step ] . Indexes are zero @-@ based , and negative indexes are relative to the end . <unk> take elements from the start index up to , but not including , the stop index . The third slice parameter , called step or stride , allows elements to be skipped and reversed . Slice indexes may be omitted , for example a [ : ] returns a copy of the entire list . Each element of a slice is a shallow copy .
In Python , a distinction between expressions and statements is rigidly enforced , in contrast to languages such as Common Lisp , Scheme , or Ruby . This leads to duplicating some functionality . For example :
List comprehensions vs. for @-@ loops
Conditional expressions vs. if blocks
The eval ( ) vs. exec ( ) built @-@ in functions ( in Python 2 , exec is a statement ) ; the former is for expressions , the latter is for statements .
Statements cannot be a part of an expression , so list and other comprehensions or lambda expressions , all being expressions , cannot contain statements . A particular case of this is that an assignment statement such as a
= 1 cannot form part of the conditional expression of a conditional statement . This has the advantage of avoiding a classic C error of mistaking an assignment operator =
for an equality operator
= = in conditions : if ( c =
1 ) { ... } is valid C code but if c = 1 : ... causes a syntax error in Python .
= = = Methods = = =
Methods on objects are functions attached to the object 's class ; the syntax <unk> ( argument ) is , for normal methods and functions , syntactic sugar for <unk> ( instance , argument ) . Python methods have an explicit self parameter to access instance data , in contrast to the implicit self ( or this ) in some other object @-@ oriented programming languages ( e.g. , C + + , Java , Objective @-@ C , or Ruby ) .
= = = Typing = = =
Python uses duck typing and has typed objects but <unk> variable names . Type constraints are not checked at compile time ; rather , operations on an object may fail , signifying that the given object is not of a suitable type . Despite being dynamically typed , Python is strongly typed , forbidding operations that are not well @-@ defined ( for example , adding a number to a string ) rather than silently attempting to make sense of them .
Python allows programmers to define their own types using classes , which are most often used for object @-@ oriented programming . New instances of classes are constructed by calling the class ( for example , <unk> ( ) or <unk> ( ) ) , and the classes are instances of the <unk> type ( itself an instance of itself ) , allowing metaprogramming and reflection .
Before version 3 @.@ 0 , Python had two kinds of classes : old @-@ style and new @-@ style . Old @-@ style classes were eliminated in Python 3 @.@ 0 , making all classes new @-@ style . In versions between 2 @.@ 2 and 3 @.@ 0 , both kinds of classes could be used . The syntax of both styles is the same , the difference being whether the class object is inherited from , directly or indirectly ( all new @-@ style classes inherit from object and are instances of type ) .
= = = Mathematics = = =
Python has the usual C arithmetic operators ( + , - , * , / , % ) . It also has * * for exponentiation , e.g. 5 * * 3
= = 125 and 9 * * 0 @.@ 5 = =
3 @.@ 0 , and a new matrix multiply @ operator is included in version 3 @.@ 5 .
The behavior of division has changed significantly over time :
Python 2 @.@ 1 and earlier use the C division behavior . The / operator is integer division if both operands are integers , and floating @-@ point division otherwise . Integer division rounds towards 0 , e.g. 7 / 3
= = 2 and -7 / 3 = =
-2 .
Python 2 @.@ 2 changes integer division to round towards negative infinity , e.g. 7 / 3
= = 2 and -7 / 3 = =
-3 . The floor division / / operator is introduced . So 7 / / 3
= = 2 , -7 / / 3 = =
-3 , 7 @.@ 5 / / 3
= = 2 @.@ 0 and <unk> / / 3 = =
<unk> . Adding from _ _ future _ _ import division causes a module to use Python 3 @.@ 0 rules for division ( see next ) .
Python 3 @.@ 0 changes / to be always floating @-@ point division . In Python terms , the pre @-@ 3 @.@ 0 / is classic division , the version @-@ 3 @.@ 0 / is real division , and / / is floor division .
Rounding towards negative infinity , though different from most languages , adds consistency . For instance , it means that the equation ( a + b ) / / b
= = a / / b + 1 is always true . It also means that the equation b * ( a / / b ) + a % b = =
a is valid for both positive and negative values of a . However , maintaining the validity of this equation means that while the result of a % b is , as expected , in the half @-@ open interval [ 0 , b ) , where b is a positive integer , it has to lie in the interval ( b , 0 ] when b is negative .