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

Python -- string, list, tuple, dictionary, common method (day03)

編輯:Python

Catalog

One 、 String and common methods

Two 、 List and common methods

3、 ... and 、 Tuples

Four 、 Dictionaries

5、 ... and 、 Common method

Sequence : stay python among A sequence is a set of values arranged in order 【 Data set 】
stay python in There are three built-in sequence types :
character string 、 list 、 Tuples
advantage : Can support indexing and slicing operations
features : The first positive index is 0, It points to the left end , When the first index is negative , It points to the right end

section :【 Advanced features 】 You can obtain any of the sequence objects according to the following table [ part ] data
Grammatical structure :[start:end:step] step Default 1

list:python A very important data structure , It's an ordered set of data
characteristic :
1: Support addition, deletion, modification and query
2: The data in the list can be changed 【 Data items can vary , The memory address doesn't change 】
3: use [] To represent the list type , Data items are separated by commas , Be careful : Data items can be any type of data
4: Support indexing and slicing for operations

Tuples : Is an immutable sequence , No changes can be made after creation
1: immutable
2: use () Create tuple type , Data items are separated by commas
3: It can be any type
4: When there is only one element in a tuple , Add a comma , No, then the interpreter will handle it as other types
5: It also supports slicing

Dictionaries : It's also python Important data types in , The dictionary has Key value pair Set of components , Usually use Key to access data , Very efficient , and list equally Support the addition of data 、 modify 、 Delete
characteristic :
1: Not a sequence type There is no concept of subscript , It's a disorder Key value set , Is a built-in advanced data type
2: use {} To represent dictionary objects , Each key value pair is separated by commas
3: key Must be immutable type 【 Tuples 、 character string 】 Values can be of any type
4: Each key must be unique , If there are duplicate keys , The latter overrides the former

One 、 String and common methods

test = 'python'
# # print(type(test))
# print(' First character :%s' %test[0])
# print(' Second character :%s' %test[1])
# for item in test:
# print(item, end=' ')
#
# print()
# for i in range(len(test)):
# print(' The first %d Characters :%s' %(i, test[i]), end=' ')
name = 'peper'
# print(' Convert initial to uppercase :%s' %name.capitalize())
a = " hello "
# b = a.strip() # Remove the space
# print(b)
# print(a.lstrip()) # Delete the space on the side
# print(a.rstrip()) # Delete the on the right
#
# print('a Memory address of :%d' %id(a)) # id You can view the memory address of an object
# b = a # Here is just a The memory address of the object is assigned to b
# print('b Memory address of :%d' %id(b))
datastr = 'I Love Python'
# print(datastr.find('P')) # Returns the corresponding subscript value
# print(datastr.find('m')) # No return found -1
#
# # ------------------- index function --------------------
# print(datastr.index('P')) # Check if there is a substring in the string , Returns the subscript , If it doesn't, it will report an error
#
# print(datastr.startswith("I")) # Judge whether xx start
# print(datastr.startswith("o"))
# print(datastr.startswith("m"))
#
# print(datastr.endswith("I")) # Judge whether xx ending
# print(datastr.endswith("o"))
# print(datastr.endswith("m"))
# print(datastr.lower()) # It's all lowercase
# print(datastr.upper()) # All capitalized
strmsg = 'hello world'
# slice [start:end:step] Left closed right away start<=value<end Range
print(strmsg) # Output complete data
print(strmsg[0])
print(strmsg[2:5]) # 2-5 Data between subscripts
print(strmsg[2:]) # The third character to the end
print(strmsg[0:3]) # 1-3 strmsg[0:3] = strmsg[:3]
print(strmsg[:3])
print(strmsg[::-1]) # Output in reverse order , A minus sign indicates the direction , Traverse from right to left 

Two 、 List and common methods

