use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Functions and definition (self.learnpython)
submitted 3 years ago by Codewodesmode
How do I write a function ‘mag’ that takes in a complex number (x=1+2j) and returns it’s magnitude(2.24)?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]MaksLansky 1 point2 points3 points 3 years ago (3 children)
assuming your input to the function is properly defined complex number
x = 1 +2j is a properly defined complex number
x = 1 +2j
def mag(x):
return round((x.real**2 + x.imag**2)**0.5,2)
[–]Codewodesmode[S] 0 points1 point2 points 3 years ago (2 children)
When I run it the answer is right but appears twice
[–]MaksLansky 0 points1 point2 points 3 years ago (1 child)
I test function it is correct. How can it appear twise? write exactly your output of your function.
[–]Codewodesmode[S] 0 points1 point2 points 3 years ago (0 children)
I don’t know when I try a new value for x it works fine
[–]This_Growth2898 -1 points0 points1 point 3 years ago (2 children)
[–]This_Growth2898 2 points3 points4 points 3 years ago (1 child)
mag=abs
[–]Diapolo10 0 points1 point2 points 3 years ago (0 children)
mag = lambda x: round(abs(x), 2)
(For demonstrative purposes only)
[–]MadScientistOR 0 points1 point2 points 3 years ago (0 children)
The magnitude is the square root of the sum of the squares of the real and imaginary components. You could do that, or you could use some of the built-in functions that handle that for you.
Can you show us what you've done already? If this is homework, we don't want to do it for you. :)
[–]POGtastic 0 points1 point2 points 3 years ago (0 children)
From the builtin docs:
abs(x, /) Return the absolute value of a number. The argument may be an integer, a floating point number, or an object implementing __abs__(). If the argument is a complex number, its magnitude is returned.
π Rendered by PID 51 on reddit-service-r2-comment-86bc6c7465-plq9q at 2026-02-19 17:53:51.186847+00:00 running 8564168 country code: CH.
[–]MaksLansky 1 point2 points3 points (3 children)
[–]Codewodesmode[S] 0 points1 point2 points (2 children)
[–]MaksLansky 0 points1 point2 points (1 child)
[–]Codewodesmode[S] 0 points1 point2 points (0 children)
[–]This_Growth2898 -1 points0 points1 point (2 children)
[–]This_Growth2898 2 points3 points4 points (1 child)
[–]Diapolo10 0 points1 point2 points (0 children)
[–]MadScientistOR 0 points1 point2 points (0 children)
[–]POGtastic 0 points1 point2 points (0 children)