Catalog
1) brief introduction
2) Program implementation code -1- for loop
3) Program implementation code [2] List expression
List expression :
----------------------- Text ---------------
Prime number , Immediate Division 1 A number that has no other factor than itself , Also known as prime number .
Sum , Immediate Division 1 A number that has other factors besides itself .
--- Be careful :1 Neither prime nor composite !
Package a function :
def primenum(start,end):
start As the starting point ,end For the end
Function main body
def primenum(start,end):
prime=[]
for i in range(start,end+1):
for j in range(2,i):# It doesn't contain 1, It itself
if i%j==0:
break
else:# Be careful else Add to for Back
prime.append(i)
return prime
The output effect is OK , But there are 0 and 1...
Add filtering effect
def primenum(start,end):
prime=[]
if start<2:start=2
for i in range(start,end+1):
for j in range(2,i):# It doesn't contain 1, It itself
if i%j==0:
break
else:# Be careful else Add to for Back
prime.append(i)
return prime
print(primenum(0,100))
result :
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
It's easy to add up numbers , Divide all by 0,1 It's good to count other numbers
def hs(start,end):
num=[]
if start<2:start=2
for i in range(start,end+1):
for j in range(2,i):
if i%j==0:
num.append(i)
break
return num
------------------------------------------ Beginner split line ---------------------------------------------------------------
Here comes the high-end game !||ヽ(* ̄▽ ̄*)ノミ|Ю The debut
The more important , Cross preposition .
a=[i for i in range(1,10)]
print(a)
>>>[1,2,3,4,5,6,7,8,9]
[ Use for Loop every time i The value of is nested into ]
a=[i for i in range(1,10) if i%2==0]
print(a)
>>>[2,4,6,8]
l - level - 了 !
if On the for Back , Every time i It will be more than if Review before copying , Otherwise it doesn't count
a=[i**2 for i in range(1,5)]
print(a)
>>>[1,4,9,16]
take i Did more
a=[j for j in range(i) for i in range(10)]
print(a)
Report errors , The first loop precedes
a=[j for i in range(10) for j in range(i)]
print(a)
>>>[0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8]
Nested loop
The method of finding composite numbers :
list
a=[]
Add the expression number one ( We use 3~100)
a=[i for i in range(3,100+1)]
Nesting , Factor
a=[j for i in range(3,100+1) for j in range(2,i)]
Judge whether to divide
a=[j for i in range(3,100+1) for j in range(2,i) if i%j==0]
Be careful :i Is the real value !
a=[i for i in range(3,100+1) for j in range(2,i) if i%j==0]
Next, filter out duplicate values :
a=list(set([i for i in range(3,100+1) for j in range(2,i) if i%j==0]))
Okay OK
Next comes prime numbers —— But judgment is much simpler , I go straight to the code
value = [i for i in range(1 if 1 >= 2 else 2, 100 + 1) if i not in a]
Some people want to find prime numbers in a row instead of composite numbers in a row , Find prime numbers in a row :
one=[i for i in range(1 if 1 >= 2 else 2, 100 + 1) if i not in [i for i in range(1,100+1) for j in range(2, i) if i % j == 0]]
---------------- End -----------------