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

0 basic learning python (10)

編輯:Python

We often need to create a new list based on the existing list , This requires list copying , To copy a list, create a slice that contains the entire list , A slice of the entire list is a slice that omits two indexes [:], If there are three people in one club and there are three people in another club , Then we can copy the list directly .

numbers=['zhang','wang','su']
a_numbers=number[:]
print(numbers)
prnit(a_numbers)
['zhang','wang','su']
['zhang','wang','su']

  Although there are the same three people, there may be different people , You can use it append() Add . For example, the first club has meng,a In the club fan.

numbers=['zhang','wang','su']
a_numbers=number[:]
numbers.append('meng')
a_numbers.append.('fan')
print(numbers)
print(a_numbers)
['zhang','wang','su','meng']
['zhang,'wang','su','fan']

In two societies , The first club has 'meng' stay , The second club has 'fan' stay , If we don't use slice copying, we can assign values directly , This meng and fan Both will appear in both lists at the same time .

stay python The list in can be modified, but you will encounter some data that cannot be changed , At this time, we need to use the meta group , Tuples are not modifiable , Unlike the list, he is in () in , Although it cannot be modified, it can still be accessed by index like a list .

dimensions=(200,50)
print(dimensions[0])
print(dimensions[1])
200
50

We first set a rectangle with the same size , Store its length and width in a tuple , Then access the index 0 and 1 The tuple... Was successfully output 0 and 1 The content of .

If we try to modify elements in tuples .

dimensions=(200,50)
dimensions[0]=250

At this time, this operation is python Prohibited ,python It will indicate that elements of tuples cannot be assigned values .

We can use for Loop through all the values in the tuple .

dimensions=(200,50)
for dimension in dimensions:
print(dimension)
200
50

Although we cannot modify the elements of tuples , But we can assign values to variables that store tuples , You can redefine the entire tuple .

dimensions=[200,50]
for dimension in dimensions:
print(dimension)
dimensions=[400,100]
for dimension in dimensions:
print(dimension)
200
50
400
100


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