Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python complex() — A Useless Python Feature?

#1
Python complex() — A Useless Python Feature?

The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those.

This article shows you how to use Python’s built-in complex() constructor. You’ll not only learn how to use it—but also why it is useless and what you should do instead in newer Python versions.

Usage


Learn by example! Here are some examples of how to use the complex() built-in function:

>>> complex(1, -2)
(1-2j)
>>> complex(2, -1)
(2-1j)
>>> complex(2, 2)
(2+2j)
>>> complex(1)
(1+0j)
>>> complex(2)
(2+0j)
>>> complex('42-21j')
(42-21j)

Syntax Complex()


You can use the complex() method with three different argument lists.

Syntax: 
complex(real) # Imaginary Part is 0j
complex(real, img) # Both real and imaginary part are given
complex(string) # String has format 'x+yj' for real part x and imaginary part y.

Arguments real The real part of the complex number
img The imaginary part of the complex number
string A string defining the real and imaginary part in the form 'x+yj' or 'x+yJ' where x and y are integers for the real and imaginary parts.
Return Value complex Returns a complex number.

Video Complex()




Interactive Shell Exercise: Understanding Complex()


Consider the following interactive code:

Exercise: Guess the output before running the code.


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!


The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


How to Create Complex Number Without complex()


Interestingly, you don’t need the complex() constructor to create a complex number! Instead, newer version of Python have built-in complex number support—just use the the syntax x+yj for real part x and imaginary part y to obtain a complex number.

a = 1+1j
b = 4+42j
c = 0+0j print('Complex Numbers:')
print(a, b, c) print('Types:')
print(type(a), type(b), type©)

The output is:

Complex Numbers:
(1+1j) (4+42j) 0j
Types:
<class 'complex'> <class 'complex'> <class 'complex'>

Summary


The Python complex() method returns a complex number object. To create a complex number:

  • Pass a string argument to convert the string to a complex number, or
  • Provide the real and imaginary parts to create a new complex number from those.

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python complex() — A Useless Python Feature? first appeared on Finxter.



https://www.sickgaming.net/blog/2020/12/...n-feature/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] 5 Expert-Approved Ways to Remove Unicode Characters from a Python Dict xSicKxBot 0 11 8 hours ago
Last Post: xSicKxBot
  [Tut] Python BS4 – How to Scrape Absolute URL Instead of Relative Path xSicKxBot 0 29 12-02-2025, 10:14 AM
Last Post: xSicKxBot
  [Tut] Python Int to String with Trailing Zeros xSicKxBot 0 35 12-01-2025, 05:47 PM
Last Post: xSicKxBot
  [Tut] How to Install xlrd in Python? xSicKxBot 0 40 12-01-2025, 01:23 AM
Last Post: xSicKxBot
  Python Modules for GUI interfaces SickProdigy 0 5,178 02-18-2024, 10:49 AM
Last Post: SickProdigy
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,395 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] Wrap and Truncate a String with Textwrap in Python xSicKxBot 0 2,054 09-01-2023, 07:45 PM
Last Post: xSicKxBot
  [Tut] Use enumerate() and zip() Together in Python xSicKxBot 0 1,803 08-28-2023, 09:35 AM
Last Post: xSicKxBot
  [Tut] Boolean Operators in Python (and, or, not): Mastering Logical Expressions xSicKxBot 0 1,812 08-27-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,969 08-26-2023, 01:28 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016