Hello, I'm a student who is learning.
We studied the decimal discrimination function as follows.
A decimal number between 10 and 50 was printed.
defis_prime(n): # decimal discrimination function
if n == 2 or n == 3: return True # decimal if number is 2 or 3
For i in range (3, n, 2): # range is an odd number from 3 to n
if n % i == 0:
Return False # If divided by odd numbers, it is not a prime number
return True
prime_list = [x for x in range(10, 51) if is_prime(x)]
print(prime_list)
Instead of is_prime(x) in the conditional expression if, in the second line of the list abbreviation expression from the bottom, I'd like to write down the function without bringing it up. But it's not easy.
prime_list = [x for x in range(10, 51) if x == 2 or x == 3 or (x for y in range(3, x, 2) if x % y != 0)]
I was wondering if I should do it the same way as above. I can't think of any more ways to use it.<
python
Functions have the ability to hide some complex algorithms.
The algorithm of determining whether it is a prime number is quite complicated. When given a number, you have to see if a number less than that is divided by a given number. The function that does this complex thing is compressed into the name called is_prime
. The concept that we call strong prime numbers contains the concept of "minus 1 and self-factor," but it's similar to simply communicating with people who know the concept with the word strong prime. This mathematical concept contains a small number of different properties, and it conveys the idea of what the prime number is like and what the prime number is like. Without this simple "name," more complex theory development based on this concept will be difficult and complicated. Every time, you have to give a lengthy explanation, "Mineral Water is Lesson 1 and its own factor."
Well, so the conclusion is that it's better not to solve is_prime, which is well defined as a function, without using a function. And while list compliance is Python's signature syntax for reducing for loops in one line (but let's say it's not necessarily the case), Python, which separates code blocks by line change and indentation, is hard to put all code into list compliance.
The current status of the code written by the questioner is very good. If you want to unpack and insert is_prime, the code will be jagged. Don't do that.
© 2024 OneMinuteCode. All rights reserved.