Hello everyone , I'm Charlie , Today I'd like to share with you some Python Basics , Let's see ~
One 、 Function introduction
So called functions , Is refers to : Make up the code of some specific functions as a whole , This whole is called a function .
Two 、 Function definition and call
What is the definition of a function : It is equivalent to defining a function that can complete certain events ; It's like building a tool .
Define function format :
def test(): print('---- Hee hee ----') print('---- This is my first function ----')
What is a function call : If you just define functions , It can't be executed automatically , You have to call it to .
Generally speaking : Defining a function is like building a tool , Calling a function is equivalent to using this tool to do what you want to do .
\# Define a function def test(): print('---- Hee hee ----') print('---- This is my first function ----') \# Call function test()
Running results :
Python It's more and more popular with developers , One of the reasons is : Rich functions , Basically, the functions you need Python Have it all. .
In development , You often need to print some debugging information , At this time, we have to output the time , This takes some time function .
1. Get current date :time.time()
import time \# introduce time modular currentTime \= time.time() print(" The current timestamp is :", currentTime)
Running results :
2. Get a time stamp in the form of a tuple :time.local(time.time())
import time localtime \= time.localtime(time.time()) print ( " The local time is :", localtime)
Running results :
import time localtime \= time.asctime( time.localtime(time.time()) ) print ( " The local time is :", localtime)
Running results :
1. Date output format datetime => string
import datetime now \= datetime.datetime.now() now.strftime('%Y-%m-%d %H:%M:%S')
2. Date output format string => datetime
import datetime t\_str \= '2019-04-07 16:11:21' d \= datetime.datetime.strptime(t\_str, '%Y-%m-%d %H:%M:%S') print(d)
Running results :
strptime yes datetime Class static methods .
3. Date comparison operation
stay datetime There is timedelta class , Objects of this class are used to represent a time interval , Like the difference between two dates or times .
Construction method :
import datetime datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
All parameters have default values 0, These parameters can be int or float, Positive or negative .
Can pass timedelta.days、tiemdelta.seconds And so on .
timedelta Class , Support plus 、 reduce 、 ride 、 Division operation , The result is the same timedelta Class .
import datetime year \= datetime.timedelta(days\=365) t\_years \= year \*10 new\_years \= ten\_years \- year print(t\_years) print(new\_years)
Running results :
date、time and datetime Class also supports timedelta Plus 、 Subtraction operation .
datetime1 \= datetime2 + timedelta timedelta \= datetime1 \- datetime2
such , It is very convenient to realize some functions .
datetime1 \= datetime2 + timedelta timedelta \= datetime1 \- datetime2
Running results :
import random a \= random.uniform(1, 5) print("a =", a) b \= random.randint(10, 50) print ("b =", b) c \= random.randrange(0, 51, 2) print ("c =", c)
Running results :
3、 ... and 、 summary
This article explains in detail Python The definition of basic function , call . This paper introduces the use of three commonly used functions . Through a small project to make readers better understand and use function , Hope it can help you study better Python.