TGViewer
Channel Public Channel
Data Science & Machine Learning

Data Science & Machine Learning

@datasciencefun

Join this channel to learn data science, artificial intelligence and machine learning with funny quizzes, interesting projects and amazing resources for free

For collaborations: @love_data
Subscribers
77.8K
Photos
907
Videos
1
Links
827

Showing posts older than #4459 ยท Back to latest

Older Posts 20 shown
Post #4458 2.9K
  • ๐Ÿ‘ 2
  • โค 1
Post #4457 2.99K
  • ๐Ÿ‘ 2
  • โค 1
Post #4455 3.02K
student = {
"name": "Rahul",
"age": 23
}
print(student.keys())
print(student.values())
print(student.items())


๐Ÿ”น 8. Real-World Data Science Example

Suppose you have student information.

students = [
{"name": "Amit", "marks": 90},
{"name": "Sara", "marks": 85}
]

for student in students:
print(student["name"], student["marks"])


Output

Amit 90
Sara 85


This is very similar to how records are stored before converting them into a Pandas DataFrame.

๐Ÿ”น 9. Common Mistakes

โŒ Trying to Modify a Tuple

colors = ("Red", "Green")
colors[0] = "Blue"


This raises a TypeError because tuples are immutable.

โŒ Accessing a Missing Dictionary Key

student = {"name": "John"}
print(student["age"])


This raises a KeyError.

A safer approach:

print(student.get("age"))


๐ŸŽฏ Practice Questions

1. Create a list of five cities and print the third city.

2. Create a tuple containing the days of the week.

3. Remove duplicate numbers from a list using a set.

4. Create a dictionary containing your name, age, and profession.

5. Print all keys and values of a dictionary using a loop.

๐ŸŽฏ Key Takeaways

โœ… Lists are ordered and mutable.

โœ… Tuples are ordered and immutable.

โœ… Sets store only unique values.

โœ… Dictionaries store data as key-value pairs.

โœ… Dictionaries and Lists are the most commonly used data structures in Data Science.

Mastering these four data structures will make it much easier to work with datasets, APIs, JSON files, and machine learning projects.

Double Tap โค๏ธ For Part-8
  • โค 9
  • ๐Ÿ‘ 1
Post #4454 2.8K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 7: Python Data Structures (Lists, Tuples, Sets & Dictionaries)

Welcome back! ๐Ÿ‘‹

So far, you've learned variables, operators, input/output, conditional statements, loops, and functions. Now it's time to learn one of the most important topics in Pythonโ€” Data Structures.

Data structures help us store, organize, and manage data efficiently. In Data Science, almost every dataset you work with will be stored or manipulated using these structures.

Python provides four built-in data structures:

โ€ข List

โ€ข Tuple

โ€ข Set

โ€ข Dictionary

Let's understand each one in detail.

๐Ÿ”น 1. List

A List is an ordered, mutable collection that allows duplicate values.

Creating a List

fruits = ["Apple", "Banana", "Mango"]
print(fruits)


Output

['Apple', 'Banana', 'Mango']


Accessing Elements

print(fruits[0])
print(fruits[2])


Output

Apple
Mango


Modifying a List

fruits[1] = "Orange"
print(fruits)


Output

['Apple', 'Orange', 'Mango']


Adding Elements

fruits.append("Grapes")
print(fruits)


Removing Elements

fruits.remove("Orange")
print(fruits)


๐Ÿ”น 2. Tuple

A Tuple is an ordered collection that cannot be modified after creation (immutable).

Creating a Tuple

colors = ("Red", "Green", "Blue")
print(colors)


Accessing Elements

print(colors[1])


Output

Green


Why Use Tuples?

Use tuples when your data should never change.

Examples:

โ€ข Months of the year

โ€ข Days of the week

โ€ข Fixed coordinates

๐Ÿ”น 3. Set

A Set is an unordered collection of unique elements.

Duplicate values are automatically removed.

Creating a Set

numbers = {1, 2, 2, 3, 4, 4, 5}
print(numbers)


Output

{1, 2, 3, 4, 5}


Adding Elements

numbers.add(6)


Removing Elements

numbers.remove(3)


Common Uses

โ€ข Remove duplicates

โ€ข Membership testing

โ€ข Mathematical set operations

๐Ÿ”น 4. Dictionary โญ

A Dictionary stores data as key-value pairs.

It is one of the most frequently used data structures in Data Science.

Creating a Dictionary

student = {
"name": "Deepak",
"age": 24,
"course": "Data Science"
}
print(student)


Accessing Values

print(student["name"])


Output

Deepak


Adding a New Key

student["city"] = "Mumbai"


Updating a Value

student["age"] = 25


Removing a Key

del student["course"]


๐Ÿ”น 5. Comparison of Data Structures

Feature | List | Tuple | Set | Dictionary

Ordered | โœ… | โœ… | โŒ | โœ…

Mutable | โœ… | โŒ | โœ… | โœ…

Duplicates Allowed | โœ… | โœ… | โŒ | Keys โŒ

Indexed | โœ… | โœ… | โŒ | By Key

๐Ÿ”น 6. Common List Methods

numbers = [10, 20, 30]
numbers.append(40)
numbers.insert(1, 15)
numbers.remove(20)
numbers.sort()
print(numbers)


๐Ÿ”น 7. Common Dictionary Methods
  • โค 5
Post #4452 3.64K
  • โค 4
Post #4451 3.52K
  • โค 1
  • ๐Ÿฅฐ 1
Post #4450 3.32K
  • โค 2
Post #4449 3.25K
  • โค 1
Post #4448 3.12K
  • โค 2
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
Post #4445 2.75K
Final 6 Hours Left!

To register for TiHAN IIT Hyderabad's AI & ML Program.

Don't miss your chance to:

โ€ข Learn from India's best scientists at TiHAN, IIT Professors and industry experts
โ€ข Direct Interview at TiHAN IIT Hyderabad with 9+ CGPA

Register before the Admission Closes!
  • โค 3
Post #4443 3.39K
  • โค 3
  • ๐Ÿฅฐ 1
Post #4442 3.2K
  • โค 4
  • ๐Ÿ”ฅ 1
Post #4441 3.24K
  • โค 3
Post #4440 3.36K
  • โค 5
Post #4437 3.29K
sales = [1000, 2000, 1500, 3000]

total = 0

for amount in sales:
    total += amount

print(total)


Output: 7500

๐Ÿ”น 11. Common Mistakes 

โŒ Forgetting to update the loop variable

count = 1
while count <= 5:
    print(count)


This creates an infinite loop because count never changes.

Correct:

count = 1
while count <= 5:
    print(count)
    count += 1


๐ŸŽฏ Practice Questions 

1. Print numbers from 1 to 10 using a "for" loop

2. Print even numbers from 2 to 20

3. Find the sum of numbers from 1 to 100

4. Print all elements of a list using a loop

5. Create a multiplication table of any number using a "for" loop

๐ŸŽฏ Key Takeaways

โœ… Use a "for" loop when the number of iterations is known

โœ… Use a "while" loop when the number of iterations depends on a condition

โœ… range() generates sequences of numbers

โœ… break exits the loop immediately

โœ… continue skips the current iteration

โœ… pass acts as a placeholder

Loops are one of the most important concepts in Python. You'll use them extensively for data processing, feature engineering, machine learning, automation, and solving coding interview questions.

Double Tap โค๏ธ For Part-6
  • โค 12
Post #4436 2.88K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 5: Python Loops ("for" & "while")

Welcome back! ๐Ÿ‘‹

In the previous lesson, you learned how Python makes decisions using if, elif, and else. Now it's time to learn how to repeat tasks automatically using loops.

Loops are one of the most powerful concepts in Python. Instead of writing the same code multiple times, you can use loops to execute a block of code repeatedly.

In Data Science, loops are commonly used to process datasets, automate repetitive tasks, iterate through lists, and build machine learning workflows.

๐Ÿ”น 1. What is a Loop?

A loop is used to execute a block of code multiple times until a condition is met.

Without loops:

print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")


Using a loop:

for i in range(5):
print("Hello")


Both produce the same output, but the loop is much shorter and easier to maintain.

๐Ÿ”น 2. Types of Loops in Python

Python provides two main types of loops:

โœ… "for" Loop

โœ… "while" Loop

๐Ÿ”น 3. The "for" Loop โญ

A "for" loop is used when you know how many times you want to repeat a task.

Syntax

for variable in sequence:
# Code to execute


Example

for i in range(5):
print(i)


Output:

0  
1
2
3
4


Notice that range(5) generates numbers from 0 to 4.

๐Ÿ”น 4. The "range()" Function โญ

The range() function generates a sequence of numbers.

Example 1

for i in range(5):
print(i)


Output: 0 1 2 3 4

Example 2

for i in range(1, 6):
print(i)


Output: 1 2 3 4 5

Example 3

for i in range(2, 11, 2):
print(i)


Output: 2 4 6 8 10

The third argument is called the step size.

๐Ÿ”น 5. Looping Through a List

fruits = ["Apple", "Banana", "Mango"]

for fruit in fruits:
print(fruit)


Output:

Apple  
Banana
Mango


๐Ÿ”น 6. The "while" Loop โญ

A "while" loop continues executing as long as the condition remains True.

Syntax

while condition:
# Code


Example

count = 1

while count <= 5:
print(count)
count += 1


Output: 1 2 3 4 5

๐Ÿ”น 7. Infinite Loop

Be careful when using "while" loops.

while True:
print("Hello")


This loop never stops unless interrupted.

Always ensure the condition eventually becomes False.

๐Ÿ”น 8. Loop Control Statements โญ

"break" - Stops the loop immediately

for i in range(10):
if i == 5:
break
print(i)


Output: 0 1 2 3 4

"continue" - Skips the current iteration

for i in range(5):
if i == 2:
continue
print(i)


Output: 0 1 3 4

"pass" - Acts as a placeholder

for i in range(5):
pass


Useful when writing incomplete code.

๐Ÿ”น 9. Nested Loops

A loop inside another loop.

