末学者笔记--Python基础二玄

Python基础二玄

【数据类型】

1.数字                                                                                 

#int整型

定义:age=10 #age=int(10)

用于标识:年龄,等级,身份证号,qq号,个数

 

#float浮点型

定义:salary=3.1  #salary=float(3.1)

用于标识:工资,身高,体重,

2.字符串(着重*)                                                                                           

单引号,多引号,三引号的区别和作用

#多行字符串需要用到三引号;双引号和单引号都是用来定义单行字符串的,那么2者有什么区别呢,举个例子

想把下面这句话定义成字符串怎么办?

what's your name

这句话当中有一个单引号了,如果我们在用单引号把他包起来,他就会报错,因此,我们需要在外面用双引号包裹起来

res = "what's your name"

 

【了解类】:

1count:统计单个字符数量

a='hello world'
res=a.count('o')

print(res)

 》》

2

》》


2index从左往右查找字符的索引,找不到报错

a='hello world'

res = a.index('o')
print(res)

》》

4     #下标编号:012......

》》

3rindex从右往左查找字符的索引,(0,6)代表索引范围

a='hello world'

res = a.rindex('o')
res1 = a.rindex('o',0,6)
print(res,res1)

》》

7 4

》》

4istitle:判断字符串是否是标题格式(英文标题皆为首字母大写即可)

a='hello world'

res = a.istitle()
print(res)

》》

False

》》

 

a='Hello World'

res = a.istitle()
print(res)

》》

True

》》

 

4isspace判断字符串里是否全是空格

a='  '

b = ''
res = a.isspace()

res1 = b.isspace()
print(res,res1)

>>

True False

>>

 

5isdigit判断是否为整数

a = 'qwe'
b='123'
res = a.isdigit()

res1 = b.isdigit()
print(res,res1)

》》

False True

》》

 

6endswith判断是否以...结尾或开头

a='Hello World'

res = a.endswith('ld')     #以...结尾,若想以...开头用a.startswith()
print(res)

》》

True

》》

 

7查找字符串的索引,find能找到就显示索引位置,找不到显示-1

 

a='holle world'
b ='fhsdoif123'
res = a.find('123')

res1 = b.find('123')
print(res,res1)

》》

-1  7

》》

 

8isalnum判断是否是(数字)或者是(字母)或者是(字母和数字的任意组合)

a = 'holleworld123'
b = 'holle world123'
res = a.isalnum()

res1 = b.isalnum()
print(res,res1)

》》

True False

》》

 

9判断是否是纯字母

a = 'holleworld123'

res = a.isalpha()
print(res)

》》

False

》》

 

10判断是否是大,小写

a = 'QWE asd'
res = a.islower()      #判断小写,判断大写改为“a.isupper()”

print(res)

》》

False

》》

 

11把大写字母变成小写(及小写变大写)

a = 'QWE asd'
res = a.lower()     #大写变小写,想小变大用a.upper()

print(res)

》》

qwe asd

》》

 

12把字符串变成标题格式

a = 'QWE asd'
res = a.title()

print(res)

》》

Qwe Asd

》》

 

【熟知类】:

1split从左往右把字符串切分成列表

a = '192.168.254.250'
res = a.split('.')     #以“.”为分割点,切段成列表

res1 = a.split('.',2)     #“2”表示只准切2刀,及分三段
res2 = a.rsplit('.',2)     #“a.rsplit”从右往左切段
print(res,res1,res2,sep=' ')     #sep=’ ’表示每输出一个值就换行输出下一个

》》

['192', '168', '254', '250']

['192', '168', '254.250']

['192.168', '254', '250']

》》

 

2)转码解码

a='我去你大爷'
res = a.encode('utf-8')    #encode将变量a转码,'utf-8'为万国码

res1=res.decode('utf-8')    #decode将res变量解码
print(res,res1,sep=' ')

》》

b'xe6x88x91xe5x8exbbxe4xbdxa0xe5xa4xa7xe7x88xb7'

我去你大爷

》》

 

3format格式化输出

name = 'qqq'
age =23

#下列3者取1:
1.res = 'my name is {},my age is {}'.format(name,age)   #{}占位,后面格式依次占据,需要注意顺序
2.res = 'my name is {1},my age is {0}'.format(age,name)   #“1,0”表示后面格式的索引号
3.res = 'my name is {na},my age is {a};my small name is {na}'.format(na=name,a=age)   #当多次使用同一变量时,可用这种,较为方便
print(res)

 

4join把可迭代对象变成字符串,括号里可以是字典,列表,元组,字符串

res = ''.join('hello''wolld')   #加入的字符串间不要用“,”
res1 = ''.join(['12','32','45'])   #加入列表的元素一定要是字符串
print(res,res1)

》》

hellowolld  123245

》》

 

5strip是去除左右两边的字符,默认为空格

