版权声明:如有引用,请附上本文链接 https://blog.csdn.net/weixin_41374099/article/details/89061644
**
前言
**
随便乱学一通。学™一天。
(看博客可以先看目录,这样也算体谅我每次辛苦安排标题格式了)
简单数据类型
整型int
print('----------------hello world----------------')
print(5+3) #8
print(4.55-2.333) #error ---- almost 2.217
print(3*7) #21
print(3/4) #0.75 单斜杠求商
print(3//4) #0 双斜杠求商的整数部分
print(17 % 3) #2 求余
print(2**5) #32 求乘方
print(int(2.333)) #2 将float类型数据转变为int类型
print(int('123')) #123 将字符串型数据转变为int类型
#print(int('123a')) 非法
print(int((1+2j).real)) #1 将复数的实部分装变为int类型
print(int((1+2j).imag)) #2 将复数的虚部分转变为int类型
print(int(True)) #1 将bool类型数据转变为int类型
print(ord('a'))
print(chr(65))
解释结果:
----------------hello world----------------
8
2.2169999999999996
21
0.75
0
2
32
2
123
1
2
1
97
65
字符串str
字符串可以是下面几种形式:
'aaa'
'111'
'''aaa111'''
"""aaa111"""
另外,字符串后两种形式可以用作多行注释,比如下面代码后面的注释。
print('----------------hello world----------------')
mystr='i love you very much, I think of her a lot.TOO BADLY'
print('\nsource string is : ',mystr
,'\nswapcase demo : ',mystr.swapcase() #小写变大写,大写变小写
,'\nupper demo : ',mystr.upper() #全变大写
,'\nlower demo : ',mystr.lower() #全变小写
,'\ntitle demo : ',mystr.title() #每个单词都大写首字母
,'\nistitle demo : ',mystr.istitle()
,'\nislower demo : ',mystr.islower()
,'\nisupper demo : ',mystr.isupper()
,'\ncapitalize demo : ',mystr.capitalize() #只把第一个单词的首字母大写,其他的单词首字母都小写
,'\nfind demo : ',mystr.find('u') #按照开头字符是0的顺序来查找
,'\ncount demo : ',mystr.count('o')
,'\nsplit demo : ',mystr.split(' ') #用' '做分离标志,来把单词分开
,'\njoin demo : ','5'.join('abcde') #联合
,'\nlength demo : ',len(mystr))
'''这是注释'''
"""这也是注释"""
解释结果:
----------------hello world----------------
source string is : i love you very much, I think of her a lot.TOO BADLY
swapcase demo : I LOVE YOU VERY MUCH, i THINK OF HER A LOT.too badly
upper demo : I LOVE YOU VERY MUCH, I THINK OF HER A LOT.TOO BADLY
lower demo : i love you very much, i think of her a lot.too badly
title demo : I Love You Very Much, I Think Of Her A Lot.Too Badly
istitle demo : False
islower demo : False
isupper demo : False
capitalize demo : I love you very much, i think of her a lot.too badly
find demo : 9
count demo : 4
split demo : ['i', 'love', 'you', 'very', 'much,', 'I', 'think', 'of', 'her', 'a', 'lot.TOO', 'BADLY']
join demo : a5b5c5d5e
length demo : 52
浮点型float
print('----------------hello world----------------')
print(2.333)
print(4.55-2.333)
print(float(3.33))
print(float('123'))
print(float(False))
print(float((1+8j).imag))
解释结果:
----------------hello world----------------
2.333
2.2169999999999996
3.33
123.0
0.0
8.0
布尔型bool
这个bool类型虽然也是用整型1当True,0当False,但是在和整型一起用的时候还是很严格的。(processing也是这样)
比如返回True/False的函数我用1或者0来接它就不行,要想当做整型来用需要转变类型。
print('----------------hello world------------')
print(3>5)
print(3<5)
def getBoolean(a,b):
if a==b :
return True
return False
a=getBoolean(3,3)
if 1 is a:
print('yes')
elif 0 is a:
print('no')
elif True is a:
print("YES")
elif False is a:
print('NO')
解释结果:
----------------hello world------------
False
True
YES
复数型complex
print('----------------hello world----------------')
f=3j;
print(f)
f+=2
print(f)
f+=2+3.33j
print(f)
print(complex(12))
print(complex(12.33))
print(complex(True))
解释结果:
----------------hello world----------------
3j
(2+3j)
(4+6.33j)
(12+0j)
(12.33+0j)
(1+0j)
复杂数据类型
集合set { }
- 差集
- 交集
- 并集
- 对称差集
print('----------------hello world----------------')
student={'Tom','Jim','Mary','Tom','Jack','Rose'}
print(student)
if 'Rose' in student:
print('Rose 在集合中')
else :
print('Rose 不在集合中')
a=set('abcdef')
b=set('abcxyz')
#difference(差集) 元素在a中,不在b中
print('difference : ',a-b)
print('difference : ',a.difference(b))
#intersection(交集) 元素同时在a,b中
print('intersection : ',a & b)
print('intersection : ',a.intersection(b))
#union(并集) 元素要么在a中,要么在b中
print('union : ',a|b)
print('union : ',a.union(b))
#symmetric difference(对称差集) 元素不在a中,或者元素不在b中
print('symmetric difference : ',a ^ b)
print('symmetric difference : ',a.symmetric_difference(b))
#print(a+b) 非法
解释结果:
----------------hello world----------------
{'Jack', 'Tom', 'Rose', 'Jim', 'Mary'}
Rose 在集合中
difference : {'d', 'f', 'e'}
difference : {'d', 'f', 'e'}
intersection : {'a', 'c', 'b'}
intersection : {'a', 'c', 'b'}
union : {'c', 'd', 'f', 'a', 'b', 'y', 'e', 'x', 'z'}
union : {'c', 'd', 'f', 'a', 'b', 'y', 'e', 'x', 'z'}
symmetric difference : {'d', 'y', 'f', 'e', 'x', 'z'}
symmetric difference : {'d', 'y', 'f', 'e', 'x', 'z'}
列表list [ ]
列表基础操作。
- 创建列表
- 删除元素
- 弹出元素
- 添加元素
- 排序列表
- 倒序列表
print('----------------hello world----------------')
list1=['Google','baidu',1997,2000]
list2=[1,2,3,4,5,6,7]
print("list1[0] : ",list1[0]) #Google
print('list2[1:5] : ',list2[1:5]) #2345
car=['a奥迪','宝马','奔驰','雷克萨斯','桑塔纳','奥迪双钻']
message='我的人生中的第一辆车是 : '+car[0].title() + "....."
print(message)
car.insert(0,'凯迪拉克') #在0的位置插入一个元素
print(car)
car.append('五菱宏光') #在末尾插入一个元素
print(car)
del car[2] #把位置2的元素删除
print(car)
a=car.pop(1) #把位置为1的元素弹出
print(car)
print(a)
car.remove('奥迪双钻') #删除元素‘奥迪双钻’
print(car)
print('------------------------------------------')
car.sort() #乱序
print(car)
car=['kaidilake','benchi','leikesasi','sangtana','wulinghongguang']
car.sort() #按照字母顺序排序
print(car)
car.sort(reverse=True) #按照字母顺序倒序排序
print(car)
print('开始的顺序:',car)
print(sorted(car)) #用sorted()排序
print(sorted(car,reverse=True)) #用sorted()排序
car=['benchi','baoma','aodi','leikesasi']
car.reverse() #这个才是真正的倒序,在原字符串基础上操作
print(car)
car=[i for i in range(14)] #用循环创建列表
print(car)
print(len(car))
car1= list(range(10)) #用list成员函数来创建列表
print(car1)
print(len(car1))
解释结果:
----------------hello world----------------
list1[0] : Google
list2[1:5] : [2, 3, 4, 5]
我的人生中的第一辆车是 : A奥迪.....
['凯迪拉克', 'a奥迪', '宝马', '奔驰', '雷克萨斯', '桑塔纳', '奥迪双钻']
['凯迪拉克', 'a奥迪', '宝马', '奔驰', '雷克萨斯', '桑塔纳', '奥迪双钻', '五菱宏光']
['凯迪拉克', 'a奥迪', '奔驰', '雷克萨斯', '桑塔纳', '奥迪双钻', '五菱宏光']
['凯迪拉克', '奔驰', '雷克萨斯', '桑塔纳', '奥迪双钻', '五菱宏光']
a奥迪
['凯迪拉克', '奔驰', '雷克萨斯', '桑塔纳', '五菱宏光']
------------------------------------------
['五菱宏光', '凯迪拉克', '奔驰', '桑塔纳', '雷克萨斯']
['benchi', 'kaidilake', 'leikesasi', 'sangtana', 'wulinghongguang']
['wulinghongguang', 'sangtana', 'leikesasi', 'kaidilake', 'benchi']
开始的顺序: ['wulinghongguang', 'sangtana', 'leikesasi', 'kaidilake', 'benchi']
['benchi', 'kaidilake', 'leikesasi', 'sangtana', 'wulinghongguang']
['wulinghongguang', 'sangtana', 'leikesasi', 'kaidilake', 'benchi']
['leikesasi', 'aodi', 'baoma', 'benchi']
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
14
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10
列表高级操作
- 列表之间的截取和拼接
- 获取列表中极值
- 获取列表中元素索引
- 统计某元素出现次数
- 清空列表
print('----------------hello world----------------')
mylist=['Google','Apple','Taobao']
print(mylist[2]) #位置为2的元素
print(mylist[-2]) #倒数第二个元素
print(mylist[1:]) #位置为1的元素及后面的元素
squares=[1,4,9,16,32]
squares +=mylist #列表拼接
print(squares)
a=['a','b','c']
n=[1,2,3]
x=[a,n] #创建嵌套列表
print(x)
print(x[0]) #访问位置为0的列表
print(x[0][1]) #访问位置为0的列表的位置为1的元素
a,b=['Google','Apple','Taobao','Microsoft','Amazon'],[456,700,200,5,6]
#比较字母大小,Z~A从大到小 如果第一个字母相同就比较第二个.....
print('列表a中最大值是:',max(a),'最小值是:',min(a),'\n列表b中最大值是:',max(b),'最小值是:',min(b))
a.extend(b)
print('扩展后的列表a : ',a)
print('列表a中5出现的次数是:',a.count(5))
a.clear()
print(a) #打印了空列表
a=b.copy()
print('复制b的元素到a:',a)
print('5的索引值是:',a.index(5))
解释结果:
----------------hello world----------------
Taobao
Apple
['Apple', 'Taobao']
[1, 4, 9, 16, 32, 'Google', 'Apple', 'Taobao']
[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b
列表a中最大值是: Taobao 最小值是: Amazon
列表b中最大值是: 700 最小值是: 5
扩展后的列表a : ['Google', 'Apple', 'Taobao', 'Microsoft', 'Amazon', 456, 700, 200, 5, 6]
列表a中5出现的次数是: 1
[]
复制b的元素到a: [456, 700, 200, 5, 6]
5的索引值是: 3
元组tuple ( ,)
print('----------------hello world----------------')
tpl1=('Google','toppr',1997,2000)
tpl2=(1,2,3,4,5,6)
print(tpl1[0])
print(tpl2[1:5])
tpl3=tpl1+tpl2
print(tpl3,'tpl3长度是:',len(tpl3))
del tpl3
#print('tpl1最大值是:',max(tpl1),'最小值是:',min(tpl1)) 非法
print('tpl2最大值是:',max(tpl2),'最小值是:',min(tpl2))
a=list(range(0,10))
print(a)
b=tuple(a)
print(b)
解释结果:
----------------hello world----------------
Google
(2, 3, 4, 5)
('Google', 'toppr', 1997, 2000, 1, 2, 3, 4, 5, 6) tpl3长度是: 10
tpl2最大值是: 6 最小值是: 1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
字典dict { ’ ’ :XXX}
dict简单操作
print('----------------hello world----------------')
mydict={'数学':'99','语文':'99','英语':'99'}
print('语文成绩是:',mydict['语文'],'数学成绩是:',mydict['数学'],'英语成绩是:',mydict['英语'])
#print('语文成绩是:',mydict[0],'数学成绩是:',mydict[1],'英语成绩是:',mydict[2]) 非法
mydict['物理']='100' #添加数据
print(mydict)
mydict['数学']='100' #修改数据
print(mydict)
del mydict['语文'] #删除元素
print(mydict)
mydict={}
print(mydict)
mydict={'Name':'Topper','Age':7,'Class':'First'}
print(len(mydict))
print(str(mydict))
print(mydict)
print(type(mydict))
user={'网名':'浪潮之巅','外号':'浪潮第一帅','职业':'职业公会人'}
print(user)
print(user.items())
for key,value in user.items():
print('Key : ',key,'Value : ',value)
for key in user.keys():
print('Key:',key)
for value in user.values():
print('value:',value)
解释结果:
----------------hello world----------------
语文成绩是: 99 数学成绩是: 99 英语成绩是: 99
{'数学': '99', '语文': '99', '英语': '99', '物理': '100'}
{'数学': '100', '语文': '99', '英语': '99', '物理': '100'}
{'数学': '100', '英语': '99', '物理': '100'}
{}
3
{'Name': 'Topper', 'Age': 7, 'Class': 'First'}
{'Name': 'Topper', 'Age': 7, 'Class': 'First'}
<class 'dict'>
{'网名': '浪潮之巅', '外号': '浪潮第一帅', '职业': '职业公会人'}
dict_items([('网名', '浪潮之巅'), ('外号', '浪潮第一帅'), ('职业', '职业公会人')])
Key : 网名 Value : 浪潮之巅
Key : 外号 Value : 浪潮第一帅
Key : 职业 Value : 职业公会人
Key: 网名
Key: 外号
Key: 职业
value: 浪潮之巅
value: 浪潮第一帅
value: 职业公会人
dict高级操作
print('----------------hello world----------------')
student1={'数学':'100','语文':'98','英语':'100'}
student2={'数学':'99','语文':'78','英语':'70'}
student3={'数学':'50','语文':'99','英语':'10'}
students=[student1,student2,student3]
for name in students:
print(name)
#将字典加入列表
students=[]
for num in range(0,30):
new_student = {'数学':'60','语文':'60','英语':'60'}
students.append(new_student)
for num in students[0:5]:
print(num)
#字典内嵌套字典
users={'交通小王':{'初级密码':'666','中级密码':'888','电话':'150XXXXXXXX'},
'公交老郑':{'初级密码':'123','中级密码':'456','电话':'890-122-233'}
}
for name,info in users.items():
print('用户名:',name,'密码组合:',info['初级密码']+info['中级密码'],'\t电话号码:',info['电话'].title())
#将列表中加入字典
noodle={'型号':'拉面','配料':['多加10块钱的肉','多放辣油']}
print('你点了一份'+noodle['型号']+'.你要的是'+noodle['配料'][1]+'对吧')
解释结果:
----------------hello world----------------
{'数学': '100', '语文': '98', '英语': '100'}
{'数学': '99', '语文': '78', '英语': '70'}
{'数学': '50', '语文': '99', '英语': '10'}
{'数学': '60', '语文': '60', '英语': '60'}
{'数学': '60', '语文': '60', '英语': '60'}
{'数学': '60', '语文': '60', '英语': '60'}
{'数学': '60', '语文': '60', '英语': '60'}
{'数学': '60', '语文': '60', '英语': '60'}
用户名: 交通小王 密码组合: 666888 电话号码: 150Xxxxxxxx
用户名: 公交老郑 密码组合: 123456 电话号码: 890-122-233
你点了一份拉面.你要的是多放辣油对吧