python commands

Upload: kevinpereyra

Post on 06-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Python Commands

    1/3

    Methods that use dot notation only work with strings.

    from datetime import datetimedatetime.now() - tira la hora y fechaimport math - importa funciones matematicaseverything = dir(math) - muestra todo lo que hay en el modulo "math"max(var) = devuelve el maximo valor en la lista var

    min(var) = devuelve el minimo valor en la lista varabs(var) = devuelve el modulo de vartype() = devuelve el tipo de valor entre parentesis (string, float, integer)

    isalpha() - Return true if all characters in the string are alphabetic and there is at least one character

    """ """ - Para escribir varias lineas de texto ininterrumpidasraw_input() - para recibir input string del usuarioint() - para recibir input numerico del usuario

    from sys import argv - importa argv desde sys. (una funcion desde una libreria)def - define una funcion.split() - separa las palabras de una oracion/variable dentro del parentesis, convirtiendolo en una lista de palabrassorted() - ordena alfabeticamente la lista/variable que hay entre los parentesis.pop() - remueve el item de posicion () de la lista previa al punto y lo imprime..append(i) - una funcion para listas que agrega el item "i" a la lista.

    for i in range(0, 6): - un loop que para cada item "i" entre el rango 0-5 hace lo que se le instruya mas abajo.

    variable = [1,2,3,'apple','microsoft'] - lista llamada "variable" con los itemsque estan separados por comas entre corchets

     and or not != (not equal) == (equal) >= (greater- than- equal) 

  • 8/17/2019 Python Commands

    2/3

    -Python's assert statement helps you find bugs more quickly and with less pain.

    list[3] - muestra el item de la posicion 3 de la lista

    -The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet

    yield - se usa para funciones. es como 'return', solamente que devuelve generadores. es decir, produce los resultados en el momento y luego se los olvida.

    stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2} - diccionario que vos elegis como llamar a cada item, en vez de solamente ser numeros como una lista. {Key, value}

    break - rompe un loop (ya sea while o for)

    selfInside the functions in a class, self is a variable for the instance/object being accessed.

    except - crea excepcion en loop.

    continue - continues with the next iteration of the loop

    finally - se usa en loops o funciones en combinacion con try. si hay alguna excepcion/error con try, primero ejecutara lo que hace finally.

    is - evalua si dos variables son iguales. a diferencia de "==", no evalua los resultados finales. evalua el valor que esta escrito en las variables.por ej: 1000 is 10**3 = False

    1000 = 10**3 = True

    f = lambda x: - permite hacer el calculo que venga despues de la "x" solamente escribiendo f() y el numero que es igual a "x" entre parentesis

    try - es como un "if", solamente que no ocurrira si ocurre lo que sucede en el "except" statement.ejemplo:>>try:>> doSomething()>>except Exception:>> pass

    class -

    exec -

    ' '.join(things) - Join things with   between them.

    '#'.join(stuff[3:5]) - Join things in position 3 and 4 with "#" between them.

    >>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27] | filter() calls our lambda function for each element of the list,>>> |and returns a new list that con

  • 8/17/2019 Python Commands

    3/3

    tains only those elements for which>>> print filter(lambda x: x % 3 == 0, foo) |the function returned "True".[18, 9, 24, 12, 27] |

    if something: | "raise" is used for raising exceptions.  raise Exception('My error!') |

      class controlled_execution: }Now, when the with statement is executed, Python evaluates the expression,

    def __enter__(self): |calls the __enter__ method on the resulting value (which is called a context

    set things up |guard), and assigns whatever __enter__ returns to the variable given by as.

    return thing |Python will then execute the code body, and no matter what happens in that

    def __exit__(self, type, value, traceback): |code, call the guard objects __exit__ method.

      tear things down ||

      with controlled_execution() as thing: |  some code |