Python Learning D4

Source: Internet
Author: User
Tags uppercase letter

List

The list is called the container type data type.
# Li = [123, ' Old boy ', True, [1, 2, 3], {' name ': ' Alex '}, (1, 2, 3)]

Index

# L1 = [' Wusir ', True, ' Alex ', ' Laonanhai ', ' Ritian ', ' Taibai ']
# print (l1[0]) # print (l1[1], type (l1[1])) # To take a value by slice # print (L1[:3]) # print (l1[2:]) # print (L1[::2])
Additions and deletions and other methods
# Increase
#append Append
#Print (L1.append (' goddess '))#l1.append (123)#L1.append ([1, 2, 1111])#print (L1)#name_list = [' Wusir ', ' Alex ', ' Laonanhai ', ' Ritian ', ' Taibai ']#While True:#username = input (' Please enter a new employee name: Q or Q exit '). Strip ()#if username.upper () = = ' Q ': break#name_list.append (username)#print (' You have successfully added%s '% username)#print (name_list)

#insert Insert
# L1.insert (1, ' a ') # print (L1)
# Extend iteration with append
# l1.extend (' ABCD ') # L1.extend ([1, 2, 3, 4, ' abc ']) # print (L1)
# Delete
# pop is deleted by index, with return value
# ret = l1.pop (0) # print (ret) # print (L1)
# Remove is deleted by element
# l1.remove (' Alex ') # print (L1)
# Clear Empty List
# l1.clear () # print (L1)

#del

" " 1, delete the list at the memory level del l1print (L1) 2, by index. Del l1[2]print (L1) 3, slice delete + step. Del l1[:-1]print (L1)"
# del L1 # print (L1) # del L1[2] # print (L1) # del L1[:-1] # print (L1) # del L1[:3:2] # print (L1)

# change
# 1, follow the index to change.  #  l1[0] = ' Xiaofeng '#  print (L1)#  l1[-1] = ' male god '#  Print (L1)#  2, according to the slice to change, the slice of the area of the contents of all deleted, in the iteration to add content.  #  l1[:3] = ' Edwin van der Sar sent to Ghfkhgh; ' # L1[:3] = [1111, 2222, 3333, 4444] # print (L1)

# look it up in the index slices.


#for循环
# For i in L1: #      Print (i)
#其他操作方法:
Number of #len
# cou = len (L1) # print (cou)
Number of #count elements
# Print (L1.count (' Taibai '))
#index searching for an index by element
# Print (L1.index (' Alex ', 3)) # L2 = [1, 3, 5, 9, 7, 6, 2, 4]
#sort from small to large, ordered by order
# L2.sort () # print (L2)
#sort from big to small, in reverse order
# L2.sort (reverse=true) # print (L2)
#翻转 Reverse
# L2.reverse () # print (L2)

Nesting of lists
L1 = [' Wusir ', ' Alex ', [, ' Taibai ', ' 98 '], 21]
# 1, turn the Wusir into full capitalization.
# l1[0] = ' Wusir ' # print (L1) # print (L1[0].upper ()) # l1[0] = L1[0].upper () # print (L1)
# 2, turn ' Taibai ' into an uppercase letter, other lowercase letters, put back in place.
# print (l1[2]) # L2 = l1[2] # l2[1] = L2[1].strip (). Capitalize () # l1[2][1] = L1[2][1].strip (). Capitalize () # print (L1) # l1[2].append (' Shu qi ') # print (L1)
# 3, add 99 to the ' 100 ' of the string with the addition of numbers.
# l1[2][0] = str (l1[2][0] + 1) # print (L1)
# 4, the string ' 98 ' becomes the number 100.
# l1[2][2] = Int (l1[2][2]) + 2 # print (L1)
# tuples

Read-only list, can not be deleted and changed only query
# Tu = (11, 22, 33, 44, 55)
#索引, Slice, step
# print (tu[0]) # print (Tu[:3:2]) # For i in Tu: #      Print (i)
# index  Len  count

Range

As a list of custom numeric ranges,
# satisfies the Gu Tou disregard butt, can add the step size, with the for loop use together.

#for I in range (0, 101): #[0,1,2,3,.... []#print (i)#For I in range (one): # [0,1,2,3,.... Ten]#print (i)#For i in range (0, 101, 3):#print (i)#for I in range (0,-1):#print (i)#L1 = [ +, +, +, +, +, +,]#For i in L1:#Print (L1.index (i))#For i in range (len (L1)):#print (i)

Prints all the elements in the L1 list, including the internal list.
L1 = ['Wusir','Alex', [99,'Taibai','98'], 21] forIinchL1:ifType (i) = =list: forJinchI:Print(j)Else:Print(i)

Python Learning D4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.