Full-text catalog, all in one step
- 1. Introduction
- 2. Basic use of python
-
- 2.1 list list <class 'list'>
-
- 2.1.1 Definition of list
- 2.1.2 Subscript index of list
- 2.1.3 Commonly used APIs for lists
-
- ->(1) If the subscript corresponding to the searched element does not exist, an error will be reported.
- ->(2) Modify elements
- ->(3) Insert element
- ->(4) Append element (one)
- ->(5) Append `a bunch` of elements
- ->(6) Delete elements (`four` ways)
- ->(7) Count the number of specified elements
- ->(8) Count of all elements in the list
- ->(9) Loop through list elements
- 2.2 Tuple
- 2.3 String
- 3. Basic grammar summary case
- 4. Summary and preview of the article
1. Introduction
1.1 Transmission above
—⇒Portal: Python Introduction 04-Loop (while and for), variables, function basics
1.2 python column transmission
—⇒ Portal: python basics column
1.2 Types and differences of data containers
Note about the dictionary
{k-v}
:
key不可以重复
However, if the value
is repeated with any key, it will not report an error ( warning ), but will directly overwrite the previous key.
Container type | Container name | symbol | Whether to modify | Is it in order? | Is it redundant |
---|---|---|---|---|---|
list | list | [] | yes | yes | yes |
gather | set | {} | yes | no | no |
string | str | “” | no | yes | yes |
tuple | tuple | () | no | yes | yes |
dictionary | dict | {“k”:“v”} | yes | no | kno vyes |
2. Basic use of python
2.1 list list <class 'list'>
List element type
不受限制
(similar to java's list不同
, also operates on it不太相似
)
- List capacity limit
2**63-1
- storable
不同类型
elements- data is
有序
stored允许重复数据
exist可修改
2.1.1 Definition of list
Introduction to different list definition methods:
-> (1) Same type ( 列表元素
type)
lists = ["a", "b", "c"]
print(lists)
# print(type(lists))
-> (2) Different types ( 列表元素
types)
lists = ["a", 1, True]
print(lists)
# print(type(lists))
-> (3) Nested list ( 列表元素
type)
lists = [["a", 1, True], ["a", 2, False]]
print(lists)
# print(type(lists))
2.1.2 Subscript index of list
下标
Search and related operations based on
-> (1) Single-level list (forward subscript)
Look at the abnormal situation:
print(lists[3])
#Error报错
: list index out of rangeIndex
subscript is out of range ( Java calls array subscript out of bounds )
lists = ["a", "b", "c"]
print(lists[0])
print(lists[1])
print(lists[2])
-> (2) Single-level list (reverse subscript)
lists = ["a", "b", "c"]
print(lists[-1])
print(lists[-2])
print(lists[-3])
-> (3) Nested list (forward subscript group)
lists1 = [["a", "b", "c"], ["1", "2", "3"]]
print(lists1[0][0]) # a
print(lists1[1][1]) # 2
print(lists1[1][2]) # 3
-> (4) Nested list (reverse subscript group)
lists1 = [["a", "b", "c"], ["1", "2", "3"]]
print(lists1[-1][-1]) # 3
print(lists1[-2][-2]) # b
print(lists1[-2][-3]) # a
2.1.3 Commonly used APIs for lists
For example list, please
先创建list
do the followinglists = ["a", "b", "c"]
->(1) If the subscript corresponding to the searched element does not exist, an error will be reported.
# 查找下表索引 不存在报错
index = lists.index("b")
print(f"b的位置是: {
index}") # 1
->(2) Modify elements
lists[0] = "a1"
print(lists)
->(3) Insert element
lists.insert(1, "b1")
print(lists)
->(4) Append element (one)
lists.append("d")
print(lists)
->(5) Append 一堆
elements
lists2 = [99, 999, 9999]
lists.extend(lists2)
print(lists)
->(6) Delete element ( 四种
method)
# 6.1 仅删除元素(根据下标)
del lists[1]
print(lists)
# 6.2 删除元素(等同于 取出下标元素并返回)
lists.pop(0)
print(lists)
# 6.3 删除元素(根据内容 找到第一个元素 删除) 只删除一个
lists.remove("b")
print(lists)
# 6.4 清空列表
lists.clear()
print(lists)
->(7) Count the number of specified elements
lists = ['a', 'a', 'b', 'b', 'c', 'd', 99, 999, 9999]
print(lists.count("a")) # 2
->(8) Count of all elements in the list
print(len(lists))
->(9) Loop through list elements
while/for循环遍历
output
defines two methods
def func_list_while(lists1):
index = 0
while index < len(lists1):
print(lists1[index])
index += 1
def func_list_for(lists1):
for index in lists1:
print(index)
code
测试
and输出
results
lists = [1, 2, 3, 4, 5, 6, 7, 8, 9]
func_list_while(lists)
print("上面while循环 下面for循环")
func_list_for(lists)
2.2 Tuple
First look
元组与list
at the differences
- list The list can be modified and is
[]
represented by:- The tuple cannot be modified after it is defined and
()
is represented by :tuple()
Application
元组
scenarios
- Encapsulate data within the program, and
不希望被篡改
- It is somewhat similar to the meaning of java's final modifier.
2.2.1 Define tuples
-> (1) Created under normal circumstances
# 1. 任意类型元素 创建
t1 = (1, "hello", True)
# 2. 空元组定义1
t2 = ()
# 3. 空元组定义2
t3 = tuple()
# 4. 定义一个元素
t4 = tuple("hello")
-> (2) Special circumstances
t6 = ("hello")
Note: What is represented here is a string.
A hint is given:让删除括号
the correct way to write it is as follows (two methods)
method one:
t5 = tuple("hello")
Method Two:
t7 = ("hello",)
Overall type query
print(type(t4))
print(type(t5))
print(type(t6)) # <class 'str'> 不加tuple就必须加','
print(type(t7))
-> (3) Nested tuple definition
t8 = ((1, 2, 3), (4, 5, 6))
print(type(t8))
# 输出结果: 6 写法
print(f"嵌套元组下标1,2 的 值是: {
t8[1][2]}")
2.2.2 Commonly used API methods for tuples
First define a t9 tuple
t9 = (1, 2, 3, 3, 5, 6)
-> (1) index() finds the subscript
print(t9.index(2)) # 2元素位置下标是1
-> (2) count() queries the number of specified elements
print(t9.count(3)) # 查看3有几个 2
-> (3) len(element) query tuple length
print(len(t9)) # 查看元组长度 6
2.2.3 Tuple loop traversal
-> (1) while loop
print("\n上面元组语法基础 下面循环 ")
index = 0
while index < len(t9):
print(t9[index])
index += 1
-> (2) for loop
for index in t9:
print(index)
2.2.4 Special cases where tuples can be modified
-> (1) Test whether the tuple can really be modified ( 报错
)
t10 = (1, 2, 3, 3, 5, 6)
t10[0] = 999 # TypeError: 'tuple' object does not support item assignment
-> (2) If a list is placed inside, can it be modified ( 可以
)
Note:
元组本身
cannot be modified
but: the elements inside are列表[]
lists可以修改
t10 = (1, 2, [40, 50, 60])
t10[2][0] = 80
print(t10) # (1, 2, [80, 50, 60]) 修改成功 元组嵌套的list可修改
2.3 String
String
容器视角
interpretation
- support
下标索引
- A string is a container of characters. A string can store any number of characters.
- Only strings can be stored and cannot be modified.
2.3.1 String container definition
# 一个字符是一个元素
str1 = "zhangSan is a good boy"
2.3.2 Common APIs for string containers
->(1) index() finds the beginning of the string
value = str1.index("is")
print(value) # 9
->(2) replace() string replacement
str cannot be modified and a new object will be generated.
str2 = str1.replace("a", "***")
print(str2) # zh***ngS***n is *** good boy
->(3) split() string splitting
str2 = str1.split(" ")
print(str1) # zhangSan is a good boy
print(str2) # ['zhangSan', 'is', 'a', 'good', 'boy']
->(4) strip() string specification
Remove the spaces before and after
strip (element) from front to back去掉空格并删除第一个元素
java is trim()
str0 = " a b c "
print(str0.strip()) # a b c
print(str0.strip("a ")) # b c 去除空格并删除输入的值(从前到后)
->(5) count() queries the number of occurrences
str1 = "zhangSan is a good boy"
print(str1.count("o")) # 3
->(6) len() query string length
str1 = "zhangSan is a good boy"
print(len(str1)) # 22
3. Basic grammar summary case
3.1 Basic case of list
3.1.1 Case details
Case Details
- Declare a list of student ages
- add an element
- Add a bunch of elements
- Take out an age subscript and output it
- Take out an age subscript backwards and output
- Find a subscript corresponding to an age Note that an error is reported
- output the age of any subscript
- Clear age list
3.1.2 Code implementation
students_age_list = [10, 20, 30, 31, 31, 20]
print(students_age_list)
students_age_list.append(50)
print(students_age_list)
students_age_list.extend([29, 50, 60])
print(students_age_list)
print(students_age_list.pop(0))
print(students_age_list.pop(-1))
print(students_age_list.index(20))
print(students_age_list[0])
print(students_age_list.clear()) # None
4. Summary and preview of the article
4.1 Summary of this article:
- List list defines common operations
- Different common operations between tuples and lists
- Common operations from the string container perspective
4.2 Preview below:
4.2.0 Portal: python column address
4.2.1 Basic 07-Data Container-(sequence slice, collection, dictionary)
4.2.2 Comprehensive case of data containers
Author pingzhuyan Thanks for watching