Directory:
I. Lists, tuples
Two. String
Three. Dictionaries, collections
Four. Conditional judgment and circulation
I. Lists, tuples
a data type built into Python. Lists: List, an ordered set of elements that can be added and removed at any time.
Cases:
>>>players = ['Lebron'Kobe'Kyrie ']>>>players['Lebron' Kobe ' ' Kyrie ']
The variable players is a list.
Get the number of list elements with the Len () function:
>>>len (players)
3
An index is used to access the elements of each position in the list, starting at 0, and getting the last element can be indexed by-1.
Slice:
>>>players = ['Lebron','Kobe','Kyrie','Dwyane']>>>players[1:3]#Remove the number between the Mark 1-3, including 1, not including 3['Kobe','Kyrie']>>>PLAYERS[1:-1]#Remove the value labeled 1 to-1, excluding -1['Kobe','Kyrie']>>>players[:3]#starting from scratch, 0 can be ignored['Lebron','Kobe','Kyrie']>>>players[2:]#take from the last start['Kyrie','Dwyane']>>>players[0:0:2]#the next 2 represents every other element to take a['Lebron','Kyrie']>>>PLAYERS[::2]#same as the previous sentence.['Lebron','Kyrie']
Additional:
Players.append ()
Insert:
>>>players = ['Lebron','Kobe','Kyrie','Dwyane','Chris']>>>players.insert (2,'Kevin')>>>players['Lebron','Kobe','Kevin','Kyrie','Dwyane','Chris']
View Code
Modify:
players[2]= ' SC '
Delete:
>>>del players[2]>>>players.remove ('Kobe'# Delete the specified element # Delete the last value of the list
Extended:
>>>players['Lebron','Kobe','Kyrie','Dwyane']>>>b = [1, 2, 3]>>>Players.extend (b)>>>players['Lebron','Kobe','Kyrie','Dwyane', 1, 2, 3]
Copy:
P_copy = Players.copy ()
Follow-up supplement
Statistics:players.count (' Lebron ')
Sort:players.sort () #Python3 different types cannot be sorted together
Invert:players.reverse ()
get subscript:Players.index ()
Tuples: Once created, cannot be modified.
Players = (' Lebron ', ' Kobe ', Kyrie ')
There are only two methods: Count,index
Two. String
Method:
Name.capitalize () #首字母大写
Name.casefold () #大写全部变小写
Name.center ("-") #输出 '----------Lebron----------'
Name.count (' a ') #统计a出现的次数
Name.encode () #将字符串编码成byte格式
Name.endswith ("a") #判断字符串是否以a结尾
"Bryce\tyang". Expendtabs (10) Output ' Bryce Yang ', convert \ t to spaces
Name.find (' a ') #查找a, found to return its index, could not find return-1
Format
>>>stu = "My name is {}, age is {}"
>>>stu.format ("Allen", 18)
' My name is Allen,age is 18 '
>>>stu = "My name is {1}, age is {0}"
>>>stu.format ("Allen", 18)
' My name is ' and ' is Allen '
>>>stu = "My name is {name} ', age was {age}"
>>>stu.format (age=18, name= "Allen")
' My name is Allen,age is 18 '
Format_map
>>>stu.format_map ({' name ': ' Allen ', ' Age ': 18})
' My name is Allen,age is 18 '
Names.isalnum () #字符串中是否只包含字母和数字
Names.isalpha () #是否只包含字母, kanji
#是否是数字
Names.isdecimal ()
Names.isdigit () #三种的区别
Names.isnumeric ()
#将字符串中的每一个元素按照指定分隔符进行拼接
Test = "everything"
v = "_". Join (Test)
Names.lower () #转为小写
Names.upper () #转为大写
Names.lstrip () #去除左空白
Names.rstrip () #去除右空白
Names.strip () #去除左右空白
Three. Dictionaries, collections
The dictionary uses a key-value (Key-value) store, which has a very fast lookup speed.
Grammar:
>>>d = {'Michael'Bob'Tracy ': >>>d['Michael']95
Compared to list, Dict has the following features:
1. The search and insertion speed is very fast and will not slow with the increase of key;
2. It takes a lot of memory and a lot of wasted memory.
The key in the dictionary is an immutable object, so strings, integers, and so on can be key, and list is mutable and cannot be a key.
Dictionaries are unordered.
Collection (SET):
>>>s = set ([+])
>>>s
{A-i}
Set can be regarded as a set of mathematical unordered and non-repeating elements, so that two sets can do the intersection and set of mathematical meanings.
Four. Conditional judgment and circulation
If...else
Age =If age>18: print('your-is ', age) Print('adult')else: Print(' YourAge was', age) print(' teenager ')
Elif is an abbreviation for else if
if< condition judgment;:
< execution >
elif< condition judgment 2>:
< execution >
elif< condition judgment 3>:
< Executive 3>
Else
< Executive 4>
For loop:
names = ['Michael'Bob'Tracy ' ] for in names: print(name)
While loop:
sum == while n > 0: = sum+n = n-2print(sum)
Break exits the loop prematurely:
n = 1 while n<=100: if n >10: break Print(n) = n+1print('END')
Continue skips the current loop and starts the next loop directly:
n = 0 while n<10: = n+1 if n%2 = =0 :Continue Print(n)
Python Learning notes (ii)