⚡ Functions, Loops & Conditional Statements in Python
Programming में तीन concepts हर beginner के लिए foundation का काम करते हैं: Functions, Loops, और Conditional Statements। Python में ये concepts हर जगह use होते हैं — चाहे data analysis करना हो, AI model बनाना हो या automation scripts लिखनी हों।
🔹 Functions
Function एक block of code होता है जिसे बार-बार use किया जा सकता है। Functions से code modular, reusable और readable बनता है।
# Function Example def greet(name): return "Hello " + name print(greet("Ravi")) print(greet("Anita"))
ऊपर दिए गए example में greet()
function किसी भी नाम को input लेकर customized message देता है।
इसी तरह आप calculation, data processing, या complex logic functions के अंदर लिख सकते हैं।
🔹 Loops
Loops का use तब होता है जब किसी task को बार-बार repeat करना हो। Python में मुख्यतः दो types के loops होते हैं:
- for loop → किसी sequence (list, string, range) पर iterate करने के लिए
- while loop → जब तक कोई condition true है, तब तक execute होता है
# For Loop Example fruits = ["Apple", "Banana", "Mango"] for fruit in fruits: print(fruit) # While Loop Example count = 1 while count <= 5: print("Count is:", count) count += 1
🔹 Conditional Statements
Conditional statements decisions लेने के लिए use होते हैं। ये check करते हैं कि condition true है या false और उसी हिसाब से code run करते हैं।
# If-Else Example age = 20 if age >= 18: print("You are eligible to vote") else: print("You are not eligible") # If-Elif-Else Example marks = 75 if marks >= 90: print("Grade A") elif marks >= 75: print("Grade B") else: print("Grade C")
💡 Functions + Loops + Conditionals (Combined Example)
तीनों concepts को मिलाकर powerful programs बनाए जा सकते हैं। Example: किसी list के numbers को even और odd में divide करना।
def categorize_numbers(numbers): even = [] odd = [] for n in numbers: if n % 2 == 0: even.append(n) else: odd.append(n) return even, odd nums = [10, 15, 20, 25, 30] even_nums, odd_nums = categorize_numbers(nums) print("Even Numbers:", even_nums) print("Odd Numbers:", odd_nums)
🌍 Real-Life Applications
- 📊 Data Analysis में loops और conditionals से datasets process करना
- 🤖 Machine Learning में functions से data preprocessing pipelines बनाना
- 🌐 Web Development में user input validate करना
- ⚙ Automation scripts में repetitive tasks को automate करना
🏆 निष्कर्ष
Functions, Loops और Conditional Statements Python की backbone हैं। इनका mastery किसी भी aspiring data scientist या AI engineer के लिए must है। Practice के साथ, आप इन concepts का use करके real-world problems solve कर सकते हैं।