Python Lists are of various types such as numbers, strings, lists, etc. in one list. Data can be stored. Both constants and variables can be elements.
l = ['a', 'b', 'c'] X = [1, 2, 3.4, "a", l]
X contains [1, 2, 3.4, "a", ['a','b','c']]. Enclose the list in [] and separate each element with ,. Copy the following program and execute it. I will explain each function.
py3:list_explain:py
z = ['a','b','c']
x = [1, 2, 3.4, "a", z]
a_in_list = 'a' in z
d_in_list = 'd' in z
print("a_in_list {0}".format(a_in_list))
print("d_in_list {0}".format(d_in_list))
print("len(z) {0}".format(len(z)))
print("max(z) {0}".format(max(z)))
print("min(z) {0}".format(min(z)))
print("x[1] {0}".format(x[1]))
print("x[-1] {0}".format(x[-1]))
print("x[1:3] {0}".format(x[1:3]))
print("x[1:] {0}".format(x[1:]))
print("x[:3] {0}".format(x[:3]))
x[1:4] = ["taro","jiro"]
print("changed x {0}".format(x))
a = z + x
print("a {0}".format(a))
y = [] #create empty list
y.append("spam")
print("y {0}".format(y))
y.append("ham")
print("y {0}".format(y))
y2 = ["egg","ham"]
y.append(y2)
y.remove('ham')
print("y {0}".format(y))
z.reverse()
print("z {0}".format(z))
sortlist = [0,1,3,7,5]
sortlist.sort()
print("sort {0}".format(sortlist))
Like the string type, the list can also be checked with ** x in list ** to see if the element exists in the list.
Like the string type, the list can get the xth element.
The same processing as a character string can be performed on an element.
x = [1, 2, 3.4, "a", z] Against x[1:4] = ["taro","jiro"] By writing [1, 'taro', 'jiro', ['a', 'b', 'c']] You can change the contents of the list in this way. The [1: 4] part of the original list [2, 3.4, "a"], that is, the three elements As a result of replacement, there are two ["taro", "jiro"]. This "replace element by specifying range" is possible for lists, but string type is not supported.
You can add elements to the list with ** List.append (element) **. Elements can also be lists. In this case, the process is to concatenate the list into the list. As you can see from the program, you can do the same with the symbol "+".
You can remove an element from the list with ** List.remove (element) **. The list can store the same element multiple times, but if there are multiple specified elements Delete the first element. If you try to delete an element that does not exist, you will get an error. It is safe to do remove () after using in to make sure it exists in advance.
You can reverse the order of the elements in the list with ** List.reverse () **.
You can sort the elements in the list in order with ** list.sort () **.
Tuples are very similar to lists. Enclose it in () instead of [].
tuple = (1,2,3.4,"a","test")
You can check the existence of elements, indexing, len, min, max as in the list, but You cannot replace elements or add or delete elements. So is there any reason to use tuples whose elements cannot be changed?
The advantage of using tuples is that the program execution speed is a little faster. If you want to prepare a list whose contents do not change, you can also include tuples as an option. There are other benefits to using tuples, but we'll talk about that later.
Next: Python Basic Course (6 sets)
Recommended Posts