for i in range(3):
for j in range(2):
print(i, j)


Output:

0 0  
0 1
1 0
1 1
2 0
2 1


๐Ÿ”น 10. Real-World Data Science Example

Calculate the total sales.
  • โค 7
  • ๐Ÿ‘ 1
Post #4435 2.58K
๐Ÿš€ ๐—™๐—ฅ๐—˜๐—˜ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐Ÿ’ป๐Ÿ”ฅ

These FREE courses can help you learn Data Analytics, Power BI & Excel skills that companies actually hire for ๐Ÿš€

โœจ What youโ€™ll learn:
โœ” Excel + Power BI ๐Ÿ“Š
โœ” Data Cleaning with Power Query
โœ” Interactive Dashboards
โœ” Modern Analytics Skills

๐Ÿ’ฏ Beginner Friendly + FREE Learning

๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:-

https://pdlink.in/4tkPNyM

๐ŸŽ“ Perfect for Students, Freshers & Career Switchers
  • โค 1
  • ๐Ÿ‘ 1
Post #4434 4.49K
if age >= 18:
print("Eligible")


โŒ Incorrect Indentation

if age >= 18:
print("Eligible")


Python requires proper indentation.

Correct:

if age >= 18:
print("Eligible")


๐Ÿ”น 11. Real-World Data Science Example

prediction = 0.82
if prediction >= 0.5:
print("Spam Email")
else:
print("Not Spam")


Many Machine Learning classification models use similar logic to convert prediction probabilities into categories.

๐ŸŽฏ Practice Questions

1. Check whether a number is positive or negative.

2. Check whether a person is eligible to vote.

3. Create a grading system using "if...elif...else".

4. Check whether a number is even or odd.

5. Determine the largest of three numbers.

๐ŸŽฏ Key Takeaways

โœ… Use "if" to execute code when a condition is True.

โœ… Use "else" to execute code when the condition is False.

โœ… Use "elif" to check multiple conditions.

โœ… Nested "if" statements allow more complex decision-making.

โœ… Logical operators (and, or, not) help combine conditions.

โœ… The ternary operator provides a concise way to write simple "if...else" statements.

Conditional statements are the foundation of decision-making in Python and are widely used in automation, data analysis, machine learning, and AI applications.

Double Tap โค๏ธ For Part-5
  • โค 14
  • ๐Ÿ˜ 1
  • ๐Ÿ˜ข 1
Post #4433 3.72K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 4: Python Conditional Statements (if, elif, else)

Welcome back! ๐Ÿ‘‹

In the previous lesson, you learned how to take input from users and display output. Now it's time to learn how Python makes decisions.

Conditional statements allow a program to execute different blocks of code based on whether a condition is True or False.

They are one of the most important concepts in Python and are used extensively in Data Science, Machine Learning, and real-world applications.

๐Ÿ”น 1. What are Conditional Statements?

Conditional statements help Python decide which code should run based on a condition.

Think of it like making everyday decisions:

โ€ข If it's raining โ†’ Carry an umbrella.

โ€ข Else โ†’ Wear sunglasses.

Similarly, Python checks a condition and decides what to execute.

๐Ÿ”น 2. The "if" Statement

The "if" statement executes a block of code only if the condition is True.

Syntax

if condition:
# Code to execute


Example

age = 20
if age >= 18:
print("Eligible to vote")


Output

Eligible to vote


๐Ÿ”น 3. The "if...else" Statement

Use "else" when you want to execute another block if the condition is False.

Example

age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")


Output

Not eligible to vote


๐Ÿ”น 4. The "if...elif...else" Statement โญ

Use "elif" when you need to check multiple conditions.

Example

marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Grade D")


Output

Grade B


Python checks conditions from top to bottom and executes the first condition that is True.

๐Ÿ”น 5. Nested "if" Statements

You can place one "if" statement inside another.

Example

age = 25
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")


Output

Eligible to vote


๐Ÿ”น 6. Using Logical Operators

Conditional statements often use logical operators.

"and"

age = 25
if age >= 18 and age <= 60:
print("Working Age")


"or"

marks = 35
if marks >= 40 or marks == 35:
print("Eligible for Grace Marks")


"not"

is_holiday = False
if not is_holiday:
print("Go to Office")


๐Ÿ”น 7. Checking Multiple Conditions

salary = 60000
experience = 4
if salary > 50000 and experience >= 3:
print("Eligible for Promotion")
else:
print("Not Eligible")


๐Ÿ”น 8. Ternary Operator โญ

A shorter way to write an "if...else" statement.

Syntax

value_if_true if condition else value_if_false


Example

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)


Output

Adult


๐Ÿ”น 9. Real-World Example

temperature = 38
if temperature > 35:
print("It's a hot day.")
elif temperature >= 20:
print("Weather is pleasant.")
else:
print("It's cold.")


๐Ÿ”น 10. Common Mistakes

โŒ Missing Colon

if age >= 18
print("Eligible")


This gives a SyntaxError.

Correct:
  • โค 5
  • ๐Ÿฅฐ 1
  • ๐Ÿ˜ 1
Older posts โ†’
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 โ†’