day 15 - 1 built in function

Built-in function

Scope dependent locales() globals()

#These two groups are beginning to get mixed up
print(locals())  #Returns all names in the local scope
print(globals()) #Returns all names in the global scope
# global   Variables these two are used for declaration. They are keywords. The above two are built-in functions
# nonlocal variable

 

Iterator generator correlation next() iter() range()

##The following two do the same thing, take the value from the iterator, and the real function is to .__next__()
# iterator.__next__()  #This is the method in iterator
# next(iterator)       #This is a built-in function

# iterator = iter(Iterable)
# iterator = Iterable.__iter__()

##The value of range() is iterative but not an iterator
range(10)
range(1,11)
range(1,11,2)
print('__next__' in dir(range(1,11,2)))
print('__next__' in dir(iter(range(1,11,2)))) #When it calls iter() After transformation, it's an iterator

 

Help related

dir View the methods owned by a variable
print(dir([]))
print(dir(1))

# View method name and usage
help
help(str)

 

Variable callable() is generally used to detect whether a variable is a function

print(callable(print))    #True

a = 1
print(callable(a))        #False
print(callable(globals))  #True

def func():pass
print(callable(func))     #True

# If a method belongs to a variable of a data type, use the.call
# If a method does not depend on any data type, it is called directly -- built-in and custom functions

#import time  #When the calling module uses import, it actually calls the "import" ()
t = __import__('time')  #Not usually
print(t.time())

 

File related open()

# f = open('E:/py/log/test.txt')  #Open a file
print(f.writable())     #have access to writable() Check if the file can be written
print(f.readable())    #have access to readable() Check if the file is readable

 

Memory dependent

#id()
#hash() - For the same hash Data hash Value is always constant during the execution of a program
#     - Addressing mode of dictionary hash To address)

print(hash(12345))                               #can hash
print(hash('hsgda I don't want you to go, nklgkds'))     #can hash
print(hash('hsgda I don't want you to go, nklgkds'))     #can hash
print(hash(('1','aaa')))                            #can hash
print(hash([]))                                       #Must not hash    

 

input() print()

ret = input('Tips: ')
# print(ret)

print('Our motherland is a garden',end='')  #Specifies that the end character of the output defaults to \n
print('Our motherland is a garden',end='')
print(1,2,3,4,5,sep='|') #Specifies the separator between output multiple values
f = open('file','w')  #In fact, the screen is also a file, just looking at the input
print('aaaa',file=f)  #The default is to print to a file, file=f What was printed was written to the document
f.close()

 

Let's take an example -- print progress bar

import time
for i in range(0,101,2):
     time.sleep(0.1)
     char_num = i//2    #Print multiple *
     per_str = '\r%s%% : %s\n' % (i, '*' * char_num) \
         if i == 100 else '\r%s%% : %s' % (i,'*'*char_num)
     print(per_str,end='', flush=True) #flush Output content to stream file immediately without caching
print('complete')
#progress Bar  #For printing progress bar

 

Execution of string type code eval() exec() compile() [not commonly used]

#'print(123)' #This can't be done
#All of the following can be executed
exec('print(123)')
eval('print(123)')

print(eval('1+2+3+4'))   # Return value
print(exec('1+2+3+4'))   #no return value
# exec and eval Can execute code of string type
# eval With return value -- simple calculation with result
# exec No return value -- simple process control
# eval It can only be used when you know exactly what the code you want to execute, because there are great security risks

#exec Example printing a right triangle
code = '''for i in range(10):
    print(i*'*')
'''
exec(code)
#compile Compile (understand)
#exec Process class
code1 = 'for i in range(0,10): print (i)'
compile1 = compile(code1,'','exec')  #compile Compile to exec This model of
exec(compile1)

#eval Computing class
code2 = '1 + 2 + 3 + 4'
compile2 = compile(code2,'','eval') #This place '' An empty string is where the file is placed
print(eval(compile2))               #If you don't read from a file, put an empty string

#single Interactive class
code3 = 'name = input("please input your name:")'
compile3 = compile(code3,'','single')
exec(compile3) #Display interactive command when executing, prompt for input
print(name)

# name #After execution name Variable value
# "'pythoner'" Can be used for troubleshooting

 

Data type: bool int float complex
Only with data type cast

Let's take a look at complex

# Plural—— complex
# Real number: rational number
#         Irrational number
# Imaginary number
# 5 + 12j  === Composite number === complex
# 6 + 15j  # j is the unit of imaginary number in python


# Floating point number (finite cyclic decimal, infinite cyclic decimal)  != Decimals: finite cyclic decimals, infinite cyclic decimals, infinite acyclic decimals
# So floating point numbers don't include infinite acyclic decimals
# Floating point number
    #354.123 = 3.54123*10**2 = 35.4123 * 10  #In this process, the decimal point is floating, so it is called floating point number
f = 1.781326913750135970  #When the decimal number is very long, it will be inaccurate
print(f)                  #Because decimals in decimal system have a slight deviation when they are converted to binary decimals (all languages do)



##Base conversion (not used)
#bin    Binary system
#oct    Octal number system
#hex    Hexadecimal

#0 of which b 0o 0x The number follows the identifier in base
print(bin(10))  #0b1010
print(oct(10))  #0o12
print(hex(10))  #0xa

 

ABS divmod round pow
 sum min max
abs absolute value
divmod (useful: such as paging)
Round exact (auto round)
pow power operation

print(abs(-5))
print(abs(5))

print(divmod(7,2))   # div division mod Remainder
print(divmod(9,5))   # It's called division and redundancy

print(round(3.14159,3))

print(pow(2,3))   #pow exponentiation  == 2**3
print(pow(3,2))
print(pow(2,3,3)) #Remainder after power operation
print(pow(3,2,1))


#sum    Summation
#min    Calculate minimum
#max    Calculate maximum

#Two values can be passed
ret = sum([1,2,3,4,5,6])
print(ret)

ret = sum([1,2,3,4,5,6,],10)
print(ret)

ret = sum([1,2,3,4,5,6,10],)
print(ret)

print(min([1,2,3,4]))
print(min(1,2,3,4))
print(min(1,2,3,-4))
print(min(1,2,3,-4,key = abs)) #Compare sizes based on absolute values, key How to add conditions

print(max([1,2,3,4]))
print(max(1,2,3,4))
print(max(1,2,3,-4))
print(max(1,2,3,-4,key = abs))

 

Simple classification:

#####zip filter map sorted is very important
 ###Bool int float sum min max range input print hash open improve bytes len important
 ##ABS divmod round POW Eval exec dir reversed STR repr dict set frozenset enumerate zip filter map sorted
 Others

Tags: Python

Posted on Sun, 01 Dec 2019 20:27:20 -0800 by dast