Monday, 20 April 2026

April Python Bootcamp Day 11

What is a Function?

A function is a reusable block of code that performs a specific task.

def greet():
print("Hello, World!")

greet()

Once defined, a function can be called multiple times:

greet()
greet()

This avoids repetition and makes your code cleaner.


 Function Syntax

def func_name(val1, val2): # parameters
# code
pass
  • def → keyword to define function
  • func_name → function name
  • val1, val2 → parameters
  • pass → placeholder

 Parameters vs Arguments

def greet(name):
print("Hello,", name)

greet("Piyush")
  • name → parameter (definition time)
  • "Piyush" → argument (calling time)

Important rule:

Number of parameters = Number of arguments (unless defaults are used)


 Functions with Return Value

def add(a, b):
return a + b

result = add(5, 3)
print(result)

Key difference:

  • print() → displays output
  • return → sends value back for reuse

 Types of Arguments

1. Positional Arguments

Order matters.

def add(b, a):
print(a)
print(b)
return a + b

add(5, 3)

Output:

3
5
8

2. Keyword Arguments

Order does not matter.

def example(name, age):
print(f"My name is {name} and my age is {age}")

example(age=30, name="Piyush")

3. Default Arguments

Used when values are not provided.

def example(name="Not Known", age="I Don't know"):
print(f"My name is {name} and my age is {age}")

example("Rafish")
example("Rahul", 45)
example()

4. Variable-Length Arguments (*args)

Used when number of inputs is unknown.

def total(*nums):
return sum(nums)

print(total(1, 2, 3, 4, 5))

5. Keyword Variable-Length Arguments (**kwargs)

Accepts multiple key-value pairs.

def details(**data):
print(data)

details(name="Piyush", age=30, phone=12345)

 Key Observations

  • Functions can accept any data type (even True, strings, etc.)
  • Flexible argument handling makes functions powerful
  • Widely used in APIs, backend systems, automation, and ML pipelines

 Assignments (Based on Today’s Concepts)

 Basic Level

  1. Create a function that prints "Hello Python"
  2. Write a function that takes a name and prints a greeting
  3. Create a function that adds two numbers and returns the result
  4. Call a function multiple times and observe output
  5. Pass different data types (int, string, boolean) to a function

 Intermediate Level

  1. Create a function using positional arguments and observe order impact
  2. Create a function and call it using keyword arguments
  3. Write a function with default parameters and test all cases
  4. Create a function using *args to find the sum of numbers
  5. Modify *args function to return maximum value

 Advanced Level

  1. Create a function using **kwargs and print all key-value pairs
  2. Build a function that accepts both *args and **kwargs
  3. Create a function that validates input before processing
  4. Write a function that returns multiple values (sum, average)
  5. Implement a mini user profile system using **kwargs

Example idea:

def profile(**data):
for key, value in data.items():
print(f"{key} : {value}")

 Summary

  • Functions make code reusable and structured
  • return is essential for real-world applications
  • Argument types provide flexibility (*args, **kwargs)
  • Understanding parameter behavior is critical for debugging

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (248) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (29) Azure (10) BI (10) Books (262) Bootcamp (6) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (31) Data Analytics (22) data management (15) Data Science (347) Data Strucures (17) Deep Learning (154) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (70) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (286) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (32) pytho (1) Python (1310) Python Coding Challenge (1128) Python Mistakes (51) Python Quiz (481) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)