โ
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
Example
# Output: You can vote
๐น 2. ifโelse Statement
Used when there are two possible outcomes.
Syntax
Example
๐น 3. ifโelifโelse Statement
Used when there are multiple conditions.
Syntax
Example
๐น 4. Nested if Statement
An if statement inside another if.
๐น 5. Short if (Ternary Operator)
๐ฏ 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
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")
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")
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")
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:
Output:
โ Uses curly brackets {}
๐น 2. Access Dictionary Values
Use the key to access values.
Output:
๐น 3. Add New Elements
Output:
๐น 4. Modify Values
๐น 5. Remove Elements
๐น 6. Important Dictionary Methods
โญ
โ Get Method:
Output:
โ Keys Method:
Output:
โ Values Method:
Output:
โ Items Method:
Output:
๐น 7. Loop Through Dictionary
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
โ 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"])
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)
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:
Example:
๐ "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:
- Read One Line:
- Read All Lines:
๐น 4. Writing to a File
โ "w" will overwrite existing content.
๐น 5. Append to File
โ Adds content without deleting old data.
๐น 6. Best Practice (Very Important โญ)
Use with statement.
โ 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
โ 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
โค2
What will the following code do?
file = open("data.txt", "w") file.write("Hello")
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
Why is the with open() statement preferred?
Anonymous Quiz
27%
A) It runs faster
55%
B) It automatically closes the file
4%
C) It deletes the file
15%
D) It prevents writing
โค2๐1๐ฅฐ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:
Output: ZeroDivisionError
This will crash the program.
๐น 2. Using tryโexcept
We use tryโexcept to handle errors.
Syntax:
Example:
Output: Error occurred
๐น 3. Handling Specific Exceptions
โ Handles only ValueError.
๐น 4. Using else
else runs if no error occurs.
Output: No error
๐น 5. Using finally
finally always executes.
๐น 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
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