Python programming-from getting started to practicing note 3_ action list

Source: Internet
Author: User

Traverse the entire list for loop

Example: lists=[' A ', ' B ', ' C ', ' D ']

For list in lists:

Print (list)

Result: A

B

C

D

    • list is a temporary variable, the for loop takes the elements in the list lists in turn, and executes the code below the indented section
    • There must be a colon after the for loop
    • Each time you execute the code in for, the line is wrapped, so the ABCD in the result is displayed on the branch
    • The indent code for the for is just one part of the loop, with no indentation. Python determines the relationship between the code and the previous line of code based on indentation
    • Error indentation is called ' indentation error ' or ' logic error '; missing colon is called ' Single character error '
Create a Value list range () function
  • Range (1,6) creates numeric values; The result of a line behavior, starting with the first value you specify, and stopping after the second value that you specify, does not contain a second value

  • Using the function list (), you can convert range () to a list, such as Number=list (range (1,6))
  • Specify step Size Example: Rang (1,10,2)---> Results: 1,3,5,7,9
  • Perform a simple statistical calculation on a list of numbers
    • Digits=list (Rang (1,10))
    • min(digits)---> results: 0 Find the minimum value of a list
    • Max (digits)---> Results: 9 Find the maximum value of the list
    • sum (digits)---> result list sum
  • List parsing
    • squares=[value**2forvalue in rang (1,4)]
    • Value is a temporary variable and is then fetched from rang (1,4)
    • Value**2 is a temporary variable, which is the value to be stored in the list squares
    • So the last squares=[1,4,9]
Use part of the list--slice
    • As with Range (), the element that precedes the specified second index stops without a second index. Note that the difference here and rang () is the index, not the value
    • digits[0,3] The first three elements of a list digits
    • DIGITS[:3] Fetch list digits from first to 3rd element
    • Digits[3:] Take list digits from 4th to terminating element
    • Digits[:] Take all the elements in the list digits
    • Digits[-3:] Using the minus sign index, take the last three elements of the list
    • Slices can also traverse
      • For DIGIT_QP in digits[0,3]:
      • Print (DIGIT_QP)
    • The copy list can use slices, list_2=list_1[:], so that list_1 and list_2 are two separate lists
    • If list_2=list_1, so that list_1 and list_2 are the same list, the pointer points to the same list
Meta-group
    • Python refers to a value that cannot be modified, called immutable:, and immutable lists are called tuples
    • Tuples and lists differ: tuples use parentheses instead of square brackets yuanzs= (200,50)
    • Modifying elements in a tuple in Python will cause an error
    • But you can assign values to the variables of the storage tuple, changing the value of the entire tuple yuanzs= (400,60)
    • The method of traversing tuples is the same as traversing a list for Yuanz in Yuanz
Formatting code
    • Code Format Setup Guide: PEP 8
    • Indent: It is recommended to indent four spaces per level, and the general text Editor can set a tab length of four spaces
    • President: It is recommended that no more than 80 characters per line of code, preferably not more than 72 characters
    • Blank line: Do not use empty lines by the way
    • PEP 8 Access https://python.org/dev/peps/pep-0008/Learn more about the Setup Guide for code formats

Python programming-from getting started to practicing note 3_ action list

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.