Pre-course review
Iterator
Iterator object must be iterable, iterables not necessarily iterator object
Iterables
Containing the __iter__
data type of the method
In addition to numeric types are iterables
Iterator object
Containing the __iter__,__next__
data type of the method
File is the iterator object
Iterables plus __iter__
will become iterator object
Iterator object plus __iter__
method is still the iterator object
for circulation principle
lt = [1,2]
lt_iter = lt._iter_()
while True:
try:
print(lt_iter._next_())
except StopIteration:
break
A triplet of expressions
Condition holds if condition else condition is not satisfied
List comprehensions
[i fo i in range(10)]
Dictionary of formula
{i:i for i in range(10)}
zip
A plurality of read iterables disposable objects each iteration of the elements, the elements makes up
Generator expressions
(I for i in range (10)) # save memory space - "Generator -" old hen
[I for i in range (10)] - "list -" a basket of eggs
Builder
Nature is an iterator, is custom iterators, function containing the yield keyword
def ge():
yield
yield
- Pause function
- A yield value taken by the next
def range(start,stop,step):
while start < stop :
yield start
start += step
for i in range(1,10,2):
print(i)
Recursion
Function calls itself, there is an exit condition
count = 0
def a():
global count
print(count)
if count == 100:
return
count += 1
a()
a()
Anonymous function
A named function
Before we given function are well-known function, which is based on the use of the function name.
Anonymous function
Anonymous function, he has no name binding, ie, once recovered, brackets to run.
Anonymous function syntax:
lambda Parameters: Return Value
f = lambda x, y:x * y
res = f(1,2)
print(res)
Built-in functions
python interpreter built-in method:
grasp:
- bytes
res = bytes('中国',encoding='utf8')
print(res)
b'\xe4\xbd\xa0\xe5\xa5\xbd'
res = '你好'.encode('utf8')
print(res)
b'\xe4\xbd\xa0\xe5\xa5\xbd'
- chr () / word ()
CHR () refer to a digital ASCII code table converted into a corresponding character; the ord () converts the character into a corresponding digital.
print(chr(65))
A
print(ord('A'))
65
- divmod () columns
print(divmod(10,3)) #取整/取余
(3,1)
- enumerate () iteration with indexes
l = ['a','b','c']
for i in enumerate(l):
print(i)
(0,'a')
(1,'b')
(2,'c')
- eval () translates into a string data type, the string is removed, the stay.
lis = '[1,2,3]'
lis_eval = eval(lis)
print(lis_eval)
[1,2,3]
- hash () whether a hash, the variable type is not hashed
print(hash(1))
1
To understanding:
- ABS () absolute value.
print(abs(-13)) # 求绝对值
13
- all () within iterables elements are all true, then it returns true.
print(all([1,2,3,4]))
print(all([]))
Ture
False
- any () iterables there is an element is true, True.
print(any([1,2,3,0]))
print(any([]))
True
False
- bin () / oct () / hex () binary, octal, hexadecimal conversion.
print(bin(17))
print(oct(17))
print(hex(17))
0b10001
0o21
0x11
- dir () of all the methods listed modules
import time
print(dir(time))
- frozenset immutable set () (non-set change, similar to the tuple)
s = frozenset({1,2,3})
frozenset({1,2,3})
- globals () / loacals () view the global variables / variable view the current location
def func():
s = 's1'
print(globals())
print('locals():',locals())
func()
locals():{'s':'s1'}
- pow()
print(pow(3,2,3)) # (3**2)%3
0
- round () rounding
print(round(10.33))
10
- slice
lis = ['a','b','c']
s = slice(1,4,1)
print(lis[s])
['b', 'c']
- sum()
print(sum(range(100)))
4950
__import__
() Introduced into the module string
time = __import__('time')
print(time.time())
Exception Handling
Exception Handling: report a mistake, for processing
Abnormal capture can only capture logic error, not a syntax error
print(1)
num = input('请输入数字:')
try:
1/int(num) # 报错之后,不运行下面的代码
except ZeroDivisionError:
print('傻逼,不能输入0')
print(2)
except Exception as e: # 万能异常,只要有错误,就捕捉
print(e) # 错误的描述信息
Process-oriented programming
" Process-oriented " (Procedure Oriented) is a process-centric programming ideas. "Procedure-oriented" may also be referred to as "record-oriented" programming ideas, they do not support rich "object-oriented" properties (such as inheritance, polymorphism), and they do not allow mixing and persistent state domain logic.
Is to analyze the steps required to solve the problem, then use the function to implement these steps, step by step, when used in a call to a turn on it.