Python _ loop (iteration) and python Loop

Source: Internet
Author: User

Python _ loop (iteration) and python Loop

# For and while

# For is used for iterative processing. What is iteration? When you don't see this word, for is to process a bunch of things one by one, for example, to eat a bag of peanuts, You have to peel them one by one.

A = 'abcde' # Each letter is treated as a peanut, and a is a bag.

For I in a: # Get the peanuts one by one from the bag, and every Peanut is named Xiao I Jun.

I = I + 'man '# Add a man to every small I-jun and stop! Add a description. in python, it is not just a value. Many things can be addition, subtraction, multiplication, division, for example, 'A' + 'B'. The result is 'AB', provided that the elements used for calculation are of the same type.

Print I # Let's see what it is. The result is aman \ nbman \ ncman \ ndman \ neman \ n. By default, a line break is generated every time.

Print I, # If you don't want to, add a comma to the end. The result is amanbmancmandmaneman.

# While is used to make some judgments. for example, the one in my c note is usually more complicated than for and the code is longer.

A = 1

While a <10: # continue as long as the judgment is true

Print a # The result is, the first time 1, and then continue the following

A + = 1 # This syntax is the same as a = a + 1. The first syntax is 2.

# Here, every time a is added with 1, after a + 1 = 10, it stops because a is no less than 10, the result is 1 \ n2 \ n3 \ n4 \ n5 \ n6 \ n7 \ n8 \ records \ n.

 

# A noun, Nested loop. I don't know what a Nested loop is. If I am not familiar with Nested loop, it means that you have not performed any database. The following shows the Nested loop.

For x in range (10): # range this is to generate a list, [,], which is described in the function.

For y in range (10 ):

'''' Is actually a loop applied in the loop. It is usually used to find the intersection, union, or something in two sets. It also uses peanuts to raise chestnuts (Chestnuts: are you talking about peanuts?) two bags, one bag with peanuts, one bag with labels, and each peanut with the label of another bag, the practice is to take a peanut from the first bag, put a label on the peanut from the second bag, and then take a peanut. This time the chestnut is 99 multiplication '''

Z = x * y

Print str (x) + '*' + str (y) + '=' + str (z) # The function section has not been written yet. Let's explain it first, str converts any type to string. Of course, if some types cannot be converted, don't rely on me, but most of them can be converted.

Print '% d * % d = % d' % (x, y, z) # You can write this statement as well, % d can be viewed in special symbols

# This is a simple Chestnut. Of course you can also look at it. You need to find a solution and think about it to make progress.

A = 1

Num = 0

While a <= 10: # while, write an accumulated python version.

N = num

Num + =

Print '% d + % d = % d' % (n, a, num)

A + = 1

# The loop is very simple, but it is very important. In actual use, it is certainly not just me. As long as the brain hole is large enough, there is nothing wrong with the loop.

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.