5-基础语法(4)

format格式化

  • 使用函数形式进行格式化
  • 大括号作占位符
s = "{} {}"
print(s.format("hello","word"))
hello word
# 设置指定位置
s  = "{0}  {1}".format("hello","word")
print(s)

s  = "{0}  {0}".format("hello","word")
print(s)

s  = "{1}  {1}".format("hello","word")
print(s)

s  = "{1}  {0}".format("hello","word")
print(s)
hello  word
hello  hello
word  word
word  hello
# 使用命名参数
s  = "我是{name},我最喜欢的女人是{lover}"
print(s.format(name="Bidding",lover="Ayue"))

# **表示解包操作
s_obj = {"name":"Bidding","lover":"Ayue"}
print(s.format(**s_obj))
我是Bidding,我最喜欢的女人是Ayue
我是Bidding,我最喜欢的女人是Ayue

str内置函数

  • python中用str表示字符串
  • help(str)

字符串查找类,find,index

  • find:查找字符串是否包含子串,返回找到的子串位置,即第一次出现的位置,找不到返回-1
  • index:查找字符串是否包含子串,返回找到的子串位置,即第一次出现的位置,找不到抛出错误
  • lfind:从左往右查找
  • rfind:从右往左查找

字符串判断类

  • 一般以is开头
  • isalpha:判断是否是字母
    • 默认的前提是字符串必须包含至少一个字符,没有返回False
    • 汉字被认为是alpha,因此此函数不能作为区分字母还是汉字,而是使用Unicode码
  • isdigit,isnumeric,isdecimal
  • 判断数字使用正则表达式,不要使用上面这三个,有坑
  • islower:是否是小写
  • isupper:是否是大写
help(str.index)
Help on method_descriptor:

