One, #布尔类型: true False
If true:
Print (' true ')
If False:
Print (' false ')
# 0 is true, non-empty is true
While 0:
Print (' Hello ')
A= "
While a:
Print (' SDD ')
Second, some of the operation of the list
1. Take the value
names=[' g ', ' P ', ' po ']
Print (names[1]) #取第二个位置
list=[1,2, ' A ', ' B ', [1,2,[' K ', 4]]
#list下标取值, subscript starting from 0
Slice value: Gu Tou regardless of tail; slice colon before the value, omit not to write annoying words to go ahead all values
After the slice colon becomes the value, omit does not write, means to go back all values; write only a colon, go to the entire list
Write only a colon, indicating that all values can be used to copy a new list;
Step default does not write, is 1, write on the steps to take the value of several elements, the step is negative, from the end of the value, a positive value, from the beginning
#切片下标的操作同样适用于字符串
Print (Names[1:3]) #取第2, 3 position, left [right]
Print (Names[:2]) before #取2
Print (Names[1:-1]) #从末尾取值, second position
2. Force shaping to convert int (); List ()
Num_list=ListRange0,21))
Print (Num_list[:11:2]) #没个两个取值
Print (Num_list[:11:-2]) #从末尾每隔两个取值
3. Increase
Num_list.append (' SSD ') #末尾插入
Num_list.insert (1, ' EE ') # Insert at specified position
4. Change
Num_list[1]=4
Print (num_list)
5. By deleting
Del Num_list[1] #指定位置删除
Num_list.pop () #默认从末尾开始删除
Num_list.pop (2) #指定位置下标删除
Num_list.remove (one) #删除指定的值
Num_list.clear () #清空
Print (num_list)
6. Built-in methods
Print (Num_list.index (9)) # Gets the subscript for the specified element
Print (Num_list.count (0)) # Gets the number of occurrences of the specified element
Num_list2=[' J ']
Num_list.extend (NUM_LIST2) #合并两个列表
Print (num_list)
Num_list.sort () #排序 increased
Num_list.sort (reverse=true) #降序
Num_list.reverse () #反转数组
Iii. some built-in methods of strings
Name= ' Lily,lucy,hanhan '
Print (Name.count (' n ')) #获取n出现的次数
Print (Name.index (' n ')) #获取元素第一次出现的位置
Print (Name.capitalize ()) #首字母大写
Print (Name.casefold ()) #首字母小写
Print (Name,center, ' * ') #20是制定的字符串长度, * The middle is your string
Name.encode () #解码, default is Utf-8, decoded into binary
Name.decode () #编码, Bytes has decode method
Name.endswith ('. com ') #查找以xx结尾的字符串
Name.find (' ly ') #查找字符串下标, and the difference between index is that find cannot find the string return-1, and index will error
Print (' Welcome {name} ' login '. Format (name= ' Jhon ')) #格式化字符串
name.isdigit# judgment is not an integer
name.islower# determine if the string is all lowercase
Name.isupper# Judging whether it's all uppercase
stu_name-[' HDHD ', ' JDD ', ' DDH '
Adr= ' Xuhui District, Shanghai '
Print (', '. Join (stu_name)) #把stu_name里的字符串以逗号链接
Print (Adr.strip) #去掉两头的xx, the default is not to write the word is to remove the space
Print (Adr.lstrip) #去掉开头的xx, the default is not to write a space
Print (Adr.replace (' Xuhui ', ' Jingan)) #静安区替换徐汇区
Print (Adr.startswith (' a ')) #判断四否已a开头
Print (Adr.endswith ()) #判断是否意xx结尾
another. Split (', ') #以xx来分割字符串, returns a list
. Splitlines () #以换行符来分割字符串, the return is a list
Common methods of operation are:. Count,.find,.join,.encode,.decode,.endswith,.startswith,.isdigit,.replace,.split,.splitlines
#元祖, the value is immutable, only the Count, index method
Mysql_config= (' 127.0.0.1 ',' 3306 ',' root ',' 123 ')
Print (Mysql_config.count (' 3306 '))#获取出现次数
Print (Mysql_config.index (' 3306 '))#获取位置
#字典
Names= {' Lidy ':[20,' Shanghai '],' Li Li ':[23,' Beijing ']}
#查
Print (names[' Lidy '])
Print (Names.get (' DKDKD '))#取值不在的话返回false, if the above query is straight, it is wrong to return.
Print (Names.keys ())#返回所有key
Print (Names.values ())Value in #返回所有key '
Names.setdefault (23, Beijing") #如果set的这个key存在的话那么
Do not move the original value, if not present will add
#增
names[' Kiki ']=[26, ' Shanghai '] # The original value exists is modified, the value does not exist is increased
#删
del names[' Lidy ']
Names.pop (' ddd ') #推荐使用
Names.popitem () # Randomly delete a key
for K,v in Names.items (): #items是把字典转成一个list
print (k,v) #获取字典中的key值和values对应得值
for K in names:
print (K,names[k]) #获取字典中的key和values对应得值, it is recommended to use
< Span style= "COLOR: #75715e" > < Span style= "COLOR: #75715e" >
Python Fundamentals 2