一 优化
username='alex'
password=‘alex123’
可以写成
username,password =‘alex’,'alex123'
二.再次优化
for i in rang(10):
if i>3:break #后面没有写内容 可以这么加
pass #代表占位符 这个条件成立,不做任何事情
三 定义常量
全是大写
MYSQL_CONNETCTION=192.168.1.1 #说明是常量 就要大写
四 安装模块命令
pip installl 模块
pip uninstalll 模块
五 模块一般搁在哪个位置
找一个叫site-packages
找不到site-packages 找dist-packages
六布尔值
大写 TRUE FALSE
0==FALES
1==TRUE
name=['a','b','c']
print ('a' in name) #TRUE
print('bc' in name) #FALES
if a in name:
print("right")
name='a lex'
print(''in name)
七 万恶的字符串拼接
name ='alex'
print("my name" + name + "and you ?"
八 列表
列表的组成a = ["alex","abc",9,"zj",6,7,8 ] alex abc 叫做元素 用逗号隔开
ab =9 #可以存入一个变量
列表a = ["alex","abc",9,"zj",6,7,8,ab ] #但是打出来确实 值
print(a)
=["alex","abc",9,"zj",6,7,8]
0 1 2
-3 -2 -1
从列表里取值
a[1]= "abc"
要想取出 abc 和9 要用作切片
a[1:3]="abc" 9 []虎头蛇尾
a[-3:]=6,7,8
列表里替换值
a= ["alex","abc",9,"zj",6,7,8 ]
a[1]="abcd"
a= ["alex","abcd",9,"zj",6,7,8 ]
列表里插入
a=["alex","abc",9,"zj",6,7,8 ]
a.insert(2,"zj")
a=["alex","abc","zj",9,"zj",6,7,8 ]
列表里加入
a=["alex","abc","zj",9,"zj",6,7,8 ]
a.append("alex")
a=["alex","abc","zj",9,"zj",6,7,8 ,'alex'] #添加到最后
列表里删除
a=["alex","abc","zj",9,"zj",6,7,8 ,'alex']
a.remove("abc")
name=['a','b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
name.pop(0)
print(name)
name=[b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
练习
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:zj
name=['a','b','c','d','e','f','g','o']
name.insert(2,"abc",)
name.insert(3,"abcd")
#print(name[3:])
#name.remove('g')
#print(name)
#del name[2:4]
print(name)
#name[2]='abc(组长)'
print(name[0:9:2])
print(name)
九 查看列表元素有几个count和所在的位置index
name1=['a','b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
if 3 in name1:
count1= name1.count(3) #count 代表列表
weizhi=name1.index(3) #index 代表第几个位置
print(count1)
print(weizhi)
name1[weizhi]=999999999999999999999999999999999999999999999999999999
print(name1)
print("3[%s]is/are in name"%count1) 思考:
name1=['a','b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
for i in range(name1.count(3)):
weizhi=name1.index(3)
name1[weizhi]=99999999999999
print(name1)
十 列表清空 name1=['a','b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
name2=name1.clear()# clear 代表把列表删除
print (name2)
十一 添加小列表
name=['a','b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
name1=['zhangsan','lisi',3,3,3,3,]
name.extend(name1) #extend 代表扩展添加小列表
print (name) 十二 小列表 排序
name=['a','b','c','d','e','f','g','o',3,3,3,4,5,6,7,45,43,3,2,34,1,23,3,3,3,]
name.reverse()
print(name)
name.soft() # 2.7试用
print(name)
十三 浅拷贝 和深拷贝
浅拷贝 :只拷贝第一层 而列表中的列表确不拷贝 列表中的[9,4,3,5]指的内存 内存改 随之也该(相当软连接)
深拷贝:完全克隆(硬链接) name3=copy.deepcopy(name)
练习
name=['a','b','c','d','e','f','g','o',[9,9,9,4,],5,6,7,45,43,9,2,34,9,23,3,3,3,]
for i in range(name.count(9)):
weizhi=name.index(9)
name[weizhi]=999999999999999 print(name)
name=['a','b','c','d','e','f','g','o',[9,9,9,4,],5,6,7,45,43,9,34,34,9,23,34,34,34,]
for i in range(name.count(34)):
weizhi=name.index(34)
name.pop(weizhi) print(name) 十四 列表的长度
name=['a','b','c','d','e','f','g','o',[9,9,9,4,],5,6,7,45,43,9,34,34,9,23,34,34,34,]
#for i in range(name.count(34)):
# weizhi=name.index(34)
# name.pop(weizhi) print(len(name)) 十五 元组
r = (1,2,3,4) 只能索引 数 读 不能修改和添加 十六 字符串
username='abc'
a=input("your name" )
if a.strip()==username: .stip()去掉前后空格
print("ok")
name='alex','zj','jz'
print(name)
name1="alex,zj,jz"
print(name1)
name2=name1.split(",")
print(name2)
('alex', 'zj', 'jz')
alex,zj,jz
['alex', 'zj', 'jz']
name1="alex,zj,jz" name2=name1.split(",") # 出来是列表
print(name2)
name3=("/".join(name2)) # /分开
print(name3)
['alex', 'zj', 'jz']
alex/zj/jz
字符串 首字母大写
name='a lex'
name1=name.capitalize()
print(name1) 字符串添加变量
mes="hello {name} {age}"
mes1=mes.format(name='zj',age=33) #format()
print(mes1)
mes="hell{0}agee{1}"
mes1=mes.format('alex',33)
print(mes1)
字符串添加引用
name='alexalex'
name1=name.center(100,'*')
print(name1)
**********************************************alexalex**********************************************
字符串找索引
name='alexalex'
name1=name.find('x')
print(name1) 结果
3 字符串输入是否是数字
age=input("your age:")
if age.isdigit(): #isdigit 代表输入是不是数字
age=int(age)
else:
print("no") 字符串判断是否有特殊字符 结果是FALSE
name='3a$df'
name1=name.isalnum()
print(name1)
字符串判断是否是以字母结尾的 结果是TRUE
name='3a$df'
name1=name.endswith('df')
print(name1)
字符串判断是否以字母开头的 结果是FALSE
name='3a$df'
name1=name.startswith('df')
print(name1)
让字符串大写 让字符串小写
name='3a$df'
name1=name.upper()
print(name1)
name2=name.lower()
print(name2) 字典 {key:value} 架构
id_db= {
110102198107011111:{
'name':'alex',
'age':22,
'addr':'shandong'
},
1020239203902930239:{
'name':'abc',
'age':24,
'addr':'dongbei'
},
110102198107011110 :{
'name':'zj',
'age':23,
'addr':'beijing'
}
}
#print(id_db) #打印字典
#print(id_db[110102198107011110])
#id_db[110102198107011110]['name']='zhang'#修改值
#id_db[110102198107011111]['wife']='jjjjjj'#创建一个新的值
#del id_db[110102198107011111]['age'] #删除
#id_db[110102198107011111].pop('age')#删除
#a=id_db.get(110102198107011111)#获取key 值
#print(a) #dict2 ={'zjjdsjsjj':29399239239,
# 110102198107011110 :{
# 'name':'zj',
# 'age':23,
# 'addr':'beikikikiki'
# }
# }
#id_db.update(dict2) #更新字典 存在相同的key 会覆盖 没有key 会增加
#print(id_db.items())#把字典改成列表
#print(id_db.keys())#把所有的key 打印出来
#print(id_db.values())#把所有的value打印出来
#print(id_db)
#110102198107011110 in id_db#判断字典里有没有key
#print('aaa')#打印aa #print(id_db.setdefault(110102198107011110))##代表在id_db中是否有key ,有的话打印出来
#print(id_db.setdefault(1101021981070111101,'hahah'))##代表在id_db中没有key的话,生出新的key 和新的值
#print(id_db.fromkeys([1,2,3,4],'ddddd'))#列表里的key {1: 'ddddd', 2: 'ddddd', 3: 'ddddd', 4: 'ddddd'}
#print(id)
for key in id_db:# 代表
print(key,id_db[key])#代表取出的key 和值