1

Why can't we enter the imaginary part in complex data type in formats other than decimal number (for example octal/binary/hex)? In real part we can use any form (hexadecimal/octal)

Ex.1:

x = 0B1111 + 20J     #  => valid

Ex. 2:

x = 0B1111 + 0b111J  #  => Syntax Error
2
  • There is no "real part". There is only a regular number added to a complex one. Commented Aug 12, 2020 at 15:22
  • Hi @MisterMiyagi Thanks for your reply.Can you please elaborate more what it mean by only regular number added. And also x = 0B1111 + 20J in this what is the regular number & real part. Commented Aug 13, 2020 at 3:21

1 Answer 1

3

Because in Python, complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range:

https://docs.python.org/3/reference/lexical_analysis.html#imaginary-literals

imagnumber ::=  (floatnumber | digitpart) ("j" | "J")

There is no binary literal support for floats therefore it raises a syntax error.

When you write 0b11+2j, the real part (0b11) is implicitly converted to a float.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.