Isn't it easy to learn Python circles with old Qi ?, Qi xue python circle

Source: Internet
Author: User

Isn't it easy to learn Python circles with old Qi ?, Qi xue python circle

In python, a loop has a statement: for statement.

Simple for loop example

>>> hello = "world">>> for i in hello:...   print i... world

How does the above for loop work?

Hello, this variable references data of the str type "world ".
Variable I uses hello to locate the referenced "world", and then obtains the reference from the first character.
When I = "w", execute print I, print out the letter w, and then loop for the second time, let I = "e", then execute print I, print the letter e. The loop continues until the last character is printed, and the loop ends automatically.
By the way, I would like to add a print technique. The print result above is arranged vertically, that is, the line feed is automatically generated after each print. If you want to print a row, you can use the following method to add a comma after the print)

>>> For I in hello :... print I ,... w o r l d >>> for I in hello :... print I + ",", # to be beautiful, you can add a comma after each character to separate... w, o, r, l, d, >>>

Because you can use the index number (offset) as the following table to obtain a character. Therefore, you can implement the same functions in the above Code through the following loop:

>>> for i in range(len(hello)):...   print hello[i]... world

The working method is as follows:

Len (hello) gets the length of the string referenced by hello, which is 5
Range (len (hello), that is, range (5), that is, [0, 1, 2, 3, 4], corresponds to the number of each letter of "world", that is, the offset.
For I in range (len (hello) is equivalent to for I in [, 4], so that I is equal to the values in the list in turn. When I = 0, print hello [0], that is, the first character. Then it repeats sequentially until the last I = 4.
In the preceding loop example, the str character is obtained in sequence and the list is involved. Well, let's take a look at the list loop:

>>> ls_line['Hello', 'I am qiwsir', 'Welcome you', '']>>> for word in ls_line:...   print word... HelloI am qiwsirWelcome you>>> for i in range(len(ls_line)):...   print ls_line[i]... HelloI am qiwsirWelcome you

Last step

We have understood the basic workflow of the for statement. If you write a general formula, it can be expressed as follows:

For target in object:
Operation statement
Use the for statement to solve a problem.

For example, a positive integer smaller than 100 that can be divisible by 3 is found.

Analysis: There are two restrictions on this problem. The first is a positive integer within 100. According to the previous study, range (1,100) can be used for implementation. The second is to solve the problem of being divisible by 3, suppose a positive integer n. If this number can be divisible by 3, that is, n % 3 (% is the remainder) is 0. so how to get n is to use the for loop.

The above is a simple analysis, and the process needs to be further detailed. Follow the previous method to draw a flowchart for solving the problem.

Write the following code as shown in the following figure.

Code:

#! /usr/bin/env python#coding:utf-8aliquot = []for n in range(1,100):  if n%3 == 0:    aliquot.append(n)print aliquot

Code running result:

[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
Here is just a simple example. You can go deep into this example to print even numbers/odd numbers in a certain range.




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.