Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती · Python basics

P7.1 Python मध्ये Function म्हणजे काय आणि का वापरायचं?

By Ravindra Bagale

हे का शिकायचं?

तोच हिशोब दहा ठिकाणी लिहिला तर चूक दहा ठिकाणी होते. Function एकदा लिहा, अनेकदा वापरा.

हे काय आहे?

def name(inputs): ने function बनतं. आतला code function call केल्यावरच चालतो.

कसं करायचं?

Step 1. def आणि नाव लिहा.

Step 2. कंसात inputs लिहा.

Step 3. आतला code indent करा.

Step 4. name(...) ने call करा.

1. You call add_vat(100)2. 100 goes into the parameter3. The body runs4. return sends back the answer5. The answer appears where you called it
वरून खाली वाचा: code पासून result पर्यंतचा प्रवास

उदाहरणं: copy करा आणि चालवा

उदाहरण A: सगळ्यात छोटं function

हा code चालवा:

def greet():
    print("Hello, how can we help?")

greet()
greet()

तुम्हाला दिसणारा output:

Hello, how can we help?
Hello, how can we help?

याचा अर्थ: Function call केल्यावरच आतला code चालला.

उदाहरण B: Input असलेलं function

हा code चालवा:

def greet(name):
    print(f"Hello {name}, how can we help?")

greet("Aisha")
greet("Marco")

तुम्हाला दिसणारा output:

Hello Aisha, how can we help?
Hello Marco, how can we help?

याचा अर्थ: Input बदलला की तेच function वेगळं उत्तर देतं.