Ir al contenido principal

Aritmética

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)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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

Entradas populares de este blog

Funciones Geometricas

En este programa encontramos múltiples formulas para la obtención de área y volumen de una gran variedad de figuras geométricas. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> import math def cuadrado(): lado=float(input("Dame el valor de lado: ")) cuadrado=[] cuadrado.append(lado) area=cuadrado[0]*cuadrado[0] cuadrado.append(area) print("Lado= {}, Area= {}".format(cuadrado[0],cuadrado[1])) def triangulo(): triangulo=[] base=float(input("Dame la base: ")) altura=float(input("Dame la altura: ")) triangulo.append(base) triangulo.append(altura) area=(triangulo[0]*triangulo[1])/2 triangulo.append(area) print("Base= {}, Altura= {}, Area= {}".format(triangulo[0],triangulo[1],triangulo[2])) def rectangulo(): rectangulo...

Fotoresistencia y foco

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> from turtle import * import turtle  import numpy as np from pylab import* import random import time import RPi.GPIO as GPIO, time t=Turtle() screen=t.getscreen() setup(400,300,0,0) screensize(150,150) colormode(255) t=Turtle() t.speed(10) screen=t.getscreen() setup(900,620,0,0) screensize(500,150) colormode(255) turtle.bgcolor("darkgray") t.hideturtle() t.penup() t.pensize(5) t.goto(-250,-150) t.pendown() t.goto(450,-150) t.penup() t.goto(-250,-150) t.pendown() t.goto(-450,-300) t.penup() t.goto(-250,-150) t.pendown() t.goto(-250,500) t.penup() t.goto(100,300) t.pendown() t.dot(40,255,255,255) t.dot(40,255,250,0) t.bgcolor("darkgray") GPIO.setmode(GPIO.BCM) valor=0 def medida (): measu...

Radar de velocidad

Las siguientes líneas de código dibujan y actúan como un radar de velocidad en pequeña escala. El sensor lee dos distancias (una inicial y una final) entre 50 y 10 cm, es a los 50 cm cuando lee la inicial y a menos de 10 cuando obtiene la final, al mismo tiempo contara las décimas de segundo para obtener el tiempo. Ya teniendo las dos variables necesarias para calcular una velocidad solo realiza la operación y muestra el resultado en la interfaz.  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> import time import botbook_gpio as gpio from turtle import * t=Turtle() screen=t.getscreen() setup(720,720,0,0) t.speed(5) t.penup() screen.bgcolor("gray") t.fillcolor("white") t.begin_fill() t.goto(200,-200) t.pendown() t.goto(...