python基础入门(三)

集合

#集合中不存在重复元素,因此主要用来去重
s=set('hello')
print(s)   #{'e', 'l', 'o', 'h'}
#添加
s.add('s')
#清空
s.clear()
print(s)
#浅拷贝
s1=s.copy()

s={'sb',1,2,3,4,5,6}
#随机删
s.pop()
#指定删除
s.remove('sb')
s.remove('hellol') #删除元素不存在会报错
s.discard('sbbbb')#删除元素不存在不会报错
print(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.intersection(l_s))
print(p_s&l_s)
#求并集
print(p_s.union(l_s))
print(p_s|l_s)
#差集
print('差集',p_s-l_s)
print(p_s.difference(l_s))
print('差集',l_s-p_s)
print(l_s.difference(p_s))

#交叉补集
print('交叉补集',p_s.symmetric_difference(l_s))
print('交叉补集',p_s^l_s)   #异或



# s1={1,2}
# s2={2,3,5}
# print(s1.isdisjoint(s2))   #没有交集返回True,否则返回False

# 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)

格式化输出字符串

msg='i am %s my hobby is %s' % ('ab','de')
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": "abc", "age": 8}
print(tpl)

#间隔空间为60空格
msg='i am %(name)+60s my hobby is alex' %{'name':'liu'}
print(msg)

#设定指定分割
print('root','x','0','0',sep=':')

函数参数

    def test(x,y,z):#x=1,y=2,z=3
        print(x)
        print(y)
        print(z)

#位置参数,必须一一对应,缺一不行多一也不行
    test(1,2,3)

#关键字参数,无须一一对应,缺一不行多一也不行
    test(y=1,x=3,z=4)

#位置参数必须在关键字参数左边
    test(1,y=2,3)#报错
    test(1,3,y=2)#报错
    test(1,3,z=2)
    test(1,3,z=2,y=4)#报错
    test(z=2,1,3)#报错

#参数组:**字典 *列表
    def test(x,*arg):    #以元组形式传入
        print(x)
        print(arg)

    test(1)     
    test(1,2,3,4,5)
    test(1,{'name':'alex'})
    test(1,['x','y','z'])      #传入为单个元素(['x', 'y', 'z'],)
    test(1,*['x','y','z'])     #加*主动声明为列表,则传入为多个元素
    test(1,*('x','y','z'))

函数局部变量与全局变量

# 全局变量变量名大写
# 局部变量变量名小写
# 优先读取局部变量,能读取全局变量,无法对全局变量重新赋值 NAME=“fff”,
# 但是对于可变类型,可以对内部元素进行操作
# 如果函数中有global关键字,变量本质上就是全局的那个变量,可读取可赋值 NAME=“fff”
如果函数的内容无global关键字,
 # - 有声明局部变量
NAME = ["a","b"]
def qupengfei():
    NAME = "c"
    print('d', NAME)
qupengfei()
 # - 无声明局部变量
NAME = ["a","b"]
def qupengfei():
    NAME.append('e')
    print('c', NAME)
qupengfei()

如果函数的内容有global关键字
  - 有声明局部变量
NAME = ["a","b"]
def qupengfei():
    global NAME
    NAME = "d"
    print('c', NAME)
qupengfei()
  #      错误示例
NAME = ["a","b"]
def qupengfei():
    NAME = "c"
    global NAME   #报错
    print('d', NAME)
qupengfei()
#  - 无声明局部变量
    NAME = ["a","b"]
     def qupengfei():
         global NAME
         NAME = ["a"]
         NAME.append('e')
         print('c', NAME)
     qupengfei()

函数前向引用

# 先定义后调用
def foo():
    print('from foo')
    bar()

foo()

def bar():
    print('from bar')
#NameError: name 'bar' is not defined

#  正确

def foo():
    print('from foo')
    bar()

def bar():
    print('from bar')
foo()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值