Welcome to old Xin's column on the Internet 《Python From zero to one 》, Here I'm going to share an appointment 300 piece Python Series articles , All articles will be combined with cases 、 Code and author's experience , I really want to share my nearly ten years programming experience with you , I hope it will be of some help to you , There are also some shortcomings in the article .

Since I was engaged in teaching , I feel more and more precious of time , Every class of students has to give repeated lessons , Starting this year , I'm going to do it step by step .

Your attention 、 Praise and forward is the biggest support for the Internet , Knowledge is priceless, man has love , I hope we can all be happy on the road of life 、 Grow up together .


List of articles

    • 1. Built in functions range()
    • 2. while loop
    • 3. for - in loop
    • 4. Flow control statement break
    • 5. Flow control statement continue
    • 6. else sentence
    • 7. Nested loop
    • a) Case study 1 Print subtitles
    • b) Case study 2 seek 1-100 Even sum of
    • a) Password verification case
    • a) Case study 1
    • a) Method 1
    • b) Method 2
    • Case study 1: Password cases
    • Case study 1
    • Case study 2
    • Case study 3


1. Built in functions range()

Used to generate a sequence of integers

establish range Three ways of objects :

  • range(5) Create a 0 To 5, In steps of 1 Integer sequence of . And contains 0, It doesn't contain 5;
  • range(5,10) Create a 5 To 10 Integer sequence of , And in steps of 1
  • range(5,10,2) Create a 5 To 10 Integer sequence of , And in steps of 2

The return value is an iterator object
range The advantages of : No matter range How long is the sequence of integers represented by object , be-all range Objects take up the same amount of memory . Because you just need to store start,stop, and step.

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14# The first way to create it r=range(10)print(r) #range(0,10)print(list(r)) # see range Object [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]# The second way to create r2=range(1,10)print(list(r2)) #[1, 2, 3, 4, 5, 6, 7, 8, 9]# The third way to create r3=range(1,10,2)print(list(r3)) #[1, 3, 5, 7, 9]


In addition, we can use in and not in Determine whether an integer is in the sequence

r3=range(1,10,2)print(list(r3)) #[1, 3, 5, 7, 9]print(10 in r3) #False


You can see False Express 10 Not in this sequence

2. while loop

Loop structure : Do the same thing over and over again , It's called cyclic structure
Cycle classification :

  • while
  • for -in

Grammatical structure :
while Conditional expression
Conditional executors ( The loop body )
 Insert picture description here

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14a=1# The expression of judgment condition while a<10:print(a)a+=1


Sum up :

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Calculation 0-100 And
'''num1=0sum=0while num1<=100:sum+=num1
    num1+=1print(' And for ',sum)


The result is :
 Insert picture description here

Increase the difficulty : Calculation 0-100 Even sum of

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Calculation 0-100 And
'''num1=1sum=0while num1<=100:if num1%2==0:sum+=num1
    num1+=1print(' Even sum is ',sum)


 Insert picture description here

3. for - in loop

 Insert picture description here
for - in loop

  • in The expression takes values successively from a string or sequence , It's also called traversal
  • for-in The traversal object must be an iteratable object

a) Case study 1 Print subtitles

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14for item in 'python':print(item)


Take the letters from the string in turn and copy them to item, And print
 Insert picture description here
range() The generated sequence of integers is also iterative .

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14for i in range(1,20,2):print(i)


b) Case study 2 seek 1-100 Even sum of

Case study : Use for Cycle calculation 1-100 Even sum of

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Use for Cycle calculation 1-100 Even sum of
'''sum=0for i in range(1,101):if i%2:sum+=iprint(" Even sum is ", sum)


4. Flow control statement break

Used to end the loop structure , Usually with branch structure if perhaps while Use it together

 Insert picture description here

a) Password verification case

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Enter the password from the keyboard , At most 3 Time , If it's right, the loop ends
'''for item in range(3):pwd=input(' Please input a password :')if pwd=='888888':print(" The password is correct ")breakelse:print(" Incorrect password ")


 Insert a code chip here 


 Insert picture description here
Use while Cycle solution

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Enter the password from the keyboard , At most 3 Time , If it's right, the loop ends
'''a=0while a<3:pwd=input(' Please input a password ')if pwd=='0000':print(' The password is correct ')breakelse:print(" Incorrect password ")a+=1


5. Flow control statement continue

Used to end the current loop , Enter the next cycle , Usually with branch structure if Use it together .

It is associated with break Is the difference between the ,break Exit the loop directly ,

a) Case study 1

Request output 1-50 Between all 5 Multiple

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Output 5 Multiple
5 The common ground of multiple of :  and 5 The remainder is 0 The number of
'''# Method 1for i in range(1,51):if i%5==0:print(i)# Method 2 Use continuefor item in range(1,51):if item%5!=0:continueprint(item)


6. else sentence

else Except for and if collocation , Can also be combined with while,for collocation ;

 Insert picture description here

Case study 1: Password cases

a) Method 1
#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14for item in range(3):pwd=input(" Please input a password ")if pwd=='8888':print(" The password is correct ")breakelse:print(' Incorrect password ')else:print(" Sorry, you have entered the wrong password three times , It's locked in ")


 Insert picture description here

b) Method 2
#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14a=0while a<3:pwd=input(" Please input a password ")if pwd=='8888':print(" The password is correct ")breakelse:print(' Incorrect password ')a+=1else:print(" Sorry, you have entered the wrong password three times , It's locked in ")


 Insert picture description here

7. Nested loop

A loop body is nested in a complete loop body

It is divided into external circulation and internal circulation .

Case study 1

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 9:14'''
Output a rectangle with three rows and four columns
'''for i in range(1,4):   # Row table , perform 3 Time for j in range(1,5): # list , Four times print('*',end='\t')print()# It's all right 


 Insert picture description here

Case study 2

Print right triangles as follows :
 Insert picture description here

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38for i in range(1,10): # Row number for j in range(1,i+1):print('*',end='')print()


Case study 3

Nine nine multiplication formula
 Insert picture description here

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38for i in range(1,10): # Row number for j in range(1,i+1):
        print(i,'*',j, '=', i*j,end='\t')print()


summary :
Today is Qingming , I went to Chaoyang Park at noon , Take your little son out for the wind . Purple leaves and pink flowers bloom in the park , A thousand steps of Forsythia leaves no dust .
This article because go out to play in the middle , Updated twice , Every case has been tested on a computer , No problem , If you have better suggestions , Welcome to exchange and study together .