Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Apuntes Python
Strings
x.index("i")
x.count("i")
x.find("la")
Convertir a minúsculas
x.lower()
Apuntes Python 1
Convertir a mayúsculas
x.upper()
x.startswith("c")
x.endwith("a")
x.strip()
x.replace(" ","")
x.split(",")
" ".join(a)
Apuntes Python 2
x+y
Repetir un string
x*3
"b" in x
x1=2
x2="perros"
x3=5.4
f"hola tengo {x1} {x2} y pensan {x3} kilos"
x2[0:2]
Apuntes Python 3
x2[1:]
x2[:2]
str(5)
int('5')
int('5',16)
float('5.4')
type(x)
int(1.7)
float()
abs(x)
Apuntes Python 4
Elevar un numero
pow(3,2)
Boolean
Negar un boolean
not(1==1)
Listas
L.append(100)
L.insert(2,"tuesday")
del L[0]
o equivalentemente:
L.pop(0)
Apuntes Python 5
Eliminar elementos que tengan un valor en especifico
L.remove("monday")
L.extend(L2)
L.sort()
sorted(L)
L.reverse()
sorted(L,reverse=True)
Sets
Crear un set
S=set(L)
S={1,23,4}
Apuntes Python 6
S1.intersection(S2)
S1.difference(S2)
S1.union(S2)
S1.pop()
S1.clear()
F1=frozenset(S1)
S1.add(500)
Tuplas
Los elementos de una tupla son inmutables
T=(1,3,6,2)
Apuntes Python 7
La funcion dir(T) muestra que cosasse pueden aplicar al objeto
Ranges
range(0,10,2)
range(10)
Diccionarios
Definir un diccionario
dic={'T1':'1','T2':'2','T3':'3'}
dic['T1']
dic['T1']='5'
del dic['T1']
Apuntes Python 8
dic.keys()
dic.values()
dic.items()
Condicionales
x=4
if x>5:
print("grater")
elif x==5:
prin("equal")
else:
print("lower")
Loops
For
for i in L:
print(i)
for i in range(10):
print(i)
for i in range(len(L)):
print(L[i])
While
Apuntes Python 9
x=1
while x<=10:
print(x)
x+=1
break
continue
try
for i in range(5):
try:
print(i/0)
except ZeroDivisionError:
print("Division by 0 is just wrong!")
for i in range(5):
try:
print(i/0)
except ZeroDivisionError:
print("Division by 0 is just wrong!")
Apuntes Python 10
finally:
print(i)
Functions
def hello():
print("hello")
def test(x):
return x
def test(x=2):
return x
def nonarg(*args):
print(args)
nonarg(2,3,4,5)
import folder.function1()
from folder import function1
from folder import *
if _name_=="_main_":
print("lala")
Librerias python
Apuntes Python 11
pip install name_library
Files
Leer un archivo
myfile = open("\home\...\file.txt","r")
myfile.mode
myfile.read()
myfile.readline()
newfile = open("\...\file.txt","w")
newfile.write("holi")
newfile.writelines(["holi","holi"])
newfile.close()
newfile= open("\...\file.txt","a")
newfile.write("holi")
newfile.writelines(["holi","holi"])
newfile.close()
Apuntes Python 12
Regex
import re
text="Los perritos son kawai"
re.match("Los",text,re.I)
import re
text="Los perritos son kawai Los"
re.search("Los",text)
import re
text="Los perritos son kawai Los"
re.findall("Los",text)
Apuntes Python 13