a = '   ====-----====张小三=====     '
res = a.strip()

print(res)
res1 = res.strip('=')
print(res1)
b = res1.strip('-')
print(b)

》》

====-----====张小三=====

-----====张小三

====张小三

》》

 

去除右边的字符,默认为空格
res = a.rstrip('=')
print(res)


去除左边的字符,默认为空格
res = a.lstrip('-')
print(res)

 

6replace替换括号里的字符,并可以指定替换次数

a = '====-----====张小三====='

res = a.replace('=','').strip('-')
print(res)
res = a.replace('=','+',3)
print(res)

》》

张小三

+++=-----====张小三=====

 

》》

 

(7)%s,%d,%f占位符

res = 'my name is %s; my age is %s' % ('张小三',23)   #%s代替任意的数据类型
print(res)

sum='my name is %s; my age is %d' % ('张小三',23)   #%d只占替整型
print(sum)
res1 = 'my high is %.2f' % 185.23343   #%f:占位代替的是浮点型,可保留小数点后几位
print(res1)

》》

my name is 张小三; my age is 23

my name is 张小三; my age is 23

my high is 185.23

 

》》

 

3.列表(中括号)                                                                                      

#[]内用逗号分隔,可以存放任意个任意类型的值比如(数字,字符串,列表,元组都OK),#用于标识存储多个值,比如一个班级的学生有多个,一个人的兴趣爱好也有多个

 

>>> test = [1,2,'a',[1,2],{'a','b'}]

>>> test

[1, 2, 'a', [1, 2], {'b', 'a'}]

>>>

#利用下标取列表里的值

>>> test[0]

1

>>>

【了解类】

1index返回元素的索引,没有查找到就报错

l1=['c','d','f','r']
res = l1.index('r')
print(res)

》》

3

》》

(count:统计元素个数

 

2insert在指定位置插入元素

l1=['c','d','f','r']

l1.insert(3,'250')    #在索引为3的元素前插入,插入是没有返回值的
print(l1)

》》

['c', 'd', 'f', '250', 'r']

》》

3sort排序及reverse倒序,反转

l1=['a','d','c','b','#','*','@','1','3','2']
l1.sort()
print(l1)
l1.reverse()
print(l1)

》》

['1', '2', '3', 'a', 'b', 'c', 'd']     #数字在前,英文在后

['d', 'c', 'b', 'a', '3', '2', '1']

》》

 

4copy拷贝列表

l1=['c','d','f','r']

res = l1.copy()
print(res)
>>

['c','d','f','r']

>>

 

5clear清空列表

l1=['c','d','f','r']
l1.clear()
print(l1)

》》

[]

》》

 

6pop剪切指定元素

l1=['c','d','f','r']
res = l1.pop(1)    #剪切是有返回值的,删除没有返回值
print(res)
print(l1)

>>

d

['c', 'f', 'r']

>>

【熟知类】

1append追加到列表末尾

l1=['c','d','f','r']
l1.append('张小三')    #只能追加到末尾
print(l1)

》》

['c', 'd', 'f', 'r', '张小三']

》》

 

2remove删除指定元素

l1=['c','d','f','d','a','r']
l1.remove('d')    #从左往右删除选定元素
print(l1)
l1.remove('d')
print(l1)

》》

['c', 'f', 'd', 'a', 'r']

['c', 'f', 'a', 'r']

》》

 

3extend可以传入可迭代对象,以单个元素为个体添加到列表当中

l1=['f']
l1.extend('hello')
print(l1)
l1.extend(['sd','f','g'])
print(l1)
l1.extend({'name':'feige','age':'23'})
print(l1)

》》

['f', 'h', 'e', 'l', 'l', 'o']

['f', 'h', 'e', 'l', 'l', 'o', 'sd', 'f', 'g']

['f', 'h', 'e', 'l', 'l', 'o', 'sd', 'f', 'g', 'name', 'age']

 

》》

4.元组(小括号)                                                                                

元组跟列表一样都可以存取多个值,只不过元组大部分是用来读的,不能修改。

#元组(如果元组里只有一个值,必须加逗号)

t1=([1,2,3],[65,3,9])
print(t1)

t2 = ([1,2,3],)
print(type(t2))

t3 = ([1,2,3])
print(type(t3))

 

E:untitledvenvScriptspython.exe E:/untitled/test2.py

([1, 2, 3], [65, 3, 9])

<class 'tuple'>

<class 'list'>

Process finished with exit code 0

 

5.字典(大括号)                                                                                         

#既然有了列表可以存取多个值,为什么还要有字典呢?举个例子比如在这个列表中有2个值一个28用来表示年纪,一个187用来标识身高,但是并没有一个说明,那个元素对应的是年纪,那个元素对应的是身高

info = [28,187]

#因此,字典就可以解决这个问题

info = {'age':28,'high':187}

printtypeinfo))

 

