Built-in functions, function variables, JSON

Source: Internet
Author: User

Tag: class res handles Chinese substitution GPO Site Map Object

A third-party variable is introduced to exchange the values of two variables:

A=1

b=2

C=0

C=a the value of a to c,c=1.

A=b the value of a to b,b=1.

B=c the value of C to b,b=1.

Print (A, b), running result: a=2 b=1

Exchange the value of two variables, only python can write this

A=1

b=2

The value of the A,b=b,a interchange variable

Print (A, b) run result: a=2 b=1

Exchange two values in a way that does not introduce a third-party variable

A=1
b=2
A=a+b #a3
B=a-b #b1
A=a-b #a2
Print (A, b), #运行结果: a=2 b=1
Built-in functions:
len int type str dict and so on are built-in functions
A=bin (1) decimal-to-binary, run result: 0b1 exists B so is the string type str
Print (A.replce (' 0b ')) replaces extra characters with spaces to display the correct binary values
Print (CHR) #打印数字对应的ascii       Run Result: A
Print (Ord (' A ')) #打印字符串对应的ascii码 Run Result: 65
Print (dir (1)) #打印传入对象的可调用方法, don't look at ' _xx_ '
A=[]
Print (dir (a))
Loop two lists at the same time, using a zip, the list with the smallest number to take the value
id=[1,2,3,0,4]
name=[' AA ', ' BB ', ' cc ', ' GG ']
name1=[' Zhang San ', ' John Doe ']
For i,k,s in Zip (id,name,name1):
Print (i,k,s) run Result: 1 AA
2 BB John Doe
Print (sorted (id,reverse=true)) #默认排序升序
Print (sorted (' 422435435 ', reverse=true))

Eval executes Python code, simple operations, defining data types and operations

A= ' + '
Print (eval (a)) Run Result: 2

Complex code cannot be executed, as shown below
A= ' def func (a):p
Print (eval (a)) operation error

exec executes complex code, and some automated code-execution sites use exec
S= ' Def a ():p rint (' sa ') '
EXEC (s) runs complex code
A () run result: SA
   map and filter   
The map loop invokes the function, placing the result of each function processing into a list
Def a (s):
If s%2==0:
return s
Else:
return ' OK '
Xa=[x For x in range (one)]
Res1=map (A,XA) before a value is a function name, the next is a circular list loop xa call function A (the value of the XA loop)
Print (list (res1)) does not write list, Print a Map object
Run Result: [0,ok,2,ok,4,ok,6,ok,8,ok,10]
filter filter returns F Al def A (s) if s%2==0:
return s If the input 0 return value is 0, not 0 is true, 0 is false so it is false;return true to determine that 0 is an even number is true will return to list--[0,2,4,6,8,10]
Else
Return False
Xa=[x for x in range (11)]
Res1=filter (A,XA)
Print (list (res1)) Run result: [2,4,6,8,10]

function is variable
def say (name):
Print (name)
Lyj=say say this function to lyj this variable lyj () equals say ()
Lyj (' lyj ') call function run Result: lyj
----------------------------------------------------------------
def add ():
Print (' Add product ')
Def view ():
Print (' View Products ')
def delete ():
Print (' Delete item ')
Choice=input (' Please enter the selection: ","). Stript ()
zd={
' 1 ': Add,
' 2 ': view,
' 3 ':d elete}
Zd[choice] () Zd[chice] is equivalent to a defined variable Add,view,delete
------------------------------------------------------------------
processing JSON: The common data type, JSON string is the string is written into the file is a string, read out is also a string
Import JSON
d={' sa ': ' Ad ',
' ASD ': ' Dasdas '}
Res=json.dumps (D,indent=4,ensure_ascii=false) turns the dictionary, list to JSON string,indent how many indents, Ensure_ascii can display the Chinese
F=open (' sa ', ' w ', encoding= ' utf-8 ')
F.write (RES)
F.close ()

--------------------------------------------------------------------------
Convert a JSON string to a list or dictionary with loads if JSON is a list style, it goes to the list type, and if the dictionary style, it goes to the dictionary type
f=open (' sa ', ' r ', encoding= ' utf-8 ')
R=f.read ()
Dict_r=json.loads (R)
The print (type (dict_r)) type is a dictionary
------------------------------------------------------------------------------
For file operations, dump writes files
F=open (' sa ', ' w ', encoding= ' utf-8 ')
Json.dump (d,f,ensure_ascii=false,indent=4)
Load Read file
F=open (' sa ', ' r ', encoding= ' utf-8 ')
Json.load (f)




Built-in functions, function variables, JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.