Learn python the third day.

Today content #: 
# the remaining portion of the function
# built-in module
# module package

1 function defined three ways without reference function
External parameters necessary to receive incoming 
DEF foo ():
Print ( '.. from foo')
foo ()

with a reference function
required to receive external parameters passed
DEF Login (User, pwd)
Print (User)
parameter passing a plurality a not less

Login ( 'CC', '123')
Login ( 'CC', '123', '111') # multiple, given
login ( 'cc') # low, given

X = 10
Y = 20 is
IF X > Y:
Print (X)
the else:
Print (Y)

comparing two operand size
DEF MAX2 (X, Y):
IF X> Y:
Print (X)
the else:
Print (Y)
MAX2 (10,30)



empty functions
encountered Some of the more difficult to achieve the function, can cause temporarily unable to continue writing code,
it is generally in the production and development, will be all the functions defined as empty functions
DEF FUNC ():
Pass #pass do not mean anything

'' '
# function return value
Results # call the function, needs to receive an internally generated function body, the return return value
# DEF MAX2 (X, Y):
# IF X> Y:
# return (X)
# the else:
# return (Y)
# RES = MAX2 (10,5)
# Print (RES)
'' '


' ''
2 function object #
# refers to the function name memory address pointed
DEF FUNC ():
Pass
Print (FUNC)
FUNC ()

DEF func2 ():
Pass
= {dict1
'. 1': FUNC,
'2': func2
}

Choice = iNPUT ( 'enter number function:'). Strip ()

# == IF Choice '. 1':
# FUNC ()
# Choice the else == ' 2 ':
# func2 ()

#. 1
# if letting the user select a key value corresponding to the target function, the function call
IF choice in dict1:
dict1 [choice] () # dict1 ['. 1 ']




'''
3 nested functions:
nested definitions: in the function, defined function
nested calls:

# function nested definitions
DEF func1 ():
Print ( 'func1 ...')
DEF func2 ():
Print ( '... func2 ')
DEF func3 ():

return func3
return func2


' ''
# internal function by the function value of the function call
# func2 = func1 ()
# func3 = func2 ()
# func3 ()

# nested function calls
DEF func1 ():
Print ( 'func1 ...')

DEF func2 ():
Print ( 'func2 ...')

DEF func3 ():
Print ( 'func3 ...')

func3 ()
func2 ()
func1 ()

4. namespace # 
# Python interpreter comes: built-in namespace
# py custom in the document against the leftmost definition: global name space
defined inside # functions: local namespace

name = 'CC'
DEF func1 ():
Print (name)

DEF func2 (): ...
Print ( 'func2 ...')
#Print (name, 'global variables')

func1 ()

Packet module # 

# Import module name
# Import B
# from
# B module to import a file
# automatically performs a code file

# Import from a B
# __name__ __: of Ba
# a

a.py
Print # ( 'from A') 
DEF func1 ():
Print ( 'from func1')

Print (__ name__) main #

# test function for
IF __name__ __ == '__ mian__':
func1 ()


# Common modules (built-in modules) 
# Time 
# JSON 
# OS 
# SYS 



# . 1 # Time 
# Import time Time # Import module 
# # obtain a timestamp 
# Print (the time.time ()) 
# # Wait 2S 
# the time.sleep (2 ) 
# Print (the time.time ()) 
# 
# 
# 2 # OS 
# Import OS 
# if # is determined cc.txt file exists 
# Print (os.path.exist ( 'cc.txt')) True # 
# Print (OS .path.exist ( 'cc1.txt')) False # 
# Print (os.path.exist (r '... \ cc.txt')) 
# 
## Receiving the root directory of the current file 
# Print (os.path.dirname (__ file__)) 
# 
# 
# 
# . 3 SYS # 
# Import SYS 
# obtain python file path environment variable # 
# Print (the sys.path) 
# # The root environment variable added to the item 
# sys.path.append (os.path.dirname (__ file__)) 
# Print (the sys.path) 


# dumps: sequence of 
# 1 is converted into json dictionary data 
# 2 then json data is converted into a string 

RES = json.dumps (USER_INFO)
 Print (RES)
 Print (type (RES)) 
with Open ( ' user.json ' , ' wt' , Encoding = ' UTF-. 8 ' ) AS F: 
    f.write (RES)



 . 4 # JSON 
Import JSON 
USER_INFO = {
     ' name ' : ' CC ' ,
     ' pwd ' : ' 123 ' 

} 


RES = json.dumps (USER_INFO )
 Print (RES)
 Print (type (RES))    # results: into double quote character string 


with Open ( ' user.json ' , ' wt ' , encoding =' UTF-. 8 ' ) AS F: 
    f.write (RES) 


# lodes: deserialize 
# json.lodes () 
# data of a file into memory json 

with Open ( ' user.json ' , ' wt ' , encoding = ' UTF-. 8 ' ) aS F: 

    # read character string is obtained 
    RES = reached, f.read () 

    # Print (type (user_dict)) # <class 
    # lodes json string format into type dict 

    user_dict = json.lodes (RES)
     Print (user_dict)   # 'name': 'CC', 'pwd': '123'
    print(type(user_dict))   # <class 'dict'>



# dump
user_info={
    'name':'cc',
    'pwd':'123'

}
with open('user.json','wt',encoding='utf-8') as f:
    # str1=json.dump(user_info)
    # f.write()

    json.dump(user_info,f)    # dump 自带write功能




# lode
with open('user.json','wt',encoding='utf-8') as f:
    # res=f.read()
    # user_dict=json.lodes(res)
    # print(user_dict)

    # lode:自动触发f.read()
    user_dict=json.lode(f)
    print(user_dict)

 

'' ' 
Crawler
to extract valuable data only want
' ''

'' '
HTTP protocol:
request the URL of:
http://www.baidu.com/
request method:
GET
request header:
the cookie: may need to focus on
User-Agent : to prove that you are a browser
NOTE: the browser's request header to find
Mozilla / 5.0 (Windows NT 10.0 .....)
Host: www.baidu.com
'' '

# Requests module
# PIP3 install Requests
# pip3 install -i Tsinghua source module name address
# pip3 install -i HTTP: //

Import Requests

Response = requests.get (URL = 'HTTP: //www.baidu.com/')
response.encoding = 'UTF-. 8'
Print (Response) # <Response [200 is]>
Print (Response.status_code) # 200

Returns a response text #
# Print (response.text)
Print (type (response.text)) # <class 'STR'>
with Open ( 'baidu.html', 'W', encoding = 'UTF-. 8') AS F :
f.write (response.text)



# crawling pear video
Import Requests
RES = requests.get ( '
https://video.pearvideo.com/mp4/adshort/20190613/cont-1565846-14013215_adpkg-ad_hd.mp4')
print(res.content)

with open('视频.mp4','wb') as f:
f.write(res.content)



 

Guess you like

Origin www.cnblogs.com/feiyufei/p/11014544.html