程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python algorithm practice week3- loop programming

編輯:Python

0x00 Problem introduction

  • problem : Input 30 Number , Output the maximum number
  • analysis
    • The first number may be the largest number , Remember it ;
    • For each subsequent number , Compare with the numbers you remember , If it's bigger than the number you remember , Then remember this number ;
    • After reading all the figures , Output the maximum number ;
  • loop
    • The phenomenon of doing something repeatedly in a program is called loop
      • For example, o 1~100 And , Find the average score of a class in a subject
  • Loop structure ( Cycle condition and cycle body )

0x01 while loop

  • problem : seek 1+2+3+4+……+100 Value
    • flow chart
  • Python Language implementation
  1. = 0
  2. = 1
  3. i < 101: sum += i i += 1 print(sum)
  • problem : seek 1~n And ,n For any positive integer
  1. = int(input(' Please enter a positive integer n:'))
  2. = 0
  3. = 1
  4. i < n+1: sum += i i += 1 print(sum)

0x02 for loop

  • Traversal cycle
    • For a data set
    • Loop through each element in the collection in turn
    • Is a certain number of cycles
  • for Circular statement syntax
  • range() function
range(start,end,step)
range() Function to create a sequence , Including the lower limit , Excluding upper limit
for example
range(101), Range 0~100
range(1,101), visit 1~100
  • problem : seek 1~100 The sum of natural numbers between
sum = 0
for i in range(1,101):
sum += i
print(sum)
  • problem : seek 1~n The sum of natural numbers between ,n For any positive integer
sum = 0
for i in range(1,101):
sum += i
print(sum)

0x03 A nested loop

  • Nesting of loops
    • It means to nest another complete loop in one loop , That is, the loop body also contains loop statements
    • while Circulation and for Loops can be nested with each other
    • Loop nested execution process An external circulation corresponds to a complete internal circulation
  • problem : Print 99 Multiplication table
# Print 99 Multiplication table
for i in range(1, 10): # The control line
for j in range(1, i + 1): # Control the column
print('{}*{}={}'.format(j, i, i * j), sep='', end='\t')
print()

0x04 break and continue

  • break To end the cycle
    • The program continues execution from the code after the loop
  • continue Used to end the current cycle
    • No longer execute the following undefined statements in the loop body
    • But don't end the current loop
  • problem : seek 200 Endogenic quilt 17 The largest positive integer divided by
    • analysis : This lookup process will iterate through... In a decreasing form 200~1 Integer between , When you find the first one that can be 17 The number of integral divisions , The cycle stops immediately
# seek 200 Endogenic quilt 17 The largest positive integer divided by
for i in range(200, 1, -1):
if i % 17 == 0:
break
print('200 Endogenic quilt 17 The largest positive integer to divide by is {}'.format(i))
  • problem : seek 1~100 The sum of all even numbers within
# seek 1~100 The sum of all even numbers within
sum = 0
for i in range(1, 101):
if i % 2 == 0:
sum += i
else:
continue
print(sum)
  • problem : Output 200 All prime numbers within , And output the number of primes
    • analysis : Prime numbers are except 1 And a number that itself cannot be divided by other numbers , This question adopts double for Cycle to achieve , Outer traversal 2~200 All integers between , The inner loop is used to determine whether a number is a prime number
# Method 1
num = 0
for i in range(2, 200):
k = True
for j in range(2, i):
if(i % j == 0):
k = False
break
if(k==True):
print(i)
num += 1
print('200 Prime numbers within {} individual '.format(num))
# Method 2
import math
num = 0
for i in range(2, 200):
m = int(math.sqrt(i))
k = True
for j in range(2, m + 1):
if(i % j == 0):
k = False
break
if(k):
print(i)
num += 1
print('200 Prime numbers within {} individual '.format(num))
  • continue Statement and break The difference between sentences
    • continue Just end this cycle , Without terminating the execution of the entire loop
    • break Statement is to end the whole loop process , No longer judge whether the conditions of the cycle are true

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved