程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Getting started with Python must see the use of simple functions in the three Python

編輯:Python

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. .

Time function

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 :

expand (datetime modular ):

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 .

Calendar function

datetime1 \= datetime2 + timedelta
timedelta \= datetime1 \- datetime2

Running results :

Random number function

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.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved