1.数字整形
python3不管数字有多大都是int型,没有long类型
1>字符串转换为数字
s1 = "123"
print(type(s1),s1)
b=int(s1)#不加base默认转换为十进制print(type(b),b)
b+= 1000
输出:
123
123
s1 = "0011"s2= "a"
print(type(s1),s1)
b= int(s1,base=2)#二进制
c= int(s2,base=16)#十六进制print(type(b),b)print(type(c),c)
输出:
0011
3
10
2>-bit_length()方法
age = 7
#7 111#3 11#1 01#当前数字的二进制至少用n位表示
r =age.bit_length()print(r)# 3
2.字符串
str
#!/usr/bin/env python#-*- coding:utf-8 -*-
test= 'helLo'v= test.capitalize() #首字母大写
print(v) #Hello
v1 = test.casefold() #所有变小写,更牛逼
print(v1) #hello
v2 = test.lower() #所有变小写
print(v2) #hello
#设置宽度,并将内容居中#20 代指总长度#* 空白未知填充,一个字符,可有可无
v3 = test.center(20,'*')print(v3) #*******helLo********
#去字符串中寻找子序列出现的次数
v4 = test.count('l',4)print(v4) #0#以什么什么结尾或开始,返回bool
v5 = test.endswith('lo')
v6= test.startswith('h')print(v5)print(v6)#从开始往后找,找到第一个之后,获取其位置
test1 = 'hellohello'v7= test1.find('oh', 4, 6) #大于等于4,小于6
print(v7) #4
#格式化,将字符串中的占位符替换为指定的值
test2 = 'i am {name}, age {a}'
print(test2) #'i am {name}, age {a}'
v8 = test2.format(name='Alex',a=18)print(v8) #i am Alex, age 18
test3= 'i am {0}, age {1}'
print(test3) #'i am {0}, age {1}'
v9 = test3.format('Alex',18)print(v9) #i am Alex, age 18
v10= test2.format_map({"name":'Alex',"a":18})print(v10) #i am Alex, age 18
v11= test3.index("am")print(v11)#判断字符串是否只包含数字和字母
v12 =test2.isalnum()print(v12) #False
s = "username\temail\tpassword\nAliex\taliex@q.com\t123456"s1= s.expandtabs(20);#把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 。
print(s1)
输出:
username email password
Aliex aliex@q.com 123456
s2 = '②'s4= s2.isdigit() #true
s5 = s2.isdecimal()#flase
print(s4,s5)
isprintable
是否存在不可显示的字符,\t = 制表符
s8 = 'fjdhf\tdhfds's9=s8.isprintable()print(s9)
True
以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
test = 'HELLO't= '_'v=t.join(test)print(v)
输出:H_E_L_L_O
ljust,rjust
test = 'alex'v= test.ljust(20,'*')print(v) #alex****************
v2= test.rjust(20,'#')print(v2) ################alex
截掉字符串的空格或指定字符(\t,\n也可以)。
test = 'alex'v1=test.lstrip() # 去掉左边的
v2=test.rstrip() # 去掉右边的
v3=test.strip() # 去掉左右两边的print(v1,v2,v3) #alex alex alex
创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
test = 'aeiou'test1= '12345'm=str.maketrans(test,test1)
v= 'ahuwfeofjivuoiewsjnaeiou'new_v=v.translate(m)print(new_v)
输出:1h5wf24fj3v5432wsjn12345
test = 'testtysusnhfhjsl'v1= test.partition('s')
v2= test.rpartition('s')print(v1,v2)
输出:
('te', 's', 'ttysusnhfhjsl') ('testtysusnhfhj', 's', 'l')
test = 'testtysusnhfhjsl'
v3 = test.split('s',2)print(v3)
输出:['te', 'tty', 'usnhfhjsl']
字符串一旦创建,不可修改
一旦修改或者拼接,都会造成重新生成字符串
range
#帮助创建连续的数字
v = range(100) #0, 1, 2, 3...
v1 = range(0, 100) #0, 1, 2, 3...
v2 = range(0, 100, 5) #0, 5, 10, 15...
for item inv:print(item)for item inv1:print(item)for item inv2:print(item)
test = input('>>>')for item inrange(len(test)):print(item,test[item])
3.列表
list # 类
li = [1, 12, 9, "age", "alex"]
中括号括起来,“,”分割每个元素,列表中的元素可以是数字,字符串...
集合内部可以放置任何东西
# 索引取值
print(li[3])
# 切片
print(li[1:3])
#!/usr/bin/env python#-*- coding:utf-8 -*-
li= ['hello', 'world', 'python', 'linux', ['other', 'thing'], 'haha']#列表可以修改,内部是以链表形式实现
li[0] = [11, 22, 33] #修改
li[2:4] = ['PYTHON', 'LINUX'] #一次修改多个
del li[1] #删除
del li[3:5]for item inli:print(item)print(li)
输出:
[11, 22, 33]
PYTHON
LINUX
[[11, 22, 33], 'PYTHON', 'LINUX']
#!/usr/bin/env python#-*- coding:utf-8 -*-
li= ['hello', 'world', 'python', 'linux', ['other', 'thing'], 'haha']#列表可以修改,内部是以链表形式实现
li[0] = ['11', '22', '33'] #修改
li[2:4] = ['PYTHON', 'LINUX'] #一次修改多个
del li[1] #删除
del li[3:5]for item inli:print(item)print(li)#操作
print(li[0][1])#字符串转换列表
s = 'shffhfjdfe'l=list(s)print(l)#列表转字符串#需要自己写for循环实现:既有数字又有字符串
new_s = ''
for i inli:
new_s+=str(i)print(new_s)#直接使用join方法:只有字符串
lii = ['hello', 'abcd', 'hsgdh']
v= ''.join(lii)print(v)#append 方法#在原来值后面追加
lii.append('hhhhh')print(lii) #['hello', 'abcd', 'hsgdh', 'hhhhh']
#clear 方法 清空列表
lii.clear()print(lii)#拷贝
liii = ['hello', 'abcd', 'hsgdh', 'hello']
v= liii.copy() #浅拷贝
print(v)#count 方法 计算元素出现的次数
c = liii.count('hello')print(c)#extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。#参数是可迭代对象
liii.extend(['abcd', 'efgh']) #['hello', 'abcd', 'hsgdh', 'hello', 'abcd', 'efgh']
liii.append(['abcd', 'efgh']) #['hello', 'abcd', 'hsgdh', 'hello', 'abcd', 'efgh', ['abcd', 'efgh']]
liii.extend('bbb') #['hello', 'abcd', 'hsgdh', 'hello', 'abcd', 'efgh', ['abcd', 'efgh'], 'b', 'b', 'b']
print(liii)#insert() 在指定索引位置插入元素
li2 = [11, 22, 33, 44]
li2.insert(0,99)print(li2)#pop() 删除某个值(可以指定索引,默认删除最后一个),并获取删除的值
v = li2.pop(1)print(li2)print(v)#remove() 删除列表中的指定值
li2.remove(33)print(li2)#reverse() 将当前列表进行反转
li2.reverse()print(li2)#sort() 排序
li2.sort() #从小到大
li2.sort(reverse=True) #从大到小
print(li2)
4.元祖
tuple
Python 的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
#!/usr/bin/env python#-*- coding:utf-8 -*-
#tuple 元组,一级不可修改,增加或删除#一般写元组的时候,推荐在最后加入一个“,”
test = ('hello', 'world',)print(test)#可以被for 循环,可迭代对象
for i intest:print(i)#字符串,列表,元组 可以相互转换
li =list(test)print(li)
v= '_'.join(test)print(v) #hello_world
#元组的一级元素不可修改,二级等元素可以修改
tu = (111, 'abc', [33, 44])
tu[2][1] = 99 #二级元素是列表,可以修改
print(tu)
输出:
('hello', 'world')
hello
world
['hello', 'world']
hello_world
(111, 'abc', [33, 99])
5.字典
dict
#字典是另一种可变容器模型,且可存储任意类型对象。#字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中
info = {"k1": "v1", #键值对
"k2": "v2"}print(info)#字典是无序的#值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
dict1 = {1: 'a', 2: 'b'}print(dict1[1])#del dict1[1]
print(dict1)for item in dict1.keys(): #键
print(item)for item in dict1.values(): #值
print(item)for item in dict1.items(): #键值对
print(item)#根据序列,创建字典,并指定统一的值
v = dict.fromkeys(['k1', 123, '999'])print(v) #{'999': None, 123: None, 'k1': None}
v = dict.fromkeys(['k1', 123, '999'], 1200)print(v) #{'k1': 1200, 123: 1200, '999': 1200}
#get() 根据key获取值,当key不存在时,默认为None或指定
v = info.get('k1', 111)print(v)#pop() 删除
v = info.pop('k1')print(v)#popitem() 随机删除
info = {"k1": "v1", #键值对
"k2": "v2"}
v=info.popitem()print(info, v)#setdefault() 设置值,若已存在,则不设置,获取当前key对应的值#若不存在,设置
v = info.setdefault('k3', 'v3')print(info, v) #{'k1': 'v1', 'k3': 'v3'} v3
#update
info.update({'k1': 'v111', 'k2': 'v222'})
info.update(k4='v4')print(info)
输出:
{'k2': 'v2', 'k1': 'v1'}
a
{1: 'a', 2: 'b'}
1
2
a
b
(1, 'a')
(2, 'b')
{'999': None, 123: None, 'k1': None}
{'999': 1200, 123: 1200, 'k1': 1200}
v1
v1
{'k1': 'v1'} ('k2', 'v2')
{'k3': 'v3', 'k1': 'v1'} v3
{'k4': 'v4', 'k3': 'v3', 'k2': 'v222', 'k1': 'v111'}
6.布尔值
bool
可变不可变类型:
可变:列表,字典
不可变:字符串,数字,元组
访问顺序:
1.顺序访问:字符串,列表,元组
2.映射:字典
3.直接访问:数字
存放元素个数:
容器类型:列表,元组,字典
原子:数字,字符串
7.集合
集合(set)是一个无序的不重复元素序列。
1.不同元素组成
2.无序
3.集合中元素必须是不可变类型
s = set('hello')print(s)
s= set(['ab', 'ab', 'sb'])print(s)#add
a = {1, 2, 3}
a.add('3') #{'3', 1, 2, 3}
a.add(3)print(a)#清空
a.clear()print(a) #set()
#pop随机删除
a = {1, 2, 3, 's'}
a.pop()#{1, 2, 's'}
print(a)#remove 可以指定删除元素,若删除元素不存在则报错
a = {1, 2, 3, 's'}
a.remove(2) #{'s', 1, 3}#a.remove(5) error
print(a)#discard 可以指定删除元素,若删除元素不存在不报错
a = {1, 2, 3, 's'}
a.discard(2) #{1, 3, 's'}
a.discard('cd')print(a)#若无集合,求下面列表的交集
python_1 = ['a', 'b', 'c']
linux_1= ['a', 'b', 'd']
python_and_linux=[]for i inpython_1:if i inlinux_1:
python_and_linux.append(i)print(python_and_linux) #['a', 'b']
#集合关系测试
python_2 = ['a', 'b', 'c']
linux_2= ['a', 'b', 'd']
python_s=set(python_2)
linux_s=set(linux_2)#并集
python_and_linux_s = python_s |linux_sprint(python_and_linux_s) #{'d', 'c', 'a', 'b'}
print(python_s.union(linux_s)) #{'b', 'a', 'c', 'd'}#交叉补集
python_and_linux_s = python_s ^linux_sprint(python_and_linux_s) #{'c', 'd'}
print(python_s.symmetric_difference(linux_s)) #{'c', 'd'}#差集
print(python_s.difference(linux_s)) #{'c'}
print(python_s - linux_s) #{'c'}
print(linux_s - python_s) #{'d'}#交集
python_and_linux_s = python_s &linux_sprint(python_and_linux_s) #{'a', 'b'}
python_and_linux_s=python_s.intersection(linux_s)print(python_and_linux_s) #{'a', 'b'}
python_s.difference_update(linux_s)print(python_s) #{'c'}
#isdisjoint() 方法用于判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
x = {"apple", "banana", "cherry"}
y= {"google", "runoob", "facebook"}
z=x.isdisjoint(y)print(z) #True
#issubset() 方法用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。
x = {"a", "b", "c"}
y= {"f", "e", "d", "c", "b", "a"}
z=x.issubset(y)print(z) #True
#update() 方法用于修改当前集合,可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略
x = {"apple", "banana", "cherry"}
y= {"google", "runoob", "apple"}
x.update(y)print(x) #{'banana', 'cherry', 'google', 'apple', 'runoob'}
#不可变集合
s = frozenset('hello')print(s)