常量定义规则:常量意义为不可做更改;常量定义名称为全大写;
如:MYSQL_CONNECTION= '192.168.1.1'
pyc:python生成的翻译文件,使计算机能够识别python语言;
列表的语法和使用:
列表参数插入语法:name.insert(2,'minggou')
name[1] = "wangminglong" --->修改列表中对应字段的内容;
追加:name.append("alex")
删除:name.remove("minggou")
定义列表:name=["1","minghu",2,43,"minggou"]
步长:print(name[::2])每隔一位取值一次;
查询列表: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)
列表合并:
name.extend(name2)
列表反转:
name.reverse()
列表排序:
name.sort() #字符串和数字结合排序,python3会报错;单独排不会报错
输入下标删除元素,默认删除最后一个:
name.pop()
拷贝列表,只拷贝第一层,不拷贝第二层,第二层拷贝内存数据共享:
name.copy()
列表嵌套:
name = ["csd",12,"asvs",[2,3,56,7],"cdc",354]
查看列表长度:
print("name",len(name))
只读列表:
r = (1,2,3,4,5) #不想被改动的列表,只读列表,元组;
字符串常用操作:
name = "qiu ye"
username = input("name:")
if username.strip() == 'qiuye': #移除空格;
print("welcome")
字符串分割:
names = "dwqd,ascd,frer"
name2 = names.split(",")
print(name2) #变为列表
print("|".join(name2)) #按| 进行分割输出;
字符串索引:
name = "qiu ye"
print('' in name) #判断字符串中是否有空格
print(name.capitalize()) #首个字母大写
name.format() #字符串格式化
例:
msg = “hello,{name},it's the {age} last time..."
msg2 = msg.format(name='Minghu',age=333)
print(msg2)
msg2 = "hh{0},ddas{1}"
print(msg2.format("alex",33))
切片:同列表切片
name = "alex li"
print(name[2:4])
print(name.center(40,'-')) #40个字符居中打印,其他字节用-代替;
print(name.find()) #指定字段查找并输出;
判断:
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() #小写
数据运算:
type("333") is str
type(a) is list
二进制表示:
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
计算机中能表示的最小单位,是一个二进制位;
计算机中能存储的最小单位,是一个二进制位(bit)
8bit = byte(字节)
1024byte = 1 kbyte
1204kbyte = 1mbyte
1024mb =1gb
运算符:
<<右移
>>左移
例:
64>>2 == 16
64<<1 == 128
python死循环:
例:
count = 0
while True:
print("this is true",count)
count +=1
字典:
id_db ={
1248357148965: { #key值必须是唯一的,不然只输出最后的一个;
'name' : "shanpao", #name:小字典的key,“shanpao”:key的value
'age' : "24"
'addr' : 'dongbei'
},
1248357448965: {
'name' : "shanpao",
'age' : "24"
'addr' : 'dongbei'
},
124835712345: {
'name' : "shanpao",
'age' : "24"
'addr' : 'dongbei'
},
}
#字典的基本格式,存储信息更灵活;
给key赋新值:
id_db[1248357448965]['name'] = "new value" #修改value;
id_db[1248357448965]['qq_of_wife'] = 123556675 #如果字典中没有此value,则添加新值;
print(id_db.setdefault(234254364),"hhh")) #取一个默认值,如果不存在,则赋一个新的值;
print(dict.fromkeys([1,2,34,5,6,8],'ddd') ) #将列表中每个值拿出来当做场景使用 --->一个坑!
id_db.popitem() #随机删除字典中的一个key --->建议不使用任何随机性的语言;
高效率的循环:
for key in id_db:
print(key,id_db[key])
例:购物车
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
else:
exit("Invaild data type...")
welcome_msg = "Welcome to our Shopping mall".center(50,'-')
print(welcome_msg)
exit_flag =False
product_list = [
('iphone',5888),
('mac air',8000),
('mac pro',9000),
('xiaomi',199),
('coffee',30),
('bike',800),
('cloth',200),]
shop_car = [] #定义购物车
while exit_flag is not True:
#for product_item in product_list:
# p_name,p_price = product_item
print("product list".center(50,'-'))
for item in enumerate(product_list):
index = item[0]
p_name = item[1][0]
p_price = item[1][1]
print(index,'.',p_name,p_price)
user_choice = input("[q=quit,c=check]what do you want to buy")
if user_choice.isdigit(): #肯定是选择商品
user_choice = int(user_choice)
if user_choice < len(product_list):
p_item = product_list[user_choice]
if p_item[1] <= salary: #工资够用
shop_car.append(p_item) #放入购物车
salary -= p_item[1] #减钱
print("Added \033[41;1m[%s]\033[0m into shop car,your current balance is \033[41;1m[%s]\033[0m" %(p_item,salary))
else:
print("your balance is \033[41;1m[%s]\033[0m,can not afford this goods..." % salary)
else:
if user_choice == 'q' or user_choice == 'quit':
print("purchased products as below".center(40,'-'))
for item in shop_car:
print(item)
print("END".center(40,'-'))
print("your balance is \033[41;1m[%s]\033[0m" % salary)
exit_flag = True
elif user_choice == 'c' or user_choice == 'check':
print("purchased products as below".center(40,'-'))
for item in shop_car:
print(item)
print("END".center(40,'-'))
print("your balance is \033[41;1m[%s]\033[0m" % salary)
转载于:https://blog.51cto.com/summer37/1842936