Author: Jiang Xia

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

| CSDN:blog.csdn.net/qq_4115394…

| the nuggets: juejin. Cn/user / 651387…

| public no. : 1024 notes

This article contains 1169 words and takes 8 minutes to read

Built-in data structures in Python include list, tuple, dict, and set. These four data structures and basic data Types (integers, floating point numbers, etc.) are collectively referred to as built-in Types. Lists and tuples are ordered lists and are called sequences. A Tuple is a set of data enclosed in parentheses and separated by commas. The elements in a Tuple can be of any type, but the difference between a Tuple and a list is that a list is mutable, whereas a Tuple is immutable. That is, once a Tuple has defined its elements, it cannot append(), inset(), pop(), and assign them as a list can. You can only get elements.

This summarizes the use of lists in Python, and the comments and analysis are all in the code.

# define a tuple... >>> person=(1,2,3) >>> >>> print(person) (1, 2, 3) >>> >>> print(len(person)) 3 >>> # Iterate over each element in the output tuple... > > > person = (" liu ", "guan yu", "zhaoyun", "zhang fei") > > > > > > for the item in person:... # loop operates on tuple elements inside... Print (item) liu bei guan yu zhaoyun Zhang fei > > > # define a variety of data types of the tuple > > > person = (1, "zhang", [" liu ", "guan yu", "zhaoyun", "zhang fei"]] > > > > > > print (person) (1, 'zhang SAN, >>> # Access the element in the tuple... Access the elements of the specified table... > > > person = (1, "zhang", "liu", "guan yu", "zhaoyun", "zhang fei") > > > > > > print (person [2]) liu bei > > > # section of access, elements within a specified range... > > > person = (1, "zhang", "liu", "guan yu", "zhaoyun", "zhang fei") > > > > > > print (person [but]), 'liu bei, guan yu,' zhaoyun ') > > > # mentioned a tuple cannot be directly modified, But if there is a list element in the progenitor, you can add elements to the list, delete elements, insert and so on... > > > person = (1, "zhang", "liu", "guan yu", "zhaoyun", "zhang fei"]] > > > person [1]. The append (" cao ") > > > > > > print (person) (1, [' zhang ', 'liu bei, guan yu,' zhaoyun, >>> # Can also be used to create a new yuan ancestor, in fact, equivalent to creating a new yuan ancestor... > > > pers1 = (" zhang ", "liu") > > > pers2 = (" guan yu ", "zhaoyun", "zhang fei") > > > > > > print (pers1 + pers2) (' zhang ', 'liu bei, "guan yu",' zhaoyun, >>> # Primitives and lists can be converted... # convert tuples to lists... > > > tuple1 = (" guan yu ", "zhaoyun", "zhang fei") > > > list1 = list (tuple1) > > > > > > print (list1) [' guan yu ', 'zhaoyun', 'zhang fei'] > > > # converts a list to tuples... > > > list1 = [" guan yu ", "zhaoyun", "zhang fei"]] > > > tuple1 = the tuple (list1) > > > > > > print (tuple1) (" guan yu ", 'zhaoyun, < span style = "box-sizing: border-box; color: RGB (51, 51, 51); line-height: 22px; font-size: 14px! Important; white-space: inherit! Important;" >>> tuple1=() >>> print(tuple1) () >>> >>> print(len(tuple1)) 0 >>> # > > > tuple1 = (" liu ") > > > print (tuple1) liu bei > > > > > > print (len (tuple1) 2 > > > # if there are two elements to define a tuple, then output "liu", "guan yu", and length of 2... > > > tuple1 = (" liu ", "guan yu") > > > print (tuple1) (" liu ", "guan yu") > > > > > > print (len (tuple1) 2 > > > # need to note that if an empty output (), two elements output also with (), When there is only one element, the output is the contents of the element, no (), and the length is 2, the same as the length of two elements. # This is because () can represent both a tuple and the parentheses in a mathematical formula, which creates an ambiguity. Therefore, Python states that in this case, the parentheses are evaluated not as an element in a tuple, but as a string. . # therefore, the definition of tuple with only 1 element must be followed by a comma, to disambiguate: this output (' liu Bei '), and the tuple length is 1... > > > tuple1 = (" liu ",) > > > print (tuple1) (' liu bei ') > > > print (len (tuple1) 1Copy the code

That’s a simple use of primitives in Python!

Finally, welcome to pay attention to the public number: 1024 notes, free access to massive learning resources (including video, source code, documents)!

Other recommendations:

  • Getting started with Python (2) : Using lists
  • Getting started with Python (4) : Using sets
  • Getting Started with Python (5) : Dict usage
  • Getting Started with Python 6: Calling custom functions
  • Getting started with Python (I) : String formatting