One: List deduction formula
Representation: [Variable (processed data) for variable i in iterated data type]
Example one: Any number within 30 that can be divisible by 3
for inch if is 0] Print (multiples) # Output: [0, 3, 6, 9, (+), (+) ,-
Example two: The square of all numbers within 30 that can be divisible by 3
for inch if i%3==0]print(L1)
Example three: Find all names in a nested list with two ' E ' names
names = [['Tom','Billy','Jefferson','Andrew','Wesley','Steven','Joe'], ['Alice','Jill','Ana','Wendy','Jennifer','Sherry','Eva']]L1=[name forIinchNames forNameinchIifName.count ("e") ==2]Print(L1)
PS: The list parsing [] replaced by () is the generator expression, in which the list deduction is more intuitive, accounting for memory, generator expression is not easy to see the content, save memory.
for inch if i%3==0)print(L1) #<generator Object <genexpr> at 0x000001de23d4de58>Print(L1. __next__())
Two: Dictionary derivation formula
Example one: Swap the key and value of a dictionary
Mcase = {'a'b': in Mcase}print(mcase_frequency)
Three: Set deduction formula
Calculates the square of each value in the list, with its own de-weight function.
for inch [1,-1, 2]} Print (squared) # Output:set ([1, 4])
Four: Built-in functions in Python
The use of 1,print
#1, remove line breakPrint('666', end="')Print('666')#2, adding a separatorPrint(1,2,3,4,5,sep='|')#1|2|3|4|5#3, writing to filef = open ('file','W', encoding='Utf-8')Print(666,file=f) f.close ()
2,
Locals () stores local variables.
Globals () stores the global variables, function names, and so on.
def func (): ' Alex ' Print (Locals ()) Print (Globals ()) func ()
3, Help (XXX) will be the object you query all the information is taken out.
4, ABS () take absolute value
Print (ABS ( -1)) Print (ABS (1))
5, Maximum and minimum values
# 1 Find the absolute maximum number of ret = max (1,2,-3,key=abs)print(ret)#2 to find the minimum ret = min ( [+/-]) Print (ret)
6,sum summation
A=sum ([1,2,8])print(a)# calculates the sum in the list, appends a number and ret = SUM ([1,2,3],10 )print(ret)
7,callable determine if this variable is a function name
' Alex ' Print (callable (name)) def func (): Print (666) Print (Callable (func))
8, hash () converts an object to a hash value through the rules of a hash table
Print (Hash ('fdsakfhdsafsda')) Print (Hash ('fd')) Print (Hash ('a','b' )) Print (hash (True))
9, all the values in the Iteration object are converted to bool values if all are true then return True
Print (All ([1,2,3,0]))
10, binary conversion
# decimal into binary Print (Bin) # Converts decimal into octal Print (Oct (9)) # Convert decimal to hexadecimal Print (Hex (33))
11, data type float, complex (plural)
Print (1.35432,type (1.35432)) Print (Float (3))
Divmod () Sub-page
Print # (quotient, remainder)
Enumerate (Iterable,start start value) enumeration
L = [' cell phone ', ' phone ', ' doll ',]
For k,v in Enumerate (l,1):
Print (K,V)
Eval has a return value that removes the quotation marks around the string and returns the contents
EXEC has no return value to remove the quotation marks around the string, executing the code inside
" {' name ': ' Alex '} " Print (eval (s), type (eval (s))) # {' name ': ' Alex '} <class ' Dict ' > " 1+2+3+4 " Print (eval (S1)) # Ten " " For i in range: print (i)'exec' (code)
Builder expressions in Python, various derivations and built-in functions