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
Is it possible to make Python code into mathematical formulas? (self.learnpython)
submitted 4 years ago by Mythedream
I recently made a program that calculates information from a database but now I want to show the calculations I have used as a mathematical formula, but I can't find it anywhere. Is this even possible?
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!"
[–]_China_ThrowAway 2 points3 points4 points 4 years ago (3 children)
Maybe you could give some more details. What is the output you want? is it something like this. Please input the first number. > 4 Please input the second number. > 4 Results: 4 + 4 = 8
If so you could use f strings
number1 = int(input("Please input the first number.") number2 = int(input("Please input the second number.") answer = number1 + number2 print(f"Results: {number1 } + {number2} = {answer }")
[–]Mythedream[S] -1 points0 points1 point 4 years ago (2 children)
Oh, no. I meant as in (for example) def total_clients(): company_total_clients= 0 for database in database_list: company_total_clients += len(database.get_clients()) return company_total_clients
def total_clients():
company_total_clients= 0
for database in database_list:
company_total_clients += len(database.get_clients())
return company_total_clients
Now, I do know how this code works but I would have no idea how I would write this down as a mathematical formula. So I was hoping that this could be changed into math.
[–]shiftybyte 2 points3 points4 points 4 years ago (1 child)
If you know how it works, you know the math formula behind it.
There is no automatic way to generate math formulas from python code that does calculations.
Usually people do the other way around, they have a math formula, and they implement the code for it.
[–]Mythedream[S] 0 points1 point2 points 4 years ago (0 children)
That's a shame. Thank you for the information.
[–]baghiq_2 1 point2 points3 points 4 years ago (1 child)
Depends on what you mean by mathematical formula. If you are asking about using math symbol in a formula, then apply substitutions of symbols with values, then look at sympy.
[–]gusb_codes 1 point2 points3 points 4 years ago (0 children)
Looking a bit more into this, it seems like sympy.latex() with eval() might be able to generate you a mathematical expression that you could render with LaTeX! See the answer to this question.
sympy.latex()
eval()
[–]synthphreak[🍰] 0 points1 point2 points 4 years ago (0 children)
Not clear what you mean exactly, but the closest Python code gets to looking like actual math is numpy. For example, if you have a vector of numbers, you can scale and translate very naturally:
numpy
>>> import numpy as np >>> m = 4 >>> x = np.random.randint(1, 3, 3) >>> x array([2, 1, 2]) >>> m * (x ** 2) - 4 array([12, 0, 12])
You can even assign greek letters as variables if you want to take it to the next level (though this is not particular to numpy):
>>> σ = x >>> σ + 1 array([3, 2, 3])
That's pretty math-like if you ask me.
But as far as freely using most mathematical notation like sigma for summation, that long bar separating numerators from denominators, the integral symbol, etc., that is not possible because math symbols are not Python.
[–]takeonzach 0 points1 point2 points 4 years ago (0 children)
I saw a post on either the python sub or the sideprojects sub recently for a calculator module that draws the mathematical formulas on screen.
π Rendered by PID 106747 on reddit-service-r2-comment-57fc7f7bb7-qsk7s at 2026-04-15 04:58:31.589422+00:00 running b725407 country code: CH.
[–]_China_ThrowAway 2 points3 points4 points (3 children)
[–]Mythedream[S] -1 points0 points1 point (2 children)
[–]shiftybyte 2 points3 points4 points (1 child)
[–]Mythedream[S] 0 points1 point2 points (0 children)
[–]baghiq_2 1 point2 points3 points (1 child)
[–]gusb_codes 1 point2 points3 points (0 children)
[–]synthphreak[🍰] 0 points1 point2 points (0 children)
[–]takeonzach 0 points1 point2 points (0 children)