python中isnumberic_python第二天

day-2笔记:

1、is 比较的是id,==比较的是value(值)

2、变量值互换方法、中间变量

方法二:x,y=y,x

3、在比较or的级别中、如果or的左边是true、那么整个结果都是true。

4、数字类型有:int、float、复数:

重点强调复数:

x=1-2j

print(x.real)    #查看复数的实部

print(x.imag)     #查看复数的虚部

5、关于进制的换算:

bin()二进制:代表0b开头

oct()八进制:代表0o开头

hex()十六进制:代表0x开头

6、在python中有多个字符,但是只有一个值。

7、关于在python的input输入的时候又空格,不利于我们后期的取值比较:所以我们用以下方法进行设置:

a=input(’>>:’)

a.replace(' ', '')        #可以用replace(' ','')来去掉字符串里的所有空格;也可以用来替换字符串,第三个参数用来表示替换的次数:

>>> name = input('ussername:').replace('aa','')

>>> aa cc

name

' cc'

#

a.strip()     #把输入的字符串的头部和尾部的空格去掉。

a.lstrip():#把左边的空格去掉

a.rstrip():#把右边的空格去掉

len()         #打印字符串的长度

8、python字符串的切割:split

Split的用法:首先要跟切割符、然后加切割的次数。

eg.:

user_info='root:0:0:0::/root:/bin/bash'

print(user_info.split(':')[5])

/root

eg1:

user_info='root:0:0:0::/root:/bin/bash'

print(user_info.split(':',1)[0])

9、查看字符串的长度使用len()方法。

split()切割字符串操作。

字符串的切片:顾头不顾尾。

name=‘hello word’

print(name[1:3])    #取从1到3

print(name[1:3:1])   #从1到3中间隔一个字符

10、判断字符串的特性:

endswith()    #以什么字符串为结尾,返回结果为布尔值

startswith()     #以什么字符串为开头,返回值为布尔

replace()        #替换字符串

11、fromat

print('{} {} {}'.format('xm','11','male'))

print('{0} {0} {1}'.format('xm','11','male'))

print('NAME:{name} AGE:{age} SEX:{sex}'.format(name='xm',age='11',sex='male'))

12、判断是否为数字:

num='123'

print(num.isdigit())    #判读是否为数字,只能判断beyt类型和unicode类型。

num.isnumberic     #判断数字类型有中文数字、罗马数字、u类型。

13、find()   #查找字符串、如果存在返回真。

count()    #统计次数

lower()    #大小字母转小写

upper()    #将小写字母转大写

index()    #取出字符在字符串中的索引位置

14、用于存放多个不同的值。

二、列表的处理方式:

1、append()    #添加值到列表从左到右

2、pop()        #按照索引数字删除

3、remove()    #按照列表的值删除

4、len()        #统计列表元素的个数

5、in        #包含

6、clearI()    #清楚列表

7、copy()        #拷贝列表

8、count()    #统计元素的count

9、extend()    #添加多个元素到列表

10、index()    #查看元素的下标

11、reverse()    #反转

12、sort()    #对列表的元素进行排序

13、insert()    #指定位置插入

14、     队列:先进先出、

堆栈:先进后出、

15、字符串占位:

msg='hello'

a,b,c,_,e=msg

print(a,b)

h e

#

msg='hello'

a,*_,b=msg

print(a,b)

16、模拟队列:

#队列、先进先出

fifo=[]

fifo.append('fist')

fifo.append('second')

fifo.append('third')

print(fifo)

print(fifo.pop(0))

print(fifo.pop(0))

print(fifo.pop(0))

print('@@@@@@@@@@@@@@@@@@@@@@')

#insert方法模拟队列:

fifo.insert(0,'fist')

fifo.insert(0,'second')

fifo.insert(0,'third')

print(fifo)

print(fifo.pop())

print(fifo.pop())

print(fifo.pop())

#模拟堆栈

lifo=[]

fifo.append('fist')

fifo.append('second')

fifo.append('third')

print(fifo)

print(fifo.pop())

print(fifo.pop())

print(fifo.pop())

#insert方法模拟堆栈

fifo.insert(0,'fist')

fifo.insert(0,'second')

fifo.insert(0,'third')

print(fifo)

print(fifo.pop(0))

print(fifo.pop(0))

print(fifo.pop(0))

17、元组不可变、主要用于读操作、是一个不可变的列表。

元组的元素是有序的,列表也是有序的。

18、字典是可变类型。但是字典的每个元素中的key是不可变的、value是可变的,比列表更加的占内存,字典的元素是无序的。

#默认值,当值不存在的时候返回默认值。

info={'name':'xm','age':'18','sex':'male'}

requst=info.pop('asdfsadf','none')

