集合(set)

# 2 无序
# 3 集合中元素必须是不可变类型
# 定义集合
s = {1,2,3,4,5}
print(s) # 输出结果 {1, 2, 3, 4, 5} # 1 集合由不同元素组成
s1 = {1,2,2,3,4,5,5,5,5,5}
print(s1) # 输出结果 {1, 2, 3, 4, 5} s2=set("hello") # 定义集合方法2
print(s2) # 输出结果 {'o', 'e', 'h', 'l'}

set中的一些常用方法(add,clear,copy,pop,remove,update)

s = {1, 2, 3, 4, 5}
s.add(7) # 增加元素
print(s) # 输出结果 {1, 2, 3, 4, 5, 7} s1 = {1, 2, 3, 4, 5}
s1.clear() # 清空
print(s1) # 输出结果 set() s2 = s.copy() # 复制一份
print(s2) # 输出结果 {1, 2, 3, 4, 5, 7} s3 = {1, 2, 3, 4, 5}
s3.pop() # 随机删除
print(s3) # 输出结果 {2, 3, 4, 5} s4 = {1, 2, 3, 4, 5}
s4.remove(3) # 指定删除
print(s4) # 输出结果 {1, 2, 4, 5}
s1={1,2,3}
s2={2,3,4}
s1.add(4) # 添加 一个元素
print(s1)
s1.update(s2) # 可以添加多个元素
s1.update((7,8)) # 也可以传元组
print(s1) # 输入结果 {1, 2, 3, 4, 7, 8}

集合(交,差,并)

python_1 = ['hanhan', 'meimei', 'haohao']
linux_1 = ['hanhan', 'meimei']
# 求这两个列表的交集
p_s = set(python_1) # 创建集合
l_s = set(linux_1) # 创建集合
print(p_s.intersection(l_s)) # 输出结果 {'hanhan', 'meimei'}
print(p_s & l_s) # 输出结果 {'hanhan', 'meimei'} # 求两个列表的并集 print(p_s.union(l_s)) # 输出结果 {'meimei', 'hanhan', 'haohao'}
print(p_s | l_s) # 输出结果 {'meimei', 'hanhan', 'haohao'} # 求差集 差集:两个集合相差的元素
print(p_s.difference(l_s)) # 输出结果 {'haohao'}
print(p_s - l_s) # 输出结果 {'haohao'}
print(l_s.difference(p_s)) # 输出结果 set()
print(l_s - p_s) # 输出结果 set() # 交叉补集
print(p_s.symmetric_difference(l_s)) # 输出结果 {'haohao'}
print(p_s^l_s) # 输出结果 {'haohao'}

字符串格式化

# 字符串格式化
msg='i am hanhan'+' my hobby is coding' # 此方法容易占空间 不太好
print(msg) # 输出结果 i am hanhan my hobby is coding msg1='i am %s my hobby is coding' % ('hanhan') # % 更实用
print(msg1) # 输出结果 i am hanhan my hobby is coding msg2='i am %s my hobby is %s and i am %d yeas old' % ('hanhan','coding',23) # % 更实用
print(msg2) # 输出结果 i am hanhan my hobby is coding and i am 23 yeas old msg4='percent %.2s' % 99.987456456456 # %.2s 就是后面字符串的简单截取
print(msg4) # 输出结果 99 msg5='percent %.3s' % 99.987456456456 # %.3s 就是后面字符串的简单截取
print(msg5) # 输出结果 99. msg6='percent %.4s' % 99.987456456456 # %.4s 就是后面字符串的简单截取
print(msg6) # 输出结果 99.9 msg3='percent %.2f' % 99.987456456456 # .2就是小数点后面保留两位有效数字
print(msg3) # 输出结果 percent 99.99 # 打印百分比
msg7='percent: %.2f %%' % 99.987456456456
print(msg7) # percent: 99.99 % # 以字典的形式来完成输入
msg8='i am %(name)s and i am %(age)d yeas old' %{'name':'hanhan','age':23}
print(msg8) # 输出结果 i am hanhan and i am 23 yeas old # 也可以加上颜色和距离
msg9='i am %(name)+30s and i am %(age)d yeas old' %{'name':'hanhan','age':23}
print(msg9) # 输出结果 i am hanhan and i am 23 yeas old print('ip','','','','',sep=':') # 输出结果 ip:107:111:23:51

format用法

msg = 'i am {} i am {} yeas old my hobby is {}'.format('hanhan', 23, 'coding')
# 如果{} 中没有索引,则将一一对应后面的值,如果对应不了则报错
print(msg) # 输出结果 i am hanhan i am 23 yeas old my hobby is coding msg1 = 'i am {0} i am {1} yeas old my hobby is {2}'.format('hanhan', 23, 'coding')
# format 将会按照前面{}中的索引来传值
print(msg1) # 输出结果 i am hanhan i am 23 yeas old my hobby is coding msg2 = 'i am {0} i am {0} yeas old my hobby is {0}'.format('hanhan', 23, 'coding')
print(msg2) # 输出结果 i am hanhan i am hanhan yeas old my hobby is hanhan
# 也可以按照对象名字来传值
msg3 = 'i am {name} i am {age} yeas old my hobby is {hobby}'.format(name='hanhan', age=23, hobby='coding')
print(msg3) # 输出结果 i am hanhan i am 23 yeas old my hobby is coding
# 也可以按照列表来传值
msg4 = 'i am {0[0]} i am {1[1]} yeas old my hobby is {0[2]}'.format(['hanhan', 80, 'coding'], [11, 23, 25])
print(msg4) # 输出结果 i am hanhan i am 23 yeas old my hobby is coding l=['hanhan',18,'coding']
# 如果需要动态传入列表 必须加上*
msg5 = 'i am {:s} i am {:d} yeas old my hobby is {:s}'.format(*l)
print(msg5) # 输出结果 i am hanhan i am 18 yeas old my hobby is coding msg6 = 'number:{:b},{:o},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,15)
print(msg6) # 输出结果 number:1111,17,15,f,F,1500.000000%

Python学习第七课——集合(set) 和 字符串拼接的更多相关文章

  1. Python学习第七课

    Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...

  2. python学习:注释、获取用户输入、字符串拼接、运算符、表达式

    注释 #为单行注释'''三个单引号(或者"""三个双引号)为多行注释,例如'''被注释的内容''' '''三个单引号还可以起到多行打印的功能. #ctrl+? 选中的多行 ...

  3. Python学习【第7篇】:字符串拼接

    1.格式化字符有%s,%d,%f浮点数 %s代表格式化字符串,s是string意思 msg = 'my name is %s'%"xiaoxing"print(msg)运行结果:m ...

  4. python学习博客地址集合。。。

    python学习博客地址集合...   老师讲课博客目录 http://www.bootcdn.cn/bootstrap/  bootstrap cdn在线地址 http://www.cnblogs. ...

  5. python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍

    目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...

  6. Python学习第六课

    Python学习第六课 课前回顾 列表 创建 通过 [] :写在[]里,元素之间用逗号隔开 对应操作: 查 增 append insert 改(重新赋值) 删除(remove del pop(删除后会 ...

  7. Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer

    原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...

  8. python学习6—数据类型之集合与字符串格式化

    python学习6—数据类型之集合与字符串格式化 1. 使用id()可以查看一个变量的内存地址: name = 'alex' id(name) 2. 进制转换 十进制转换为二进制等: a = 10 # ...

  9. Python学习笔记七

    面向对象编程 面向对象的特性如下: 类:具有相同属性和方法的一类事物,成为类. 对象:类的实例化后的结果,一个类可以实例化多个对象,每个对象也可以不同的属性. 封装:在类中对数据的赋值,类里面包含着类 ...

随机推荐

  1. C/C++程序从文本文件中读取(保存)数据

    :本文仅供初学者参阅,解惑 在C程序中: 与程序代码外的数据(文件)打交道,我们使用到流(stream)这个概念,实现进程的虚拟内存与文件之间的数据交换. ——文件流:C标准库提供了FILE(之所以命 ...

  2. win10配置cuda和pytorch

    简介 pytorch是非常流行的深度学习框架.下面是Windows平台配置pytorch的过程. 一共需要安装cuda.pycharm.anancoda.pytorch. 主要介绍cuda和pytor ...

  3. 手把手教你使用Hexo+GitHub搭建自己的个人博客网站

    安装nodejs环境 这个直接搜索安装即可,安装完成之后,通过如下命令检测环境变量是否安装成功: λ node -v # 输出版本号 v12.13.1 正确输入版本号即可. 安装cnpm cnpm是淘 ...

  4. 【代码总结】MYSQL数据库的常见操作

    ============================== MYSQL数据库的常见操作 ============================== 一.mysql的连接与关闭 -h:指定所连接的服 ...

  5. 《实战Java高并发程序设计》读书笔记二

    第二章  Java并行程序基础 1.线程的基本操作 线程:进程是线程的容器,线程是轻量级进程,是程序执行的最小单位,使用多线程而不用多进程去进行并发程序设计是因为线程间的切换和调度的成本远远的小于进程 ...

  6. MAVLINK协议

    参考https://cloud.tencent.com/developer/news/48344 https://www.cnblogs.com/lovechen/p/5801679.html htt ...

  7. 洛谷 P2925 [USACO08DEC]干草出售Hay For Sale

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P2925 这是一道简单的01背包问题,但是按照正常的01背包来做会TLE一个点,所以要加一个特判(见代 ...

  8. linux/centos之配置tomcat

    一:下载tomcat压缩包 在http://archive.apache.org/dist/tomcat/中下载合适版本的tomcat,也可以在官网上下载,只是一般只有最新版本,选择二进制的后缀为ta ...

  9. js 常用字符正则匹配

    写代码时需要js验证密码,百度到的验证方法,图方便保存收藏,如感兴趣请移步原博主博文!http://blog.csdn.net/x_i_y_u_e/article/details/47730135 1 ...

  10. css3内外阴影同时显示

    内外阴影同时显示: box-shadow: 0px 0px 0.4rem rgba(255,255,255,0.5) inset,0px 0px 0.7rem rgba(185,119,143,0.9 ...