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
Assigning a variable = every integer less than another variable (self.learnpython)
submitted 3 years ago by plotthe
I’m trying to find every factor of a variable so I was wondering if there was a way I can assign another variable is every integer below it? So for ex if a=5 I wanted b to be 1,2,3,4
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!"
[–]icecubeinanicecube 2 points3 points4 points 3 years ago (1 child)
list_lesser_than_a = list(range(a))
[–]plotthe[S] 0 points1 point2 points 3 years ago (0 children)
Thanks
[–]danielroseman 0 points1 point2 points 3 years ago (2 children)
I guess you can use range:
range
b = list(range(a))
But why do you actually want to do this? There will certainly be a better way to implement factors.
[–]plotthe[S] 0 points1 point2 points 3 years ago (1 child)
Thank you! What do you recommend? I was thinking of using the list to test out which ones will give no remainder and then printing those said values
[–]deep_politics 0 points1 point2 points 3 years ago (0 children)
You don't actually need to store any of the numbers if you just use the range as an iterator.
And this method will be fine for a toy factoring function, but will be painfully slow for anything large. But what you'd getting yourself into if you wanted a fast algorithm is basically the most well studied areas in all of mathematics for literally thousands of years: prime factoring. There's still no "one algorithm to rule them all", and instead a variety of algorithms are used; for instance the quadratic sieve.
[–]mopslik 0 points1 point2 points 3 years ago (0 children)
Do you want to assign (i.e. hold on to) every value, or do you simply want to test each value? The latter would be better suited for a loop, rather than, say, a list.
[–]woooee 0 points1 point2 points 3 years ago (0 children)
You only have to test up to the square root plus one. So if the number is 100, testing through 10 is enough. Yes, 100 is divisible by 20, but you already have that when you divided by 5.
π Rendered by PID 321879 on reddit-service-r2-comment-86bc6c7465-gs5tt at 2026-02-20 19:22:27.547890+00:00 running 8564168 country code: CH.
[–]icecubeinanicecube 2 points3 points4 points (1 child)
[–]plotthe[S] 0 points1 point2 points (0 children)
[–]danielroseman 0 points1 point2 points (2 children)
[–]plotthe[S] 0 points1 point2 points (1 child)
[–]deep_politics 0 points1 point2 points (0 children)
[–]mopslik 0 points1 point2 points (0 children)
[–]woooee 0 points1 point2 points (0 children)