Data Science & Machine Learning
77.8K subscribers
899 photos
1 video
68 files
818 links
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
Download Telegram
โœ… Conditional Statements (ifโ€“else) ๐Ÿโšก

Conditional statements allow programs to make decisions based on conditions.

๐Ÿ‘‰ Used heavily in:
โœ” Data filtering
โœ” Business rules
โœ” Machine learning logic

๐Ÿ”น 1. if Statement
Used to execute code when a condition is True.

โœ… Syntax
if condition:
# code


Example
age = 20
if age >= 18:
print("You can vote")

# Output: You can vote

๐Ÿ”น 2. ifโ€“else Statement
Used when there are two possible outcomes.

Syntax
if condition:
# code if true
else:
# code if false


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


๐Ÿ”น 3. ifโ€“elifโ€“else Statement
Used when there are multiple conditions.

Syntax
if condition1:
# code
elif condition2:
# code
else:
# code


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


๐Ÿ”น 4. Nested if Statement
An if statement inside another if.

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


๐Ÿ”น 5. Short if (Ternary Operator)
age = 20
print("Adult") if age >= 18 else print("Minor")


๐ŸŽฏ Todayโ€™s Goal
โœ” Understand if
โœ” Use ifโ€“else
โœ” Use elif for multiple conditions
โœ” Learn nested conditions

๐Ÿ‘‰ Conditional logic is used in data filtering and decision models.

Double Tap โ™ฅ๏ธ For More
โค16๐Ÿ‘1
Which keyword is used to check a condition in Python?
Anonymous Quiz
9%
A) check
82%
B) if
4%
C) when
4%
D) condition
โค3
What will be the output?

x = 10 if x > 5: print("Yes") else: print("No")
Anonymous Quiz
88%
Yes
12%
No
โค3
Which keyword is used to check multiple conditions?
Anonymous Quiz
14%
A) elseif
59%
B) elif
23%
C) else if
4%
D) multiple
โค4
๐Ÿ”น Q4. What will be the output?

x = 7 if x > 10: print("A") elif x > 5: print("B") else: print("C")
Anonymous Quiz
13%
A
75%
B
10%
C
2%
D
โค2
What will be the output?

age = 16 print("Adult") if age >= 18 else print("Minor")
Anonymous Quiz
25%
Adult
75%
Minor
โค5๐Ÿ˜1
Now, let's move to the next topic of Data Science Roadmap:

โœ… Python Dictionaries ๐Ÿ“š

Dictionaries are one of the most important data structures in Python, especially in data science and real-world datasets. They store data in keyโ€“value pairs.

๐Ÿ”น 1. What is a Dictionary?
A dictionary stores data in key:value format.

โœ… Example:

student = { "name": "Rahul", "age": 22, "course": "Data Science" }
print(student)


Output: {'name': 'Rahul', 'age': 22, 'course': 'Data Science'}

โœ” Uses curly brackets {}

๐Ÿ”น 2. Access Dictionary Values

Use the key to access values.

student = { "name": "Rahul", "age": 22 }
print(student["name"])


Output: Rahul

๐Ÿ”น 3. Add New Elements

student = { "name": "Rahul", "age": 22 }
student["city"] = "Delhi"
print(student)


Output: {'name': 'Rahul', 'age': 22, 'city': 'Delhi'}

๐Ÿ”น 4. Modify Values

student["age"] = 23


๐Ÿ”น 5. Remove Elements

student.pop("age")


๐Ÿ”น 6. Important Dictionary Methods
โญ

โœ… Get Method:
print(student.get("name"))


Output: Rahul

โœ… Keys Method:
print(student.keys())


Output: dict_keys(['name', 'age'])

โœ… Values Method:
print(student.values())


Output: dict_values(['Rahul', 22])

โœ… Items Method:
print(student.items())


Output: dict_items([('name', 'Rahul'), ('age', 22)])

๐Ÿ”น 7. Loop Through Dictionary

student = { "name": "Rahul", "age": 22 }

for key, value in student.items():
print(key, value)


Output:
name Rahul
age 22

๐ŸŽฏ Todayโ€™s Goal
โœ” Understand keyโ€“value pairs
โœ” Access dictionary values
โœ” Add or update data
โœ” Loop through dictionary

๐Ÿ‘‰ Dictionaries are widely used in APIs, JSON data, and machine learning datasets.

Double Tap โ™ฅ๏ธ For More
โค21๐Ÿฅฐ1
Which symbol is used to create a dictionary in Python?
Anonymous Quiz
18%
A) []
9%
B) ()
71%
C) {}
3%
D) <>
โค2๐Ÿ˜ข1
What will be the output?

student = { "name": "Rahul", "age": 22 } print(student["name"])
Anonymous Quiz
81%
A) Rahul
9%
B) name
3%
C) 22
7%
D) Error
โค3
Which method returns all keys of a dictionary?
Anonymous Quiz
14%
A) values()
14%
B) items()
60%
C) keys()
11%
D) get()
โค3
What will be the output?

