TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4446 3.63K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 6: Python Functions
 
So far, you've learned variables, operators, input/output, conditional statements, and loops. As your programs grow larger, writing the same code repeatedly becomes inefficient. That's where functions come in. 

A function is a reusable block of code that performs a specific task. Functions make your code cleaner, easier to maintain, and reusable. 

Functions are heavily used in Data Science, Machine Learning, and AI because they allow you to organize complex workflows into smaller, manageable pieces.

๐Ÿ”น 1. What is a Function? 
A function is a named block of code that executes only when it is called. 

Instead of writing the same logic multiple times, you write it once inside a function and reuse it whenever needed. 

Example
def greet():
    print("Welcome to Data Science!")

greet()

Output 
Welcome to Data Science!

๐Ÿ”น 2. Why Do We Use Functions? 

Functions help you: 
โœ… Avoid writing duplicate code 
โœ… Improve code readability 
โœ… Make debugging easier 
โœ… Reuse code in multiple places 
โœ… Build modular applications 

๐Ÿ”น 3. Defining a Function 

Syntax
def function_name():
    # Function body

Example:
def welcome():
    print("Hello, World!")

welcome()

๐Ÿ”น 4. Function Parameters 
Parameters allow you to pass information into a function.
def greet(name):
    print("Hello", name)

greet("Deepak")

Output 
Hello Deepak 

Here, "name" is called a parameter.

๐Ÿ”น 5. Function Arguments 
When calling a function, the values you pass are called arguments.
def square(number):
    print(number * number)

square(5)

Output 
25 

Here: 
โ€ข "number" โ†’ Parameter
โ€ข "5" โ†’ Argument

๐Ÿ”น 6. Returning Values 
A function can return a value using the return keyword.
def add(a, b):
    return a + b

result = add(10, 20)
print(result)

Output 
30 

Using return allows the function's result to be stored or used later.

๐Ÿ”น 7. Default Parameters 
You can assign default values to parameters.
def greet(name="Guest"):
    print("Hello", name)

greet()
greet("Rahul")

Output
Hello Guest
Hello Rahul

๐Ÿ”น 8. Multiple Return Values 
A function can return more than one value.
def calculate(a, b):
    return a + b, a * b

sum_value, product = calculate(4, 5)
print(sum_value)
print(product)

Output
9
20

๐Ÿ”น 9. Scope of Variables 
Variables created inside a function are called local variables.
def demo():
    message = "Inside Function"
    print(message)

demo()

Trying to access
message

outside the function will produce an error because it exists only inside the function.

๐Ÿ”น 10. Built-in Functions 
Python provides many ready-to-use functions. 

Examples:
numbers = [5, 2, 8, 1]
print(len(numbers))
print(max(numbers))
print(min(numbers))
print(sum(numbers))

Output
4
8
1
16

๐Ÿ”น 11. Real-World Data Science Example 
Calculate the average marks of students.
def average(marks):
    return sum(marks) / len(marks)

scores = [80, 75, 92, 88]
print(average(scores))

Output 
83.75 

Functions like this are commonly used while cleaning data, calculating statistics, and building machine learning pipelines.

๐Ÿ”น 12. Common Mistakes 
โŒ Forgetting to Call the Function
def greet():
    print("Hello")

# Nothing happens because the function isn't called.

Correct:
greet()

โŒ Forgetting to Return a Value
def add(a, b):
    a + b

# Correct:
def add(a, b):
    return a + b

๐ŸŽฏ Practice Questions 
1. Write a function to add two numbers.
2. Create a function to calculate the square of a number.
3. Write a function that checks whether a number is even or odd.
4. Create a function to calculate the average of a list.
5. Write a function that returns the largest of three numbers.

Double Tap โค๏ธ For Part-7
  • โค 18
More from @datasciencefun
  1. Oct 8, 2026๐Ÿ”ฌ Kandinsky 6.0 Video: Training Details and Architecture The new Kandinsky 6.0 Video lineโ€ฆ
  2. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  3. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  4. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  5. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’