diff options
author | Philip Withnall <philip@tecnocode.co.uk> | 2013-08-27 19:37:20 (GMT) |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2013-08-27 19:37:20 (GMT) |
commit | c7d3e0944689aba97a35b7d265f663914eb94413 (patch) | |
tree | 7ea5f72c80479e91856d6f67b8285a14a5462834 | |
parent | eb84ab541306f17cf211b4b6223e5adf65af0c45 (diff) |
Add a section explaining operators
This should hopefully clear up some of the confusion about
runtime evaluation order of operators, and of their types.
-rw-r--r-- | .be/dcda61d1-5133-456a-b88c-6bf21e61777b/bugs/6ff0e3bf-8d9d-4329-ac36-f5e10b48535d/values | 2 | ||||
-rw-r--r-- | introduction-to-python.tex | 151 |
2 files changed, 152 insertions, 1 deletions
diff --git a/.be/dcda61d1-5133-456a-b88c-6bf21e61777b/bugs/6ff0e3bf-8d9d-4329-ac36-f5e10b48535d/values b/.be/dcda61d1-5133-456a-b88c-6bf21e61777b/bugs/6ff0e3bf-8d9d-4329-ac36-f5e10b48535d/values index 6e0bec6..29c9e04 100644 --- a/.be/dcda61d1-5133-456a-b88c-6bf21e61777b/bugs/6ff0e3bf-8d9d-4329-ac36-f5e10b48535d/values +++ b/.be/dcda61d1-5133-456a-b88c-6bf21e61777b/bugs/6ff0e3bf-8d9d-4329-ac36-f5e10b48535d/values @@ -26,7 +26,7 @@ - "status": "open", + "status": "fixed", diff --git a/introduction-to-python.tex b/introduction-to-python.tex index 5747428..884542e 100644 --- a/introduction-to-python.tex +++ b/introduction-to-python.tex @@ -75,6 +75,152 @@ cadenas con 10 numeros \end{frame} +\section{\en{Operators}\es{Operadores}} + +\en{So far, we've used several mathematical and string \emph{operators}. +Operators in Python are very similar to those in mathematics: symbols which +take one or two \emph{operands} (parameters or inputs) and evaluate to a single +result. (Formally, they are unary or binary infix functions.)} +\es{Hasta el momento, hemos utilizado varios \emph{operadores} matemáticos y de +cadena. Los operadores en Python son muy similares a las de las matemáticas: +símbolos que tienen uno o dos \emph{operandos} (parámetros o entradas) y +evaluar a un solo resultado. (Formalmente, son funciones infijos unario o +binario).} + +\en{As in mathematics, operators must be used with the correct number and type +of operands. It wouldn't make sense to write $6 +$ as a sum, for example; or to +write $15 8$ and expect to calculate the answer $15 + 8 = 23$.} +\es{Al igual que en las matemáticas, los operadores deben ser utilizados con el +número y el tipo correcto de operandos. No tendría sentido escribir $6 +$ como +una suma, por ejemplo, o para escribir $15 8$ y esperar para calcular la +respuesta $15 + 8 = 23$.} + +\en{When Python runs a program, operators are evaluated in order of precedence. +For example, multiplication always takes precedence over addition, as in +mathematics. Brackets can be used to enforce an ordering. During evaluation, +Python effectively repeatedly replaces each operator and its operands with the +result of the operation.} +\es{Cuando Python se ejecuta un programa, los operadores se evalúan en el orden +de precedencia. Por ejemplo, la multiplicación siempre tiene prioridad sobre la +suma, como en matemáticas. Paréntesis pueden hacer cumplir una orden. Durante +la evaluación, Python efectivamente reemplaza repetidamente cada operador y sus +operandos con el resultado de la operación.} + +\begin{frame}[fragile] +\frametitle{\en{Operator evaluation}\es{Evalución de operadors}} +\begin{block}{\en{Integers}\es{Enteros}} +\begin{lstlisting}[language=Python] +15 * 7 + (6 / 2) +== 105 + (6 / 2) +== 105 + 3 +== 108 +\end{lstlisting} +\end{block} +\begin{block}{\en{Strings}\es{Cadenas}} +\begin{lstlisting}[language=Python] +'Soy %s' % ('Sr' + ' Gustavo' + ' Hernandez') +== 'Soy %s' % ('Sr Gustavo' + ' Hernandez') +== 'Soy %s' % 'Sr Gustavo Hernandez' +== 'Soy Sr Gustavo Hernandez' +\end{lstlisting} +\end{block} +\end{frame} + +\en{Here are the type signatures of a number of Python operators. These show +how the operators should be used, with which types of variables, and what type +of output they evaluate to. Note: Almost all integer types (\texttt{int}) below +can be replaced with float. $\alpha$ represents any type. Assignment operators +do not have a return value (so, for example, \texttt{5 == (a += 2)} is not +valid Python.} +\es{Aquí están las firmas de tipo de una serie de operadores de Python. Estos +muestran cómo se deben utilizar los operadores, con el que los tipos de +variables, y qué tipo de salida que evalúan. Nota: Casi todos los tipos de +enteros (\texttt{int}) aquí pueden ser reemplazados con \texttt{float}. +$\alpha$ representa cualquier tipo. Los operadores de asignación no tienen un +valor de retorno (así, por ejemplo, \texttt{5 == (a + = 2)} no es válido +Python.} + +\begin{frame}[fragile] +\frametitle{\en{Operators}\es{Operadores}} +\begin{block}{\en{Numerical}\es{Númerico}} +\begin{align*} + \mathtt{int}\; \mathtt{+} \;\mathtt{int} &\to \mathtt{int} \\ + \mathtt{int}\; \mathtt{-} \;\mathtt{int} &\to \mathtt{int} \\ + \mathtt{int}\; \mathtt{*} \;\mathtt{int} &\to \mathtt{int} \\ + \mathtt{int}\; \mathtt{/} \;\mathtt{int} &\to \mathtt{int} \\ + \mathtt{int}\; \mathtt{\%} \;\mathtt{int} &\to \mathtt{int} \\ + \mathtt{int}\; \mathtt{**} \;\mathtt{int} &\to \mathtt{int} \\ + \mathtt{int}\; \mathtt{//} \;\mathtt{int} &\to \mathtt{int} +\end{align*} +\end{block} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\en{Operators}\es{Operadores}} +\begin{block}{\en{String}\es{Cadena}} +\begin{align*} + \mathtt{str}\; \mathtt{+} \;\mathtt{str} &\to \mathtt{str} \\ + \mathtt{str}\; \mathtt{\%} \;\mathtt{tuple} &\to \mathtt{str} +\end{align*} +\end{block} + +\begin{block}{\en{Comparison}\es{Comparación}} +\begin{align*} + \mathtt{int}\; \mathtt{>} \;\mathtt{int} &\to \mathtt{bool} \\ + \mathtt{int}\; \mathtt{<} \;\mathtt{int} &\to \mathtt{bool} \\ + \mathtt{int}\; \mathtt{>=} \;\mathtt{int} &\to \mathtt{bool} \\ + \mathtt{int}\; \mathtt{<=} \;\mathtt{int} &\to \mathtt{bool} \\ + \alpha\; \mathtt{==} \;\alpha &\to \mathtt{bool} \\ + \alpha\; \mathtt{!=} \;\alpha &\to \mathtt{bool} +\end{align*} +\end{block} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\en{Operators}\es{Operadores}} +\begin{block}{\en{Assignment}\es{Asignación}} +\begin{align*} + \alpha\; &= \;\alpha \\ + \mathtt{int}\; &\mathtt{+=} \;\mathtt{int} \\ + \mathtt{int}\; &\mathtt{-=} \;\mathtt{int} \\ + \mathtt{int}\; &\mathtt{*=} \;\mathtt{int} \\ + \mathtt{int}\; &\mathtt{/=} \;\mathtt{int} \\ + \mathtt{int}\; &\mathtt{\%=} \;\mathtt{int} \\ + \mathtt{int}\; &\mathtt{**=} \;\mathtt{int} \\ + \mathtt{int}\; &\mathtt{//=} \;\mathtt{int} +\end{align*} +\end{block} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\en{Operators}\es{Operadores}} +\begin{block}{\en{Logical}\es{Lógico}} +\begin{align*} + \mathtt{bool}\; \mathtt{and} \;\mathtt{bool} &\to \mathtt{bool} \\ + \mathtt{bool}\; \mathtt{or} \;\mathtt{bool} &\to \mathtt{bool} \\ + \mathtt{not} \;\mathtt{bool} &\to \mathtt{bool} +\end{align*} +\end{block} + +\begin{block}{\en{Membership}\es{Afiliación}} +\begin{align*} + \alpha\; \mathtt{in} \;\alpha~\mathtt{seq} &\to \mathtt{bool} \\ + \alpha\; \mathtt{not~in} \;\alpha~\mathtt{seq} &\to \mathtt{bool} +\end{align*} +\end{block} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\en{Operators}\es{Operadores}} +\begin{block}{\en{Identity}\es{Identidad}} +\begin{align*} + \alpha\; \mathtt{is} \;\alpha &\to \mathtt{bool} \\ + \alpha\; \mathtt{is~not} \;\alpha &\to \mathtt{bool} +\end{align*} +\end{block} +\end{frame} + + \section{Variables} \en{Python has \emph{variables} which allow results to be stored in memory so they @@ -117,6 +263,11 @@ the user using the \texttt{raw\_input()} function.} \es{Variables son necesarios cuando se manipulan valores desconocidos, tales como los datos del usario introducida por la función \texttt{raw\_input()}.} +\en{A single equals sign, \texttt{=}, is used to assign a value to a variable. +It is called the \emph{assignment operator}.} +\es{Un solo signo igual, \texttt{=}, se utiliza para asignar un valor a una +variable. Se llama el \emph{operador de asignación}.} + \begin{frame}[fragile] \frametitle{Variables} \begin{lstlisting}[language=Python,numbers=none] |