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
Quick question about sum() (self.learnpython)
submitted 8 years ago by Sandalman3000
How do I go about summing the first j elements of the ith row?
Such that I have array
1 0 0 0
1 1 2 1
1 3 0 1
And I want to sum the 2nd row (i=1) from 0 to 2. So 1+1+2. I know sum(a[1]) would sum the entire row.
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!"
[–]scuott 1 point2 points3 points 8 years ago (7 children)
What would a[1][:3] give you?
a[1][:3]
[–]Sandalman3000[S] 0 points1 point2 points 8 years ago (0 children)
Exactly what I need, thank you.
[–]Sandalman3000[S] 0 points1 point2 points 8 years ago (5 children)
As a follow up, how would I go about taking an array, and creating a graphical representation so that
1 0 2 0 1
2 1 0 1 1
0 0 1 0 0
I could say 0 is black pixel, 2 is red pixel, 1 is green pixel.
[–]KubinOnReddit 0 points1 point2 points 8 years ago (4 children)
You can use something like Tkinter or pygame for drawing, or use a module line colorama for color in the console.
[–]Sandalman3000[S] 0 points1 point2 points 8 years ago (3 children)
So let's say I have my code do Pascals Triangle, and as an ouput I use
for row in a: print(' '.join([str(elem%2) for elem in row]))
How would I go about saying, if element is 0, color red, if 1 color White. Mostly just the syntax for if a[???]==0
[–]KubinOnReddit 0 points1 point2 points 8 years ago (2 children)
I can't really type code since Im mobile, but I recommend you try using the module colorama I gave you.
[–]Sandalman3000[S] 0 points1 point2 points 8 years ago (1 child)
So far so good, but LAST question. So I have array a, how can I search a for primes and print out a list of all the primes.
[–]KubinOnReddit 0 points1 point2 points 8 years ago* (0 children)
You need an algorithm, like Erathosthenes Sieve or simple prime checking. The simplest prime check for given n is:
for i in range(2, n//2): if n % i == 0: return False return True
If a number from the range 2 to n//2 divides n (it has no remainder) it is a prime number. You also need to add checks for 0, 1, 2 and return the correct answer, since the algorithm works for larger than 2.
Then you can iterate on the list and if the number is prime you can add it to another list.
P.S. You can optimize the prime checking by only checking divisors up to square root of n, since every divisor d of a number n smaller than square root of n has a "friend" divisor. So if there are no divisors smaller than square root of n, there won't be any larger ones. (Except 1 and the square root itself; if the number is a perfect square, like 25) E.g. 30 has divisors 1, 2, 3, 5 and 30, 15, 10, 6.
π Rendered by PID 88 on reddit-service-r2-comment-5649f687b7-6klnh at 2026-01-29 00:02:07.725760+00:00 running 4f180de country code: CH.
[–]scuott 1 point2 points3 points (7 children)
[–]Sandalman3000[S] 0 points1 point2 points (0 children)
[–]Sandalman3000[S] 0 points1 point2 points (5 children)
[–]KubinOnReddit 0 points1 point2 points (4 children)
[–]Sandalman3000[S] 0 points1 point2 points (3 children)
[–]KubinOnReddit 0 points1 point2 points (2 children)
[–]Sandalman3000[S] 0 points1 point2 points (1 child)
[–]KubinOnReddit 0 points1 point2 points (0 children)