Python learning list of Guibs

Source: Internet
Author: User
Tags python list macbook
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Python learning list of Guibs

# List # A list consists of a series of elements arranged in a specific order. the elements and elements can have no relationship. # In Python, square brackets [] are used to represent the list and comma is used, to separate the elements, ages = ['swift ', "python", "objective-C"] print (AGES) # access list elements # The list is an ordered set, therefore, to access any element in the list, you only need to tell Python the location or index of the element. # In Python, the index of the first element is 0 print (ages [0]). print (ages [1]) print (ages [2]) # When the list is not empty: # set the index to-1, you can return the last element print (ages [-1]) in the Python list # set the index to-2. you can return the second-to-last element print (ages [-2) in the Python list. ]) # Set the index to-3. you can return print (ages [-3]), the last and third element in the Python list. # use the element wanna_use = "I 'd like to use" + ages [1] in the list. title () + ". "print (wanna_use) # add, modify, and delete elements # modify the list element motorcycles = ['Honda', 'yamaha', 'suzuki'] print (motorcycles) motorcycles [0] = 'ducata' print (motorcycles) # Add list elements # add at the end of the list #[. append ()] motorcycles. append ('yamaha') print (motorcycles) # insert an element in the list #[. insert ()] motorcycles. insert (1, "hl") print (mot Orcycles) # delete a list element # [del] print (motorcycles) del motorcycles [1] print (motorcycles) # delete a specified element and return the deleted value #[. pop ()] poped_motor = motorcycles. pop (0) print (poped_motor) # Delete an element by value #[. remove ()] # only print (motorcycles) motorcycles, the first element that matches the value, will be deleted. remove ('yamaha') print (motorcycles) # organization list # sort the list permanently #[. sort ()] cars = ['BMW ', 'Audi', 'Toyota ', 'mini'] print (cars) cars. sort () print (cars) # flashback cars. sort (reverse = True) print (cars )# Use the sorted () list for temporary sorting. cars2 = ['BMW ', 'Audi', 'Toyota ', 'mini'] print (cars2) print (sorted (cars2 )) print (cars2) # reverse print (sorted (cars2, reverse = True) # permanently reverse the list order #[. reverse ()] foods = ['apple', 'pear ', 'banana'] print (foods) foods. reverse () print (foods) # determine the length of the list # [len ()] print (len (foods) # When using the list, avoid index errors # operation List # traverse the entire list magicians = ['Alice ', 'David', 'privacy lina'] for magician in magicians: print (magician. title () +", That was a great trick! ") Print (" I can't wait to see your next trick \ n ") print (" Thank you everyone ") # Create a value list # generate a series of numbers # [range ()] for value in range (1, 5): print (value) #1, 2, 3, 4 # specify the step for value in range (1, 5, 2): print (value) #1 3 # use the function list () to convert range () the result is converted to the list numbers = list (range (1, 6) print (numbers) # number list processing numbers = [1, 3, 2, 7, 4, 5, 6] print (max (numbers) # maximum value print (min (numbers) # minimum value print (sum (numbers )) # summation # List parsing squares = [value ** 2 for value in range (1, 10)] print (squares) # use part of the list # Slice [to create a slice, you can specify the index of the first and last elements to be used + 1] computers = ['MacBook ', 'MacBook Pro', 'imac ', 'Mac Pro'] print (computers []) # obtain the print (computers [1:]) element of 1 <= index <2) # obtain 1 <= index element print (computers [: 2]) # obtain index <2 element print (computers [:-1]) # obtain the print (computers [-3:]) element before the last element # obtain the last three elements # traverse the slice for computer in computers []: print (computer. upper () # copy the list # Create a shard wanna_food = ['apple', 'pear ', 'banana'] print (wanna_food) that omits the index restriction) buy_food = wanna_food [:] print (buy_food) # if slice is not used. directly assigning the old list to the new array is equivalent to directly referencing the same list, rather than copying copies.

The above is the Python learning _ list of Guibs. For more information, see PHP Chinese network (www.php1.cn )!

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.