![]() |
|
[Tut] Python complex() — A Useless Python Feature? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python complex() — A Useless Python Feature? (/thread-98817.html) |
[Tut] Python complex() — A Useless Python Feature? - xSicKxBot - 12-18-2020 Python complex() — A Useless Python Feature? The Python Code: complex()This article shows you how to use Python’s built-in Code: complex()Usage Learn by example! Here are some examples of how to use the Code: complex()Code: >>> 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 Code: complex()Code: 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 | Code: realThe real part of the complex number | Code: imgThe imaginary part of the complex number | Code: stringA string defining the real and imaginary part in the form Code: 'x+yj'Code: 'x+yJ'Code: xCode: yReturn Value | Code: complexReturns a complex number. Video Complex() Interactive Shell Exercise: Understanding Complex() Consider the following interactive code: https://trinket.io/embed/python/fdfd3b9a88 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 Code: complex()Code: x+yjCode: xCode: yCode: a = 1+1j
b = 4+42j
c = 0+0j print('Complex Numbers:')
print(a, b, c) print('Types:')
print(type(a), type(b), type(c))The output is: Code: Complex Numbers:
(1+1j) (4+42j) 0j
Types:
<class 'complex'> <class 'complex'> <class 'complex'>Summary The Python Code: complex()
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. |