🟢 Beginner  ·  Lesson 10

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.

💡 At a Glance
PointDetails
Course AreaCore Python
Basic programming concepts used to write Python programs.
Main Usereusing the same logic
Example Filefunctions.py
Practice FocusRun, 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.

TermMeaning
defKeyword used to define a function.
parameterVariable written inside a function definition.
argumentActual value passed to a function while calling it.
returnStatement used to send a result back from a function.
reusabilityWriting code once and using it many times.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
def calculate_percentage(obtained, total):
    percentage = (obtained / total) * 100
    return percentage
result = calculate_percentage(445, 500)
print("Percentage:", result)

Complete Example Program

Python – functions.py
def calculate_percentage(obtained, total):
    percentage = (obtained / total) * 100
    return percentage

result = calculate_percentage(445, 500)
print("Percentage:", result)

Expected Output

Percentage: 89.0

Program Explanation

  • def calculate_percentage(obtained, total): creates a reusable function.
  • percentage = (obtained / total) * 100 stores a value in percentage.
  • return percentage sends 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

  1. Type the program in functions.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to reusing the same logic.
  4. 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

TermMeaning
defKeyword used to define a function.
parameterVariable written inside a function definition.
argumentActual value passed to a function while calling it.
returnStatement used to send a result back from a function.
reusabilityWriting code once and using it many times.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
def calculate_percentage(obtained, total):
    percentage = (obtained / total) * 100
    return percentage
result = calculate_percentage(445, 500)
print("Percentage:", result)

Complete Example Program

Python – functions.py
def calculate_percentage(obtained, total):
    percentage = (obtained / total) * 100
    return percentage

result = calculate_percentage(445, 500)
print("Percentage:", result)

Expected Output

Percentage: 89.0

Program Explanation

  • def calculate_percentage(obtained, total): creates a reusable function.
  • percentage = (obtained / total) * 100 stores a value in percentage.
  • return percentage sends 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

  1. Program ko functions.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. reusing the same logic par ek छोटा example banayen.
  4. 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.

← Back to Python Tutorial