The text and code are released under the CC0 license; see also the companion project, the Python Data Science Handbook.
Python se planteó como un lenguaje de aprendizaje, pero la facilidad de su uso de su sintaxis clara lo hace preferido tanto por principiantes como expertos. Se considera estilo seudocódigo y a menudo es mucho más fácil de entender un script Python que uno en C.
Mira el siguiente ejemplo:
In [1]:
# set the midpoint
midpoint = 5
# make two empty lists
lower = []; upper = []
# split the numbers into lower and upper
for i in range(10):
if (i < midpoint):
lower.append(i)
else:
upper.append(i)
print("lower:", lower)
print("upper:", upper)
El script es algo tonto, pero ilustra varios aspectos de la sintaxis de Python.
#
El programa empieza por:
# set the midpoint
Los comentarios de una línea empiezan por (#
).
Se pueden tener comentarios en lineas completas o para describir alguna sentencia. Por ejemplo
x += 2 # shorthand for x = x + 2
Python no tiene comentarios multilíneas, como /* ... */
usado en C y C++, aunque a veces se usan cadenas multi-líneas para eso.
The next line in the script is
midpoint = 5
This is an assignment operation, where we've created a variable named midpoint
and assigned it the value 5
.
Notice that the end of this statement is simply marked by the end of the line.
This is in contrast to languages like C and C++, where every statement must end with a semicolon (;
).
In Python, if you'd like a statement to continue to the next line, it is possible to use the "\
" marker to indicate this:
In [2]:
x = 1 + 2 + 3 + 4 +\
5 + 6 + 7 + 8
It is also possible to continue expressions on the next line within parentheses, without using the "\
" marker:
In [3]:
x = (1 + 2 + 3 + 4 +
5 + 6 + 7 + 8)
Most Python style guides recommend the second version of line continuation (within parentheses) to the first (use of the "\
" marker).
Sometimes it can be useful to put multiple statements on a single line. The next portion of the script is
lower = []; upper = []
This shows the example of how the semicolon (;
) familiar in C can be used optionally in Python to put two statements on a single line.
Functionally, this is entirely equivalent to writing
lower = []
upper = []
Using a semicolon to put multiple statements on a single line is generally discouraged by most Python style guides, though occasionally it proves convenient.
Next, we get to the main block of code:
for i in range(10):
if i < midpoint:
lower.append(i)
else:
upper.append(i)
This is a compound control-flow statement including a loop and a conditional – we'll look at these types of statements in a moment. For now, consider that this demonstrates what is perhaps the most controversial feature of Python's syntax: whitespace is meaningful!
In programming languages, a block of code is a set of statements that should be treated as a unit. In C, for example, code blocks are denoted by curly braces:
// C code
for(int i=0; i<100; i++)
{
// curly braces indicate code block
total += i;
}
In Python, code blocks are denoted by indentation:
for i in range(100):
# indentation indicates code block
total += i
In Python, indented code blocks are always preceded by a colon (:
) on the previous line.
The use of indentation helps to enforce the uniform, readable style that many find appealing in Python code. But it might be confusing to the uninitiated; for example, the following two snippets will produce different results:
>>> if x < 4: >>> if x < 4:
... y = x * 2 ... y = x * 2
... print(x) ... print(x)
In the snippet on the left, print(x)
is in the indented block, and will be executed only if x
is less than 4
.
In the snippet on the right print(x)
is outside the block, and will be executed regardless of the value of x
!
Python's use of meaningful whitespace often is surprising to programmers who are accustomed to other languages, but in practice it can lead to much more consistent and readable code than languages that do not enforce indentation of code blocks. If you find Python's use of whitespace disagreeable, I'd encourage you to give it a try: as I did, you may find that you come to appreciate it.
Finally, you should be aware that the amount of whitespace used for indenting code blocks is up to the user, as long as it is consistent throughout the script. By convention, most style guides recommend to indent code blocks by four spaces, and that is the convention we will follow in this report. Note that many text editors like Emacs and Vim contain Python modes that do four-space indentation automatically.
In [4]:
x=1+2
x = 1 + 2
x = 1 + 2
Abusing this flexibility can lead to issues with code readibility – in fact, abusing white space is often one of the primary means of intentionally obfuscating code (which some people do for sport). Using whitespace effectively can lead to much more readable code, especially in cases where operators follow each other – compare the following two expressions for exponentiating by a negative number:
x=10**-2
to
x = 10 ** -2
I find the second version with spaces much more easily readable at a single glance. Most Python style guides recommend using a single space around binary operators, and no space around unary operators. We'll discuss Python's operators further in Basic Python Semantics: Operators.
In [5]:
2 * (3 + 4)
Out[5]:
They can also be used to indicate that a function is being called.
In the next snippet, the print()
function is used to display the contents of a variable (see the sidebar).
The function call is indicated by a pair of opening and closing parentheses, with the arguments to the function contained within:
In [6]:
print('first value:', 1)
In [7]:
print('second value:', 2)
Some functions can be called with no arguments at all, in which case the opening and closing parentheses still must be used to indicate a function evaluation.
An example of this is the sort
method of lists:
In [8]:
L = [4,2,3,1]
L.sort()
print(L)
The "()
" after sort
indicates that the function should be executed, and is required even if no arguments are necessary.
print()
FunctionAbove we used the example of the print()
function.
The print()
function is one piece that has changed between Python 2.x and Python 3.x. In Python 2, print
behaved as a statement: that is, you could write
# Python 2 only!
>> print "first value:", 1
first value: 1
For various reasons, the language maintainers decided that in Python 3 print()
should become a function, so we now write
# Python 3 only!
>>> print("first value:", 1)
first value: 1
This is one of the many backward-incompatible constructs between Python 2 and 3.
As of the writing of this book, it is common to find examples written in both versions of Python, and the presence of the print
statement rather than the print()
function is often one of the first signs that you're looking at Python 2 code.
This has been a very brief exploration of the essential features of Python syntax; its purpose is to give you a good frame of reference for when you're reading the code in later sections. Several times we've mentioned Python "style guides", which can help teams to write code in a consistent style. The most widely used style guide in Python is known as PEP8, and can be found at https://www.python.org/dev/peps/pep-0008/. As you begin to write more Python code, it would be useful to read through this! The style suggestions contain the wisdom of many Python gurus, and most suggestions go beyond simple pedantry: they are experience-based recommendations that can help avoid subtle mistakes and bugs in your code.