Data Science · मराठी आवृत्ती · Python basics
P7.1 Python मध्ये Function म्हणजे काय आणि का वापरायचं?
हे का शिकायचं?
तोच हिशोब दहा ठिकाणी लिहिला तर चूक दहा ठिकाणी होते. Function एकदा लिहा, अनेकदा वापरा.
हे काय आहे?
def name(inputs): ने function बनतं. आतला code function call केल्यावरच चालतो.
कसं करायचं?
Step 1. def आणि नाव लिहा.
Step 2. कंसात inputs लिहा.
Step 3. आतला code indent करा.
Step 4. name(...) ने call करा.
उदाहरणं: 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 वेगळं उत्तर देतं.