Functions in Python
Python में Functions
What are Functions in Python?
Functions in Python means a function is a reusable block of code that performs a specific task and can return a result.
In real programs, this topic helps in reusing the same logic. Learn the idea first, then type the program yourself and compare the output.
| Point | Details |
|---|---|
| Course Area | Core Python Basic programming concepts used to write Python programs. |
| Main Use | reusing the same logic |
| Example File | functions.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for reusing the same logic.
- It connects with dividing big programs into small parts.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| def | Keyword used to define a function. |
| parameter | Variable written inside a function definition. |
| argument | Actual value passed to a function while calling it. |
| return | Statement used to send a result back from a function. |
| reusability | Writing code once and using it many times. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
def calculate_percentage(obtained, total):
percentage = (obtained / total) * 100
return percentage
result = calculate_percentage(445, 500)
print("Percentage:", result)Complete Example Program
def calculate_percentage(obtained, total):
percentage = (obtained / total) * 100
return percentage
result = calculate_percentage(445, 500)
print("Percentage:", result)Expected Output
Program Explanation
def calculate_percentage(obtained, total):creates a reusable function.percentage = (obtained / total) * 100stores a value in percentage.return percentagesends a result back from the function.result = calculate_percentage(445, 500)stores a value in result.print("Percentage:", result)displays information or calculated result on the screen.
Where will you use it?
- Reusing the same logic.
- Dividing big programs into small parts.
- Creating cleaner projects.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Type the program in
functions.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to reusing the same logic.
- Write 5 lines explaining the logic in your own words.
Summary
Functions in Python is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
Python Functions क्या है?
Python Functions ka matlab hai: A function is a reusable block of code that performs a specific task and can return a result. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki reusing the same logic jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye reusing the same logic me kaam aata hai.
- Ye dividing big programs into small parts se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| def | Keyword used to define a function. |
| parameter | Variable written inside a function definition. |
| argument | Actual value passed to a function while calling it. |
| return | Statement used to send a result back from a function. |
| reusability | Writing code once and using it many times. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
def calculate_percentage(obtained, total):
percentage = (obtained / total) * 100
return percentage
result = calculate_percentage(445, 500)
print("Percentage:", result)Complete Example Program
def calculate_percentage(obtained, total):
percentage = (obtained / total) * 100
return percentage
result = calculate_percentage(445, 500)
print("Percentage:", result)Expected Output
Program Explanation
def calculate_percentage(obtained, total):creates a reusable function.percentage = (obtained / total) * 100stores a value in percentage.return percentagesends a result back from the function.result = calculate_percentage(445, 500)stores a value in result.print("Percentage:", result)displays information or calculated result on the screen.
Practical Uses
- Reusing the same logic.
- Dividing big programs into small parts.
- Creating cleaner projects.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Program ko
functions.pyfile me type karke run karein. - Values change karke output compare karein.
- reusing the same logic par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
Functions in Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.