Constant definition rule: constant meaning is not changed; Constant definition name is all uppercase;
such as: mysql_connection= ' 192.168.1.1 '
Pyc:python generated translation files that enable the computer to recognize the Python language;
Syntax and use of the list:
List parameter insertion syntax: Name.insert (2, ' Minggou ')
NAME[1] = "Wangminglong"---> Modify the contents of the corresponding field in the list;
Append: Name.append ("Alex")
Delete: Name.remove ("Minggou")
Definition list: name=["1", "Minghu", 2,43, "Minggou"]
Step: Print (Name[::2]) every other place value once;
Query list: which in name
If 9 in Name:
Num_of_ele = Name.count (9)
Position_of_ele = Name.index (9)
Name[position_of_ele] = 999
Print ("[%s] 9 is in name,postion:[%s]"% (Num_of_ele,position_of_ele))
Print (name)
For I in Range (Name.count (9)): #修改所有9
Ele_index = Name.index (9)
Name[ele_index] = 9999999
Print (name)
List Merge:
Name.extend (name2)
List inversion:
Name.reverse ()
List sort:
Name.sort () #字符串和数字结合排序, Python3 will be an error, separate row will not error
Enter the subscript to delete the element and delete the last one by default:
Name.pop ()
Copy the list, copy only the first layer, do not copy the second layer, the second layer copies the memory data sharing:
Name.copy ()
List nesting:
name = ["CSD", "Asvs", [2,3,56,7], "CDC", 354]
View List Length:
Print ("Name", Len (name))
Read-only list:
R = (1,2,3,4,5) #不想被改动的列表, read-only list, tuple;
String Common operations:
name = "Qiu Ye"
Username = input ("Name:")
If Username.strip () = = ' Qiuye ': #移除空格;
Print ("Welcome")
String segmentation:
names = "Dwqd,ascd,frer"
name2 = Names.split (",")
Print (name2) #变为列表
Print ("|". Join (name2)) #按 | to split the output;
String index:
name = "Qiu Ye"
Print ("in name) #判断字符串中是否有空格
Print (Name.capitalize ()) #首个字母大写
Name.format () #字符串格式化
Cases:
msg = "Hello,{name},it ' The {age} last time ..."
MSG2 = Msg.format (name= ' Minghu ', age=333)
Print (MSG2)
MSG2 = "Hh{0},ddas{1}"
Print (Msg2.format ("Alex", 33))
Slices: Slice with same list
name = "Alex Li"
Print (Name[2:4])
Print (Name.center, '-') #40个字符居中打印, other bytes used-instead;
Print (Name.find ()) #指定字段查找并输出;
Judge:
age = Int (input ("Your Age:"))
If Age.isdigit (): #判断是否为数字
age = Int (age)
Else
Print ("Invalid data type")
Name.isalnum () #不能包含特殊字符
Name.endswith () #判断结束
Name.startswith () #判断开始
Name.upper () #大写
Name.lower () #小写
Data operations:
Type ("333") is Str
Type (a) is List
Binary representation:
256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1
The smallest unit that can be represented in a computer is a bits;
The smallest unit that can be stored in a computer is a bits (bit)
8bit = byte (bytes)
1024byte = 1 KByte
1204kbyte = 1mbyte
1024MB =1GB
Operator:
<< Right Shift
>> left Shift
Cases:
64>>2 = = 16
64<<1 = = 128
Python dead Loop:
Cases:
Count = 0
While True:
Print ("This is true", count)
Count +=1
Dictionary:
id_db ={
1248357148965: {#key值必须是唯一的, otherwise only the last one is output;
' Name ': ' Shanpao ', #name: Dictionary key, ' Shanpao ': Key's value
' Age ': ' 24 '
' Addr ': ' Dongbei '
},
1248357448965: {
' Name ': "Shanpao",
' Age ': ' 24 '
' Addr ': ' Dongbei '
},
124835712345: {
' Name ': "Shanpao",
' Age ': ' 24 '
' Addr ': ' Dongbei '
},
}
#字典的基本格式, the storage information is more flexible;
Assign a new value to key:
id_db[1248357448965][' name '] = "new value" #修改value;
id_db[1248357448965][' qq_of_wife ' = 123556675 #如果字典中没有此value, the new value is added;
Print (Id_db.setdefault (234254364), "HHH")) #取一个默认值, if not present, assigns a new value;
Print (Dict.fromkeys ([1,2,34,5,6,8], ' DDD ')) #将列表中每个值拿出来当做场景使用---> A pit!
Id_db.popitem () #随机删除字典中的一个key---> recommends not to use any random language;
High-efficiency loops:
For key in id_db:
Print (Key,id_db[key])
Example: Shopping Cart
salary = () Salary.isdigit (): salary = (Salary): () Welcome_msg = .center (,) (welcome_msg) exit_flag =product_list = [ (,), (,), (,), (,), (,), (,), (,),]shop_car = [] exit_flag : (. Center (,)) item (product_ List): index = item[] p_name = item[][] p_price = item[][] (Index,,p_name,p_price) user_choice = () user_choice.isdigit (): user_choice = (user_ Choice) user_choice < (product_list): p_item = product_list[user_choice] p_item[] <= salary: shop_car.append (P_item) salary -= p_item[] (% (p_item,salary)) : (% salary) : user_ choice == user_choice == : (. Center (,)) item shop _car: (item) (. Center (,)) (% salary) exit_ flag = user_choice == user_choice == : (. Center (,)) item shop_car: (item) (. Center (,)) (% salary)
Python data operations, dictionaries, lists