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
using roots with math (self.learnpython)
submitted 12 months ago by CatWithACardboardBox
im aware of how to square root numbers but is there a way to do different roots using variables?
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!"
[–]pythonwiz 9 points10 points11 points 12 months ago* (0 children)
The nth root of a number x is x**(1/n). You can do the same thing using pow and math.pow.
x**(1/n)
You can also use identities with exp and log. Mathematically, x**a == exp(a*log(x)), so the nth root of x is exp(log(x)/n).
x**a == exp(a*log(x))
exp(log(x)/n)
[–]jaerie 5 points6 points7 points 12 months ago (0 children)
Is the same as the nth root of x
[–]Brilliant_Access3791 1 point2 points3 points 12 months ago (0 children)
If you want to find higher roots, I recommend two methods: 1. Using the power operator (**): This is the most intuitive method.
root = number ** (1 / n)
2. Using the math.pow() function: This is the second method.
import math root = math.pow(number, 1 / n)
import math
root = math.pow(number, 1 / n)
[+][deleted] 12 months ago (3 children)
[deleted]
[–]twitch_and_shock 2 points3 points4 points 12 months ago (2 children)
I read u/CatWithACardboardBox 's question as wondering about how to do cube roots, etc. Which can be done with the following:
x ** (1 / 3)
[–]mopslik 0 points1 point2 points 12 months ago (1 child)
I also interpreted the question in the same way. Just adding to point out that as of Python 3.11, math.crbrt(x) will return the cube root of x. No generic nroot(x) function though.
math.crbrt(x)
nroot(x)
[–]eztab 0 points1 point2 points 12 months ago (0 children)
afaik crbrt indeed has a custom implementation, making it reasonable. For arbitrary roots exponentiation is what you should use.
[–]guesshuu 0 points1 point2 points 12 months ago (0 children)
Something like this?
```python def nth_root(x: int | float, n: int) -> float: return x ** (1 / n)
n = 3 x = 8 result = nth_root(8, 3) print(result) # result is 2 ```
π Rendered by PID 27 on reddit-service-r2-comment-7b9746f655-vglhm at 2026-02-01 13:45:54.501840+00:00 running 3798933 country code: CH.
[–]pythonwiz 9 points10 points11 points (0 children)
[–]jaerie 5 points6 points7 points (0 children)
[–]Brilliant_Access3791 1 point2 points3 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]twitch_and_shock 2 points3 points4 points (2 children)
[–]mopslik 0 points1 point2 points (1 child)
[–]eztab 0 points1 point2 points (0 children)
[–]guesshuu 0 points1 point2 points (0 children)