Scientific notation in Python

Asked

Viewed 5,265 times

1

I thought about making an electrostatic formula that would give the result in scientific notation, but I don’t know how to leave it in scientific notation.

Is there a library for that?

n = int(input("Digite o valor de n "))
e = 1.6*10**-19
q = int(input("Digite o valor de Q "))
resultado = n*e


if(q !=0):
    resultado = q / e
    print(f'{resultado:.0e}')
else:
    resultado = n*e
    print(f'{resultado:.0e}')
  • A tip: to get more pythonic, you can take the if parentheses of your code.

2 answers

6

You don’t need any library for this, Python itself already has tools for formatting in scientific notation, just use the e in formatting:

>>> número = 123456789.123456
>>> print('{:e}'.format(número))
1.234568e+08

Or you can use your own f-strings:

>>> número = 123456789.123456
>>> print(f'{número:e}')
1.234568e+08

If you need to limit the number of decimals, you can:

>>> número = 123456789.123456
>>> print(f'{número:.2e}')
1.23e+08

For more information on formatting syntax, read:

  • It worked out what Voce explained to me and I understood but this having a small problem in the formula is: Q = NE. every time the end result of right but that high 10 some number is with example: 800 = ne 5e+21

  • I’m trying to help your problem, but I can’t understand your comment.

  • The problem is: n = int(input("Type the value of n ")) e = 1.610**-19 q = int(input("Type the value of Q ")) result = ne if(q !=0): result = q / and print(f'{result:.0e}') Else: result = ne print(f'{result:.0e}') 800 = n1,6*10lift19 the result is coming out 5e+21 that and means 10 wanted to appear the 10 instead of the variable I don’t know if you understand me

  • I’m sorry I didn’t know it looked awful in comments always the result is with the "e" because instead of it give the result in number that is 10 it is giving the variable and to represent the 10

  • Got it now. Scientific notation in Python uses the letter and to represent the 10^.

  • So there’s no way he could represent the 10? Just the letter and?

  • Natively in Python, no. You’ll have to look for functions that someone might have implemented. Change the question and make it more specific. Say in the question that you don’t want to use the default Python "e", that you want "10 " or any other kind of representation. Probably someone already has the solution to your problem.

  • And just one last comment. It is common knowledge in the scientific community that this "e" in the middle of numbers in this way means "10". Unless you want another form of data visualization, I don’t see a reason to change this symbol, which is the pattern.

  • Thus Obigado!!!

  • Very cool (+1); possibly print(f'{número:.2e}'.replace("e"," 10^"))

Show 5 more comments

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.