PART – A
1. Define Dictionary:
A dictionary in Python is a built-in data type that stores data as key–value pairs. Each key is unique, and it
is used to access its corresponding value. Dictionaries are unordered, mutable (changeable), and indexed
by keys instead of positions.
1. How will you declare a dictionary?
A dictionary is declared using curly braces {} or the dict() function.
Example using {}:
my_dict = {"name": "Hari", "age": 20, "course": "AI"}
Example using dict():
my_dict = dict(name="Hari", age=20, course="AI")
You can access values using keys:
print(my_dict["name"]) # Output: Hari
1. Define List Comprehension:
List comprehension is a short and efficient way to create lists in Python using a single line of code. It
combines loops and optional conditions into a compact syntax.
Basic syntax:
[expression for item in iterable if condition]
Example:
squares = [x*x for x in range(5)]
With condition:
even_numbers = [x for x in range(10) if x % 2 == 0]
It improves readability and performance compared to traditional loops.
2. Syntax and Example for List Comprehension:
Syntax:
[expression for item in iterable if condition]
Example:
squares = [x*x for x in range(5)]
Output: [0, 1, 4, 9, 16]
With condition:
even_numbers = [x for x in range(10) if x % 2 == 0]
2. What do you mean by File?
A file is a collection of data stored on a storage device (like a hard disk) permanently. In Python, files are
used to store and retrieve data even after the program ends.
2. Modes of File:
File modes define how a file is opened in Python.
• r – Read mode (default, file must exist)
• w – Write mode (creates new file or overwrites existing file)
• a – Append mode (adds data to the end of file)
• r+ – Read and write mode
• w+ – Write and read (overwrites file)
• a+ – Append and read
(For binary files, add b: rb, wb, etc.)
3. Define Exception:
An exception is an error that occurs during the execution of a program, which interrupts the normal flow of
the program.
3. How will you handle exception?
Exceptions are handled in Python using try, except, else, and finally blocks.
Syntax:
try:
# code that may cause error
except:
# code to handle error
Example:
try:
x = int(input("Enter a number: "))
print(10 / x)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
finally:
print("Execution completed")
3. List the different types of error:
• Syntax Error – Error due to incorrect syntax (e.g., missing colon)
• Runtime Error (Exception) – Error during execution (e.g., division by zero)
• Logical Error – Program runs but gives wrong output
Common exceptions in Python:
• ZeroDivisionError
• ValueError
• TypeError
• IndexError
• KeyError
4. Example program for ValueError (VALUE ACCEPT error):
(ValueError occurs when the input type is not correct)
try:
num = int(input("Enter a number: "))
print("You entered:", num)
except ValueError:
print("Invalid input! Please enter a valid number.")
4. Program to Copy Contents from One File to Another:
# open source file in read mode
source = open("[Link]", "r")
# open destination file in write mode
destination = open("[Link]", "w")
# read content from source file
data = [Link]()
# write content to destination file
[Link](data)
# close both files
[Link]()
[Link]()
print("File copied successfully!")
(Alternative using with for better practice:)
with open("[Link]", "r") as source, open("[Link]", "w") as destination:
[Link]([Link]())
4. State the V’s of Big Data:
The main V’s of Big Data are:
• Volume – Large amount of data
• Velocity – Speed at which data is generated and processed
• Variety – Different types of data (text, images, videos, etc.)
• Veracity – Accuracy and reliability of data
• Value – Useful insights obtained from data
(These are commonly called the 5 V’s of Big Data)
5. What is Data Science?
Data Science is a field that involves collecting, analyzing, and interpreting large amounts of data using
techniques from statistics, programming, and machine learning to extract useful insights and support
decision-making.
5. List the steps involved in Data Science Processing:
The main steps are:
1. Data Collection – Gathering data from various sources
2. Data Cleaning – Removing errors and handling missing data
3. Data Transformation – Converting data into a suitable format
4. Data Analysis – Applying statistical and analytical methods
5. Model Building – Creating models using machine learning
6. Evaluation – Checking model performance
7. Visualization & Interpretation – Presenting results using charts/graphs
8. Deployment – Using the model in real-world applications
5. What is Network Data?
Network data refers to data that represents connections or relationships between different entities (nodes).
It is usually structured as graphs consisting of nodes (vertices) and edges (links).
Example:
• Social media networks (users connected as friends)
• Computer networks (devices connected through the internet)
6. Illustrate the role of Data Science in Medical Research:
Data Science plays an important role in improving healthcare and medical research:
• Disease Prediction – Helps predict diseases like cancer, diabetes using patient data
• Drug Discovery – Speeds up the process of finding new medicines
• Medical Imaging Analysis – Analyzes X-rays, MRI scans using AI
• Personalized Treatment – Suggests treatments based on patient history
• Epidemic Tracking – Tracks and predicts spread of diseases (like COVID-19)
• Clinical Decision Support – Assists doctors in making better decisions
6. Differentiate Structured and Unstructured Data:
Structured Data Unstructured Data
Organized in rows and columns Not organized in a fixed format
Stored in databases (tables) Stored in files like text, images, videos
Easy to search and analyze Difficult to process and analyze
Example: Excel, SQL tables Example: Emails, social media posts
6. Example of Unstructured Data:
• Text documents (PDF, Word files)
• Images (photos, medical scans)
• Videos
• Audio files
• Social media posts (comments, tweets)
7. What is Data Cleansing?
Data cleansing (or data cleaning) is the process of detecting and correcting errors in data. It involves
removing duplicate data, fixing missing or incorrect values, and ensuring the data is accurate and consistent.
7. Define Data Modeling:
Data modeling is the process of creating a structure or representation of data and its relationships. It
helps in organizing data in a way that it can be easily stored, accessed, and analyzed.
7. List the Applications of Data Science:
• Healthcare – Disease prediction and diagnosis
• Finance – Fraud detection and risk analysis
• E-commerce – Recommendation systems (Amazon, Flipkart)
• Social Media – User behavior analysis
• Transportation – Traffic prediction and route optimization
• Education – Student performance analysis
• Marketing – Customer segmentation and targeted advertising
8. Compare Big Data and Data Science:
Big Data Data Science
Refers to large volumes of data Refers to analyzing data for insights
Focus on storage and processing Focus on analysis and decision-making
Uses tools like Hadoop, Spark Uses statistics, ML, Python, R
Deals with data size, speed, variety Extracts meaning and knowledge from data
8. State the concept of Outliers:
Outliers are data values that are significantly different from other values in a dataset. They may occur due
to errors or unusual conditions and can affect analysis results.
Example:
In the dataset: 10, 12, 11, 13, 100 → 100 is an outlier.
8. Define Data Exploration Process:
Data exploration is the process of analyzing and understanding data using statistical methods and
visualization techniques before applying models.
It includes:
• Checking data structure
• Identifying patterns and trends
• Detecting outliers and missing values
• Summarizing data using charts and graphs
PART – B
File Handling in Python
1. Introduction:
File handling in Python is used to store data permanently in files and to read, write, or modify that data.
It allows programs to work with external files like text files (.txt), binary files, etc.
2. Types of Files:
• Text Files – Store data in readable form (e.g., .txt)
• Binary Files – Store data in binary format (e.g., images, videos)
3. File Operations:
The main file operations are:
• Open a file
• Read from a file
• Write to a file
• Append to a file
• Close the file
4. Opening a File:
Syntax:
file = open("filename", "mode")
Common Modes:
• r → Read
• w → Write (overwrites file)
• a → Append
• r+, w+, a+ → Read & Write
5. Reading from a File:
• read() → Reads entire content
• readline() → Reads one line
• readlines() → Reads all lines as list
Example:
file = open("[Link]", "r")
content = [Link]()
print(content)
[Link]()
6. Writing to a File:
file = open("[Link]", "w")
[Link]("Hello World")
[Link]()
7. Appending to a File:
file = open("[Link]", "a")
[Link]("\nNew Line Added")
[Link]()
8. Using with Statement (Best Practice):
Automatically closes the file.
with open("[Link]", "r") as file:
print([Link]())
9. Example Program (Read and Write File):
# Writing into a file
with open("[Link]", "w") as file:
[Link]("Welcome to Python File Handling\n")
[Link]("This is a sample file.")
# Reading from the file
with open("[Link]", "r") as file:
content = [Link]()
print("File Content:\n", content)
10. Advantages of File Handling:
• Stores data permanently
• Helps in data sharing
• Useful for large data storage
• Supports data processing and analysis
Conclusion:
File handling is an essential concept in Python that allows efficient data storage, retrieval, and
manipulation, making it useful in real-world applications like logging, databases, and data analysis.
Advanced List Processing Methods in Python
Python provides several powerful methods to process lists efficiently. These are called advanced list
processing techniques.
1. List Comprehension
A concise way to create and process lists.
Example:
squares = [x*x for x in range(5)]
print(squares)
2. map() Function
Applies a function to all elements in a list.
Example:
nums = [1, 2, 3, 4]
result = list(map(lambda x: x*2, nums))
print(result)
3. filter() Function
Filters elements based on a condition.
Example:
nums = [1, 2, 3, 4, 5, 6]
even = list(filter(lambda x: x % 2 == 0, nums))
print(even)
4. reduce() Function
Applies a function cumulatively (from functools module).
Example:
from functools import reduce
nums = [1, 2, 3, 4]
sum_val = reduce(lambda x, y: x + y, nums)
print(sum_val)
5. Sorting Methods
• sort() – sorts original list
• sorted() – returns new sorted list
Example:
nums = [4, 1, 3, 2]
[Link]()
print(nums)
new_list = sorted(nums, reverse=True)
print(new_list)
6. List Slicing
Extracts parts of a list.
Example:
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
7. Nested List Processing
Working with lists inside lists.
Example:
matrix = [[1, 2], [3, 4], [5, 6]]
for row in matrix:
print(row)
8. enumerate() Function
Gives index and value together.
Example:
fruits = ["apple", "banana", "cherry"]
for index, value in enumerate(fruits):
print(index, value)
9. zip() Function
Combines multiple lists.
Example:
names = ["A", "B", "C"]
marks = [90, 80, 85]
combined = list(zip(names, marks))
print(combined)
Conclusion:
Advanced list processing methods like list comprehension, map, filter, reduce, slicing, sorting, zip, and
enumerate help in writing efficient, readable, and concise Python programs for handling data.
Exception Handling in Python
1. Introduction:
Exception handling is a mechanism in Python used to handle runtime errors so that the program does not
crash and continues execution normally.
An exception occurs when an error happens during program execution (e.g., division by zero, invalid input).
2. Need for Exception Handling:
• Prevents program termination
• Improves program reliability
• Handles unexpected user input
• Maintains normal flow of execution
3. Types of Errors:
• Syntax Errors – Errors in code structure
• Runtime Errors (Exceptions) – Occur during execution
• Logical Errors – Incorrect output without crashing
4. Exception Handling Blocks:
a) try Block
Contains code that may cause an exception
try:
x = int(input("Enter number: "))
b) except Block
Handles the exception
except ValueError:
print("Invalid input")
c) else Block
Executes if no exception occurs
else:
print("No error occurred")
d) finally Block
Always executes (cleanup code)
finally:
print("Execution completed")
5. Complete Example Program:
try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 / num2
except ZeroDivisionError:
print("Error: Cannot divide by zero")
except ValueError:
print("Error: Invalid input")
else:
print("Result:", result)
finally:
print("Program ended")
Case 1: Valid Input (No Error)
Input:
Enter first number: 10
Enter second number: 2
Output:
Result: 5.0
Program ended
Case 2: Division by Zero
Input:
Enter first number: 10
Enter second number: 0
Output:
Error: Cannot divide by zero
Program ended
Case 3: Invalid Input (Non-numeric)
Input:
Enter first number: abc
Output:
Error: Invalid input
Program ended
6. Multiple Exceptions:
A single try block can have multiple except blocks to handle different errors.
7. Generic Exception Handling:
try:
x = int("abc")
except Exception as e:
print("Error:", e)
8. Raising Exceptions (raise keyword):
You can manually raise exceptions.
age = -5
if age < 0:
raise ValueError("Age cannot be negative")
9. Advantages of Exception Handling:
• Avoids program crash
• Improves user experience
• Makes debugging easier
• Ensures smooth execution
Conclusion:
Exception handling is an essential feature in Python that helps manage errors effectively using try, except,
else, and finally blocks. It ensures that programs run smoothly even when unexpected situations occur.
Different Methods Used in Dictionary
A dictionary provides many built-in methods to store, access, and manipulate data efficiently.
1. Creating a Dictionary
my_dict = {"name": "Hari", "age": 20}
2. get() Method
Used to safely access values.
print(my_dict.get("name")) # Hari
print(my_dict.get("marks", 0)) # Default value if key not found
3. keys() Method
Returns all keys.
print(my_dict.keys())
4. values() Method
Returns all values.
print(my_dict.values())
5. items() Method
Returns key-value pairs.
print(my_dict.items())
6. update() Method
Updates or adds new key-value pairs.
my_dict.update({"age": 21, "city": "Madurai"})
print(my_dict)
7. pop() Method
Removes a specific key.
my_dict.pop("age")
print(my_dict)
8. popitem() Method
Removes the last inserted item.
my_dict.popitem()
print(my_dict)
9. clear() Method
Removes all elements.
my_dict.clear()
print(my_dict)
10. copy() Method
Creates a copy of dictionary.
new_dict = my_dict.copy()
11. setdefault() Method
Returns value of key, if not present inserts key with default value.
my_dict.setdefault("country", "India")
print(my_dict)
12. Example Program (Using Multiple Methods):
student = {"name": "Hari", "marks": 85}
# Access value
print([Link]("name"))
# Add/update
[Link]({"marks": 90, "grade": "A"})
# Display keys and values
print([Link]())
print([Link]())
# Remove item
[Link]("grade")
print("Final Dictionary:", student)
Output:
Hari
dict_keys(['name', 'marks', 'grade'])
dict_values(['Hari', 90, 'A'])
Final Dictionary: {'name': 'Hari', 'marks': 90}
Conclusion:
Dictionary methods like get(), keys(), values(), items(), update(), pop(), clear(), copy(), and setdefault()
help in efficient data manipulation and management, making dictionaries very powerful in Python
programming.
Data Science – Concept and Applications
1. Introduction to Data Science:
Data Science is an interdisciplinary field that involves collecting, processing, analyzing, and interpreting
data to extract meaningful insights and support decision-making. It combines statistics, programming,
mathematics, and machine learning.
2. Key Components of Data Science:
• Data Collection – Gathering data from sources like databases, sensors, web
• Data Cleaning – Removing errors, missing values, duplicates
• Data Analysis – Using statistical techniques to find patterns
• Machine Learning – Building models for prediction and automation
• Data Visualization – Representing data using graphs and charts
• Decision Making – Using insights for business or research decisions
3. Tools and Technologies:
• Programming: Python, R
• Libraries: Pandas, NumPy, Matplotlib
• Databases: SQL, MongoDB
• Big Data Tools: Hadoop, Spark
4. Applications of Data Science:
a) Healthcare
• Disease prediction and diagnosis
• Medical image analysis (X-rays, MRI)
• Drug discovery
b) Finance
• Fraud detection
• Risk analysis
• Stock market prediction
c) E-commerce
• Product recommendation systems
• Customer behavior analysis
• Sales prediction
d) Social Media
• Sentiment analysis
• User engagement tracking
• Content recommendation
e) Transportation
• Traffic prediction
• Route optimization
• Autonomous vehicles
f) Education
• Student performance analysis
• Personalized learning systems
g) Marketing
• Customer segmentation
• Targeted advertising
5. Advantages of Data Science:
• Helps in better decision-making
• Improves efficiency and accuracy
• Enables automation
• Provides competitive advantage
Conclusion:
Data Science plays a crucial role in modern industries by transforming raw data into valuable insights. Its
wide range of applications in healthcare, finance, e-commerce, and other fields makes it one of the most
important technologies today.
Different Facets of Data with Examples
1. Introduction:
The term facets of data refers to the different characteristics, forms, and aspects of data that help in
understanding, processing, and analyzing it effectively.
2. Facets of Data:
a) Structured Data
Data that is organized in a fixed format (rows and columns).
Examples:
• Database tables
• Excel sheets
Features:
• Easy to store and analyze
• Highly organized
b) Unstructured Data
Data that has no predefined format or structure.
Examples:
• Images, videos
• Emails, social media posts
Features:
• Difficult to analyze
• Requires advanced tools (AI/ML)
c) Semi-Structured Data
Data that is partially organized with tags or markers.
Examples:
• JSON, XML files
Features:
• Flexible structure
• Easier than unstructured data to process
d) Qualitative Data
Data that describes qualities or characteristics (non-numeric).
Examples:
• Colors, names, feedback comments
Features:
• Descriptive in nature
• Cannot be measured directly
e) Quantitative Data
Data that is numeric and measurable.
Examples:
• Age, marks, salary
Types:
• Discrete (countable) → Number of students
• Continuous → Height, weight
f) Big Data (5 V’s)
Large and complex data characterized by:
• Volume – Huge amount of data
• Velocity – Speed of data generation
• Variety – Different formats
• Veracity – Data accuracy
• Value – Useful insights
Example:
• Social media platforms generating millions of posts daily
g) Metadata
Data about data (information describing other data).
Examples:
• File size, file type
• Date created
h) Time-Series Data
Data collected over time intervals.
Examples:
• Stock prices
• Temperature readings
3. Conclusion:
Understanding different facets of data such as structured, unstructured, qualitative, quantitative, and big
data characteristics helps in choosing the right tools and techniques for effective data analysis and
decision-making.
Step-by-Step Activities in the Data Science Process
1. Problem Definition
• Understand the problem clearly
• Define objectives and goals
Example: Predict student performance or sales forecasting
2. Data Collection
• Gather data from various sources
• Sources: databases, APIs, surveys, web scraping
3. Data Cleaning (Preprocessing)
• Remove missing, duplicate, or incorrect data
• Handle inconsistencies and noise
Example: Filling missing values or removing errors
4. Data Integration
• Combine data from multiple sources into one dataset
Example: Merging customer data from different systems
5. Data Transformation
• Convert data into suitable format
• Normalize, scale, or encode data
6. Exploratory Data Analysis (EDA)
• Analyze data to find patterns and trends
• Use statistics and visualization (graphs, charts)
7. Feature Selection/Engineering
• Select important variables (features)
• Create new features for better prediction
8. Model Building
• Apply machine learning algorithms
Examples: Linear regression, decision trees
9. Model Evaluation
• Test model performance using metrics
Examples: Accuracy, precision, recall
10. Model Deployment
• Deploy model in real-world applications
Example: Recommendation system in apps
11. Monitoring and Maintenance
• Continuously check model performance
• Update model when needed
Conclusion:
The data science process is a systematic approach involving problem understanding, data preparation,
analysis, modeling, and deployment, which helps in extracting meaningful insights and making data-
driven decisions.
Process of Data Preparation
1. Introduction:
Data preparation is the process of collecting, cleaning, and transforming raw data into a usable format
for analysis and modeling. It is one of the most important steps in data science, as the quality of data directly
affects results.
2. Steps in Data Preparation:
a) Data Collection
• Gather data from multiple sources
• Sources include databases, APIs, surveys, web data
Example: Collecting student marks from different departments
b) Data Cleaning
• Remove errors, duplicates, and missing values
• Correct inconsistent data
Techniques:
• Filling missing values (mean, median)
• Removing duplicates
Example: Replacing missing marks with average values
c) Data Integration
• Combine data from different sources into a single dataset
Example: Merging customer data from sales and marketing systems
d) Data Transformation
• Convert data into suitable format for analysis
Techniques:
• Normalization (scaling values)
• Encoding categorical data (e.g., Male = 0, Female = 1)
e) Data Reduction
• Reduce size of data while maintaining important information
Methods:
• Removing unnecessary columns
• Dimensionality reduction
f) Handling Outliers
• Detect and treat unusual values that may affect analysis
Example: Removing extreme values like very high salary entries
g) Data Validation
• Ensure data is accurate and consistent
Example: Checking if age is within valid range
h) Data Formatting
• Organize data into proper structure (tables, rows, columns)
• Convert data types (string to integer, etc.)
3. Importance of Data Preparation:
• Improves accuracy of analysis
• Reduces errors
• Enhances model performance
• Saves time in later stages
4. Conclusion:
Data preparation is a crucial step in data science that ensures high-quality, reliable, and well-structured
data, enabling better analysis and accurate decision-making.