A brief introduction to the third chapter of Python programming from getting started to practicing

Source: Internet
Author: User

What is a list?

The official description is made up of elements that are listed in a characteristic order. In fact, you can see a lot of strings of the orderly combination of it, the content can be deleted at any time, add, modify. The following is a list that will print out the brackets and quotation marks when the Python prints the list.
>>> name = [' Liubin ', ' Liujian ', ' Liuliu ']>>> print (name) [' Liubin ', ' Liujian ', ' Liuliu ']

An ordered list

As with most programming languages, the index of the first element of a list is 0, not 1. If you want to output the last element can use -1, the penultimate second can use -2, and so on.

>>> language = [' Python ', ' C + + ', ' Java ', ' PHP ', ' Ruby ']>>> print (language[0]) python>>> print (Language[2]) java>>> print (language[-1]) ruby>>> print (Language[-2].lower ()) PHP

modifying, adding, and deleting elements of a list

Modification: The re-assignment of the element is actually

Motorcycles = ['Honda','Yamaha','Suzuki']Print(motorcycles) ['Honda','Yamaha','Suzuki']motorcycles[0]='Ducati'Print(motorcycles) ['Ducati','Yamaha','Suzuki']

Added at the end:append ()

Motorcycles = ['Honda','Yamaha','Suzuki']Print(motorcycles) ['Honda','Yamaha','Suzuki']motorcycles.append ('Ducati')Print(motorcycles) ['Honda','Yamaha','Suzuki','Ducati']

Insert in list:Insert (Index, String)

Motorcycles = ['Honda'Yamaha'Suzuki '  'Ducati')print(motorcycles)

The DEL statement deletes the element and needs to know that the element is indexed in the list, and after it is deleted, it cannot be accessed again.

Motorcycles = [' Honda ', ' Yamaha ', ' Suzuki ']print (motorcycles) [' Honda ', ' Yamaha ', ' Suzuki ']del motorcycles[0]print ( Motorcycles) [' Yamaha ', ' Suzuki ']

Pop () pop-up element, the default is the top-most element, is the last one, if you know the position of the element in the list, you can add an index popup

Motorcycles = ['Honda','Yamaha','Suzuki']Print(motorcycles) ['Honda','Yamaha','Suzuki']popped_motorcycle=Motorcycles.pop ()Print(motorcycles) ['Honda','Yamaha']Print(popped_motorcycle) Suzuki
 motorcycles = [ " honda  " ,  " yamaha   ", "  suzuki   " first_owned  = Motorcycles.pop (0)  print  ( " the first Motorcycle I Owned was a   " + first_owned.title () + "  .   " ) The first motorcycle I owned was a Honda.  

Sometimes you do not know the position of the element, but know the content of the element, you can use the Remove ( ' string ') or remove (the variable) to delete, remove () can only delete the first specified value, the deleted value may appear in the list more than once, you need to use the later learned loop.

Motorcycles = ['Honda','Yamaha','Suzuki','Ducati']Print(motorcycles) ['Honda','Yamaha','Suzuki','Ducati']motorcycles.remove ('Ducati')Print(motorcycles) ['Honda','Yamaha','Suzuki']

Permanent sort

Method sort () to sort the list in a permanent order

>>> car = ['BMW','Audi','Toyota','Subaru']>>>Print(CAR) ['BMW','Audi','Toyota','Subaru']>>>Car.sort ()>>>Print(CAR) ['Audi','BMW','Subaru','Toyota']>>> Car.sort (reverse=True)>>>Print(CAR) ['Toyota','Subaru','BMW','Audi']

Passing parameters to a method reverse=ture can be sorted backwards

Temporary sort

function sorted () to sort the list in a temporary order

>>> car = ['BMW','Audi','Toyota','Subaru']>>>Print(sorted (car)) ['Audi','BMW','Subaru','Toyota']>>>Print(CAR) ['BMW','Audi','Toyota','Subaru']

You can also pass parameters to the function sorted () If you want to display the list in the reverse order of alphabetical order Reverse=ture

Flip List

Method reverse () anyway list

>>> car = [' BMW ', ' Audi ', ' Toyota ', ' Subaru ']>>> car.reverse () >>> print (car) [' Subaru ', ' Toyota ', ' Audi ', ' BMW ']

To find the length of a list

The function len () can know the length of the list, and Python calculates the number of elements, starting with 1.

>>> car = ['bmw','Audi','Toyota ','Subaru']>>> len (car)4

A brief introduction to the third chapter of Python programming from getting started to practicing

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.