By yatu


2019-10-09 14:21:47 8 Comments

Trying to generate a decimal number using scientific notation (or exponential notation), for instance:

1.e-2
# 0.01

I realised that using a variable as the exponent produces an error:

n = -2
1.e**n

File "", line 2

   1.e**n
     ^
SyntaxError: invalid syntax

Now I know that I can analogously write it in the form m × 10n:

n = -2
1.*10**n
# 0.01

However I was wondering if there was a way around so I can ideally stick to the former notation.

1 comments

@blue_note 2019-10-09 14:28:47

No, and there can't be. In the first case, it's a literal and it can be parsed in a special way. In the second case, it's an expression. There's no way for the language to differentiate what you mean from any normal expression.

See example grammar for how float literals are parsed. It regards older version of python, but I can't find it in the grammar of recent versions.

https://docs.python.org/2.0/ref/floating.html

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] How to read a text file into a string variable and strip newlines?

  • 2011-12-03 16:47:54
  • klijo
  • 1146965 View
  • 857 Score
  • 22 Answer
  • Tags:   python

32 Answered Questions

[SOLVED] Understanding slice notation

18 Answered Questions

[SOLVED] Using global variables in a function

17 Answered Questions

[SOLVED] Are static class variables possible in Python?

26 Answered Questions

[SOLVED] How do I pass a variable by reference?

42 Answered Questions

[SOLVED] Checking whether a variable is an integer or not

  • 2010-08-17 10:15:08
  • Hulk
  • 1142112 View
  • 815 Score
  • 42 Answer
  • Tags:   python

9 Answered Questions

[SOLVED] How to access environment variable values?

1 Answered Questions

3 Answered Questions

Sponsored Content