
ManaTrick
June 20, 2025 at 07:01 AM
Today, Let’s move on to the next topic in the Python :
🔹 *Functions in Python*
🤔 *What is a Function?*
A function is a reusable block of code that performs a specific task.
Think of it as a machine: you give it some input (arguments), it processes something, and then gives you output.
✅ *Defining a Function*
Here’s the basic structure:
def greet():
print("Hello, Python learner!")
To call or run the function:
greet()
🟢 *Output:*
Hello, Python learner!
✅ *Function with Parameters*
You can make your function accept values using parameters:
def greet(name):
print(f"Hello, {name}!")
greet("Sam")
🟢 *Output:*
Hello, Sam!
✅ *Return Values*
Functions can return values instead of just printing them:
def add(a, b):
return a + b
result = add(3, 5)
print(result)
🟢 *Output:* 8
🔨 *Mini Project: Prime Number Checker*
Let’s use what we learned to create a small, useful function!
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
# Try it!
num = int(input("Enter a number: "))
if is_prime(num):
print("It's a prime number.")
else:
print("Not a prime number.")
🧠 *This project introduces:*
- Logic inside a function
- Conditional checks
- Looping through a range
*React with ❤️ once you’re ready for the quiz*
*ManaTrick* : https://whatsapp.com/channel/0029VaLxL5o2v1IsQgjEKC2H