List is a type of value, a list contains many values, you can add or delete an item from the list.
Flowing is the example of two lists, the first list is filled with numbers, the second list is filled with strings.
You can put values that have different types to an list, for example:
Now we have a list, but how to get a specific value from the list? For example, how to get the second value in the list?
Use Index.
In the list list = [1,"a",2,"b"]
, the index for 1
is 0, index for "a"
is 1, index increases after each item in the list. Note: Remember the index for the first item in the list is 0, not 1.
We can get a value from a list using index like this:
list[1]
means the item in the list which have an index of 1. The 1
in the square bracket represents the index.
We can also get a part from the list like this:
list[3:6]
means get the part of the list starting from index 3 to index 6. The number before colon is the starting index, the number after colon is the ending index.
If you want to add one value to the end of the list, use list.append(item)
If you want to insert a value to the middle of the list, use list.insert(index, item)
Example:
If you want to change the value of one item in the list, use list[index] = newValue
Example:
If you know the index and want to remove it from the list, use list.pop(index)
If you know the value and want to remove it from the list, use list.remove(value)
Example:
Length of the list: len(list)
If the list is all number or all string, you can use max(list)
and min(list)
to get the max and min value in the list.
Example:
Yes, you can add 2 lists XD
Operator | Example | Meaning | Result Value Type |
---|---|---|---|
+ | list1 + list2 | append list2 to the end of list1 | list |
* | list * a | repeat a list for a times | list |
in | a in list | If value a is in the list | boolean |
A tuple is almost same as a list, except you can’t change the value of it after you define it.
For example:
Because of tuple is not changeable after defining it, so you can’t add or update or remove an item from the tuple.
You can get value from a tuple, get length, max value and min value from a tuple.
Because operator doesn’t change the item in a tuple, so you can use operators with tuple.
Next: Advanced Function