python1-集合、函数-(全局变量与局部变量)
集合
# s=set('hello')
# print(s)
#
# s=set(['alex','alex','sb'])
# print(s)
# s={1,2,3,4,5,6}
#添加
# s.add('s')
# s.add('3')
# s.add(3)
# print(s)
# s.clear()==>清空
# print(s)
# s1=s.copy()==>拷贝
s={'sb',1,2,3,4,5,6}
#随机删
# s.pop()
#指定删除(remove())
# s.remove('sb')
# s.remove('hellol') #删除元素不存在会报错
# s.discard('sbbbb')#删除元素不存在不会报错discard()
# print(s)
# python_l=['lcg','szw','zjw','lcg']
# linux_l=['lcg','szw','sb']
# p_s=set(python_l)
# l_s=set(linux_l)
# #求交集intersection()
# print(p_s,l_s)
# print(p_s.intersection(l_s))
# print(p_s&l_s)
# #求并集union()
# print(p_s.union(l_s))
# print(p_s|l_s)
# #差集difference()
# print('差集',p_s-l_s)
# print(p_s.difference(l_s))
# print('差集',l_s-p_s)
# print(l_s.difference(p_s))
#交叉补集symmetric_difference()
# print('交叉补集',p_s.symmetric_difference(l_s))
# print('交叉补集',p_s^l_s)
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
# print('差集',p_s-l_s)
# p_s=p_s-l_s
p_s.difference_update(l_s)
print(p_s)
# s1={1,2}
# s2={2,3,5}
# print(s1.isdisjoint(s2))
s1={1,2}
s2={1,2,3}
print(s1.issubset(s2))#s1 是s2 的子集
print(s2.issubset(s1))#False
print(s2.issuperset(s1))#s1 是s2 的父集
s1={1,2}
s2={1,2,3}
# s1.update(s2) #更新多个值
# s1.add(1,2,3,4) #更新一个值
# s1.union(s2) #不更新
print(s1)
s=frozenset('hello')
print(s)
names=['alex','alex','wupeiqi']
names=list(set(names))
print(names)
msg='i am %s my hobby is %s' % ('lhf','alex')
print(msg)
msg='i am %s my hobby is %s' % ('lhf',1)
msg='i am %s my hobby is %s' % ('lhf',[1,2])
print(msg)
name='lhf' age=19 msg='i am %s my hobby is %s' % (name,age) print(msg)
打印浮点数
tpl = "percent %.2f" % 99.976234444444444444 print(tpl)
打印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444 # print(tpl)
实例
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
print(tpl)
msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
print(msg)
插入时间
msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg)
',sep=':')
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
tpl = "i am {:s}, age {:d}".format("seven", 18)["seven", 18]
l=["seven", 18]
tpl = "i am {:s}, age {:d}".format('seven',18)
print(tpl)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)
python_l=['lcg','szw','zjw']
linux_l=['lcg','szw']
python_and_linux_l=[]
for p_name in python_l:
if p_name in linux_l:
python_and_linux_l.append(p_name)
print(python_and_linux_l)
python1-集合、函数-(全局变量与局部变量)的更多相关文章
- Python新手学习基础之函数-全局变量和局部变量
全局变量和局部变量 我们通常把定义在函数外的变量成为全局变量,定义在函数内的变量称为局部变量,顾名思义,全局变量的作用域是整个代码段,局部变量的作用域就只是在它所属的代码段,变量只在它的作用域里有效. ...
- Day 10:函数全局变量和局部变量及函数嵌套
全局变量:在所有函数之外赋值的变量,是全局变量. 局部变量:在函数内的变量是,局部变量 一个函数被调用时,就创建了一个局部作用域.在这个函数内赋值的所有变量,存在于该局部作用域内.该函数返回时,这个局 ...
- python函数 全局变量和局部变量
li1=[1,2,3,4,5] str1='abc' def func1(): li1=[7,8,9] str1='efg' print(str1) func1() print(li1)#输出的结果为 ...
- python函数的 全局变量与局部变量
一.函数的全局变量 1.什么是全局变量 顶着头开始写,没有任何缩进,在py文件的任何位置都能调用 #!/usr/bin/env python # _*_ coding:utf8 _*_ name=&q ...
- python3--函数(函数,全局变量和局部变量,递归函数)
1.1函数 1.1.1什么是函数 函数就是程序实现模块化的基本单元,一般实现某一功能的集合.函数名:就相当于是程序代码集合的名称参数:就是函数运算时需要参与运算的值被称作为参数函数体:程序的某个功能, ...
- JavaScript 引入方式 语言规范 语言基础 数据类型 常用方法 数组 if_else 比较运算符 for while 函数 函数的全局变量和局部变量 {Javascript学习}
Javascript学习 JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript ...
- Python3基础(3)集合、文件操作、字符转编码、函数、全局/局部变量、递归、函数式编程、高阶函数
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
- Python学习日记(五)——初识函数(set、深浅拷贝、三目运算、函数、全局变量和局部变量)
基本数据类型补充 set set集合,是一个无序且不重复的元素集合 #创建 s = {11,22,33,44}#类似字典 s = set() #转换 l = (11,22,33,44) s1 = se ...
- python1-集合、函数(全局变量与局部变量)
集合(set) # s=set('hello')# print(s)## s=set(['alex','alex','sb'])# print(s) # s={1,2,3,4,5,6} #添加# s. ...
- python基础五(函数、全局变量和局部变量)
一.全局变量和局部变量 全局变量,即可以在当前文件所有地方都可使用的变量,一次修改,整个文件都影响.如果函数体内部使用全局变量,要先声明global 局部变量,只能在函数体内部使用,除了函数体就不可使 ...
随机推荐
- JSESSIONID的简单说明
原文地址:http://blog.csdn.net/chunqiuwei/article/details/23461995 1)第一次访问服务器的时候,会在响应头里面看到Set-Cookie信息(只有 ...
- html是什么?一个完整的html代码告诉你(完整实例版)
html什么意思?这篇文章主要为大家仔细的解释了HTML文档的一个基础的完整代码,还有具体的实例解释,让大家能一下就看懂HTML的基础结构和用法.下面我们一起来看看吧一.html是什么?点击查看htm ...
- openwrt如何关掉防火墙?
答: 1. 阻止防火墙服务开机自动启动 /etc/init.d/firewall disable 2. 关闭防火墙 /etc/init.d/firewall stop
- SP913 QTREE2 - Query on a tree II
思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #i ...
- .Net dependent configuration
error info: 解决方案:在.exe.config文件中配置Newtonsoft.Json所用版本 <runtime> <assemblyBinding xmlns=&quo ...
- bzoj2131 免费的馅饼——树状数组优化dp
中文题目,问你最后能最多够得到多少价值的馅饼.因为宽度10^8且个数为10^5.所以不可以用dp[x][y]表示某时间某地点的最大权值. 假设你在x点处接到饼后想去y点接饼.那么需要满足的条件是t[y ...
- 七牛云图片的存储与处理--基于node
1. 手动上传 . 快速入门,这个简单,可以参考七牛官方文档: https://developer.qiniu.com/kodo/manual/1233/console-quickstart#step ...
- 【Axure RP8.1】一款专业的快速原型设计工具
Axure RP是一款专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写.Axure RP是美国Axur ...
- JSR107缓存规范
Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvider定义了创建. ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...