Python study notes nine _ built-in functions, json, modules

1. Built-in functions

Some functions that come with python can be used directly

print (max(111,12)) #take the maximum value 
print (min(12,23)) #take the minimum value 
print (abs(-1)) #take the absolute value 
print (round(11.23454,2)) #take a few Digit decimals 
print (sorted([1,23,45,66,33,2])) #Sort , can be used for dictionary sorting 
dic = {1:2,3:4,5:6 }
 print (sorted(dic.items ())) #Sort by the key of the dictionary 
print (sorted(dic.items(),key = lambda   x:x[1])) #Sort by the value of the dictionary 
print (id(dic)) #See the memory address 
print ( type(dic)) #See the data type 
print ()#Print 
input() #Input 
list()   #Turn list, string to list with .split 
set() #Turn set 
str() #Turn string, list to string with .join 
dict() #Turn dictionary int 
( ) #turn int 
float() #turn float type 
len() #take length

 

print (dir( ' abc ' )) #The way to view the string, don't look at the _xx_ 
print (dir({})) #Print the callable method of the incoming object 
print (all([1,2,3 ,4])) #Determine whether the values ​​in the iterable object are all true 
print (any([0,1,2,3,4,])) #Determine whether one of the values ​​in the iterable object is true 
print (bin(10)) #Decimal to binary, 0b1010 
print (bin(10).replace( ' 0b ' , '' )) # 1010 
print (hex(111)) #Number to hexadecimal 
print (oct( 111)) #Convert the number to octal 
print (bool( 's ' )) #Convert an object to a boolean type 
print (bytearray( ' abcd ' ,encoding= ' utf-8 ' )) # Convert a string to a modifiable bytes,bytearray(b'abcd') 
print ( chr(69)) #Print the ascii corresponding to the number 
print (ord( ' E ' )) #Print the ascii code corresponding to the string 
exec ( ' def a():print("I am a") ' ) #Execute python code 
print (eval( ' [] ' ))   #Execute python code, only simple, define data types and operations [],{} 
a = eval(' 1+2 ' )
 print (a)
 print (filter( lambda x: x > 5, [12, 3, 12, 2, 1, 2, 35]))   #Filter the following iterative objects according to the previous method 
print (map( lambda x: x > 5, [1, 2, 3, 4, 5, 6 ]))
 print ( frozenset ({1, 2, 3, 3})) #define   an unmodifiable set 
print (globals ())   #Return all variables in the program, what is returned is a dictionary 
print (locals()) #Return   local variables 
print (hash( ' aaa ' ))   #Hash a string into a number

Second, json processing

JSON is a data type of key-value data structure common to all languages, much like a dictionary in python

json file to python dictionary

python dictionary to string and write to json file

3. Module

A module is actually a python file
1. Standard modules, standard packages
#python comes with these modules,
import string, random, datetime, os, json that can be used directly by import
2. Third-party modules, some modules written by others, You have to install it before you can use
#1, the fool-like
pip install pymysql
1. What to do if there is no pip command:
1. Click python console in pycharm
2. Find the python installation directory
3, then add the scripts directory under the installation directory to Go to the environment variable and you can
ps: add 2 to the environment variable in the PATH
, Unknown or unsupported command 'install' How to solve this question
1. Open C:\strawberry\perl\bin\
2. Put all pip in this directory Change to another name , this has no effect on
others Package 1. Unzip the compressed package 2. Enter the unzipped folder 3. Run python setup.py install on the command line 3. Write your own python file







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324739478&siteId=291194637