- Python Basics
-
Overview
- Introduction to Python and Installation
- Variables and Data Types
- Conditional Statements (if-else)
- Loops (for, while)
- Functions and Lambda Expressions
- Lists, Tuples, and Dictionaries
- File Handling (Reading/Writing Files)
- Exception Handling (Try, Except)
- Modules and Packages
- List Comprehensions and Generators
Functions and Lambda Expressions
Add to Bookmark1. Introduction
Functions allow code reuse by defining blocks that can be executed multiple times. Python also supports lambda expressions, which provide a quick way to define simple functions.
2. Defining Functions in Python
A function is defined using the def
keyword.
Syntax
def function_name(parameters):
# Function body
return value # Optional
Example: Simple Function
def greet():
print("Hello, World!")
greet()
Output
Hello, World!
3. Function Parameters and Arguments
Example: Function with Parameters
def greet(name):
print(f"Hello, {name}!")
greet("Aayush")
Output
Hello, Aayush!
Multiple Parameters
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Output
8
4. Default Arguments
Default values can be assigned to function parameters.
def power(base, exponent=2):
return base ** exponent
print(power(3)) # Uses default exponent (2)
print(power(3, 3)) # Uses provided exponent (3)
Output
9
27
5. Keyword Arguments (kwargs
)
Allows passing arguments using key-value pairs.
def person_info(name, age):
print(f"Name: {name}, Age: {age}")
person_info(age=25, name="Ankit") # Order does not matter
Output
Name: Ankit, Age: 25
Arbitrary Keyword Arguments (**kwargs
)
Allows passing multiple named arguments.
def print_info(**info):
for key, value in info.items():
print(f"{key}: {value}")
print_info(name="Ankit", age=30, city="Delhi")
Output
name: Ankit
age: 30
city: Delhi
6. Returning Values from Functions
A function can return a value using return
.
def multiply(a, b):
return a * b
result = multiply(4, 5)
print(result)
Output
20
7. Lambda Functions (Anonymous Functions)
A lambda function is a one-line function without a name.
Syntax
lambda arguments: expression
Example: Lambda Function
square = lambda x: x ** 2
print(square(5))
Output
25
Using Lambda in map()
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x ** 2, numbers))
print(squared)
Output
[1, 4, 9, 16]
Using Lambda in filter()
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
Output
[2, 4, 6]
8. Nested Functions
A function inside another function.
def outer():
def inner():
print("Inside inner function")
inner()
outer()
Output
Inside inner function
9. Function Scope (Local vs. Global Variables)
- Local variables exist only inside a function.
- Global variables exist outside functions and can be accessed within functions.
Example
x = 10 # Global variable
def show():
x = 5 # Local variable
print("Inside function:", x)
show()
print("Outside function:", x)
Output
Inside function: 5
Outside function: 10
10. Summary
Functions allow code reuse and modularity.
Functions can have parameters, return values, and default arguments.
Lambda expressions provide a compact way to define functions.map()
and filter()
work well with lambda functions.**kwargs
allows handling multiple keyword arguments.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Career Guide: Natural Language Processing (NLP)
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Variable Assignment in Python
- Government Datasets from 50 Countries for Machine Learning Training
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- Top 10 Blogs of Digital Marketing you Must Follow
- Best Platform to Learn Digital Marketing in Free
- The Ultimate Guide to Data Science: Everything You Need to Know
- How AI is Making Humans Weaker – The Hidden Impact of Artificial Intelligence
- String Operations in Python
- Python Challenging Programming Exercises Part 1
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Datasets for Speech Recognition Analysis
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- AI Agents: The Future of Automation, Work, and Opportunities in 2025
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset