Foros del Web » Programando para Internet » Python »

Convertir de negativo a positivo.

Estas en el tema de Convertir de negativo a positivo. en el foro de Python en Foros del Web. Hola: Quiero convertir los números negativo a positivo. Estoy usando este ejemplo que he modificado cuando vi otros ejemplos por aquí. http://www.forosdelweb.com/f130/dife...tiguos-730591/ http://www.forosdelweb.com/f16/numer...sitivo-418002/ @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); ...
  #1 (permalink)  
Antiguo 26/11/2010, 02:10
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 10 meses
Puntos: 8
Convertir de negativo a positivo.

Hola:

Quiero convertir los números negativo a positivo. Estoy usando este ejemplo que he modificado cuando vi otros ejemplos por aquí.

http://www.forosdelweb.com/f130/dife...tiguos-730591/
http://www.forosdelweb.com/f16/numer...sitivo-418002/

Código Python:
Ver original
  1. a=[1, 3, 6, 10]
  2.  
  3. def restar_valores(lista):
  4.     """ resta los valores de la lista"""
  5.    
  6.     for x in range(len(lista)-1):
  7.         print "la resta de %s y %s es igual a %s " % (lista[x],
  8.                                                       lista[x+1],
  9.                                                       lista[x] - lista[x+1])
  10.  
  11. restar_valores(a)

Saludo.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar
  #2 (permalink)  
Antiguo 26/11/2010, 09:41
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Convertir de negativo a positivo.

Si quieres convertir un numero negativo a positivo puedes usar la funcion abs en el modulo math.
Código Python:
Ver original
  1. import math
  2. print math.abs(-3)

O puedes hacer un simple if
Código Python:
Ver original
  1. if x < 0:
  2.     x = -x
  #3 (permalink)  
Antiguo 26/11/2010, 11:25
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 10 meses
Puntos: 8
Respuesta: Convertir de negativo a positivo.

gracias, también lo hice con
Código Python:
Ver original
  1. abs()
.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar

Etiquetas: negativo, positivo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:13.