In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use three simple functions in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use three simple functions in Python.
A brief introduction to functions
Hello, everyone. I am an advanced Go. The so-called function means that the code of some specific function is grouped into a whole, which is called a function.
Second, function definition and call
What is the definition of a function: it is equivalent to defining a function that can accomplish certain events; it is like creating a tool.
Define the function format:
Def test (): print ('- hee -') print ('- this is my first function -')
What is a function call: if a function is only defined, it cannot be executed automatically and must be called.
In popular terms: defining a function is equivalent to creating a tool, and calling a function is equivalent to using this tool to do what you want to do.
# define a function def test (): print ('-') print ('- this is my first function -') # call function test ()
Running result:
One of the reasons why Python is becoming more and more popular among developers is that Python has a wealth of functions and basically all the functions it needs.
Time function
In development, you often need to print some debugging information, and then you have to output time, which requires some time functions.
1. Get the current date: time.time () import time # introduces the time module currentTime = time.time () print ("current timestamp is:", currentTime)
Running result:
two。 Get the timestamp in tuple form: time.local (time.time ()) import time localtime = time.localtime (time.time ()) print ("local time is:", localtime)
Running result:
Import time localtime = time.asctime (time.localtime (time.time () print ("Local time:", localtime)
Running result:
Extension (datetime module):
1. Date output formatted datetime = > string
Import datetime now = datetime.datetime.now () now.strftime ('% Y-%m-%d% HGV% MRV% S')
two。 Date output formatted string = > datetime
Import datetime t_str = '2019-04-07 16 t_str 11 print 21D = datetime.datetime.strptime (t_str,'% Y-%m-%d% HV% MV% S')
Running result:
Strptime is a static method of the datetime class.
3. Date comparison operation
In the datetime module, there is a timedelta class whose objects are used to represent a time interval, such as 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 a default value of 0, which can be int or float, positive or negative.
The corresponding time value can be obtained through timedelta.days, tiemdelta.seconds, etc.
An instance of the timedelta class that supports addition, subtraction, multiplication, division, and so on, and the result is also an instance of the 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 result:
The date, time, and datetime classes also support addition and subtraction with timedelta.
Datetime1 = datetime2 + timedelta timedelta = datetime1-datetime2
In this way, it is very convenient to achieve some functions.
Calendar function import calendar dar = calendar.month (2016, 8) print ("calendar for August 2016:") print (dar)
Running result:
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 result:
At this point, I believe you have a deeper understanding of "how to use the three simple functions in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.