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

Python learning notes: tuples

編輯:Python
# 1、 Definition of tuple
t = tuple('python')
t1 = ('p', 'y', 't', 'h', 'o', 'n')
t2 = ('my', 'name', 'is', 'datian')
t3 = 'my', 'age', 'is', 20
t4 = ('solo' ,)
print(t)
print(t4)
print(t1)
print(t1[2])
print(t1[:2]) # Take two small tuples from the large tuple , amount to java In the array ,python Tuples in are immutable sequences
# 2、 Tuple operation
# 1)join meaning : Take the following as an example , Will be Space perhaps + Add to the middle of every string , Finally, put them together into a large string
# remarks :t1 All elements in the must be of string type
print(" ".join(t1))
print("+".join(t1))
t5 = 'd', 'a', 't', 'i', 'a', 'n'
# 2)count function : Count how many a
print(t5.count('a'))
# 3)index function : see a In which position , If there is no error report
print(t5.index('a'))
# 4) Tuple length
print(len(t5))
t6 = tuple(range(100))
print(len(t6))
print(t6[-1])
# Determine whether a tuple contains an element
# Example :999 Is here or not t6 in
print(999 in t6)
# Cycle through each data in the tuple
t7 = ('d', 'a', 't', 'i', 'a', 'n')
# Method 1
for i in t7:
print(i)
# Method 2
i = 0
while i < len(t7):
print(t7[i])
i += 1

 

 


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