Master Python Functions: From Basics to Time & Random Utilities
This article introduces Python function fundamentals, shows how to define and call functions, demonstrates common time‑related functions from the time and datetime modules, and provides examples of calendar and random number utilities, all illustrated with clear code snippets and output screenshots.
Hello everyone, I'm a Go advanced learner sharing some Python basics.
1. Function Overview
A function groups specific code into a reusable unit.
2. Function Definition and Invocation
Defining a function creates a tool you can later call to execute its code.
def test():
print('----嘻嘻----')
print('----这是我的第一个函数----')Calling the function runs its body.
# Define a function
def test():
print('----嘻嘻----')
print('----这是我的第一个函数----')
# Call the function
test()Output:
Python’s rich standard library provides many built‑in functions, making it popular among developers.
Time Functions
Printing timestamps and local time often helps with debugging.
1. Get current timestamp: time.time()
import time # import time module
currentTime = time.time()
print("当前时间戳为:", currentTime)2. Get local time tuple: time.localtime(time.time())
import time
localtime = time.localtime(time.time())
print("本地时间为 :", localtime)import time
localtime = time.asctime(time.localtime(time.time()))
print("本地时间为 :", localtime)Extended: datetime module
1. Format datetime to string
import datetime
now = datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')2. Parse string to 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)strptime is a static method of the datetime class.
3. Date comparison with timedelta
The timedelta class represents a time interval and supports arithmetic.
import datetime
year = datetime.timedelta(days=365)
t_years = year * 10
new_years = t_years - year
print(t_years)
print(new_years)date, time, and datetime objects can be added to or subtracted from timedeltas.
datetime1 = datetime2 + timedelta
timedelta = datetime1 - datetime2This makes many operations convenient.
Calendar Function
import calendar
dar = calendar.month(2016, 8)
print("2016年8月份的日历:")
print(dar)Random Number Functions
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)3. Summary
This article explained how to define and call Python functions, introduced common time‑related functions, and demonstrated calendar and random utilities, providing practical code examples to help readers better understand and apply functions in Python.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
