Functions and the Standard Library
Harry
· 14 Sep 2026
· 3 views
Advertisement
Defining Functions
def greet(name, greeting="Hi"):
return f"{greeting}, {name}!"
print(greet("Priya"))
print(greet("Amit", "Namaste"))Default and Keyword Arguments
def describe(title, *, author="Unknown"):
return f"{title} by {author}"
print(describe("Python Basics", author="Groovy Grails"))The * forces keyword-only arguments after it.
Returning Multiple Values
def min_max(nums):
return min(nums), max(nums)
lo, hi = min_max([3, 1, 4, 1, 5])
print(lo, hi)Lambda Functions
square = lambda x: x * x
print(square(5))
nums = [3, 1, 4, 1, 5]
nums.sort(key=lambda n: -n) # sort descending
print(nums)Using the Standard Library
import math
import random
import datetime
print(math.sqrt(144)) # 12.0
print(random.randint(1, 10))
print(datetime.date.today())
import os
print(os.getcwd()) # current directoryKey Points
- Functions start with
defand end by returning a value. - Default values keep function calls concise.
math,random,datetime,osare part of the standard library.