<class 'dict'>    #dict表字典类型

 

#copy拷贝字典

info.copy()

 

#清除一个字典

info.clear()

 

1pop通过key剪切value

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
res = info.pop('name')
print(res)
print(info)

》》

张小三

{'age': 23, 'high': 175}

》》

 

2setdefault设置默认值

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
info.setdefault('name','李柱')
print(info)
info.pop('name')
print(info)
info.setdefault('name','李柱')    #设置的默认值必须在字典内没有这个key时才生效
print(info)

》》

{'name': '张小三', 'age': 23, 'high': 175}

{'age': 23, 'high': 175}

{'age': 23, 'high': 175, 'name': '李柱'}

》》

 

(3)fromkeys快速定义一个空字典且每个key为同一值

res = {}.fromkeys(['张三','李四','王五'], '1902班')
print(res)

》》

{'张三': '1902班', '李四': '1902班', '王五': '1902班'}

》》

 

4)剪切键值对(从末尾选择剪切)

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
res1 = info.popitem()
res2 = info.popitem()
print(res1,res2)
print(info)

》》

('high', 175) ('age', 23)

{'name': '张小三'}

》》

 

5)列出所有keyvalues

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
res = info.values()    #列values
print(res)
res = info.keys()    #列keys
print(res)
res = info.items()    #列全部
print(res)

》》

dict_values(['张小三', 23, 175])

dict_keys(['name', 'age', 'high'])

dict_items([('name', '张小三'), ('age', 23), ('high', 175)])

 

》》

 

6get通过keyvalue,没取到值返回None,不可以赋值

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
res = info.get('high')
res1 = info.get('gender')
print(res,res1)

》》

175  None

》》

 

7['key']可以取value,没取到会报错,可以赋值

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
res = info['high']
print(res)
info['name'] = '李四'    #赋值,更改
print(info)

》》

175

{'name': '李四', 'age': 23, 'high': 175}

》》

 

(8)update把两个字典合并成一个字典,如果key一样修改原value

info = {
    'name': '张小三',
    'age':23,
    'high':175
}
info.update({'gender':'男'})     #合并
print(info)
info.update({'name':'李四'})     #修改
print(info)

》》

{'name': '张小三', 'age': 23, 'high': 175, 'gender': '男'}

{'name': '李四', 'age': 23, 'high': 175, 'gender': '男'}

》》

 

 

6. 集合(大括号)                                                                                      

跟列表相似,但其内的value输出时会去重输出。

s1 = {1,2,3,3,5}
print(s1)

print(type(s1))

》》

{1, 2, 3, 5}

<class 'set'>

》》

当其为空时,默认是字典类型:

S2 = {}
print(type(s2))

 

<class 'dict'>

 

1intersection求交集

s1 = {1,2,3,8,4,5,7,9,0,6}
s2 = {2,4,8,3,11}
res = s1.intersection(s2)
print(res)

》》

{8, 2, 3, 4}

》》

 

2difference求差集

s1 = {1,2,3,8,4,5,7,9,0,6}
s2 = {2,4,8,3,11}
res = s1.difference(s2)
print(res)
res = s2.difference(s1)
print(res)

》》

{0, 1, 5, 6, 7, 9}

{11}

》》

 

3union求并集

s1 = {1,2,3,8,4,7,9,0,6}
s2 = {2,4,8,3,5}
res = s1.union(s2)
print(res)

》》

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

》》

 

(4)issubset判断是否是子集

s1 = {1,2,3,8,4,7,9,0,6}
s2 = {2,4,8,3,}
res = s2.issubset(s1)
print(res)

》》

True

》》

 

7.布尔                                                                                                           

#布尔类型就是TrueFalse

>>> a=100

>>> b=200

>>>

>>> a > b #不成立就是False,也就是假

False

>>> a < b #成立就是True, 也就是真

True

谨记

#0,None,空都为假,其余为真

 

s1 = {1,2,3,4}
print(type(s1))

 

8.切片                                                                                      

根据索引来截选列表或元组或字符串中的几个字符

1)特点:顾头不顾尾

t1 = (1,2,3,7,5,4)
str1 = 'helloworld'

res = t1[1:3]

print(res)
res = str1[-4:-1]
print(res)

》》

(2, 3)

orl

》》

 

2打印出索引为1往右(包括1)

str1 = 'helloworld'
res = str1[1:]

print(res)

》》

elloworld

》》

 

3)打印出索引小于2的字符(不包括2)

str1 = 'helloworld'
res = str1[:2]

print(res)

》》

he

》》

 

4)含有步长截取

str1 = 'helloworld'
res = str1[1:8:2]    #从索引1到8(含左不含右),步长为2

print(res)

》》

elwr

》》

——————————————————————————分割线———————————————————————————————— 

原文地址:https://www.cnblogs.com/feige2L/p/10945571.html