<> One , Derivation

<>1, Push to expression (python specific )
# Create a 1 reach 10 list list_a = [for i in range(1, 11)] print(list_a)
​ 1 reach 10 List of
list_a = [i for i in range(1,11)] # Create an automatic 1 reach 10 List of print(list_a) # for
The code block below the loop moves to for In front of , And this derivation will put i Each time the value of is retained
​ 1 reach 10, Even list
# Create a list , Let the elements in this list be 1 reach 10 Even number of list_a = [i for i in range(1, 11) if i % 2 == 0]
print(list_a) # Yes if judge , The judgment sentence is written in for After the loop ,i According to i True and false reserved value of
​ 1 reach 10, Cardinality variable string , Even expansion 10 Times list
list_a = [i*10 if i % 2 == 0 else str(i) for i in range(1, 11)] print(list_a)
# if Conditional judgment contains else The words of , Write it as a trinomial operation for Cycle front
​ List derivation , It mainly performs less complicated functions , The code for the derivation must be written in one line !!!

<>2, Dictionary derivation
# Create a dictionary , In the form of key value pairs dict_a = {i: j for i, j in [('a', 1), ('b', 2), ('c', 3)]}
print(dict_a) # The values of tuples in the list are assigned to i,j And generate a dictionary
<> Two , iterator

*
Before an iterator can be implemented, there must be an iteratable object

*
What objects are iteratable objects : If an object implements __iter__ This magic method

<>1, Create a list iterator

​ iterator : Yes __iter__ and __next__ The object of the method
a = [1, 2, 3, 4, 5] # Create an iteratable object b = iter(a) # hold a adopt iter This function is converted to an iterator , Assign to b print(b)
<list_iterator object at 0x0000022BA5DCB080> # List iteration objects

""" The iterator itself needs to support these two methods to be called an iterator : 1,iterator.__iter__() 2,iterator.__next__() Functions that generate iterators :
1,iterator = iter(a) 2,iterator = a.__iter__() Value in iterator : 1,next(iterator)
2,iterator.__next__() be careful , If the iterator has finished its value , Will return StopIteration error """
​ The difference between iteratable objects and iterators :

​ 1, Elements can be extracted from iteratable objects , It's not really taken out , It still exists

​ 2, To take an element from an iterator is to take it out directly , Take it and it's gone

<> Three , generator

* A list is generated by a list derivation , The generator is generated by tuple derivation
* Generators belong to iterators , It's done __iter__ and __next__ These two magic methods
<>1, Create a generator
a = (i for i in range(1, 11)) print(a) <generator object <genexpr> at
0x0000023A61639620> # A generator object is implemented by tuple derivation
<>2, Yes yield Keyword generator function
def func(num): a = 0 while a<num : yield a a += 1 #
This function uses the yield keyword , This is a generator function , The object returned is a generator object """ 1, Returns the function object yield An object : 2, Pause this function
3, Wait for the next time next Reactivate function """

<> Four , modular

* What is a module ? .py File for
<>1, The function of using another file in the same directory in a file !





​ Import only one tool in a toolbox , Three import methods :

​ 1,from hold-all import tool 1, tool 2, tool 3

​ 2, from hold-all import * Import all tools

​ 3,import pandas as pd Alias the module



<>2, If the function name of a module is too long , Alias

​ import pandas as pd

​ from modular import The name of this module is so long and disgusting as func

<>3, If two files are not in the same directory
imoprt sys print(sys.path) # sys.path Print out python Module location of sys.path.append(r
'D:/python/PycharmCode/test/test') # example # Just import the directory where the module is located , You don't have to be precise to the file
​ python Go back when importing a file sys.path Look inside , Import found , Error not found

<>4, Modules have red lines when importing modules

<> Five , Packages and package management

<>1, The concept of package

​ 1.1, Put a lot of modules in one folder , You can form a package

​ 1.2, When a folder has a __Iinit__.py File for , The folder name is the package name

​ 1.3, stay pycharm Create a package in

<>2, Package management

​ When many modules are placed in a folder , In order to facilitate the application of modules in the package , Package management is introduced

<>3, Import module in package

<>


Technology
©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )