text
stringlengths
0
6.86k
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 eg 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 eg 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 eg 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 eg 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 ( eg 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 eg 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 eg 7 / 3
= = 2 and 7 / 3 = =
2
python 2 @@ 2 changes integer division to round towards negative infinity eg 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