Get started with Python with C or C + + basics: Python Crash Course 4 action list 4.1--4.3

Source: Internet
Author: User
Tags for in range

The list of actions, that is, the traversal list . What we're going to learn in this chapter is how to iterate through lists.

4.1--4.2 Traversal List

  Iterate through the list with a for loop.

Unlike a For loop for C + + or C, Python's for loop is easier to understand.

See an example:

1Fruits = ['Apple','Banana','Orange'];2  forFruitinchFruits:3     Print("I like"+ fruit.title () +". ");4 Print("I really love fruit!");

The syntax structure is for + a variable (defined by itself, passing the argument from the list to the variable, such as the second row of fruit) + list name (fruits), in each loop.

Here are two points to note:

1. After the for loop, the code that does not indent is executed only once, not repeatedly. This can be understood in conjunction with the C for loop struct ' {} ':

That is, Python differs from the relationship between the line of code and the previous line of code based on {}, while Python is judged by c++,c++ .

2. Do not forget to add a colon!!! , a colon is required for the end of the python for Loop code line.

4.3 Creating a list of values

  5 functions:

1. Range ().

2. List ().

3. Min ().

4. Max ().

5. SUM ().

A, Range ()

1. Use the range () function to pay attention to a bad behavior, which is the following code:

 1  for  value in  range (1, 5 2   (value)  3   Print  ( \n    )  6  for  value in  range (1, 6 7  print  (value); 

The intent of the above code is to print 1 to 5 numbers sequentially, but the first code can only print 1 to 4, which is the "one line" behavior that is often seen in programming languages .

The second code, however, achieves the intent.

2. Specify the step size of the range () function:

The code is as follows:

 for  in range (2, 2):    print(value)

Meaning: for (int i = 2; i < i + = 2) cout << i << Endl; -----learned C + + should be very clear.

Second, list () function

The list () function is used to create lists of numbers. It converts the result of range () directly into a list.

Examples are as follows:

1 numbers = List (range (1, 6))2print(numbers)34  Print('\ n')56 even_numbers = List (range (2, 11, 2 ))7print(even_numbers)

The results of the two examples above are: [1, 2, 3, 4, 5] and [2, 4, 6, 8, 10].

The essence is to push the value from range () into the stack .

Three, three functions for performing simple statistical operations on a list

Like the Max in C + +, the min function, but this three is quite powerful with traversing the entire list to find the maximum, minimum, and value.

Sample code:

1 digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]2print(min (digits))3pri NT(max (digits))4print(sum (digits))

Four, list parsing

The explanation in the code parsing book is that the code for the For loop and the creation of the new element is merged into a single line and the new element is automatically appended .

This will make the code more concise.

For example, to make a set of squares, the normal code is as follows:

1 squares = []2 for in range (1, one):3     square = value **24    squares.append (square)5     6     print(squares )

Or:

1 squares = []2 for in range (2, one, 2):3     Squares.append (value**2)4     5     print(squares)

If code is parsed, combine the for loop and the Create step directly:

 for  in range (1, one)]print(squares)

Very concise. However, as a learning C + + person, the first value*2 again for the loop will feel a logical disorder.

To be Continued ...


If there is any mistake, please comment!

Get started with Python with C or C + + basics: Python Crash Course 4 action list 4.1--4.3

Related Article

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.