data = {"a":1, "b":2} data["c"] = 3 print(data)
Anonymous Quiz
9%
A) {'a':1, 'b':2}
65%
B) {'a':1, 'b':2, 'c':3}
18%
C) Error
8%
D) {'c':3}
โค1๐Ÿค”1
Which method is used to remove an element from a dictionary?
Anonymous Quiz
43%
A) remove()
16%
B) delete()
36%
C) pop()
5%
D) clearitem()
โค8
Data Science Roadmap

โœ… Python File Handling

๐Ÿ๐Ÿ“‚ File handling allows Python programs to read and write data from files.

๐Ÿ‘‰ Very important in data science because most datasets come as:
โœ” CSV files
โœ” Text files
โœ” Logs
โœ” JSON files

๐Ÿ”น 1. Opening a File
Python uses the open() function.
Syntax: open("filename", "mode")
Example: file = open("data.txt", "r")
๐Ÿ‘‰ "r" โ†’ Read mode

๐Ÿ”น 2. File Modes
- "r" โ†’ Read file
- "w" โ†’ Write file (overwrites existing content)
- "a" โ†’ Append file (adds to existing content)
- "r+" โ†’ Read and write

๐Ÿ”น 3. Reading a File
- Read Entire File: file.read()
- Read One Line: file.readline()
- Read All Lines: file.readlines()

๐Ÿ”น 4. Writing to a File
file = open("data.txt", "w")
file.write("Hello Data Science")
file.close()

โš  "w" will overwrite existing content.

๐Ÿ”น 5. Append to File
file = open("data.txt", "a")
file.write("\nNew line added")
file.close()

โœ” Adds content without deleting old data.

๐Ÿ”น 6. Best Practice (Very Important โญ)
Use with statement.
with open("data.txt", "r") as file:
content = file.read()
print(content)

โœ” Automatically closes the file.

๐Ÿ”น 7. Why File Handling is Important?
Used for:
โœ” Reading datasets
โœ” Saving results
โœ” Logging machine learning models
โœ” Data preprocessing

๐ŸŽฏ Todayโ€™s Goal
โœ” Understand file modes
โœ” Read files
โœ” Write files
โœ” Use with open()

๐Ÿ‘‰ File handling is used heavily when working with CSV datasets in data science.

Double Tap โ™ฅ๏ธ For More
โค13
Which function is used to open a file in Python?
Anonymous Quiz
7%
A) file()
63%
B) open()
19%
C) read()
10%
D) openfile()
โค3
Which mode is used to read a file?
Anonymous Quiz
6%
A) "w"
3%
B) "a"
86%
C) "r"
5%
D) "rw"
โค2
What will the following code do?

file = open("data.txt", "w") file.write("Hello")
Anonymous Quiz
5%
A) Reads file
2%
B) Deletes file
90%
C) Writes text to file
4%
D) Prints file content
โค1
Which method reads the entire file content?
Anonymous Quiz
11%
A) readline()
27%
B) readlines()
58%
C) read()
3%
D) get()
โค1
โค2๐Ÿ‘1๐Ÿฅฐ1
Top Programming Languages for Beginners ๐Ÿ‘†
โค5๐Ÿ‘1
โœ… Python Exception Handling (tryโ€“except) ๐Ÿโš ๏ธ

Exception handling helps programs handle errors gracefully instead of crashing.

๐Ÿ‘‰ Very important in real-world applications and data processing.

๐Ÿ”น 1. What is an Exception?

An exception is an error that occurs during program execution.

Example:
print(10 / 0)

Output: ZeroDivisionError

This will crash the program.

๐Ÿ”น 2. Using tryโ€“except

We use tryโ€“except to handle errors.

Syntax:
try:
# code that may cause error
except:
# code to handle error

Example:
try:
x = 10 / 0
except:
print("Error occurred")

Output: Error occurred

๐Ÿ”น 3. Handling Specific Exceptions

try:
num = int("abc")
except ValueError:
print("Invalid number")

โœ” Handles only ValueError.

๐Ÿ”น 4. Using else

else runs if no error occurs.

try:
x = 10 / 2
except:
print("Error")
else:
print("No error")

Output: No error

๐Ÿ”น 5. Using finally

finally always executes.

try:
file = open("data.txt")
except:
print("File not found")
finally:
print("Execution completed")


๐Ÿ”น 6. Common Python Exceptions

โ€ข ZeroDivisionError: Division by zero
โ€ข ValueError: Invalid value
โ€ข TypeError: Wrong data type
โ€ข FileNotFoundError: File does not exist

๐ŸŽฏ Today's Goal

โœ” Understand exceptions
โœ” Use tryโ€“except
โœ” Handle specific errors
โœ” Use else and finally

๐Ÿ‘‰ Exception handling is widely used in data pipelines and production code.

Double Tap โ™ฅ๏ธ For More
โค11