Vous êtes sur la page 1sur 7

La variable__name__es una variable global que indica desde dnde se est ejecutando el archivo.

Si este se importa como un


mdulo, entonces contiene el nombre del mdulo; si se ejecuta directamente el archivo, entonces tiene el valor "__main__":
Por tanto, se usaif __name__ == "__main__"para colocar un bloque de cdigo que solo se debe ejecutar si se ejecuta el script
directamente.
1 kibibyte (KiB) = 1024 B = 210 bytes.
1 kilobyte (KB) = 1000 B = 103 bytes.

Su nombre proviene de la contraccin de kilo binary byte.

Python passes parameters to a function using a technique known as pass by reference. This means that when you pass
parameters, the function refers to the original passed values using new names.
Python does not support pass by value.
Modules:
A module is collection of related functions and variables.
The only difference between this and a regular Python program is the intended use: A module is a toolbox of helpful functions
that you can use when writing other programs. Thus a module usually does not have a main() function.
Data Structures: collections of values along with commonly performed functions.
1) Tuples: is an immutable sequence of 0 or more values. items = (-6, 'cat', (1, 2))
2) Lists: Lists are essentially the same as tuples but with one key difference: Lists are mutable. numbers = [7, -7, 2, 3, 2]
3) Dictionaries: A Python dictionary is an extremely efficient data structure for storing pairs of values in the form key:value.
4) Sets: In Python, sets are collections of 0 or more items with no duplicates. A set is similar to a dictionary that only has keys
and no associated values.
List Comprehensions.
>>> [n ** 3 for n in range(1, 11)]
Sequences
In Python, a sequence is an ordered collection of values. Python has three built-in sequence types: strings, tuples, and lists.

OOP:
To create new kinds of objects, you must first create aclass. A class is essentially a blueprint for creating an object of a particular
kind. The class specifies what data and functions the objects will contain, and how they relate to other classes.
An objectencapsulatesboth the data and functions that operate on that data.
A function defined inside a class is called a method.
__init__ is called a constructor, because it constructs the object.
self is a variable that refers to the object itself
All classes should have an __init__(self) method whose job is to initialize the objectfor example, initializing an objects variables.
The __init__ method is only called once when the object is created.
When creating your own classes and objects, it is almost always worthwhile to write __str__ and __repr__ functions.
They are extremely useful for displaying the contents of your objects, which is helpful when debugging your programs.
object variables private starting with two underscores
Inheritance.
Overriding methods,
Polymorphism.
Class Definitions
A class serves as the primary means for abstraction in object-oriented programming.
In Python, every piece of data is represented as an instance of some class.
A class provides a set of behaviors in the form of member functions (also known
as methods), with implementations that are common to all instances of that class.
A class also serves as a blueprint for its instances, effectively determining the way
that state information for each instance is represented in the form of attributes (also
known as fields, instance variables, or data members).

NumPy
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
a powerful N-dimensional array object
sophisticated (broadcasting) functions
tools for integrating C/C++ and Fortran code
useful linear algebra, Fourier transform, and random number capabilities

Iterating Over Arrays: The iterator object nditer, introduced in NumPy 1.6, provides many flexible ways to visit all the elements
of one or more arrays in a systematic fashion.
>>> a = np.arange(6).reshape(2,3)
>>> a array([[0, 1, 2], [3, 4, 5]])
>>> for x in np.nditer(a, op_flags=['readwrite']):
...
x[...] = 2 * x
...
>>> a array([[ 0, 2, 4], [ 6, 8, 10]])
NumPy has an method called nditer , which can be used to efciently iterate over multiple array operands. It has both a Python and
C API, allowing iteration in Python as well as in C or Cython. Among other features, it allows manual control over the preferred order,
allows a manually written inner loop and can allow or disallow broadcasting and reductions.

def numpysum(n):
a = numpy.arange(n) ** 2
b = numpy.arange(n) ** 3
c=a+b
return c
-----------------------------------------------------# Creating a 5x5x5 cube of 1's
# The astype() method sets the array with integer elements.
cube = np.zeros((5,5,5)).astype(int) + 1
-----------------------------------------------------# Array of zero integers
arr = np.zeros(2, dtype=int)
-----------------------------------------------------# Array of zero floats
arr = np.zeros(2, dtype=np.float32)
------------------------------------------------------

- reshape
- arange
- dtype

>>> os.chdir('/home/rcuriel/Documentos/Python Area/Dron')


>>> os.getcwd()

Modules
If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost.
Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for
the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets
longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that
youve written in several programs without copying its definition into each program.
To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the
interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main
module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py
appended. Within a module, the modules name (as a string) is available as the value of the global variable __name__.

fibo.py

Python interpreter

>>> import fibo


>>> fibo.fib (1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2 (100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo
>>> fib = fibo.fib
>>> fib (500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

>>> from fibo import fib, fib2 >>> from fibo import *
>>> fib (500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

Executing modules as scripts:


When you run a Python module as the main file:
python fibo.py <arguments>

$ python fibo.py 50

1 1 2 3 5 8 13 21 34
The code in the module will be executed, just as if you imported it, but
with the __name__ set to "__main__". (adding at the end of your module)

if __name__ == "__main__":
import sys
fib (int(sys.argv[1]))
If the module is imported, the code is not run:
>>> import fibo
>>>