Inquiries about Python Boolean function and list utilization

Asked 2 years ago, Updated 2 years ago, 138 views

# Initialize the list to contain the prime numbers
primes = []
for n in range(2, 101):
# Let's just say that n is a prime number
    is_prime = False
    For num in range(2, n): # For num between 2 and (n-1)
        If n % num == 0: # If any of these numbers has an abbreviation of n,
            is_prime = False #Not a decimal
    If is_prime==False: # Add prime to the list if it is a decimal
        The prime.append(n) #append() method adds n to the list
print(primes)

This was originally the code for the problem of finding the prime number.But I was curious, so I manipulated some things inside

# Initialize the list to contain the prime numbers
primes = []
for n in range(2, 101):
# Let's just say that n is a prime number
    is_prime = False
    For num in range(2, n): # For num between 2 and (n-1)
        If n % num == 0: # If any of these numbers has an abbreviation of n,
            is_prime = False #Not a decimal
    If is_prime: # Add prime to the list
        The prime.append(n) #append() method adds n to the list
print(primes)

I just changed them all to false The result value was []. And so I added == False. if is_prime==False: # If it is a decimal, add it to the list called prime

# Initialize the list to contain the prime numbers
primes = []
for n in range(2, 101):
# Let's just say that n is a prime number
    is_prime = False
    For num in range(2, n): # For num between 2 and (n-1)
        If n % num == 0: # If any of these numbers has an abbreviation of n,
            is_prime = False #Not a decimal
    If is_prime==False: # Add prime to the list if it is a decimal
        The prime.append(n) #append() method adds n to the list
print(primes)

And then the result sent me the whole number [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

I'm so curious why it's so different. In Operation 1, I just changed all True to False, but I wonder why the value is different from True (I told you to put it in the list when the initial value was False and False, but why is the value different from == False in Operation 2?) It's hard because it's my first time with Python Thank you for your answer. Have a nice day

boolean python

2022-09-20 10:51

1 Answers

First of all, let me point out that there seems to be an error in the "original problem."

# Let n be a decimal
    is_prime = False

If n is a decimal, then is_prime = True. That's what I'm saying, right?

if is_prime: is converted to if is_prime is Truth: and is_prime was false so that if is passed. Regarding I think this article will be helpful.

This is an empirical tip, and if you don't get any strong or all of them, it's because something that needs to change properly in the middle has never changed. If you look at the manipulation 1 code you posted, is_prime is actually born with a fate that cannot be changed to anything other than False.

Please let me know if there are any questions you haven't solved yet. The attitude of exploration is very good.


2022-09-20 10:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.