Product Manager learns Python: for Loop, while loop, product manager python

Source: Internet
Author: User

Product Manager learns Python: for Loop, while loop, product manager python

Python has two types of loops: for Loop and while loop.

1. for Loop

A for loop can be used to traverse an object (traversal: In general, the first element in the loop is accessed to the last element in sequence ). The basic structure of the for Loop is as follows:

Take a look at this case:

Design a function, create 10 texts on the desktop, and name them one by one using numbers.

1 def text_create (): 2 path = '/Users/duwangdan/Desktop/'3 for text_name in range (1, 11 ): 4 # range functions 5 with open (path + str (text_name) + '.txt ', 'w') as text: 6 #... 7 text is described in detail in the usage text of. write (str (text_name) 8 text. close () 9 print ('done') 10 11 text_create ()

Now let's understand each line of code.

Row 1st: defines a text_create function;

Row 3: Assign the variable path to the desktop path;

Row 3rd: Load each number in the range of 1-10 into the text_name variable, and name a file each time;

Row 3: Open the txt file on the desktop and write data to each text;

Row 3: name each file in sequence;

Row 3: close the file;

Row 9th: After a naming operation is executed, a Done is displayed;

Row 3: Call a function.

The case mentions "with... ",In Python, the "with... as" syntax is used to replace the traditional "try... finally.

For example, open the test file on the desktop, try to read the file content, and close the file.

1 file = open('/Users/duwangdan/Desktop/test.txt')2 try:3     data = file.read()4 finally:5     file.close()

Although this code is executed well, it is relatively lengthy. After it is expressed with "with... as", the code will be more concise.

1 with open('/Users/duwangdan/Desktop/test.txt') as file:2     data = file.read()

In addition to the preceding single-layer loop, there is also a common loop, which isNested loop.

For example, the nested loop is used to implement the 9-9 multiplication.

1 for i in range(1,10):2     for j in range(1,10):3         print('{} X {} = {}'.format(i,j,i*j))

The outermost loop Stores numbers 1-9 in sequence in variable I. Each time variable I gets a value, the inner loop Stores numbers 1-9 in variable j in sequence; finally, the current I, j, And I * j values are printed.

The format function is added from Python2.6 to format the string, which can be implemented through {}. format. In the above case, the values of I, j, And I * j are respectively stored in the previous {}, and then formatted and unified.

2. while Loop

A for Loop and A while LOOP both have the same point that they can be used to loop and do a duplicate thing. The difference is that a for loop stops when the sequence is exhausted, while a while loop stops when the condition is not met.

For details, see the following example:

1 count = 02 while True: 3 print ('Repeat once ') 4 count = count + 15 if count = break

The variable count is assigned 0, while True indicates that the condition is True and "repeat" is displayed. At this time, count is assigned count + 1 again. If count is 3, the loop exists.The break in the last sentence jumps out of the previous loop. If it is not exceeded, the program will go through endless loops, resulting in an endless loop.

If you do not want to use break to jump out of the loop, you can also change the condition for setting the loop.

The preceding example can be converted to the following:

1 count = 02 while (count <3): 3 print ('Repeat once ') 4 count = count + 1

At this point, the logic determines that all the loop content has ended, and a small exercise will be performed later. It is a small game that almost everyone has played: Guess the size.

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

----- End -----

Author: du wangdan, Public Account: du wangdan, Internet product manager.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.