Python Cheat Sheet Part 03 - Functions & Modules
Welcome to Cyber Supto! I'm Supto. In this Python Cheat Sheet series, we are learning Python in a quick, practical way with simple examples you can use while coding.
In Part 01 we covered Python basics and syntax, and in Part 02 we explored control flow like conditions and loops. In this part, we will focus on functions and modules, which help organize code and make programs reusable.
What is a Function?
A function is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you can define a function and call it whenever needed.
def greet():
print("Hello from Cyber Supto")
greet()
The def keyword is used to define a function.
Function Parameters
Functions can accept inputs called parameters.
def greet(name):
print("Hello", name)
greet("Supto")
This allows the function to work with different values.
Multiple Parameters
def add(a, b):
print(a + b)
add(5, 3)
Functions can accept multiple parameters separated by commas.
Return Statement
The return keyword sends a result back from a function.
def multiply(a, b):
return a * b
result = multiply(4, 5)
print(result)
Returning values makes functions more useful in larger programs.
Default Parameters
You can assign default values to parameters.
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Supto")
If no argument is provided, the default value is used.
Keyword Arguments
Python allows passing arguments by name.
def user_info(name, age):
print(name, age)
user_info(age=20, name="Supto")
This improves readability and flexibility.
Arbitrary Arguments (*args)
Use *args when you don't know how many arguments will be passed.
def total(*numbers):
print(sum(numbers))
total(1,2,3,4)
Arbitrary Keyword Arguments (**kwargs)
Use **kwargs for variable keyword arguments.
def profile(**data):
print(data)
profile(name="Supto", role="Developer")
Lambda Functions
A lambda is a small anonymous function.
square = lambda x: x * x
print(square(5))
Lambda functions are useful for short operations.
What is a Module?
A module is a file that contains Python code such as functions, variables, or classes. Modules help organize large programs.
Importing a Module
import math
print(math.sqrt(16))
This imports the built-in math module.
Import Specific Functions
from math import sqrt
print(sqrt(25))
This imports only the required function.
Module Alias
You can rename modules using as.
import math as m
print(m.pi)
This makes code shorter and cleaner.
Creating Your Own Module
Create a file called utils.py:
def greet(name):
print("Hello", name)
Then import it in another file:
import utils
utils.greet("Supto")
This helps organize large projects.
Useful Built-in Modules
- math – mathematical operations
- random – random number generation
- datetime – date and time handling
- os – operating system operations
- sys – system-specific parameters
Quick Tips
- Use functions to avoid repeating code
- Keep functions small and focused
- Use modules to organize large programs
- Name functions clearly based on their task
- Reuse functions whenever possible
Other Parts of This Python Cheat Sheet
- Python Cheat Sheet Part 01 - Python Basics & Syntax
- Python Cheat Sheet Part 02 - Control Flow (If, Else, Loops)
- Python Cheat Sheet Part 04 - Data Structures (Lists, Tuples, Sets, Dictionaries)
- Python Cheat Sheet Part 05 - Advanced Python & Best Practices
This Python Cheat Sheet series on Cyber Supto is designed as a quick reference for developers and beginners learning Python.
Thanks for reading,
Cyber Supto.
Post a Comment