Python Learning-3 days-lists and tuples

Source: Internet
Author: User

The list is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on. (Python is called a list, while other language classes are called "arrays")

Python has 6 built-in types of sequences, but the most common are lists and tuples.

Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.

A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.

Data items for a list do not need to have the same type

Create a list by enclosing separate data items separated by commas in square brackets.

#!/usr/bin/env python#  Create an array list a_list=[' Wang ', ' Li ', ' Zhang ', ' Zhao ', ' 1 ', ' 2 ', ' 3 ', ' Cheng ', ' Linux ', ' MySQL ', ' 9 ']#  Print List position is 1 value print  (a_list[1]) #  print position 0-6 position index value (general Gu Tou disregard tail, result is 0-5 position index value output) print  (a_list[:6 ]) #  within several values that have been printed print  (A_list[:6][2:5]) #在列表值   ' Li '   after inserting a ' chun '   content A_list.insert (2 , ' Chun ') print  (a_list) #在列表后追加一个 ' He '   content a_list.append (' he ') print (a_list) #删除 ' Cheng '   content A_ List.remove (' Cheng ') print (a_list) #删除多个使用列表值得位置索引来删除del  a_list[8:10]print  (a_list) #每隔一个取一个值 ":: 2"   is the print value print  (A_list[::2]) #查询某个值是否在列表内print   ("Zhao"  in a_list) from the beginning to the end, separated by 2 characters Number of occurrences of the statistic element in the list print  (A_list.count ("Zhao")) #sort   Sort the values in the table, the numbers in front, the letters in the back list1 = [' 1 ', ' 6 ', ' 4 ', ' 3 ', ' 2 ', ' 5 ', ' Wang ', ' Zhao ', ' an ']list1.sort () print  (List1) #获取该列表里值得个数print   (len (list1)) #列表的拼截   use ' + ' #: list2  and list3  are synthesized into a list4  list. list2=[' 8 ', ' 7 ', ' 6 ']list3=[' 9 ', ' Ten ', ' One ']list4=list2+list3print  (list4) #重复列表值使用  *  multiplication sign:Repeat the value of list2  2 times. print  (list2*2) #in   Determine if the value exists in the list name=[' Wang ', ' Qian ', ' Sun ']if  ' Wang '  in name:     print (' wang  exists in name  ') #元组  --  use tuple to convert the list to Narimoto group form. Name1=tuple (name) print  (name1) #可以使用list   convert tuples to lists. yuan= (' Zhou ', ' Qing ', ' Zhen ') yuan1=list (yuan) print  (yuan1) #元组和列表的区别, list with "" brackets, tuples with () parentheses, and tuples cannot be modified, added, deleted inside of the worthwhile. Other interception, merging is basically the same as the list.

The difference between a tuple and a list:

A Python tuple is similar to a list, except that the elements of a tuple cannot be modified.

Tuples use parentheses, and the list uses square brackets.

Tuple creation is simple, just add elements in parentheses and separate them with commas.


This article is from the "Carefree" blog, please be sure to keep this source http://birdcai.blog.51cto.com/11216068/1829525

Python Learning-3 days-lists and tuples

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.