Estos programas contienen los operadores necesarios para realizar operaciones aritméticas en Python.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog1(simple_ops)
#OPERACIONES SENCILLAS CON PYTHON
x=15
y=4
#SUMA
# Output: x+y=19
print('x+y=', x+y)
#RESTA
# Output: x-y=11
print('x-y=', x-y)
#MULTIPLICACION
# Output: x*y=60
print('x*y=', x*y)
#DIVISION
# Output: x/y=3.75
print('x/y=', x/y)
#DIVISION ENTERA
# Output: x//y=3 (Division entera)
print('x//y=', x//y)
#POTENCIA
# Output: x**y=50625 (Potencia)
print('x**y=', x**y)
#MODULO
# Output: x%y=3
print('x modulo y=', x%y)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog2(comparative_ops)
#USAMOS OPERADORES PARA REALIZAR COMPARACIONES ENTRE VARIABLES
x=10
y=12
#MAYOR QUE
# Output: x>y is False
print('x > y is', x>y)
#MENOR QUE
# Output: x<y is True
print('x < y is', x<y)
#IGUAL
# Output: x==y is False
print('x == y is', x==y)
#DISTINTO
#Output: x!=y is True
print(' x!= y is', x!=y)
#MAYOR O IGUAL QUE
#Output: x>=y is False
print(' x>= y is', x>=y)
#MENOR O IGUAL QUE
#Output: x<=y is True
print('x <= y is', x<=y)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog3(word_ops)
#USO DE COLOR
from colorama import init, Fore, Back, Style
init()
x=True
y=False
#OPERACIONES BINARIAS CON PALABRAS
# Output: x and y is False
print(Fore.RED+'x and y is', x and y)
# Output: x or y is True
print(Back.WHITE+'x or y is', x or y)
# Output: not x is False
print(Fore.WHITE+Back.BLUE+'not x is', not x)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog4(binary_ops)
#OPERACIONES BINARIAS CON PALABRAS Y OPERADORES
#operacion de cada digito binario
operacion=1|2
#suma/ 01+10=11
print(operacion)
#or verdadero o falso
op=1 or 2
print(op)
operacion2=1&2
#multiplicacion/ 01*10=00
print(operacion2)
#and
op2=1 and 2
print(op2)
operacion3=2^3
#resta/ 10 - 11= 00
print(operacion3)
#or exclusivo
operacion4= ~0
#negacion
print(operacion4)
#not
op4=not 3
print(op4)
#desplazamiento a la derecha divide
n=12
op5=n>>2
print(op5)
op6=op5>>1
print(op6)
#desplazamiento a la derecha multiplica
m=12
op7=m<<2
print(op7)
print(op7<<1)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog1(simple_ops)
#OPERACIONES SENCILLAS CON PYTHON
x=15
y=4
#SUMA
# Output: x+y=19
print('x+y=', x+y)
#RESTA
# Output: x-y=11
print('x-y=', x-y)
#MULTIPLICACION
# Output: x*y=60
print('x*y=', x*y)
#DIVISION
# Output: x/y=3.75
print('x/y=', x/y)
#DIVISION ENTERA
# Output: x//y=3 (Division entera)
print('x//y=', x//y)
#POTENCIA
# Output: x**y=50625 (Potencia)
print('x**y=', x**y)
#MODULO
# Output: x%y=3
print('x modulo y=', x%y)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog2(comparative_ops)
#USAMOS OPERADORES PARA REALIZAR COMPARACIONES ENTRE VARIABLES
x=10
y=12
#MAYOR QUE
# Output: x>y is False
print('x > y is', x>y)
#MENOR QUE
# Output: x<y is True
print('x < y is', x<y)
#IGUAL
# Output: x==y is False
print('x == y is', x==y)
#DISTINTO
#Output: x!=y is True
print(' x!= y is', x!=y)
#MAYOR O IGUAL QUE
#Output: x>=y is False
print(' x>= y is', x>=y)
#MENOR O IGUAL QUE
#Output: x<=y is True
print('x <= y is', x<=y)
Prog3(word_ops)
#USO DE COLOR
from colorama import init, Fore, Back, Style
init()
x=True
y=False
#OPERACIONES BINARIAS CON PALABRAS
# Output: x and y is False
print(Fore.RED+'x and y is', x and y)
# Output: x or y is True
print(Back.WHITE+'x or y is', x or y)
# Output: not x is False
print(Fore.WHITE+Back.BLUE+'not x is', not x)
Prog4(binary_ops)
#OPERACIONES BINARIAS CON PALABRAS Y OPERADORES
#operacion de cada digito binario
operacion=1|2
#suma/ 01+10=11
print(operacion)
#or verdadero o falso
op=1 or 2
print(op)
operacion2=1&2
#multiplicacion/ 01*10=00
print(operacion2)
#and
op2=1 and 2
print(op2)
operacion3=2^3
#resta/ 10 - 11= 00
print(operacion3)
#or exclusivo
operacion4= ~0
#negacion
print(operacion4)
#not
op4=not 3
print(op4)
#desplazamiento a la derecha divide
n=12
op5=n>>2
print(op5)
op6=op5>>1
print(op6)
#desplazamiento a la derecha multiplica
m=12
op7=m<<2
print(op7)
print(op7<<1)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Prog5(op_ternario)
#operadores ternarios
a=10
b=4
val1=5
val2=20
#val=(a==b)?100:200 condicionales
variable=(val1,val2)[a==b]
print(variable)
x=3
y=10
var=(x if (a==b) else y)
print(var)
v1="Falso"
v2="Verdadero"
v=int(input("Dame un valor: "))
print(v)
print((v1,v2)[v==100])
var=1
if var>0:
print("Tiene valor")
else:
print("No tiene valor")
Prog5(op_ternario)
#operadores ternarios
a=10
b=4
val1=5
val2=20
#val=(a==b)?100:200 condicionales
variable=(val1,val2)[a==b]
print(variable)
x=3
y=10
var=(x if (a==b) else y)
print(var)
v1="Falso"
v2="Verdadero"
v=int(input("Dame un valor: "))
print(v)
print((v1,v2)[v==100])
var=1
if var>0:
print("Tiene valor")
else:
print("No tiene valor")
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Comentarios
Publicar un comentario