π Python Basics Part-3 ππ₯
β Functions in Python
Functions help you write reusable and organized code.
Instead of writing the same code again and again, you can put it inside a function and call it whenever needed. π»
In this part, youβll learn:
β Functions
β Parameters & Arguments
β Return Statement
β Lambda Functions
β Recursion Basics
β Scope
π§ 1. What is a Function?
A function is a block of code that performs a specific task.
Example:
def greet():
print("Welcome to Python")
greet()
π Output:
Welcome to Python
β `def` is used to create functions.
π₯ 2. Function Parameters
Parameters allow functions to accept values.
Example:
def greet(name):
print("Hello", name)
greet("Python")
π Output:
Hello Python
β‘ 3. Multiple Parameters
Functions can take multiple inputs.
Example:
def add(a, b):
print(a + b)
add(10, 20)
π Output:
30
π 4. Return Statement
`return` sends a value back from a function.
Example:
def square(num):
return num * num
result = square(5)
print(result)
π Output:
25
β `return` is very important in real projects.
π― 5. Default Parameters
You can assign default values to parameters.
*Example:*
def country(name="India"):
print(name)
country()
country("Japan")
π Output:
India
Japan
β‘ 6. Lambda Functions
Small anonymous functions written in one line.
Example:
square = lambda x: x * x
print(square(4))
π Output:
16
β Mostly used in data analysis and automation.
π 7. Recursion Basics
A function calling itself is called recursion.
*Example:*
def countdown(n):
if n == 0:
return
print(n)
countdown(n - 1)
countdown(5)
π Output:
5
4
3
2
1
π 8. Variable Scope
Scope decides where variables can be used.
π Local Variable
Created inside a function.
Example:
def test():
value = 100
print(value)
test()
π Global Variable
Created outside a function.
Example:
message = "Python"
def show():
print(message)
show()
π¦ 9. Modules in Python
Modules help organize code into separate files.
*Example:*
import math
print(math.sqrt(25))
π Output:
5.0
π₯ 10. Import Statements
Used to use built-in or external libraries.
Example:
from random import randint
print(randint(1, 10))
π Practice Programs
β Calculator using Functions
β Factorial Program
β Prime Number Checker
β Password Generator
β Simple Quiz App
π₯ Common Beginner Mistakes
β Forgetting indentation
β Missing `return` statement
β Confusing parameters and arguments
β Infinite recursion
π‘ Pro Tip
β Write small functions
β Keep functions reusable
β Use meaningful function names
Good functions make code cleaner and easier to debug.
π *Double Tap β€οΈ For Part-4
π¨ Stop using AI like a beginner.
This single ChatGPT prompt can generate an institutional-level crypto trading report similar to what professional hedge funds use.
Save this thread before you lose it. π
π SQL Scenario-Based Interview Questions with Answers (Part 9)
πΌ FAANG & Product Company SQL Interview Scenarios
π Question 81: Find Users Who Were Active on 3 Consecutive Days
*Table:* `user_activity (user_id, activity_date)`
WITH activity AS (
SELECT DISTINCT
user_id,
activity_date
FROM user_activity
),
groups AS (
SELECT
user_id,
activity_date,
activity_date -
ROW_NUMBER() OVER (
PARTITION BY user_id
ORDER BY activity_date
) * INTERVAL '1 day' AS grp
FROM activity
)
SELECT
user_id,
COUNT(*) AS consecutive_days
FROM groups
GROUP BY user_id, grp
HAVING COUNT(*) >= 3;
π Question 82: Find the Top 5 Selling Products in Each Category
*Tables:* `products (product_id, category)` `sales (product_id, quantity)`
WITH product_sales AS (
SELECT
p.category,
s.product_id,
SUM(s.quantity) AS total_quantity
FROM sales s
JOIN products p
ON s.product_id = p.product_id
GROUP BY p.category, s.product_id
)
SELECT *
FROM (
SELECT *,
DENSE_RANK() OVER (
PARTITION BY category
ORDER BY total_quantity DESC
) AS rnk
FROM product_sales
) t
WHERE rnk <= 5;
π Question 83: Find Customers Whose Latest Order Is Their Highest Value Order
Table: `orders (customer_id, order_date, amount)`
WITH customer_orders AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS latest_order,
RANK() OVER (
PARTITION BY customer_id
ORDER BY amount DESC
) AS highest_order
FROM orders
)
SELECT
customer_id,
order_date,
amount
FROM customer_orders
WHERE latest_order = 1
AND highest_order = 1;
π Question 84: Calculate Rolling 30-Day Revenue*
Table: `sales (sale_date, amount)`
SELECT
sale_date,
SUM(amount) OVER (
ORDER BY sale_date
RANGE BETWEEN INTERVAL '29 days' PRECEDING
AND CURRENT ROW
) AS rolling_30_day_revenue
FROM sales;
π Question 85: Find Products Purchased by Exactly One Customer
Table: `orders (customer_id, product_id)`
SELECT
product_id
FROM orders
GROUP BY product_id
HAVING COUNT(DISTINCT customer_id) = 1;
π Question 86: Find the Longest Inactive Period for Each Customer*
Table: `orders (customer_id, order_date)`
WITH gaps AS (
SELECT
customer_id,
order_date,
order_date -
LAG(order_date) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS inactive_days
FROM orders
)
SELECT
customer_id,
MAX(inactive_days) AS longest_gap
FROM gaps
GROUP BY customer_id;
π Question 87: Calculate Revenue Share by Product Category
*Tables:* `products (product_id, category)` `sales (product_id, amount)`
SELECT
category,
SUM(amount) AS revenue,
ROUND(
100.0 * SUM(amount) /
SUM(SUM(amount)) OVER (),
2
) AS revenue_share
FROM sales s
JOIN products p
ON s.product_id = p.product_id
GROUP BY category
ORDER BY revenue DESC;
π Question 88: Find Customers Who Purchased in Every Month of a Year
Table: `orders (customer_id, order_date)`
SELECT
customer_id
FROM orders
WHERE EXTRACT(YEAR FROM order_date) = 2025
GROUP BY customer_id
HAVING COUNT(
DISTINCT EXTRACT(MONTH FROM order_date)
) = 12;
*π Question 89: Find the Most Frequently Bought Product After Product A*
*Table:* `order_items (order_id, product_id, sequence_no)`
SELECT
b.product_id,
COUNT(*) AS purchase_count
FROM order_items a
JOIN order_items b
ON a.order_id = b.order_id
AND b.sequence_no = a.sequence_no + 1
WHERE a.product_id = 'Product_A'
GROUP BY b.product_id
ORDER BY purchase_count DESC
LIMIT 1;
Build a dynamic search bar in Excel that filters data instantly as you type.
This tutorial shows how to use FILTER, SEARCH, and ISNUMBER to create a searchable table and find data faster.
(Save and share this video).
#Excel#ExcelTutorial#ExcelTips#SpreadsheetTips #MicrosoftExcel
π§΅ Thread: Excel Formulas Every Beginner Should Know
Most people only use about 10% of Excelβs capabilities.
If you learn these essential formulas, youβll work faster, make fewer mistakes, and stand out at work.
Hereβs your beginner-friendly guide. π§΅π
DAY 7 of 20 Days Series of #SQL Basics for Beginners.
Raw data is noisy.
GROUP BY turns thousands of rows into meaningful insights.
Learn how to summarize data with:
β COUNT()
β SUM()
β AVG()
β Multiple Groups
π Save this cheat sheet β it's one of the most important SQL skills you'll learn.
π If your Excel files are difficult to manage, slow to update, or full of errors, youβre probably making one of these five common mistakes.
(Save this thank me later).
Statistics helps you move beyond reporting to making informed decisions. Understanding concepts like mean, median, variance, correlation, confidence intervals, and hypothesis testing gives you the confidence to analyze data with accuracy and explain insights clearly. #Statistics #DataAnalytics #DataScience #Python #SQL