index(...)
    S.index(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found, 
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Raises ValueError when the substring is not found.

内容判断类

  • startswith/endswith:是否以xxx结尾

操作类函数

  • format:格式化用
  • strip:去掉字符串两侧的空格
  • lstrip/rstrip:去掉子串左边/右边的空格
  • join:拼接字符串
# join的例子
s1 = '$'
s2="_"
s3="#"
ss=["hello","word","joins"]

print(s1.join(ss))
print(s2.join(ss))
print(s3.join(ss))
hello$word$joins
hello_word_joins
hello#word#joins

python的内置类型

  • str
  • list
  • tuple
  • set
  • dict

list列表

  • 一组有序数据组成的列
  • 列表可以存储的不仅仅是同一类数据
# list

list1 = [1,2,3,34,"hhh",True]
print(list1)
[1, 2, 3, 34, 'hhh', True]
# list创建的另一种方式
list2 = list()

print(list2)
print(type(list2))
[]
<class 'list'>

常见的内置函数

  • help():帮助函数
  • type():查看变量的类型
  • id():显示变量的ID
  • print():输出变量

列表的常见操作

  • 获取:通过索引获取
  • 切片:用[x:y]表示,截取列表的任意一段,包前不包后,返回结果是列表
# 切片
list3 = [1,2,3,4,5]
list4 = list3[1:3]
print(list4)

# id()判断切片是否生成了新的列表,即内存地址是否不一样
print(id(list3))
print(id(list4))

print(list3[3:])
print(list3[:3])
print(list3[:])
# 最后一个参数2表示步长
print(list3[::2])

# 切片时下标超出范围不报错
# 下标可以为负数,正数表示从左往右,0开始;负数表示从右往左,-1开始
# 步长为正数,表示从左往右;步长为负数,表示从右往左
print(list3[-5:-2])  #默认从左往右
[2, 3]
1760334141192
1760333328840
[4, 5]
[1, 2, 3]
[1, 2, 3, 4, 5]
[1, 3, 5]
[1, 2, 3]

tuple元组

  • 类似于列表,但是不允许修改
  • 使用小括号
  • 如果元组只有一个元素的时候,在元素后面添加一个逗号,表示这是一个元组
  • 有序
  • 可以访问不可以修改
  • 元素可以使任意类型
  • 支持索引操作
  • 支持分片操作、返回的是一个元组
  • 元组支持加法和乘法
    • 两个元组相加
    • 一个元组和一个整数相乘
  • 元组支持成员检测
    • in
    • not in
  • 元组支持遍历
  • 元组嵌套:元组作为元组的元素
# tuple创建
tp = ()
print(type(tp))

tp1 = (1,)
print(tp1)

tp2 = (1,2,3,4,5)
print(tp2)

# 这样也是一个元组,不建议
tp3 = 100,
print(tp3)
print(type(tp3))

# 要求参数必须可迭代
tp4 = tuple([1,2,3,4,5])
print(tp4)
print(type(tp4))
print(tp4[1])
print(tp4[1:])
print(tp4[:2])
<class 'tuple'>
(1,)
(1, 2, 3, 4, 5)
(100,)
<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'tuple'>
2
(2, 3, 4, 5)
(1, 2)
# 元组相加

ta = (1,2,3,4)
tb = ("hello","word")

tc = ta+tb
print(tc)


# 元组相乘

td = ta * 2
print(td)
(1, 2, 3, 4, 'hello', 'word')
(1, 2, 3, 4, 1, 2, 3, 4)
# tuple成员检测

if "hello" in tb:
    print("hello in tb 元组")
hello in tb 元组
# 元组遍历

ta = ((1,2,3),("a","b","c"),(True,False))

for i in ta:
    print(i)
    for j in i:
        print(j)
(1, 2, 3)
1
2
3
('a', 'b', 'c')
a
b
c
(True, False)
True
False
# 元组遍历
# 单层循环访问双层元组,  不建议使用
# 有限制:i,j,k要对应里层元组的个数

ta = ((1,2,3),("a","b","c"),(True,False,None))

for i,j,k in ta:
    print(i,j,k)

1 2 3
a b c
True False None

常用元组函数

  • len():元组长度
  • max():元组中的最大值
  • min():元组中的最小值
  • count():对元组中某一元素计数
  • index():某一元素所在的位置
ta = [1,2,3,4,5]

print(len(ta))
print(max(ta))
print(min(ta))
print(ta.count(2))
print(ta.index(3))
5
5
1
1
2
# python对两个值互换的简单方法
a = 100
b = "hhhh"

a,b = b,a

print(a)
print(b)
hhhh
100

set集合

  • 与数学中的集合概念一致,大括号表示
  • 无序、唯一
  • 定义:大括号、set()
  • in操作
# 集合

s1 = set()
print(s1)

s2 = set([1,2,3,1,1])
print(s2)

# 集合会将重复的元素去掉,去重
s3 = {1,2,3,4,5,2,1,1,1,"hahahha"}
print(s3)
set()
{1, 2, 3}
{1, 2, 3, 4, 5, 'hahahha'}
# in操作

if 1 in s2:
    print("1 in s2")
    
    
# 遍历
for i in s3:
    print(i)
    
# 集合中放元组

s4 = {(1,2,3),("ss","dd",12),12}
print(s4)


1 in s2
1
2
3
4
5
hahahha
{('ss', 'dd', 12), 12, (1, 2, 3)}
# 集合的生成式

sa = {1,2,3,4,5,6}

sb = {i for i in sa if i % 2 == 0}
print(sb)
{2, 4, 6}

集合的内置函数

  • len():集合的长度
  • max():集合的最大值
  • min():集合的最小值
  • add():向集合中添加元素
  • clear():清空集合
  • remove():删除集合的某个元素,元素不存在时报错
  • discard():删除集合的某个元素,元素不存在时不报错
  • pop():弹出集合的某个元素,是随机的,返回弹出的元素
sa = {1,2,3}
sa.add(4)
print(sa)

sa.remove(2)
print(sa)

sa.discard(1)
print(sa)

sa.clear()
print(sa)
print(len(sa))
{1, 2, 3, 4}
{1, 3, 4}
{3, 4}
set()
0

集合的数学操作

  • intersection():交集,返回值时集合
  • difference():差集,返回值时是集合
  • 减号也可表示差集
  • union():并集,返回值集合
sa = {1,2,3,4,5}
sb = {4,5,6,7,8}

# sa和sb的交集
print(sa.intersection(sb))

# 在sa中不在sb中的差集
print(sa.difference(sb))
print(sa-sb)

# 在sb中不在sa中的差集
print(sb.difference(sa))
print(sb-sa)

# 并集
print(sa.union(sb))
{4, 5}
{1, 2, 3}
{1, 2, 3}
{8, 6, 7}
{8, 6, 7}
{1, 2, 3, 4, 5, 6, 7, 8}

frozenset冰冻集合

  • 不允许修改的集合
发布了247 篇原创文章 · 获赞 23 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/LiyangBai/article/details/101635928