print(requst)

none

requst=info.get('name1','none')

print(requst)

none

#显示key和value

print(info.keys())    #显示字典所有的key    #for循环遍历的时候默认遍历key

print(info.values())    #显示字典所有的value

info={'name':'xm','age':'18','sex':'male'}

for n in info.items():    #items 取完整的字典

print(n)

('name', 'xm')

('age', '18')

('sex', 'male')

for key,value in info.items():

print(key,value)

name xm

age 18

sex male

info={'name':'xm','age':'18','sex':'male'}

dic=info.fromkeys(['name','ex','age','num'],1111111)    #默认共享value

print(dic)

{'name': 1111111, 'ex': 1111111, 'age': 1111111, 'num': 1111111}

print(dict([('name','hehe'),('age','11'),('sex','male')]))    #生成字典

dic=dict.fromkeys(['name','ex','age','num'],111)    #生成字典,dict调用。还有一种方法是直接手动创建。

print(dic)

info={'name':'xm','age':'18','sex':'male'}

print(info)

dic={'a':1,'b':2,'name':'hehe'}

info.update(dic)

print(info)

# {'name': 'xm', 'age': '18', 'sex': 'male'}

# {'name': 'hehe', 'age': '18', 'sex': 'male', 'a': 1, 'b': 2}

19、集合:

python=['alex','egon','yuanhao']

linux=['alex','egon']

a=[]

for p in python:

if p in linux:

a.append(p)

print(a)

集合:又不同的元素组成、集合是无序的,可以去重,集合的元素可以是数字、字符串。

集合内的元素必须是不可变的数据类型。集合内的元素必须是唯一。

使用set可以生成集合。集合不能取单个值。主要用于做关系运算。

集合的方法暂时不记录。

s1=set('heheh')

print(s1)

{'h', 'e'}

交集是取两个集合中公共的部分、并集是取全部并取重。

s1 = {1,2,4,5}

s2 = {3,2,6,5}

#交集

print(s1 & s2)

#并集

print(s1 | s2)

#差集

print(s1 - s2)

print(s2 - s1)

#对称差集

print(s1 ^ s2)

#父集

print(s1 >= s2)

#子集

print(s2 <= s1)

小结:

按照存储值得个数:

标量/原子类型:数字、字符串

容器类型:列表、元组、字典

按照可变不可变区分:

可变:列表、字典

不可变:数字、字符串、元组

按照访问顺序区分:

直接访问

数字

顺序访问(序列类型)    字符串,列表,元组

key值访问(映射类型)    字典

20、字符编码:字符编码就是将编码从一种格式转化为另一种格式的一个标准。

将内存中的字符刷到硬盘的过程叫做编码。

将硬盘中的文件中的字符读取到内存的过程叫做解码。

内存存放字符的格式为Unicode格式。

硬盘存放字符的格式为utf-8格式

必须要记住以什么格式存放一定要以什么格式取。

python字符串默认是unicode 格式:

unicode ---> encode  ---> bytes

bytes   ---> dedcode ---> unicode

unicode 字符编码可以跨平台。

python2 字符串的默认编码就是bytes,如果要转格式需要在字符串前面加u

x=u'你吵啥'

21、文件的操作:

read()    读取文件。需要注意编码

readline()    读取一行

readlins()    读取文件的所有行,组成一个列表

#读取文件的内容

f = open('a.txt','r',encoding='utf-8')

res=f.read()

print(res)    #但是文件必须首先存在

#读取一行:

f = open('a.txt','r',encoding='utf-8')

res=f.readline()

print(res,end='')

print(res,end='')

print(res,end='')    #默认情况一行结束后会有\n,正常显示需要加end=''

#

with open('a.txt','r',encoding='utf-8') as f:

print(f.read())    #该方法不需要关闭文件。但是此时a.txt文件中如果有中文,如果不加encoding,读取会报错。

读取完毕之后记得要关闭文件。如果不想手动关闭记得使用with open

closed #标记文件是否已经关闭,由close()改写

input = open('data', 'r')

#第二个参数默认为r

input = open('data')

读二进制文件

input = open('data', 'rb')

#写入文件

写文本文件

output = open('data', 'w')

写二进制文件

output = open('data', 'wb')

追加写文件

output = open('data', 'w+')

写入多行

file_object.writelines(list_of_text_strings)

注意,调用writelines写入多行在性能上会比使用write一次性写入要高。

#写入

f = open('a.txt','w',encoding='utf-8')

f.write('111111111111\n')    #写入文件时、文件不存在会自动创建、写入的内容是重定向到文件。

f.close()

修改文件的内容、首先先打开一个副本文件、然后逐行读取逐行替换、然后在删除原文件、将副本rename成原文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值