current position:Home>Python interface automation test framework (basic part) -- loop statement of process control for & while
Python interface automation test framework (basic part) -- loop statement of process control for & while
2022-01-31 06:49:03 【I am the white moonlight you can't get】
「 This is my participation 11 The fourth of the yuegengwen challenge 9 God , Check out the activity details :2021 One last more challenge 」
Preface
Very happy ,python Learned the loop statement , It's getting more and more fun , Learn here , Basically, it can also handle some pipeline work , It's just a little less decorative and not so flexible 、 Low reusability .
Loop statement
python There are... In the loop statement for and while; As shown in the figure above , Loops basically need to do something , Because you don't need all the data , I'll also bring if Conditionally control the output .
while loop
General sentence pattern
# As a condition , by True Just execute the code block inside , by false Then exit the loop
while (condition):
print(" Execute statement ")
Copy code
Hint :python There is no do...while loop
- Seems to be python Most users , Not used while...else Such conditions
while False:
print(" Will not be executed ")
else:
print(" When while Condition is False Execution only ")
Copy code
- Example : seek 1-100 The sum between , Just use while Loop through
dig=100
sum=0
count=1
while count <=dig:
sum+=count
count+=1
print("1 To {} The sum between is :{}".format(dig,sum))
# Output results :1 To 100 The sum between is :5050
Copy code
-
Code analysis :
-
- count As while Cycle counter , It cannot exceed the target value dig; Otherwise exit the loop , and sum+=count, Every time sum+count Then reassign to sum
-
Of course, this is the result of our study , How do you make sure the result is correct ?
-
- Two ways , Do it yourself , What a joke ; Of course, find rules , remove 100 and 50, Add the head and tail to get 100 The number of 49 Yes , Such as :1+99、2+98、3+97, And so on , until 49+51, That is, there will be 49 Add the head and tail together to get 100, Plus 100+50, It's not 5050
-
- The second method is naturally to use programs to verify ,python Provides sum()、range() function , It needs to be criticized , In the above example sum=0, That is, it will cover the original sum() function , So... Cannot be used in the current code block sum() Function to sum .
# sum() The argument to the function must be of iteratable type ,str With the exception of
s=sum(range(1,101))
print(s) # Output :5050
# The built-in functions will be introduced later range function
Copy code
- while True: This loop has a special scenario , That is, the client needs real-time requests , Infinite loops are very useful .
for loop
This is a bit of a special loop , Because it is for...in The combination of ,in What is it ? Our members , Actually for Namely while, It's just condition Instead of x in xxx: nothing more , So it can't be for True But can x in xxx; Think about it :while Can you match x in xxx Well ?
- for Loop traversal list Or sequence types such as strings
a="1234567"
for i in a:
print(i)
# Output :
1
2
3
4
5
6
7
Copy code
- for Cycles often follow range() Functions are mixed together ;range() A function is a conditional function that is left closed and right open
for i in range(5):
print(i)
# Output :
0
2
3
4
Copy code
range() function
- range() The default function is from 0 Start , The interval can be specified :range(1,5)
for i in range(1,5):
print(i)
# Output :
1
2
3
4
Copy code
- Recall what I said before str、tuple、list Formula of type for slice value :(n:m:k), But in range It needs to be changed into a comma :range(n,m,k),m>n,k Step length
# Odd number
for i in range(1,10,2):
print(i)
# Output :
1
3
5
7
9
Copy code
continue and break
Literally , One is continue Continue to cycle , One break Is to interrupt the loop
- For example while True: It's an infinite loop , If accompanied by if Under controlled conditions , When the conditions are met break Out of the loop
# cmd Get into python Interactive command mode
>>> count=1
>>> while True:
... print(count)
... if count==4:
... print(count)
... break
... count+=1
...
1
2
3
4
4
Copy code
- If the above example is replaced by continue, Then infinite loop , It hasn't stopped yet , It ends itself .
- Be careful 1: All from for or while The loop that jumps out will not execute the following else;
- Be careful 2:continue and break If the position of the is in front of a piece of code , Then the code behind it will not be executed .
i=1
while i<10:
if i==3:
continue
# Will not execute the following i+=1,i Forever 3, And then there's an infinite loop
print(" Unable to execute ")
i+=1
Copy code
- So think about It goes on , It's just continue Add conditions before :i+=1
# cmd Input python Go into interactive mode :
>>> i=1
>>> while i<10:
... print(i)
... if i==3:
... i+=1
... continue
... print(" Statements that cannot be executed ")
... print(" When i==3, This sentence does not execute ,{}".format(i))
... i+=1
...
1
When i==3, This sentence does not execute ,1
2
When i==3, This sentence does not execute ,2
3
4
When i==3, This sentence does not execute ,4
5
When i==3, This sentence does not execute ,5
6
When i==3, This sentence does not execute ,6
7
When i==3, This sentence does not execute ,7
8
When i==3, This sentence does not execute ,8
9
When i==3, This sentence does not execute ,9
# Did you miss i==3 The sentence of does not execute !!!
Copy code
- break Keyword is also an example :
# cmd Input python Go into interactive mode
>>> i=1
>>> while i<10:
... print(i)
... if i==3:
... i+=1
... break
... print(" Statements that cannot be executed ")
... i+=1
... print(" Only until i==3")
... else:
... print(" Jump out of the loop above , Will not execute the code block here ")
...
1
Only until i==3
2
Only until i==3
3
Copy code
pass keyword
It's an empty statement , Do nothing , It's space occupying ; Maintain structural integrity .
if i in range(4):
pass
# Doing nothing , But it will traverse
Copy code
Think about it :while Can you match x in xxx Well ?
copyright notice
author[I am the white moonlight you can't get],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310649023677.html
The sidebar is recommended
- My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
- Python interface automation testing framework -- if you want to do well, you must first sharpen its tools
- Python multi thread crawling weather website pictures and saving
- How to convert pandas data to excel file
- Python series tutorials 122
- Python Complete Guide - printing data using pyspark
- Python Complete Guide -- tuple conversion array
- Stroke the list in python (top)
- Analysis of Python requests module
- Comments and variables in Python
guess what you like
-
New statement match, the new version of Python is finally going to introduce switch case?
-
Fanwai 6 Different operations for image details in Python opencv
-
Python crawler native code learning (I)
-
Python quantitative data warehouse building series 2: Python operation database
-
Python code reading (Part 50): taking elements from list intervals
-
Pyechart + pandas made word cloud pictures of 19 report documents
-
[Python crawler] multithreaded daemon & join() blocking
-
Python crawls cat pictures in batches to realize thousand image imaging
-
Van * Python | simple crawling of a planet
-
Input and output of Python practice
Random recommended
- Django ORM details - fields, attributes, operations
- Python web crawler - crawling cloud music review (3)
- Stroke list in python (bottom)
- What cat is the most popular? Python crawls the whole network of cat pictures. Which one is your favorite
- [algorithm learning] LCP 06 Take coins (Java / C / C + + / Python / go / trust)
- Python shows the progress of downloading files from requests
- Solve the problem that Django celery beat prompts that the database is incorrectly configured and does not support multiple databases
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python quantitative data warehouse construction 3: data drop library code encapsulation
- The source code and implementation of Django CSRF Middleware
- Python hashlib module
- The cover of Python 3 web crawler development (Second Edition) has been determined!
- The introduction method of executing Python source code or Python source code file (novice, please enter the old bird and turn left)
- [Python basics] explain Python basic functions in detail, including teaching and learning
- Python web crawler - crawling cloud music review (4)
- The first step of scientific research: create Python virtual environment on Linux server
- Writing nmap scanning tool in Python -- multithreaded version
- leetcode 2057. Smallest Index With Equal Value(python)
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python crawler actual combat, requests module, python realizes capturing a video barrage
- [algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)
- Test platform series (71) Python timed task scheme
- Java AES / ECB / pkcs5padding encryption conversion Python 3
- Loguru: the ultimate Python log solution
- Blurring and anonymizing faces using OpenCV and python
- How fast Python sync and async execute
- Python interface automation test framework (basic) -- common data types list & set ()
- Python crawler actual combat, requests module, python realizes capturing video barrage comments of station B
- Python: several implementation methods of multi process
- Sword finger offer II 054 Sum of all values greater than or equal to nodes | 538 | 1038 (Java / C / C + + / Python / go / trust)
- How IOS developers learn python programming 3-operator 2
- How IOS developers learn python programming 2-operator 1
- [Python applet] 8 lines of code to realize file de duplication
- Python uses the pynvml tool to obtain the working status of GPU
- Data mining: Python actual combat multi factor analysis
- Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency
- Use Python VTK to batch read 2D slices and display 3D models
- Complete image cutting using Python version VTK
- Python interface automation test framework (basic) -- common data types Dict
- Python specific text extraction in actual combat challenges the first step of efficient office