# li=[] # An empty list
# li=[1,2,3," Hello "]
# print(len(li)) #len Function can get the number of data in the list object
# strA=' I like python'
# print(len(strA))
# print(type(li))
# lookup
listA=['abcd',785,12.23,'qiuzhi',True]
# print(listA) # Output the complete list
# print(listA[0]) # Output the first element
# print(listA[1:3]) # From the second to the third element
# print(listA[2:]) # From the third element to the last element
# print(listA[::-1]) # Negative numbers are output from the right to the left
# print(listA*3) # Output the data in the list more than once 【 Copy 】
# print('-------------- increase -----------------------')
# print(' Before appending ',listA)
# listA.append(['fff','ddd']) # Additional operations
# listA.append(8888)
# print(' After adding ',listA)
# listA.insert(1,' This is the data I just inserted ') # The insert You need to perform a position insertion
# print(listA)
# rsData=list(range(10)) # Cast to list object
# print(type(rsData))
# listA.extend(rsData) # expand It is equal to adding in batch
# listA.extend([11,22,33,44])
# print(listA)
# print('----------------- modify ------------------------')
# print(' Before the change ',listA)
# listA[0]=333.6
# print(' After the modification ',listA)
listB=list(range(10,50))
print('------------ Delete list Data item ------------------')
print(listB)
# del listB[0] # Delete the first element in the list
# del listB[1:3] # Batch delete multiple data slice
# listB.remove(20) # Remove the specified element Parameters are specific data values
listB.pop(1) # Remove the specified item Parameter is an index value
print(listB)
print(listB.index(19,20,25)) # What is returned is an index subscript
# li=[] # An empty list
# li=[1,2,3," Hello "]
# print(len(li)) #len Function can get the number of data in the list object
# strA=' I like python'
# print(len(strA))
# print(type(li))
# lookup
listA=['abcd',785,12.23,'qiuzhi',True]
# print(listA) # Output the complete list
# print(listA[0]) # Output the first element
# print(listA[1:3]) # From the second to the third element
# print(listA[2:]) # From the third element to the last element
# print(listA[::-1]) # Negative numbers are output from the right to the left
# print(listA*3) # Output the data in the list more than once 【 Copy 】
# print('-------------- increase -----------------------')
# print(' Before appending ',listA)
# listA.append(['fff','ddd']) # Additional operations
# listA.append(8888)
# print(' After adding ',listA)
# listA.insert(1,' This is the data I just inserted ') # The insert You need to perform a position insertion
# print(listA)
# rsData=list(range(10)) # Cast to list object
# print(type(rsData))
# listA.extend(rsData) # expand It is equal to adding in batch
# listA.extend([11,22,33,44])
# print(listA)
# print('----------------- modify ------------------------')
# print(' Before the change ',listA)
# listA[0]=333.6
# print(' After the modification ',listA)
listB=list(range(10,50))
print('------------ Delete list Data item ------------------')
print(listB)
# del listB[0] # Delete the first element in the list
# del listB[1:3] # Batch delete multiple data slice
# listB.remove(20) # Remove the specified element Parameters are specific data values
listB.pop(1) # Remove the specified item Parameter is an index value
print(listB)
print(listB.index(19,20,25)) # What is returned is an index subscript

3、 ... and 、 Tuples

# Tuple creation It can't be modified
tupleA=() # An empty tuple
# print(id(tupleA))
tupleA=('abcd',89,9.12,'peter',[11,22,33])
# print(id(tupleA))
# print(type(tupleA))
# print(tupleA)
# Tuple query
# for item in tupleA:
# # print(item,end=' ')
# print(tupleA[2:4])
# print(tupleA[::-1])
# print(tupleA[::-2]) # Represents an inverted string Every two
# # print(tupleA[::-3]) # Represents an inverted string Go every three
# # print(tupleA[-2:-1:]) # Take the subscript upside down by -2 To -1 The interval of
# # print(tupleA[-4:-2:]) # Take the subscript upside down by -2 To -1 The interval of
# tupleA[0]='PythonHello' # FALSE
# tupleA[4][0]=285202 # You can modify the data of list type in tuple
# print(tupleA)
# print(type(tupleA[4]))
tupleB=('1',) # When there is only one data item in a tuple , You must add... After the first data item comma
# print(type(tupleB))
tupleC=(1,2,3,4,3,4,4,1) #tuple(range(10))
print(tupleC.count(4)) # You can count the number of occurrences of elements

Four 、 Dictionaries

# How to create a dictionary
dictA={"pro":' art ','shcool':' Beijing Film Academy '} # An empty dictionary
# Add dictionary data
dictA['name']=' Li Yifeng ' #key:value
dictA['age']='30'
dictA['pos']=' singer '
# End adding
# print(dictA) # Output complete dictionary
# print(len(dictA)) # Data item length
# print(type(dictA))
# print(dictA['name']) # Get the corresponding value through the key
# dictA['name']=' Nicholas Tse ' # Modify the value of the key
dictA['shcool']=' The university of Hong Kong '
# dictA.update({'height':1.80}) # Can be added or updated
# print(dictA)
# Get all keys
# print(dictA.keys())
# Get all values
# print(dictA.values())
# Get all the keys and values
# print(dictA.items())
# for key,value in dictA.items():
# print('%s==%s'%(key,value))
# Delete operation
# del dictA['name'] Delete by specifying the key
# dictA.pop('age') Delete by specifying the key
print(dictA)
# How to sort according to key Sort
# print(sorted(dictA.items(),key=lambda d:d[0]))
# according to value Sort
# print(sorted(dictA.items(),key=lambda d:d[1]))
# Dictionary copy
import copy
# dictB=copy.copy(dictA) # Shallow copy
dictC=copy.deepcopy(dictA) # Deep copy
print(id(dictC))
print(id(dictA))
# dictB['name']='peter'
dictC['name']=' Lau Andy '
print(dictC)
print(dictA)

5、 ... and 、 Common method

# There is a common way + * in
# String merge
strA=' Life is too short '
strB=' I use Python'
# list Merge
listA=list(range(10))
listB=list(range(11,20))
print(listA+listB)
# print(strA+strB)
# Copy *
# print(strA*3)
# print(listA*3)
# in Whether the object exists The result is a bool value
print(' I ' in strA) #False
print(9 in listA) #False
dictA={"name":"peter"}
print("name" in dictA)


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