-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
A function is a block of code that runs only when it is called.
It can take inputs (parameters) and return an output (return value).
def greet():
print("Hello!")
Call the function:
greet()
def greet(name):
print("Hello", name)
greet("John")
def greet(name="Guest"):
print("Hello", name)
greet() # Hello Guest
greet("Anil") # Hello Anil
def add(a, b):
return a + b
print(add(2, 3)) # 5
def show_names(*names):
for name in names:
print(name)
show_names("A", "B", "C")
def student(name, age):
print(name, age)
student(age=18, name="Priya")
def details(**info):
print(info["name"])
details(name="Amit", age=25)
Use pass
to avoid empty function errors.
def test():
pass
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5)) # 120
Q1. Write a Python program to create a function that prints "Welcome to Python"
.
Q2. Write a Python program to create a function that takes a name as input and prints a greeting like "Hello, CodePractice!"
.
Q3. Write a Python program to use a default parameter in a function, such as greet(name="Guest")
.
Q4. Write a Python program to create a function that returns the square of a number.
Q5. Write a Python program to call a function using keyword arguments (e.g., info(name="Sanju", age=27)
).
Q6. Write a Python program to use *args
to accept multiple values and print their total.
Q7. Write a Python program to use **kwargs
to pass and print key-value pairs inside a function.
Q8. Write a Python program to create a function that returns the sum of two numbers.
Q9. Write a Python program to create a function to multiply 3 numbers using default arguments (e.g., multiply(a, b=1, c=1)
).
Q10. Write a Python program to write a recursive function to calculate the factorial of a number.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms