Inteligencia Artificial Lenguaje Python
Python
David Masip Rodó
PID_00174138
Los textos e imágenes publicados en esta obra están sujetos –excepto que se indique lo contrario– a
una licencia de Reconocimiento-NoComercial-SinObraDerivada (BY-NC-ND) v.3.0 España de
Creative Commons. Podéis copiarlos, distribuirlos y transmitirlos públicamente siempre que citéis
el autor y la fuente (FUOC. Fundació per a la Universitat Obertade Catalunya), no hagáis un uso
comercial y no hagáis una obra derivada. La licencia completa se puede consultar en
http://creativecommons.org/licenses/by-nc-nd/3.0/es/legalcode.es
CC-BY-NC-ND • PID_00174138
El lenguaje Python
Índice
Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
Objetivos . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
1.
Instalación de Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.1.
Operadores . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.2.
Cadenas de caracteres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
Control de flujo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
3.1.
Sentencias condicionales: la instrucción if . . . . . . . . . . .. . . . . . . . .
12
3.1.1.
Sentencias if-elif-else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
Secuencias iterativas: bucles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
3.2.1.
Bucles for ... in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
3.2.2.
while . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . .
14
Funciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
4.1.
Parámetros de entrada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
4.2.
Valores de retorno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .
18
Tipos de datos en Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
5.1.
Tuplas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
5.2.
Listas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
215.3.
Diccionarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
5.4.
Conjuntos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
5.5.
Ficheros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
5.5.1.
Ficheros de texto. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
25
Python y la orientación a objetos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27
6.1.
Los objetos en Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27
6.1.1.
3.
3.2.
4.
5.
6.
7.
Creación de instancias (objetos) de una clase . . . . . . . . .
28
6.2.Herencia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
6.3.
Encapsulación . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
6.4.
Polimorfismo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
Python como lenguaje funcional ....
Regístrate para leer el documento completo.