程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

【Python 21 Days Learning Challenge】-List & Tuple & Range

編輯:Python

活動地址:CSDN21天學習挑戰賽

​Let's start:
Whether it is for future study needs or for work needs after three years,都需要用到python的工具,Also follow the opportunity of this event,把pythonLearn it systematically.跟著大佬,讓pythonKnowledge from entry to mastery.


文章目錄

      • ​1 列表
      • 2 元組
        • 2. 1 定義
        • 2.2 索引與切片
      • 3 range() - 范圍


​1 列表

列表——list–容器(書包) : 有序、可變、支持索引
列表:存儲數據、存儲字符串、數字、布爾型、列表、集合、元組、字典

  • list[1:3]左閉右開

定義一個列表:

lst=["alex",123,True,"憨憨","alex"] #Separate elements with commas,The address is stored
# print(lst)
# print(id(lst[0]))#id獲取對象的內存地址
# #['alex', 123, True, '憨憨'],輸出有序

列表是可變的:

 lst[0]="alexxx" #Strings are not mutable variables
print(lst)

If you continue to add list content:

lst.append("烤鴨") #Add at the end of the list
print(lst)
lst.insert(2,"黑哥") #Added after the second element.But try not to use it in the future,若數據量太大,影響效率
print(lst)
lst.extend("今天是個好日子") #迭代添加,Add today's good one by one,Can't put numbers,Because numbers cannot be looped
#['alex', 123, True, '憨憨', '今', '天', '是', '個', '好', '日', '子']
print(lst)

列表刪除元素:

lst=["alex",123,True,"憨憨","alex1"]
lst.pop() #彈,默認刪除最後一個
print(lst)
lst.pop(1)
print(lst) # Delete by the specified index
lst.remove("憨憨") #Delete by element name,If there are multiple elements with the same name,刪除第一個,It will not be deleted thereafter
print(lst)
del lst #Force destroy the list
del lst[0:3] #刪除0-3,切片刪除
print(lst)
del lst[0:4:3] #刪除0-4,0開始之後,Interrupt two numbers in the middle and delete them once,步長刪除
print(lst)
lst.clear() #清空
print(lst)

修改列表內容:

lst=["alex",123,True,"憨憨","alex1"]
lst[3]="Dahanhan"
print(lst)
lst[1:2]="12345" #切片修改
#['alex', '1', '2', '3', '4', '5', True, '憨憨', 'alex1']
print(lst)
lst[1:4]=1,"daaa",3
print(lst)
lst[1:4:2]="12" #步長不為1的,必須一一對應,多一個或者少一個都不行
print(lst)

列表的嵌套:

lst=[1,"春生","小東北","渣弟",["大黑哥",["孫悟空","弼馬溫","齊天大聖"],"馮強","海峰",["太白金星","女神","吳超",["蕭峰"]]]]
print(lst[-1])
# ['大黑哥', '馮強', '海峰', ['太白金星', '女神', '吳超', ['蕭峰']]]
a=lst[-1]
print(a[-1])
#輸出:['太白金星', '女神', '吳超', ['蕭峰']]
# No matter what type of slice,Both are source data types
#想要獲取'弼馬溫'
a=lst[-1][1][1]
print(a)

2 元組

2. 1 定義

首先舉一個例子:

t=123,'abc',["come","here"]
print(t)

元組是用圓括號括起來的,元素之間用逗號隔開.如上輸出:

(123, 'abc', ['come', 'here'])

其中,A tuple is also a sequence,與字符串、列表類似.

  • Elements in a tuple cannot be changed,與字符串類似
  • The storage type of a tuple can be any type of data,這一點與列表相同

2.2 索引與切片

Operations are similar to lists:

t=1,'23',[123,'abc'],["come","here"]
print(t[2])
print(t[1:])
print(t[2][0])
[123, 'abc']
('23', [123, 'abc'], ['come', 'here'])
123

需要注意的是,If a tuple has only one element,A comma should be added after the element.如下:

a=(3)
print(type(a))
b=(3,)
print(type(b))
<class 'int'>
<class 'tuple'>

Conversion between lists and tuples

  • 元組->列表 tuple->list
t=1,'23',[123,'abc'],["come","here"]
print(t)
tls=list(t)
print(tls)
#輸出:
#(1, '23', [123, 'abc'], ['come', 'here'])
#[1, '23', [123, 'abc'], ['come', 'here']]
  • 列表->元組 list->tuple
tu_tuple=tuple(tls)
print(tu_tuple)
#輸出:
#(1, '23', [123, 'abc'], ['come', 'here'])

3 range() - 范圍

python3 中的range是一個迭代對象,How to write and how to print

print(list(range(0,10)))
# 輸出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #顧頭不顧尾(左閉右開)

python2 中的range返回的是一個列表

print(range(0,10))
# # 輸出:range(0, 10)

示例展示:

print(list(range(10,1,-1))) #[10:1:-1]
#輸出:[10, 9, 8, 7, 6, 5, 4, 3, 2]
#使用for循環和range打印100~1
for i in range(100,0,-1):
print(i,end=" ")
#輸出:100 99 98 97 96 95 94 93 92 91 ..... 1

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved