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

2. Common operations of Python data types

編輯:Python

str String operation ( Immutable type Value change id Also change )

** increase **
character string + character string
"%s %d" %(" character string ",18)
format
"my name is {0},age is {1}" .foramt(" The sea ",18)
join
str = " ".join(["aaa","bbb","ccc"])
** Delete **
del
name=" The sea " del name
** Change **
Change to lowercase lower
Capitalize upper
title case capitalize
Cut strings into lists split
"hello world python" Default space segmentation msg.split()
"hello * world * python" Segment according to the specified characters msg.split('*')
Existing characters , New characters , Number
replace
msg.replace(" you "," I ")
msg.replace("a","b",1)
Remove extra spaces from the string
strip
input(" Please enter a user name ").strip()
** check **
Subscript search srt[0]
Subscript slice ( Head and tail ) srt[0:n]
step str[0:n:2]
String length len(str)
Determine whether a large string exists in and not in return true or false
"srt" in str1
"str" not in str1
Find the position of the sub character in the large string
find
msg.find() No return found -1, Find the return subscript position
The lookup character appears several times in a large string
count
mag.count(' The sea ')
Determine whether the string is a pure number
isdigit
Judge whether it is all letters
isalpha
Compare whether the starting elements are the same
startswith
Compare whether the ending elements are the same
endswith
** escape **
Commonly used /n /t
Escape r
r." life zone /n Safety belt /t Bring it all "

int or float Numeric type operation ( Immutable type Value change id Also change )

** The assignment operation **
Normal assignment =
Addition assignment +=
Subtraction assignment -=
Multiplication assignment *=
division assignment /=
Take the assignment %=
Power assignment **=
Floor division assignment //=

bool Boolean type

true and false
** Key knowledge **
All data types have their own Boolean values
none、0、 empty ( An empty string , An empty list , An empty dictionary ) In three cases, the Boolean value is false
The rest are true

list list ( Variable type Value change id unchanged )

** increase **
Add an element
list.append('aaa')
Add multiple elements
list.extend(['bbb','ccc'])
Insert a... At the specified location
list.insert(1,'ddd')
** Delete **
Index delete
del list[2]
Specify the delete
list.remove('ccc')
Take a value from the list
list.pop(0)
** Change **
list[0]="aaa"
trans
list.reveres()
positive sequence
list.sort()
** check **
How many list elements
len()
Is it in the list
in and not in
See how many elements exist in the list
list.conut(''aaa)
View the subscript of the element
list.index('aaa')
Index value
list[0]

tuple Tuples ( Immutable type )

** increase **
Can not add
** Delete **
Can't delete
** Change **
A single value in a tuple cannot be modified
The value of the list in the tuple can be modified
t[2][0]='aaa'
** check **
Easy to use t(1,2,[a,b,c])
t[0]
Index like a list , section , length len,count Number ,index Find the index of the element , Members of the operation

dict Dictionaries ( Variable type Value change id unchanged )

** increase **
Write something that is not in the dictionary key Can increase
info['addr']=' wuhan '
** Delete **
Empty
info.clear()
Delete a value
del info['name']
Take away the dictionary value
res = info.pop('addr')
** Change **
info['name']=' Li Si '
If there is one, don't move , If nothing, add
info.setdefault('name','xxx')
info.update('name':'yyy')
** check **
info{'name':' Zhang San ','age':18}
info['name']
info['age']
see id
id(info)
Check the number of key value pairs
len(info)
No, key No mistake. , return none
info.get("xxx")
Take out all the values
info.values()
Take out all the keys
info.keys()
Take out all key value pairs
info.items()

set aggregate ( Variable type Value change id unchanged )

** Relationship between operation **
s1={'a','b','c'} s2={'b','c','d'} Each value must be of an immutable type
take 2 A collection of the same elements intersection
print(s1 & s2)
take 2 A collection of all the elements Combine
print(s1 | s2)
s1 Go to Offset their intersection Difference set
print(s1 - s2)
s2 Go to Offset their intersection Difference set
print(s2 - s1)
Cross complement Not also included in s1 and s2 The elements of
print(s1 ^ s2)
** increase **
s.add(' Blue ocean ')
** Delete **
s.pop(0)
s.reverse(' Blue ocean ')
** Change **
s,update([' Black Sea ',' The Yellow Sea '])
Set conversion list
L = list(s1)
List conversion set
s1 = set(L)

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