赞
踩
s = {1,10,2,3,7,4}
print(s,type(s)) # {1, 2, 3, 4, 7, 10} <class 'set'>
s = {1,10,2,3,7,4,1,2,2}
print(s) # {1, 2, 3, 4, 7, 10}
s = set([1,2,3,4,5,4,5,4,5,4])
print(s,type(s)) # {1, 2, 3, 4, 5} <class 'set'>
s1 = set({'a':1,'b':2})
print(s1,type(s1)) # {'a', 'b'} <class 'set'>
s2 = set('python')
print(s2,type(s2)) # {'o', 't', 'n', 'p', 'y', 'h'} <class 'set'>
错误的:
s = {[1,2,3],[4,5,6]}
print(s) # TypeError: unhashable type: 'list'
s3 = {'a','b',1,2,3}
print(s3,type(s3)) # {1, 2, 3, 'a', 'b'} <class 'set'>
print(list(s3)[0]) # 1
s3 = {'a','b',1,2,3}
print('b' in s3) # True
s3 = {'a','b',1,2,3}
print('b' in s3) # 5
s3 = {'a','b',1,2,3}
s3.add(55)
s3.add(60)
print(s3) #{1, 2, 3, 'a', 55, 'b', 60}
s = {'a','b',1,2,3}
s1 = set('python')
s.update(s1)
print(s) #{1, 'a', 3, 2, 'p', 'h', 't', 'n', 'b', 'y', 'o'}
s = {'a','b',1,2,3}
result = s.pop()
print(result) # 结果是s的随机一个
s = {'a','b',1,2,3}
s.remove(1)
print(s) # {2, 3, 'b', 'a'}
s = {'a','b',1,2,3}
s.clear()
print(s) # set()
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
res = s & s2
print(s,s2,'result =',res) # {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} result = {3, 4, 5}
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
res = s | s2
print(s,s2,'result =',res) # {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} result = {1, 2, 3, 4, 5, 6, 7}
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
res = s - s2 # 返回 1,2
res = s2 - s # 返回 6,7
print(s,s2,'result =',res)
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
res = s ^ s2
print(s,s2,'result =',res) # {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} result = {1, 2, 6, 7}
a = {1,2,3}
b = {1,2,3,4,5}
res = a <= b
print(res) # True
a = {1,2,3}
b = {1,2,3,4,5}
result = {1,2,3} < {1,2,3}
print(result) # False
a = {1,2,3}
b = {1,2,3,4,5}
res = a >= b
print(res) # False
a = {1,2,3}>
b = {1,2,3,4,5}
res = a > b
print(res) # False
对象是内存中专门用来存数据的一块区域
函数用来保存一些可以执行的代码,并且可以在需要的时候对这些语句多次调用
创建函数
语法:
def 函数名([形参1,形参2,形参3...]):
代码块
def fn():
print('这是我第一个函数')
print('abc','bcd')
print('python')
print('hahaha')
# 打印fn
print(fn) # <function fn at 0x0058A930>
# 运行函数
print(fn())
1、在定义函数的时候,可以在函数名后面的括号里面定义数量不等的形参
2、多个形参我们用逗号(,)隔开
3、形参(形式参数)定义形参就是相当于在函数内部定义了变量,但是并没有赋值
4、实参(实际参数)函数定义时指定了形参,那么 调用函数的时候也必须传递是实参
5、实参将会赋值给对应的形参,有几个形参就传几个实参
# 定义一个函数来实现任意两个数字的和
def sum():
a = 1
b = 2
res = a + b
print(res)
sum()
结果:3
def fn2(a,b):
# a = None
# b = None
print('a =',a)
print('b =',b)
fn2(101,11)
结果:a = 101
b = 11
def fn(a = 5,b = 8,c = 10):
print('a =', a)
print('b =', b)
print('c =', c)
fn()
# 结果:
a = 5
b = 8
c = 10
位置传参也可以和关键字传参混合使用
混合使用的时候,必须把位置位置参数写到关键字参数前面,位置传参写到最后面
def fn(a = 5,b = 8,c = 10):
print('a =', a)
print('b =', b)
print('c =', c)
fn(b=1,c=2,a=3)
# 结果:
a = 3
b = 1
c = 2
def fn3(a):
print('a =',a)
b = 123
b = 'python'
b = True
b = False
b = [123]
fn3(fn) # 结果: a = <function fn at 0x00AEA930>
def fn4(a,b):
print(a + b)
fn4(1,1) # 2
fn4(1,'2') # TypeError: unsupported operand type(s) for +: 'int' and 'str'
def fn5(a):
# 在函数中对形参重新复制不会影响其他变量
a = 30
print('a =', a, id(a)) #结果 a = 30 1381525120
c = 10
c = [1,2,3]
fn5(c)
print('c =',c,id(c)) # 结果 c = [1, 2, 3] 16795128
def fn6(a):
# 如果形参在执行的时候,当我们通过形参去修改对象时,会影响到指向该对象的变量
a[0] = 50
print('a =', a, id(a)) # a = [50, 2, 3] 16795128
c = 10
c = [1, 2, 3]
fn6(c)
print('c =', c, id(c)) # c = [50, 2, 3] 16795128
def s(*a):
# 定义一个变量来保存 结果
r = 0
# 遍历元祖,将元祖中的元素取出来并进行累加
for n in a:
r += n
print(r)
s(1,2,3,4,5,6)
结果: 21
def fn2(a,b,*c):
print('a =',a)
print('b =',b)
print('c =',c)
fn2(1,2,3,4,5,6)
结果: a=1
b=2
c=(3,4,5,6)
def fn2(a,*b,c=6):
print('a =',a)
print('b =',b)
print('c =',c)
结果: a = 1
b = (2, 3, 4, 5, 6)
c = 6
def fn2(*a,b,c):
print('a =',a)
print('b =',b)
print('c =',c)
fn2(1,2,3,4,b=5,c=6)
结果: a = (1, 2, 3, 4)
b = 5
c = 6
def fn2(*,a,b,c):
print('a =',a)
print('b =',b)
print('c =',c)
fn2(a=3,b=4,c=5)
def fn3(b,c,**a):
print('a =', a,type(a))
print('b =',b)
print('c =',c)
fn3(b=1,d=2,c=3,e=4,f=5)
# 结果:a = {'d': 2, 'e': 4, 'f': 5} <class 'dict'>
# b = 1
# c = 3
def fn4(a,b,c):
print('a =', a)
print('b =', b)
print('c =', c)
t = (5,6,7,8)
d = {'a':100,'b':200,'c':300}
fn4(**d)
# 结果:
a = 100
b = 200
c = 300
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。