List of learning notes [Python]

Source: Internet
Author: User

# coding=utf-8# An initial list fruits = ["apple", "Banama", "Peach"]print fruitsprint fruits[0] #列表长度print len (fruits) # Append a data item fruits.append ("Watermelon") print fruits# tail Delete a data item fruits.pop () Print fruits# trailing append a data item collection fruits.extend ([' Pear ', ' Grape ']) print fruits# Locate and delete a specific data item in the list Fruits.remove ("Apple") print fruits# Add a data item in front of a specific location Fruits.insert (0, "Apple") Fruits.insert (3, "Apple") print fruits# two mixed data #python list can contain mixed types of data, both strings and numbers in the same list and other types of data are ok fruits = [" Apple "," Banama "," Pear ", 21]print fruits# three iterations fruits = [" apple "," Banama "," Pear "]for fruit in Fruits:print" fruit: "+fru it# Four list of storage lists fruits = ["Watermelon", ["Apple", 10],["Banama", 21],["Pear", []] #每次处理列表的一项时你都要查看一下这一项是不是列表 # It allows you to check whether a particular identifier contains a specific data type print isinstance (fruits,list) for fruit in Fruits:if isinstance (fruit,list): for item in F Ruit:print Item Else:print fruit# Five handles a list of multiple nested list # "lists in the list ... List, with a for loop, your code becomes overly complex (for loops in the For loop). We should use functions instead of repeating code. def listnest (lists): For item in lists: #如果item是个列表 if IsinstaNCE (item,list): #递归调用 listnest (item) else:print Itemprint "five-processing multilayer nested list" listnest (fruits) 





List of learning notes [Python]

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.