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

Python list and tuple

編輯:Python

1 List definition

Elements in the list : brackets , Comma separated

names=['jack','andy','kiki','alicy']# List of names
message_name=f"My sister's name is {names[3].title()}"# List subscript from 0 Start
print(names)
print(f"{message_name}" ",she is 7 years old")

Running results :

['jack', 'andy', 'kiki', 'alicy']
My sister's name is Alicy,she is 7 years old

2 A list of characters

2.1 The list of operations

names=['jack','andy','kiki','alicy']# List of names
print(f" Original list :{names}")
names[0]='jack_rep'# Replace the first element
print(f" Replace the first element :{names}")
names.append('alicy_apd')# Add an element at the end
print(f" At the end of append Add an element :{names}")
names.insert(1,'jack_inst')# The first element is inserted ‘jack_inst’
print(f" First element insert Insert ‘jack_inst':{names}")
del names[1]
print(f"del Delete ‘jack_inst':{names}")
popped_name=names.pop(1)# Delete the element at the corresponding position , And back to
print(f"pop Deleted values popped_name={popped_name}")
print(f"pop Post list :{names}")
names.remove('alicy_apd')# Delete alicy_apd
print(f"remove List after operation :{names}")
print(f"sorted Temporary sort :{sorted(names,reverse=True)}")# Sort from large to small
names.sort(reverse=False)# Permanent sorting of list elements , Sort from small to large
print(f"sort Permutation :{names}")
names.reverse()
print(f" List inversion reverse:{names}")
names.reverse()
print(f" The list is inverted again reverse two :{names}")
print(f" List length (len):{len(names)}")
print(f" Through the index -1 Return to the last element :{names[-1]}")

Running results :

Original list :['jack', 'andy', 'kiki', 'alicy']
Replace the first element :['jack_rep', 'andy', 'kiki', 'alicy']
At the end of append Add an element :['jack_rep', 'andy', 'kiki', 'alicy', 'alicy_apd']
First element insert Insert ‘jack_inst':['jack_rep', 'jack_inst', 'andy', 'kiki', 'alicy', 'alicy_apd']
del Delete ‘jack_inst':['jack_rep', 'andy', 'kiki', 'alicy', 'alicy_apd']
pop Deleted values popped_name=andy
pop Post list :['jack_rep', 'kiki', 'alicy', 'alicy_apd']
remove List after operation :['jack_rep', 'kiki', 'alicy']
sorted Temporary sort :['kiki', 'jack_rep', 'alicy']
sort Permutation :['alicy', 'jack_rep', 'kiki']
List inversion reverse:['kiki', 'jack_rep', 'alicy']
The list is inverted again reverse two :['alicy', 'jack_rep', 'kiki']
List length (len):3
Through the index -1 Return to the last element :kiki

2.2 Traverse the list

names=['jack','andy','kiki','alicy']# List of names
print(f" Original list :{names} \n adopt for Loop through list elements :")
for name in names:
print(f"My name is {name.title()}.")

Running results :

Original list :['jack', 'andy', 'kiki', 'alicy']
adopt for Loop through list elements :
My name is Jack.
My name is Andy.
My name is Kiki.
My name is Alicy.

3 List of values

3.1 Create a list of values

(1) list Method +range

numlist=list(range(2,13,2))# Ships 2 To 13 Even number between
print(numlist)

Running results :

[2, 4, 6, 8, 10, 12]

(2) Create... Through loops

square=[]
for value in range(1,11):#1 To 11( barring 11) loop
square.append(value**2)
print(square)

Running results :

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

(3) List of analytical - The first and second methods are combined

square_list=[value**2 for value in range(1,11)]
print(square_list)

Running results :

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3.2 A numerical list calculation or operation

(1) extremum Sum up

numlist=list(range(2,13,2))# Ships 2 To 13 Even number between
num_max=max(numlist)
num_min=min(numlist)
num_sum=sum(numlist)
print(numlist)
print(f"max:{num_max},\nmin:{num_min},\nsum:{num_sum}")

Calculation results :

[2, 4, 6, 8, 10, 12]
max:12,
min:2,
sum:42

(2) Traverse operation

numlist_d=[]
numlist=list(range(1,11))
for num in numlist:# Traverse the list
if num%2==0:
numlist_d.append(num)
print(f"numlist by :{numlist}")
print(f"numlist The even number contained in is :{numlist_d}")

Running results

numlist by :[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numlist The even number contained in is :[2, 4, 6, 8, 10]
 

4 Tuples

And list list Same operation for , The difference from a list is :

(1) Parentheses identify

(2) Values in tuples cannot be changed

Creating a tuple

namelist_yz=(20,30,50)
print(f" Tuples namelist_yz:{namelist_yz}")

Running results :

Tuples namelist_yz:(20, 30, 50)

Try changing the value of the tuple
 

namelist_yz[0]=10

Edit the results :


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