1. The first-class objects and use the function name
Function name can be assigned as a value
def func():
print(1)
print(func)
a = func
a() # <function func at 0x000001A24F8E9B70> 1
Function name can be used as an element stored in the container
def func():
print(1)
lst = [func,func,func]
for i in lst:
i() # 1 1 1
Function name can be used as an argument to another function
def func(f):
f()
def foo():
print(123)
func(foo) # 123
It can be used as the return value of the function name
def func():
def foo():
print(123)
return foo
a = func()
a() # 123
2.f format
F formatted role
Filling string
s = f"你好{'常鑫'}"
s1 = F"{你好'常鑫'}"
Variable filling
s = "常鑫"
S1 = f"你还是挺好的{s}"
print(s1)
Filling formula
s1 = "{35+15}"
print(s1)
Filled with expressions
a = 10
b = 20
s1 = f"{a if a>b else b}"
print(s1)
Filled with braces
s1 = f"{{{{{{'常鑫'}}}}}}"
print(s1) # {{{'常鑫'}}}
3. iterator
Iterators, a tool
Iterables:
list,dict,str,set,tuple -- 可迭代对象 使用灵活
Check whether the iteration
method one:
list.__iter__()
Second way:
View source
Three ways:
print(dir(list))
As long as official statements have __iter __ () method is iterable
Advantages and disadvantages
1. Advantages
1.1 flexible
1.2 Direct View value
2. shortcomings
2.1 consumes memory
Iterator
As long as official statements have __iter __ () method __next __ () method is iterator
句柄"f"是个迭代器
f = open("xxxx","w")
f.__iter__()
f.__next__()
lst = [1,2,3,4,6]
new_list = lst.__iter__() #将可迭代对象转换成迭代器
new_list.__iter__()
new_list.__next__()
s = "123434"
new_s = s.__iter__() #将可迭代对象转换成迭代器
print(new_s)
new_s.__iter__()
new_s.__next__()
new_s = s.__iter__() #将可迭代对象转换成迭代器
print(new_s)
new_s.__iter__()
print(new_s.__next__())
print(new_s.__next__())
print(new_s.__next__())
s = "12343" # 更改版for的本质
s = [1,2,3,4,5,7]
count = len(s)
new_s = s.__iter__()
while count:
print(new_s.__next__())
count -= 1
s = "12345"
new_s = s.__iter__()
while True:
try:
print(new_s.__next__()) # for真实本质
except StopIteration:
break
# except Exception:
# print("我是万能的!")
# break
4. recursive
definition
1. calls itself (continue to call themselves) - Dead recursion
2. there is a clear termination condition
Meet the above two is valid recursive
Delivery: execution conditions have been met until the end
Return: From the beginning of the end of back-off condition
Official statement: maximum level of 1000, the actual test 998/997
示例: 猜3
def age(n): # 1,2,3
if n == 3:
return "猜对了"
else:
return age(n+1)
print(age(1))
break down
def age2(n):
if n == 3:
return "猜对了"
def age1(n):
if n == 3:
return "猜对了"
else:
age2(n+1)
def age(n):
if n == 3:
return "猜对了"
else:
age1(n+1)
age(1)
to sum up
Iterables:
Advantages: flexible, you can directly see the value of
disadvantages: total memory, not the value of the iteration
Iterator:
Advantages: save memory, inert mechanism
Disadvantages: not flexible, the operation is more complicated, can not see the elements directly
Iterator characteristics:
1. disposable (no run out)
2 can not retrograde (not reverse)
3. an inert mechanism (save memory)
What is iterables:
has many proprietary methods, with __iter __ () method is a iterable
What is an iterator:
has __iter __ () and __next __ () method is iterator
When using iterators: when a container large amount of data when using an iterator