ok, I understand now
Best posts made by William W. Hensley
Latest posts made by William W. Hensley

RE: Creating a list from the the intersection of two lists containing unique integers
Thanks! This works and it only takes up 4 lines of code. The computation time is also much faster than my code. I think that I understand why it works faster as well. Your code executes after looping 23 times. However, my code executes after 11 iterations of 23 or 253 iterations which is 11 times more than my code. Also, it took 13.7 milliseconds, whereas as mine took 185 milliseconds which is 13.5 times as fast. I suspect that the method that I used where I changed my list into a set and then returned to transform it into a list also took more time than your method you recommended to me, which could partially explain the discrepancy in computation time as well.

RE: Creating a list from the the intersection of two lists containing unique integers
Woah, this is very compact code. Yes, I've been introduced to list comprehensions but one thing that is incorrect about the code is that it needs to only include unique values. This code prints some numbers repeated times.

Creating a list from the the intersection of two lists containing unique integers
I've been working on beginner python exercises and one exercise required me to take two lists, say for example these two
a = [1,1,1,2,3,4,5,5] b = [1,2,4,6]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates).
I have written my code and I think that I have a solution to the problem. The problem is that when I look at the solutions I've seen online, it seems like my solution is unique. Not that this is necessarily a bad thing but I'm interested in knowing if I wrote good quality/efficient code. Here is a link to my code. https://onlinegdb.com/rkMQ1lbqLa = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,8,5,7,9,21,55,89,89,55] b = [1,1,2,3,5,8,13,21,34,55,89] intersection = [] for i in a: for j in b: if j == i: intersection.append(j) intersection_set = set(intersection) new_list = list(intersection_set) print(new_list)
I'm specifically curious to know if the nested for loop is a bad idea or if there's another concept that I could use that would result in more compact code or reduce computation assuming huge amounts of data were being compiled.

RE: PYTHON: Checking Primality of Integers
Of course! This might be a dumb question but where do you do that? It's just that I've been searching for several minutes with no success.

RE: PYTHON: Checking Primality of Integers
Thank you so much!! I understand your explanation perfectly. I already knew that when you look at range of something it includes (num1), therefore it doesn't include the last integer. I simply incorrectly assumed that because in the "range(lower,upper)", because you had the lower defined as 2, that it would check it, even though the upper, in this case, 2, would not be considered. I should have known better because even I explained that if I checked 3 for primality, the for loop would only run once. Therefore, it would make sense that if I was checking 2, then the for loop wouldn't even run once. Thanks so much, I can finally work on something else like functions and get this program behind me.

RE: PYTHON: Checking Primality of Integers
Also, I'm unsure how to use some of code compilers that you've provided however I have tried codepen and don't think I can use it for python. With that being said, below is a link to a compiler that has my code. Perhaps this could be of help.
https://onlinegdb.com/rJs7ORYKU 
RE: PYTHON: Checking Primality of Integers
First off, thanks for your friendly response. It's a breath of fresh air in comparison to StackOverflow.
Second, I probably incorrectly assumed that the average person did know, which looking back in retrospect, I too didn't know until I studied the fundamental theorem of arithmetic. Therefore I should have been more clear about that.Thirdly, the problem is that the code still prints 2 as a prime number, which is great but I don't understand why it prints it considering that the check for primes involves the modulus arithmetic and 2 modulus 2 is 0, which makes me think that the for loop should break out of the loop. The problem is that because of my small experience in coding, there is something in that code that I'm not understanding. Based on when I dissected the code, I believe that my confusion is related to the break statement.

PYTHON: Checking Primality of Integers
Goal: I want to better understand a snippet of code.
Experience: I am not an absolute beginner but I'm pretty new and have little experience. This is my first post on this website.
Problem: I don't understand why when the code checks for the primality of 2, it prints 2 as a prime number.
Background: A few days ago I decided that I wanted to be comfortable coding in python. I reviewed a lot of basic concepts that I had learned before so I would be more prepared to learn more complicated concepts. I finally came across an exercise that required me to find and print prime numbers. Below is the snippet of code that is slightly modified from a YouTube video.
lower = 2 upper = 100 for num in range(lower,upper+1): for i in range(2,num): if (num%i)==0: break else: print(num)
Here is also a link to the source of the code that I'm studying. If you are checking 2 for primality then in the code, when it comes to the modulus operation (num%i)==0, if it equals that, then supposedly you are dealing with a composite number. if my bounds were held in the natural numbers and greater than 2, the nested for loop will check for all of the numbers except for 1 and the number of interest, therefore checking for all possible factors. I'm under the belief that if the nested for loop does not find a number that is divisible by num, then the for loop will reach the end of the range and then execute the else component, which is to print num. In this case, the "num" is a prime number. The problem is that when the lower limit is 2, and I check num%i, this will translate numerically to 2%2, which equals 0. Because of this, I don't know how or why it prints the number 2 as a prime number. This doesn't happen with any other lower limit. If the lower limit is 3, then it checks 3%2, which isn't 0. In this case, the nested for loop ends at the first iteration and then the else statement is proceeded to be executed. However, clearly, if the code is executed, it absolutely prints all prime numbers, obviously including 2.