程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - advanced features

編輯:Python

One 、 Generative

1. List generator

A list generator is an expression used to generate a list in a specific syntax form . yes Python Provides a concise form of generating lists , Can quickly generate a new list.
Common grammar :[exp for iter_var in iterable]
Syntax format with filter function : [exp for iter_var in iterable if_exp]
Loop nesting syntax format : [exp for iter_var_A in iterable_A for iter_var_B in iterable_B]

such as There are the following requirements :

import string
import random
li = []
for i in range(100):
s = ''.join(random.sample(string.ascii_letters,4))
li.append(s)
print(li)

You can use the following line of code to do :

codes = [''.join(random.sample(string.ascii_letters,4)) for i in range(100)] ## The generated string Stored in a list
print(codes)

Empathy , Similar needs

# nums = []
# for num in range(1,101):
# if num%3==0:
# nums.append(num)
# print(nums)

It can still be done in one line of code

nums = [num for num in range(1,101) if num%3==0]
print(nums)

2. Set generative

s= {i**2 for i in range(10)}
print(s)

3. Dictionary generative

d = {i:i**2 for i in range(10)}
print(d)

Two 、 generator

1. The first method of generator implementation

### The first method of generator implementation : Rewrite the generative expression into a generator
nums = (i**2 for i in range(1000))
print(nums) ## Output generator
# Print generation elements , Method 1 : Generate... One by one Below next Indicates build by build Say it when you use it
print(next(nums)) #0
print(next(nums)) #1
print(next(nums)) #4
print(next(nums)) #9
# Print generation elements , Method 2 :for Cycle generation
for i in nums:
print(i)

2. The second method of generator implementation

### The second part of the generator implementation 2 Methods :yield keyword
# return: Function encountered return Just go back to ,return The following code does not execute .
# yield: encounter yield Then stop executing the code , When called again next When the method is used , Will continue from where it stopped last time , encounter yield stop it .
def login():
a = 1
return 'login'
# print(1) ## return The following will not be implemented So this sentence is invalid
print(login())
def login():
print('step1')
yield 1
print('step2')
yield 2
print('step3')
yield 3
g = login()
print(next(g))
** Here is the output :**
step1
1

3、 ... and 、 Closure

Its characteristics are as follows :

# 1. Functions are nested inside functions
#2. The return value of an external function is a reference to an internal function
#3. Internal functions can use variables of external functions

##1. Functions are nested inside functions
# def tiemit():
# def warpper():
# print('warpper')
# print('timeit')
# tiemit()
Output :
timeit
#2. The return value of an external function is a reference to an internal function
def timeit():
def warpper():
print('warpper')
print('timeit')
return warpper
in_fun = timeit() ## wrapper function , in_fun In essence wrapper function
in_fun()
Output :
timeit
warpper
#3. Internal functions can use variables of external functions
# def timeit(name):
# def warpper():
# print('warpper'+name)
# print('timeit')
# return warpper
# in_fun = timeit(name='mananananan') ## wrapper function , in_fun In essence wrapper function
# in_fun()
Output :
timeit
warppermananananan

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved