Funciones integradas en Python.
Funciones Built-in
type("Hola")
str
type(5)
int
type(10/2)
float
type(5==5)
bool
type(5=='5')
bool
num = input("Indique un número:")
print(7==num) # ¿alguna vez dará True?
type(7==num)
Indique un número:7 False
bool
len("Hola") # longitud 4
4
len([])
0
len([1,2,3])
3
x = 2
eval("1+3*x") # 7
7
sum([1, 2, 3]) # 6 suma los elementos de una lista
6
pow(2,3) # función potencias
8
abs(-3)
3
round(6.9)
7
round(3.14159, 3)
3.142
edad = 5
#print('Solo tiene ' + edad + ' años') # TypeError: can only concatenate str (not "int") to str
print('Solo tiene ' + str(edad) + ' años') # Solo tiene 5 años str() convierte un número en un string
print('Solo tiene {} años'.format(edad)) # Solo tiene 5 años
print(f'Solo tiene {edad} años') # Solo tiene 5 años
Solo tiene 5 años Solo tiene 5 años Solo tiene 5 años
print(int(5.7)) # 5
5
float("3.14")
3.14
chr(65) # 'A' carácter código ASCII
'A'
ord
¶Es la función inversa a chr
ord("A")
65
Para pasar un número a binario.
bin(3)
'0b11'
int('0b11', 2) # proceso inverso, convierte un binario en decimal, se indica base 2
3
There are only 10 kinds of people in the world: those who understand binary, and those who don't.
hex(10)
'0xa'
int('0xa', 16) # proceso inverso, convierte un hexadecimal en decimal, se indica base 16
10
Devuelve el hash de un valor.
hash("hola")
-367871500807587473
hash(2) == hash(2.0) # el hash de un número entero coincide con el hash de ese número pero flotante
True
Tecleando topics obtenemos palabras sobre las que podemos buscar ayuda.
Para salir de la ayuda presione q
help()
Welcome to Python 3.10's help utility! If this is your first time using Python, you should definitely check out the tutorial on the internet at https://docs.python.org/3.10/tutorial/. 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, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> q 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.
Solicitar al usuario dos números y calcular el máximo y el mínimo.
num1 = float(input("Indique un número: "))
num2 = float(input("Indique otro número: "))
print(f'El máximo entre {num1} y {num2} es {max(num1,num2)}')
print(f'El mínimo entre {num1} y {num2} es {min(num1,num2)}')
Indique un número: 5 Indique otro número: -2 El máximo entre 5.0 y -2.0 es 5.0 El mínimo entre 5.0 y -2.0 es -2.0