Você está na página 1de 8

APRENDIENDO PYTHON DESDE CERO PARTE 4 ( mas numeros y operaciones) LONG INTEGERS (enteros largos) Los enteros largos

son un poco exoticos, pero son numeros que pueden ser tan largos como memoria tengamos mientras que luego de escribirlos le coloquemos la letra L. Veamos estos ejemplos en la ventana interactiva de python.

Al hacer una operacin siendo uno de los miembros un entero LONG, lo cual esta expresado por la L al final, el resultado sera un long tambien.

Vemos que el resultado mientras que tengamos memoria, tratara de escribirse tan largo como se pueda. En el ejemplo siguiente vemos que en las nuevas versiones de python (mayor que 2.2), si escribimos una operacin que deberia dar overflow porque ninguno de los dos operadores es long, python convierte el resultado a long y lo muestra sin hacer overflow.

NUMEROS COMPLEJOS Python posee soporte para numeros complejos, para el que los utiliza, los mencionaremos por encima mostrando algunos ejemplos, sabemos que son dos partes una real ( la expresada con jota minuscula) y otra imaginaria (expresada con jota mayuscula)

Alli hay tres ejemplos, realmente los numeros complejos se usan mas que nada en usos cientificos, y raramente en uso comun, pero dejamos constancia de como se usan en python brevemente.

NOTACION HEXADECIMAL Y OCTAL Ademas de la notacion decimal (base 10) que se utiliza normalmente, python puede expresar numeros en notacion hexadecimal (base 16) y octal(base 8 ) Los octales se representan comenzando en 0 por ejemplo 01, 010, 0100 corresponden a los decimales (1, 8 y 64) y los hexadecimales se les agrega delante el prefijo 0x 0x01, 0x10, 0xFF que corresponden a los decimales (1, 16, 255) Creo que es parte de un manual de matematica y no de programacion en python, ensear la forma de convertir de un tipo al otro y excede el objetivo este curso, pero veremos las funciones que tiene python predefinidas para hacerlo. La funciones oct y hex permiten convertir decimales a octal y hexa respectivamente

alli vemos que oct (64) transforma el decimal 64 en octal, y lo mismo hexa (64) transforma el decimal 64 en hexa. La funcion opuesta para convertir a decimal de octal y de decimal a hexa es la funcion int

Esta funcion de dos argumentos, el primero es el numero a convertir, y el segundo argumento, si es 8, convierte a octal, y si es 16 convierte a hexa, usando una sola funcion para ambos casos. Segn lo visto debemos tener cuidado siempre de no poner ceros delante de numeros decimales, por ejemplo si quiero escribir el decimal 10, no debo ponerle un cero delante pues 010, sera interpretado como el octal 8, asi que siempre que escriba un decimal debo cuidarme de nunca ponerle ceros delante en ningun caso. OTRAS HERRAMIENTAS NUMERICAS A diferencia de las funciones predefinidas como vimos en el caso de int, oct,hex, que se pueden utilizar sin importarlas de un modulo externo, hay funciones que para utilizarlas hay que importar algun modulo, en el cual estan definidas. Por ejemplo si importamos el modulo math

Cuando escribimos el punto nos aparecen diferentes funciones que podemos utilizar de este modulo, por ejemplo

math.pi

Nos devuelve la constante pi, lo mismo trae incluidas mas constantes y funciones utiles que se pueden consultar en la documentacion del modulo.

Como habiamos visto el comando dir nos mostraba todas las posibles funciones y variables del modulo math, que podemos utilizar al importarlo. Alli vemos por ejemplo la funcion sin (seno)

Bueno es muy facil darse cuenta por el nombre que hace cada funcion, aunque hay una forma mas detallada de obtener informacion de las funciones incluidas en un modulo. En este caso tipeemos

help (math) el resultado es el siguiente >>> help (math) Help on built-in module math: NAME math FILE (built-in) DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(...) acos(x) Return the arc cosine (measured in radians) of x. asin(...) asin(x) Return the arc sine (measured in radians) of x. atan(...) atan(x) Return the arc tangent (measured in radians) of x. atan2(...) atan2(y, x) Return the arc tangent (measured in radians) of y/x. Unlike atan(y/x), the signs of both x and y are considered. ceil(...) ceil(x) Return the ceiling of x as a float. This is the smallest integral value >= x. cos(...) cos(x) Return the cosine of x (measured in radians). cosh(...)

cosh(x) Return the hyperbolic cosine of x. degrees(...) degrees(x) -> converts angle x from radians to degrees exp(...) exp(x) Return e raised to the power of x. fabs(...) fabs(x) Return the absolute value of the float x. floor(...) floor(x) Return the floor of x as a float. This is the largest integral value <= x. fmod(...) fmod(x,y) Return fmod(x, y), according to platform C. x % y may differ. frexp(...) frexp(x) Return the mantissa and exponent of x, as pair (m, e). m is a float and e is an int, such that x = m * 2.**e. If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0. hypot(...) hypot(x,y) Return the Euclidean distance, sqrt(x*x + y*y). ldexp(...) ldexp(x, i) -> x * (2**i) log(...) log(x[, base]) -> the logarithm of x to the given base. If the base not specified, returns the natural logarithm (base e) of x. log10(...) log10(x) -> the base 10 logarithm of x. modf(...) modf(x)

Return the fractional and integer parts of x. Both results carry the sign of x. The integer part is returned as a real. pow(...) pow(x,y) Return x**y (x to the power of y). radians(...) radians(x) -> converts angle x from degrees to radians sin(...) sin(x) Return the sine of x (measured in radians). sinh(...) sinh(x) Return the hyperbolic sine of x. sqrt(...) sqrt(x) Return the square root of x. tan(...) tan(x) Return the tangent of x (measured in radians). tanh(...) tanh(x) Return the hyperbolic tangent of x. DATA e = 2.7182818284590451 pi = 3.1415926535897931 ----------------------------------------------------------------------------------------------------------------------Como vemos nos da una descripcion mas detallada de las funciones y posibilidades del modulo, si quisieramos solo info de una de ellas, la tipeamos dentro del parentesis.

Como vimos nos devuelve la info solo de asin y no de todo el modulo. Si escribimos

Nos sale la ayuda general Welcome to Python 2.4! This is the online help utility. If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://www.python.org/doc/tut/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, or topics, type "modules", "keywords", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose summaries contain a given word such as "spam", type "modules spam". You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt. >>> help () Welcome to Python 2.4! This is the online help utility. If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://www.python.org/doc/tut/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, or topics, type "modules", "keywords", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose summaries contain a given word

such as "spam", type "modules spam". You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt. Y una ventana para tipear sobre que queremos ayuda

si pongo

Me muestra el mismo resultado que si tipeo help (math). Bueno despacio terminamos de ver tipos de numeros y operaciones incluidas e importadas de otros modulos, en la parte siguiente comenzaremos a ver el siguiente tipo de objeto que son las strings. Hasta la parte 5 Ricardo Narvaja

Você também pode gostar