a =3
b =3.14
c =3+4jprint(type(a),type(b),type(c))#结果:<class'int'><class'float'><class'complex'>#判断变量是否是该类型print(isinstance(a,int))#结果:True
Number实例代码
import math
print(math.factorial(32))#计算32的阶乘263130836933693530167218012160000000print(0.4-0.3==0.1)#实数之间尽量避免直接比较大小Falseprint(0.4-0.3)0.10000000000000003print(math.isclose(0.4-0.3,0.1))#测试两个实数是否足够接近True
#python中复数的运算
c =3+4jprint(c+c)#复数相加 print(c.real)#复数的实部print(c.imag)#复数的虚部print(3+4j.imag)#相当于3+(4j).imag#结果:(6+8j)3.04.07.0
text ='''beautiful is better than ugly.
explicit is better than implicit
simple is better than complex
flat is better than nested
'''print(len(text))#字符串长度,即所有字符的数量print(text.count('is'))#字符串中单词is出现的次数print('beautiful'in text)#测试字符串中是否包含单词beautiful1214True
字符串操作方法:(以下“str”表示为一个字符串)
str[ ]:索引
str[ : ]:剪切
len(str):长度
str.upper():字符串中字母大写
str.lower():字符串中字母小写
str.capitalize()
str.title()
str.swapcase()
text ='beautiful is better than ugly is.'print(text.upper())#字符串中字母大写print(text.lower())#字符串中字母小写print(text.capitalize())#句首字母大写print(text.title())#每个字符首字母大写print(text.swapcase())#原来的大写变小写,小写变大写
BEAUTIFUL IS BETTER THAN UGLY IS.
beautiful is better than ugly is.
Beautiful is better than ugly is.
Beautiful Is Better Than Ugly Is.
BEAUTIFUL IS BETTER THAN UGLY IS.
text ='Beautiful is better than ugly.'print(text.split())#使用空白字符进行分割print(text.split(maxsplit =1))print(text.rsplit(maxsplit =2))print('1,2,3,4'.split(','))print(','.join(['1','2','3','4']))['Beautiful','is','better','than','ugly.']['Beautiful','is better than ugly.']['Beautiful is better','than','ugly.']['1','2','3','4']1,2,3,4
text ='python是一门非常棒的编程语言。'#replace() 方法返回替换后的新字符串,可以直接再次调用replace()方法print(text.replace('棒','优雅').replace('编程','程序设计'))print(text)#text没有发生变化,是因为字符串是不可变的,replace()是产生一个新的字符串
python是一门非常优雅的程序设计语言。
python是一门非常棒的编程语言。
for v in str:字符串迭代
index(x):从左到右依次检测x第一次出现的索引值,如果不存在,返回异常
rindex(x):从右到左依次检测x第一次出现的索引值,索引值为从左数的,如果不存在,返回异常
text ='处处飞花飞处处;声声笑语笑声声。'print(text.rindex('处'))print(text.index('声'))print(text.count('处'))684
text ='beautiful is better than ugly is.'print(text.startswith('beautiful'))print(text.startswith('Beautiful'))print(text.endswith(('.',',',':')))TrueFalseTrue
strip() 、rstrip()、lstrip():从两侧、右侧、左侧截取字符串的指定字符
text ='******=======text========###$$$$*****'print(text.strip('*'))#从两端删除*号#从两端删除=#*,参数中无需规定顺序,类似于一个字符串列表。# 右端只删除了*是因为参数中没有$,所以遇到$时候停止print(text.strip('=#*'))print(text.lstrip('*='))#从左端删除*=#从右端删除*$=,只删除了*$,到#停止是因为参数里没有#print(text.rstrip('*$='))
结果:
=======text========###$$$$
text========###$$$$
text========###$$$$***********=======text========###
Process finished with exit code 0
d ={'Michael':95,'Bob':75,'Tracy':85}print(d['Michael'])95
d ={'a':1,'b':2,'b':3}print(d){'a':1,'b':3}
创建字典的另一种方法:dict函数
items =[('name','Ruby'),('age',25)]
d =dict(items)print(d)
d =dict(name ='Ruby',age =25)print(d)#结果:{'name':'Ruby','age':25}{'name':'Ruby','age':25}
字典的常用方法:
copy():返回字典高层结构的一个拷贝,但是不复制嵌入结构,只复制对那些结构的引用。
items =[('name','Ruby'),('age',25),('sex','female')]
d =dict(items)print(d['name'])print(d.get('age','sex'))#如果字典中有age,则返回该键相对应的值,如果没有则返回get函数的第二个参数。#结果:
Ruby
25print(list(d))#返回字典的键print(list(d.values()))#返回字典的值print(list(d.items()))#返回字典的所有元素#结果:['name','age','sex']['Ruby',25,'female'][('name','Ruby'),('age',25),('sex','female')]#用for循环,返回字典的所有元素:for k,v in d.items():print(k,v,sep =' ')#结果:
name Ruby
age 25
sex female
'''
字典元素的修改,添加与删除
'''#当以指定“键”为下标给字典元素赋值时,1)若是“键”存在,则修改;2)若是不存在,则添加。
items =[('name','Ruby'),('age',25),('sex','female')]
d =dict(items)
d['name']='zhaojiaxuan'
d['address']='Nei Meng gu 'print(d){'name':'zhaojiaxuan','age':25,'sex':'female','address':'Nei Meng gu '}#用update()方法可以将另一个字典的元素一次性全部添加到当前字典,如果键相同就替换,如没有像关键就添加
items =[('name','Ruby'),('age',25),('sex','female')]
d =dict(items)
b ={'name':'zhaojiaxuan','address':'liulin'}
d.update(b)print(d){'name':'zhaojiaxuan','age':25,'sex':'female','address':'liulin'}#删除
items =[('name','Ruby'),('age',25),('sex','female')]
d =dict(items)print(d.pop('name'))#用pop()删除指定“键”对应的元素,同时返回对应的“值”print(d)
Ruby
{'age':25,'sex':'female'}# 使用popitem()删除
items =[('name','Ruby'),('age',25),('sex','female')]
d =dict(items)print(d.popitem())#删除最后一个元素,并以元组的形式返回最后一个元素的键值对print(d)('sex','female'){'name':'Ruby','age':25}#使用del函数删除
items =[('name','Ruby'),('age',25),('sex','female')]
d =dict(items)del d['name']print(d){'age':25,'sex':'female'}
s ={3,5,9,7,6}
s.add(2)#给集合增加一个元素2print(s)
s ={3,5,9,7,6}
d ={8,2,9}
s.update(d)#将集合d更新到集合s中print(s)
s.pop()#随机删除一个元素print(s)
s.remove(5)#删除5print(s)
s.discard(2)#删除一个不存在的元素,不会报错print(s)#结果:{2,3,5,6,7,9}{2,3,5,6,7,8,9}{3,5,6,7,8,9}{3,6,7,8,9}{3,6,7,8,9}
综合示例
list=[1,2,3]tuple=(1,2,3)dict={'a':97,'b':98,'c':88}set={1,2,3}print(dict['a'])#字典的下标是“键”,输出为对应“键”的“值”print(tuple.index(2))#查看元组中元素2首次出现的位置#结果:971#查看字典中那些“键”对应的“值”为98for key,value indict.items():#items为元素if value ==98:print(key)#结果:
b
map函数
s =[]for i inrange(5):
s.append(i)print(s)
a =map(str,s)#map函数就是把转换函数,将s中元素从int转换为strprint('1'in a)#结果:[0,1,2,3,4]True