Data Science Using Python
Data Science Using Python
Data Science Using Python
2. Key Components:
Programming Skills: Proficiency in languages like Python or R for data manipulation and analysis.
Data Cleaning and Preprocessing: Dealing with missing values, outliers, and ensuring data quality.
Machine Learning: Applying algorithms to create models for predictions and classifications.
Domain Knowledge: Understanding the context and specific needs of the industry or field.
Programming Languages: Python and R are widely used for data science.
Libraries and Frameworks: Pandas, NumPy, scikit-learn (for Python), and tidyverse (for R).
Many universities and online platforms offer courses and degrees in data science.
Books, online tutorials, and open-source documentation are valuable learning resources.
Data Visualization:
Informed Decision-Making:
Efficient Operations:
Business Innovation:
Risk Management:
Identifies and assesses risks in various domains, including finance and healthcare.
Personalization:
Facilitates personalized user experiences in areas like marketing and recommendation systems.
Competitive Advantage:
Social Impact:
Contributes to social good through applications in areas like public health and disaster response.
Informed Decision-Making:
Predictive Analytics:
Operational Efficiency:
Personalization:
Healthcare Advancements:
Customer Understanding:
Enhances supply chain management through demand forecasting and inventory optimization.
Social Impact:
Contributes to social good through applications in public health, education, and disaster response.
Continuous Improvement:
Data-Driven Culture:
Cost Reduction:
Identifies and assesses risks in various domains, including finance and cybersecurity.
In summary, the advantages of Data Science are diverse and impactful, offering organizations and
individuals the tools and insights needed to thrive in a data-driven world.
Data Collection:
Ensure data quality and assess the suitability of the data for analysis.
Explore and visualize the data to understand patterns, relationships, and potential insights.
Use statistical methods to summarize and describe the main characteristics of the data.
Feature Engineering:
Create new features or transform existing ones to enhance the performance of machine learning
models.
Select relevant features that contribute to the predictive power of the model.
Model Building:
Select appropriate machine learning algorithms based on the nature of the problem (classification,
regression, clustering, etc.).
Split the data into training and testing sets for model training and evaluation.
Model Training:
Train the chosen machine learning model using the training dataset.
Model Evaluation:
Use metrics such as accuracy, precision, recall, and F1 score to evaluate model effectiveness.
Model Interpretation:
Deployment:
Update the model as needed to adapt to changes in data patterns or business requirements.
Communication of Results:
Iterative Process:
Data Science is often an iterative process, involving revisiting and refining steps based on new
insights or changing requirements.
Ethical Considerations:
Consider ethical implications related to privacy, bias, and fairness in data analysis.
Documentation:
Document the entire process, including data sources, methodologies, and findings.
Facilitate knowledge transfer and replication of the analysis.
It's important to note that these steps are not strictly linear and may involve going back and forth
based on the evolving understanding of the data and problem at hand. The iterative nature of Data
Science allows for continuous improvement and refinement throughout the analysis process.
Problem Definition:
Collaborate with stakeholders to clearly define business problems or research questions that can be
addressed through data analysis.
Identify and collect relevant data from various sources, both internal and external.
Conduct exploratory data analysis (EDA) to understand the structure, patterns, and characteristics of
the data.
Feature Engineering:
Create new features or transform existing ones to enhance the performance of machine learning
models.
Model Development:
Select appropriate machine learning algorithms based on the nature of the problem (classification,
regression, clustering, etc.).
Model Deployment:
Implement models into production environments, ensuring seamless integration with existing
systems.
Communication of Results:
Collaborate with domain experts, business analysts, and decision-makers to understand the context
of the analysis.
Work closely with IT, engineering, and other departments for successful project implementation.
Continuous Learning:
Stay current with advancements in data science, machine learning, and relevant technologies.
Ethical Considerations:
Consider ethical implications related to privacy, bias, and fairness in data analysis.
Documentation:
Educational Outreach:
The responsibilities of a Data Scientist often require a combination of technical skills, domain
expertise, and effective communication to ensure that data-driven insights contribute to
organizational success.
Education:
Educational Background:
A bachelor's degree in a relevant field such as Computer Science, Statistics, Mathematics, Physics,
Engineering, or a related discipline.
Many data scientists hold advanced degrees (master's or Ph.D.) in data science, machine learning,
statistics, or a related field.
Completion of specialized courses or certifications in data science, machine learning, and statistical
analysis can enhance qualifications.
Certifications from recognized organizations, such as Coursera, edX, or professional associations like
the Data Science Council of America (DASCA), are valuable.
Technical Skills:
Programming Languages:
Proficiency in programming languages such as Python and/or R is crucial for data manipulation,
analysis, and modeling.
Strong foundation in statistics and mathematics for data analysis, hypothesis testing, and predictive
modeling.
Machine Learning:
Knowledge of machine learning algorithms and frameworks (e.g., scikit-learn, TensorFlow, PyTorch).
Skills in data cleaning, preprocessing, and manipulation using tools like Pandas in Python.
Data Visualization:
Proficiency in data visualization tools (e.g., Matplotlib, Seaborn, Plotly) to present findings in a clear
and concise manner.
Familiarity with big data technologies, such as Hadoop and Spark, for handling large-scale datasets.
Soft Skills:
Analytical Thinking:
Communication Skills:
Collaboration:
Ability to collaborate with cross-functional teams, including domain experts, engineers, and business
leaders.
Domain Expertise:
Industry Knowledge:
Understanding of the specific industry or domain in which the data scientist works, allowing for more
relevant and impactful analyses.
Additional Qualifications:
Coding Practices:
Adherence to best coding practices, version control, and familiarity with software development
methodologies.
Problem-Solving:
Strong problem-solving skills and the ability to think creatively about data-related challenges.
Continuous Learning:
Commitment to staying updated with the latest developments in data science and related
technologies.
Ethical Considerations:
Awareness of ethical considerations related to data privacy, bias, and responsible use of data.
Project Management:
Basic project management skills to effectively plan and execute data science projects.
It's important to note that the field of data science is dynamic, and individuals may continue to build
on their qualifications throughout their careers. Real-world experience through internships, personal
projects, or contributions to open-source projects can also be valuable in showcasing practical skills.
Employers may prioritize a combination of education, technical proficiency, and relevant experience
when hiring data scientists.
Python is a widely adopted and popular programming language in the field of data science for
several compelling reasons:
Python has a simple and readable syntax, making it accessible for beginners and easy to understand
for experienced programmers.
Its readability helps in writing clean and concise code, facilitating collaboration among data science
teams.
Python has a rich ecosystem of libraries and frameworks specifically designed for data science and
machine learning, including:
Python has a large and active community of developers and data scientists.
The community contributes to a wealth of resources, tutorials, and forums, making it easy to find
help and share knowledge.
Versatility:
Python is a versatile language that can be used for a wide range of tasks beyond data science,
including web development, automation, scripting, and more.
This versatility allows data scientists to integrate their work into larger applications seamlessly.
Open Source:
Python is an open-source language with a permissive license, fostering collaboration and sharing of
tools and libraries within the community.
This open nature facilitates innovation and the development of a diverse set of tools.
Integration Capabilities:
Python can easily integrate with other languages and technologies, allowing data scientists to use
specialized libraries or tools when needed.
Data Visualization:
Python offers multiple powerful libraries for data visualization, such as Matplotlib, Seaborn, and
Plotly, allowing data scientists to create compelling and informative visualizations.
Community-Driven Packages:
The Python Package Index (PyPI) hosts a vast collection of community-driven packages and modules.
Data scientists can leverage these packages to extend Python's functionality and address specific
data science challenges.
Many large corporations and tech companies have embraced Python for data science and machine
learning, contributing to its widespread adoption.
This industry acceptance has led to increased support and resources for Python in the data science
ecosystem.
Python is one of the most sought-after skills in the job market, especially in the field of data science.
Many data science job postings specifically mention Python as a required or preferred skill.
In summary, Python's simplicity, extensive libraries, community support, and versatility make it a
preferred choice for data scientists. Its popularity continues to grow as it remains at the forefront of
data science and machine learning developments.
Unit 2 - Introduction to Python
What is Python
Python is a high-level, general-purpose programming language known for its readability, simplicity,
and versatility. It was created by Guido van Rossum and first released in 1991. Python is designed to
be easy to understand and write, with a clean and straightforward syntax that emphasizes code
readability.
Python uses a clear and readable syntax, making it easy for beginners to learn and write code.
The language emphasizes the use of indentation for code blocks, enhancing code readability.
Python is an interpreted language, meaning that it is executed line by line by an interpreter, rather
than being compiled into machine code.
Python comes with a comprehensive standard library that includes modules and packages for various
tasks, simplifying development by providing pre-built functionality.
Dynamically Typed:
Python is dynamically typed, meaning that variable types are determined at runtime. This allows for
flexibility but requires careful attention to variable types in code.
Python supports object-oriented programming principles, allowing developers to create and use
classes and objects.
Extensive Ecosystem:
The Python ecosystem includes a vast collection of third-party libraries and frameworks that extend
its functionality, such as Django for web development, NumPy for scientific computing, and
TensorFlow for machine learning.
Python has a large and active community of developers who contribute to its growth and
development.
Extensive documentation and tutorials are available, making it easy for developers to find resources
and support.
Cross-Platform Compatibility:
Python is platform-independent, meaning that Python code can run on various operating systems
without modification.
Open Source:
Python is open source, and its source code is freely available. This encourages collaboration and the
development of a wide range of applications and tools.
Python's popularity has grown significantly over the years, and it is widely used in industry,
academia, and the open-source community. Its versatility, ease of use, and strong community
support make it a popular choice for developers worldwide.
Features of Python
Python is a versatile and powerful programming language that offers a wide range of features. Here
are some key features of Python:
Python's syntax is designed to be clear and readable, emphasizing code readability and reducing the
cost of program maintenance.
The use of indentation for code blocks enforces a clean and consistent coding style.
Interpreted Language:
Python is an interpreted language, allowing for easy and quick development. Code is executed line by
line by the Python interpreter.
Python is a general-purpose language suitable for various applications, including web development,
data science, machine learning, automation, scripting, and more.
High-Level Language:
Python abstracts many low-level details, making it easier for developers to focus on solving problems
without dealing with complex memory management.
Dynamically Typed:
Python is dynamically typed, meaning that variable types are determined at runtime. This offers
flexibility but requires careful attention to variable types.
Python comes with a comprehensive standard library that includes modules and packages for various
tasks, reducing the need for external dependencies.
Python has a large and active community of developers who contribute to its growth and
development.
Extensive documentation and online resources make it easy for developers to find support and learn
Python.
Cross-Platform Compatibility:
Python code is platform-independent, meaning it can run on various operating systems without
modification.
Extensive Ecosystem:
The Python ecosystem includes a rich collection of third-party libraries and frameworks, such as
Django for web development, NumPy for scientific computing, and TensorFlow for machine learning.
Integration Capabilities:
Python can easily integrate with other languages and technologies, making it suitable for
interoperability with existing systems and tools.
Open Source:
Python is open source, and its source code is freely available. This fosters collaboration, community
contributions, and the development of a wide range of applications and tools.
Rapid Prototyping:
Python's simplicity and readability make it an excellent choice for rapid prototyping and
development.
Support for Modular Programming:
Python supports modular programming, allowing developers to break down large programs into
smaller, manageable modules.
Python provides built-in high-level data structures like lists, dictionaries, and sets, simplifying
complex data manipulations.
Python employs automatic memory management, handling memory allocation and deallocation,
reducing the risk of memory leaks.
These features collectively contribute to Python's popularity and make it a favored choice for a wide
range of applications and industries. The language continues to evolve, with the Python community
actively contributing to its growth and improvement.
History of python
Python, a high-level programming language, has a rich history that dates back to the late 1980s. Here
is a timeline highlighting key events in the history of Python:
Python was conceived by Guido van Rossum, a Dutch programmer, in the late 1980s. The
development started as a Christmas project over the holidays in December 1989.
The first official release, Python 0.9.0, was released on February 20, 1991. This version included
many fundamental features of the language.
Python 1.0 was released on January 26, 1994. It included new features such as lambda, map, filter,
and reduce.
Python 2.0, released on October 16, 2000, introduced list comprehensions and garbage collection.
Python 3.0, a significant and backward-incompatible release, was released on December 3, 2008. It
aimed to clean up and simplify the language, addressing various design issues.
2010s: Coexistence of Python 2 and Python 3:
The transition from Python 2 to Python 3 was gradual. Both versions coexisted for several years, with
Python 2 reaching its end of life on January 1, 2020.
In 2018, the Python Software Foundation adopted a new governance model called PEP 8000, which
aimed to provide a more inclusive and transparent decision-making process.
Python 2 officially reached its end of life on January 1, 2020. This marked the end of official support
and updates for Python 2, encouraging the community to transition to Python 3.
Python 3.9, released on October 5, 2020, included new features and optimizations, continuing the
evolution of the language.
Python continues to be actively developed, with regular releases introducing new features,
improvements, and optimizations.
The Python community remains vibrant, with a large number of contributors, extensive
documentation, and a wealth of third-party libraries and frameworks.
Python's success can be attributed to its simplicity, readability, versatility, and a strong and active
community that contributes to its ongoing development and growth. The language is widely used in
various domains, including web development, data science, machine learning, scientific computing,
and more.
You can write Python code using a simple text editor like Notepad (on Windows) or TextEdit (on
macOS) or use a more feature-rich integrated development environment (IDE) like VSCode, PyCharm,
or Jupyter Notebook.
Open your chosen editor or IDE and write your Python code. A simple "Hello, World!" program is a
common starting point:
print("Hello, World!")
Save Your Python File:
Save your Python code with a .py extension. For example, you might save it as hello.py.
Type python or python3 (depending on your system) followed by the name of your Python file and
press Enter:
bash
python hello.py
Look for a "Run" or "Execute" button in the IDE, often represented by a play icon.
If you are using Jupyter Notebook, open your notebook file (with a .ipynb extension).
Run Cells:
Use the "Run" button or press Shift+Enter to run the cells containing your Python code.
Output:
Regardless of the method used to execute your Python program, you should see the output in the
command line, terminal, or IDE console. In the case of the "Hello, World!" program, the output
would be:
Output:
Hello, World!
Basic syntax
The basic syntax of Python is designed to be simple and readable. Here are some fundamental
elements of Python syntax:
1. Indentation:
Python uses indentation to define blocks of code. Indentation is crucial for determining the scope of
functions, loops, and conditional statements.
if x > 0:
print("Positive number")
else:
print("Non-positive number")
2. Variables:
Variables are used to store data. You can assign values to variables using the = operator.
x = 10
name = "John"
3. Data Types:
Python has several built-in data types, including integers, floats, strings, booleans, lists, tuples, and
dictionaries.
num = 42 # integer
pi = 3.14 # float
4. Print Statement:
print("Hello, World!")
5. Comments:
Comments start with the # symbol and are used for adding explanations within the code.
# This is a comment
6. Conditional Statements:
if, elif (else if), and else are used for conditional execution.
if x > 0:
print("Positive number")
elif x == 0:
print("Zero")
else:
print("Negative number")
7. Loops:
for i in range(5):
print(i)
while x > 0:
print(x)
x -= 1
8. Functions:
def greet(name):
greet("Alice")
9. Lists:
my_list = [1, 2, 3]
10. Dictionaries:
11. Tuples:
my_tuple = (1, 2, 3)
These are some of the basic elements of Python syntax. As you progress, you'll encounter more
advanced concepts and features that make Python a powerful and versatile programming language.
variables
In Python, variables are used to store and manage data. Here are some key points about variables in
Python:
Variable Assignment:
Variables are created using the assignment operator =. You assign a value to a variable like this:
x = 10
name = "John"
pi = 3.14
is_true = True
Variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
Choose meaningful and descriptive variable names for better code readability.
Data Types:
Python is dynamically typed, meaning you don't need to explicitly declare the data type of a variable.
The interpreter infers the type based on the assigned value:
x = 10 # Integer
pi = 3.14 # Float
Reassigning Variables:
You can change the value of a variable by assigning a new value to it:
x=5
print(x) # Output: 5
x = "Hello"
Multiple Assignment:
a, b, c = 1, 2, 3
Constants:
While Python doesn't have true constants, naming conventions in uppercase are often used to
indicate that a variable is intended to be a constant:
PI = 3.14
GRAVITY = 9.8
Deleting Variables:
x = 10
del x
# Trying to access x now would result in an error
Understanding and using variables effectively is fundamental to programming in Python. They serve
as containers for storing and manipulating data throughout your code.
Keywords
In Python, keywords are reserved words that have special meanings and cannot be used as
identifiers (names for variables, functions, etc.). These keywords define the syntax and structure of
the language. Here is a list of Python keywords:
as elif if or yield
These keywords are case-sensitive, meaning that True and true are different and have distinct
meanings in Python. It's important to avoid using these words as variable names or identifiers in your
code to prevent conflicts and unexpected behavior.
Datatypes in python
Python supports several built-in data types that allow you to represent and work with different kinds
of data. Here are some of the fundamental data types in Python:
Numeric Types:
x=5
pi = 3.14
complex: Complex type represents complex numbers.
complex_num = 2 + 3j
Text Type:
str: String type represents textual data. Strings can be enclosed in single or double quotes.
name = "John"
Boolean Type:
is_true = True
Sequence Types:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_range = range(5)
Set Types:
my_set = {1, 2, 3}
Mapping Type:
dict: Dictionary type represents key-value pairs, where keys are unique.
None Type:
NoneType: The None type represents the absence of a value or a null value.
empty_variable = None
Binary Types:
binary_data = b'hello'
mutable_binary_data = bytearray(b'hello')
memory_view = memoryview(b'hello')
Operators
Operators in Python are special symbols or keywords that perform operations on operands. They are
used to manipulate data and perform various computations. Here are some common types of
operators in Python:
1. Arithmetic Operators:
+ # Addition
- # Subtraction
* # Multiplication
/ # Division
% # Modulus (remainder)
// # Floor Division
** # Exponentiation
2. Comparison Operators:
== # Equal to
!= # Not equal to
3. Logical Operators:
or # Logical OR
4. Assignment Operators:
= # Assignment
5. Identity Operators:
6. Membership Operators:
| # Bitwise OR
^ # Bitwise XOR
~ # Bitwise NOT
8. Unary Operators:
~ # Bitwise NOT
Indentation
In Python, indentation is a fundamental aspect of the language syntax. Unlike many programming
languages that use braces or keywords to denote code blocks, Python uses indentation to indicate
the grouping of statements. Here are the key points regarding indentation in Python:
Whitespace Matters:
Python uses spaces or tabs at the beginning of a line to define the level of indentation.
Indentation Levels:
Statements with the same level of indentation are considered part of the same block.
A colon : at the end of a control statement (like if, else, for, while, etc.) signals the start of an
indented code block.
if condition:
statement1
statement2
It is recommended to use spaces for indentation consistently. Mixing tabs and spaces can lead to
indentation errors.
Recommended Indentation:
The Python community conventionally uses four spaces for each level of indentation. This is
recommended in the official PEP 8 style guide.
def example_function():
if x > 0:
print("Positive number")
else:
print("Non-positive number")
End of Indentation:
if condition:
# Indented block
statement1
statement2
next_statement
Indentation is used consistently in loops, function definitions, and other control flow structures.
# Indented block
statement1
statement2
def my_function():
# Indented block
statement1
statement2
Blank lines are allowed and do not affect indentation. However, the level of indentation must remain
consistent within a block.
if condition:
statement1
statement2
Conditional statements
Conditional statements in Python are used to control the flow of the program based on specified
conditions. The key conditional statements in Python are:
1. if Statement:
The if statement is used to execute a block of code only if a specified condition is true.
if condition:
statement1
statement2
2. if-else Statement:
The if-else statement allows the program to execute one block of code if a condition is true and
another block if the condition is false.
if condition:
statement2
else:
statement3
statement4
3. if-elif-else Statement:
The if-elif-else statement is an extension of the if-else statement. It allows you to check multiple
conditions sequentially.
if condition1:
statement1
statement2
elif condition2:
statement3
statement4
else:
statement5
statement6
Example:
x = 10
if x > 0:
print("Positive number")
elif x == 0:
print("Zero")
else:
print("Negative number")
4. Nested if Statements:
You can nest if statements within each other to create more complex conditional structures.
if condition1:
if condition2:
statement1
else:
statement2
else:
statement3
These conditional statements allow you to create flexible and dynamic programs by making decisions
based on the evaluation of conditions. The indentation in Python plays a crucial role in defining the
scope of each block of code within these statements.
Looping Statements
Looping statements in Python allow you to execute a block of code repeatedly. The primary looping
statements are:
1. for Loop:
The for loop is used for iterating over a sequence (that is either a list, tuple, dictionary, string, or
range).
statement1
statement2
Example:
print(fruit)
2. while Loop:
The while loop is used to repeatedly execute a block of code as long as a specified condition is true.
while condition:
statement1
statement2
Example:
count = 0
print(count)
count += 1
3. break Statement:
The break statement is used to exit the loop prematurely based on a certain condition.
if condition:
# Code to be executed
break
Example:
if fruit == "banana":
break
print(fruit)
4. continue Statement:
The continue statement is used to skip the rest of the code inside a loop for the current iteration and
move to the next iteration.
for variable in sequence:
if condition:
# Code to be skipped
continue
statement1
statement2
Example:
if fruit == "banana":
continue
print(fruit)
In this example, when the loop encounters the "banana" element, the continue statement skips the
print(fruit) statement for that iteration and moves to the next iteration.
Both for and while loops can have an optional else clause, which is executed when the loop condition
becomes false.
statement1
statement2
else:
statement_after_loop
while condition:
statement1
statement2
else:
statement_after_loop
Example:
print(fruit)
else:
In this example, after iterating through all the fruits, the else block is executed because there are no
more items in the sequence.
These looping statements provide flexibility for iterating over sequences and performing repetitive
tasks in Python. Understanding how to use loops effectively is essential for writing efficient and
readable code.
Pass
In Python, pass is a null statement or a no-operation statement. It serves as a placeholder where
syntactically some code is required, but no action is desired or necessary. It is often used in situations
where the syntax demands a statement, but you don't want to execute any code. For example:
if x < 0:
print("Negative number")
else:
Here, the pass statement is used as a placeholder in the else block. Without it, there would be a
syntax error, as Python expects an indented block of code following the else keyword. The pass
statement allows the program to run without any actual code execution in that block.
Similarly, you might use pass when defining a function, class, or loop that needs to have a body, but
you're not ready to implement it yet:
def my_function():
It's important to note that pass does not do anything at runtime. It's simply a way to satisfy the
Python syntax requirements when a statement is expected but no action is necessary.
Unit 3 - Control structures and Strings
Strings
In Python, strings are used to represent sequences of characters. Strings are one of the fundamental
data types, and they are immutable, meaning that once a string is created, it cannot be changed.
Here are some key features and operations related to strings in Python:
Creating Strings:
Strings can be created using single quotes (') or double quotes ("). Triple quotes (''' or """) are used
for multiline strings.
multiline_string = '''This is a
multiline
string.'''
Accessing Characters:
Individual characters in a string can be accessed using indexing. Python uses 0-based indexing, so the
first character is at index 0.
my_string = "Python"
String Slicing:
my_string = "Python"
String Concatenation:
string1 = "Hello"
string2 = "World"
result = string1 + ", " + string2 # 'Hello, World'
String Repetition:
original_string = "abc"
String Methods:
Strings have numerous built-in methods for various operations, such as converting case, finding
substrings, splitting, joining, and more.
uppercase_string = my_string.upper()
substring_index = my_string.find("is")
String Formatting:
String formatting allows you to create formatted strings by inserting values into placeholders.
name = "Alice"
age = 30
Escape Characters:
Raw Strings:
Raw strings are specified by prefixing the string literal with an 'r' or 'R', and they treat backslashes as
literal characters.
These are some of the basic operations and features related to strings in Python. Strings are versatile
and play a crucial role in various programming tasks.
Basic string operations
Strings in Python support various operations that allow you to manipulate and work with them. Here
are some basic operations and functions related to strings in Python:
1. Concatenation:
str1 = "Hello"
str2 = "World"
2. Repetition:
original_str = "abc"
3. Indexing:
Access individual characters in a string using indexing. Python uses 0-based indexing.
my_str = "Python"
4. Slicing:
my_str = "Python"
5. Length:
my_str = "Python"
length = len(my_str) # 6
6. Conversion:
index = my_str.find("is") # 7
8. Strip:
Split a string into a list of substrings and join a list of strings into one.
my_str = "apple,orange,banana"
10. Formatting:
name = "Alice"
age = 30
11. Comparison:
str1 = "apple"
str2 = "orange"
Lists
In Python, a list is a versatile and mutable data structure that allows you to store a collection of
items. Lists are defined by enclosing elements in square brackets [ ] and separating them with
commas. Here are some key features and operations related to lists in Python:
Creating Lists:
empty_list = []
Accessing Elements:
Slicing:
Modifying Lists:
# Modifying an element
List Operations:
# Concatenating lists
# Replicating lists
index = fruits.index("kiwi") # 3
count = fruits.count("cherry") # 1
Sorting Lists:
List Comprehension:
Lists are widely used in Python due to their flexibility and ease of use. They can store different data
types, and their dynamic nature makes them suitable for a variety of tasks.
Tuples
In Python, a tuple is an ordered, immutable sequence of elements. Tuples are similar to lists, but the
key difference is that once a tuple is created, its elements cannot be modified or changed. Tuples are
defined by enclosing elements in parentheses () and separating them with commas. Here are some
key features and operations related to tuples in Python:
Creating Tuples:
empty_tuple = ()
Accessing Elements:
Slicing:
Immutability:
fruits[0] = "orange" # TypeError: 'tuple' object does not support item assignment
Tuple Operations:
# Concatenating tuples
index = fruits.index("banana") # 1
count = fruits.count("cherry") # 1
Tuple Unpacking:
a, b, c = fruits
Benefits of Tuples:
Immutability: Once a tuple is created, its elements cannot be modified, making it suitable for
situations where data should remain constant.
Performance: Tuples are generally faster than lists, especially for read-only operations.
Hashability: Tuples are hashable and can be used as keys in dictionaries, unlike lists.
Tuples are often used when the data should remain constant throughout the program, such as
representing coordinates, RGB color values, or records from a database.
Dictionaries
In Python, a dictionary is a mutable, unordered collection of key-value pairs. Each key in a dictionary
must be unique, and the associated value can be of any data type. Dictionaries are defined by
enclosing key-value pairs in curly braces {} and separating them with commas. Here are key features
and operations related to dictionaries in Python:
Creating Dictionaries:
empty_dict = {}
banana_count = fruits_dict["banana"] # 3
# Modifying a value
fruits_dict["cherry"] = 7
fruits_dict["orange"] = 4
removed_value = fruits_dict.pop("banana")
Dictionary Operations:
keys_list = list(fruits_dict.keys())
values_list = list(fruits_dict.values())
Dictionary Comprehension:
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Nested Dictionaries:
Benefits of Dictionaries:
Key-Value Structure: Dictionaries provide an intuitive key-value structure, making it easy to represent
relationships between entities.
Dynamic Size: Dictionaries can dynamically grow or shrink as items are added or removed.
Fast Access: Accessing a value by key in a dictionary is efficient, even for large datasets.
Versatility: Dictionaries are versatile and can represent a wide range of data structures, including
sets, graphs, and more.
Dictionaries are commonly used for tasks involving mappings, configurations, and data organization
where key-based access is crucial.
Unit 4 - Functions and modules
Functions
In Python, a function is a reusable block of code that performs a specific task or set of tasks.
Functions help modularize code, making it more readable, maintainable, and reusable. Here are key
features and concepts related to functions in Python:
Defining a Function:
You define a function using the def keyword, followed by the function name, parameters in
parentheses, and a colon. The function body is indented.
def greet(name):
print(f"Hello, {name}!")
Calling a Function:
To execute the code inside a function, you call the function by using its name followed by
parentheses and passing any required arguments.
greet("Alice")
Function Parameters:
Functions can take parameters, which are values passed to the function when it is called. Parameters
are specified in the function definition.
return a + b
Return Statement:
Functions can return a value using the return statement. If no return statement is present, the
function returns None by default.
return a + b
Default Parameters:
You can provide default values for function parameters. If a value is not passed for a parameter, the
default value is used.
Keyword Arguments:
When calling a function, you can use keyword arguments to specify the values for parameters by
name.
greet(name="Bob", greeting="Hi")
Functions can accept a variable number of arguments using *args for positional arguments and
**kwargs for keyword arguments.
Lambda functions are small, anonymous functions defined using the lambda keyword.
multiply = lambda x, y: x * y
Scope of Variables:
Variables defined inside a function have local scope, while variables defined outside functions have
global scope.
global_var = 10
def my_function():
local_var = 5
my_function()
Docstrings:
Docstrings are used to provide documentation for functions. They are placed in triple-quotes
immediately following the function definition.
def my_function(parameter):
"""
"""
Recursion:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
Functions are a fundamental concept in Python programming, allowing developers to create reusable
and organized code. They play a crucial role in building modular and maintainable software.
Local Variables:
def my_function():
local_var = 10
print(local_var)
my_function() # Prints 10
# print(local_var) # This would result in an error because local_var is not defined in this scope.
Global Variables:
global_var = 20
def another_function():
print(global_var)
another_function() # Prints 20
def modify_global():
global global_var
global_var = 30
modify_global()
print(global_var) # Prints 30
When a variable is referenced within a function, Python searches for that variable in the following
order:
Enclosing Scope: In the local scopes of any enclosing functions (for nested functions).
Global Scope: Outside any function, at the top level of the module.
If a variable is not found in the local scope, Python looks in the enclosing and global scopes.
However, if a variable is modified (assigned a new value) inside a function without using the global
keyword, Python assumes it's a local variable.
Here's an example illustrating the difference between local and global variables:
global_var = 50
def my_function():
local_var = 25
my_function()
print("Outside function - Global Variable:", global_var)
In the example above, local_var is a local variable and is accessible only inside the my_function
function. global_var is a global variable and is accessible both inside and outside the function.
math Module:
The math module provides mathematical functions for basic arithmetic operations, advanced
mathematical operations, and constants.
import math
# Constants
random Module:
The random module provides functions for generating pseudo-random numbers and performing
random selections.
import random
# Random selections
my_list = [1, 2, 3, 4, 5]
These are just a few examples of the functions provided by the math and random modules. Both
modules offer a variety of functions to perform different mathematical and statistical operations, and
they are commonly used in various applications, including scientific computing, simulations, and
games.
Unit 5 - Classes & Objects
classes and objects
In Python, a class is a blueprint for creating objects, and an object is an instance of a class. Classes
provide a way to structure and model data, and they encapsulate data and behavior into a single
unit. Here's an overview of classes and objects in Python:
Defining a Class:
You define a class using the class keyword, followed by the class name and a colon. The class body
contains attributes (variables) and methods (functions).
class Dog:
# Class attribute
# Constructor method
# Instance attributes
self.name = name
self.age = age
# Instance method
def bark(self):
To create an object (instance) of a class, you call the class as if it were a function, passing any
required arguments to the constructor (__init__ method).
dog1 = Dog("Buddy", 3)
dog2 = Dog("Max", 5)
# Accessing attributes
print(dog1.name) # Buddy
print(dog2.age) #5
# Calling methods
dog1.age = 4
print(dog1.age) #4
Methods:
Instance Methods: Operate on an instance of the class and can access and modify instance
attributes.
Class Methods: Operate on the class itself and can access and modify class attributes.
Static Methods: Don't depend on instance or class attributes and are defined with the
@staticmethod decorator.
class Circle:
pi = 3.14
self.radius = radius
def area(self):
@classmethod
cls.pi = new_pi
@staticmethod
def circumference(radius):
# Using methods
circle1 = Circle(5)
print(circle1.area()) # 78.5
circle1.set_pi(3.14159)
print(circle1.circumference(5)) # 31.4159
Inheritance:
Inheritance allows a class to inherit attributes and methods from another class. The new class is
called the derived class or subclass, and the class it inherits from is the base class or superclass.
class Car:
self.make = make
self.model = model
def display_info(self):
print(f"{self.make} {self.model}")
class ElectricCar(Car):
super().__init__(make, model)
self.battery_capacity = battery_capacity
def display_info(self):
super().display_info()
Classes and objects are fundamental concepts in object-oriented programming (OOP), and they
provide a way to structure and organize code in a modular and reusable manner.
In Python, methods are functions that are associated with an object. When defining methods within
a class, there are two common types of methods: instance methods and class methods. Additionally,
the self argument is a special parameter used in instance methods to reference the instance of the
class. Let's explore class methods and the self argument:
Class Methods:
A class method is a method that is bound to the class and not the instance of the class. It is defined
using the @classmethod decorator, and it takes the class itself as its first parameter, commonly
named cls (though you can use any valid variable name).
class MyClass:
self.instance_variable = instance_variable
@classmethod
def class_method(cls):
print(cls.class_variable)
In Python, the self argument is a convention used in instance methods to represent the instance of
the class. It is the first parameter in every instance method, though you can use any valid variable
name (e.g., this), but it is highly recommended to use self for clarity.
class MyClass:
self.instance_variable = instance_variable
def instance_method(self):
In the example above, instance_method can access and modify both class-level and instance-level
attributes because it has access to the instance through the self parameter.
It's important to note that when calling an instance method, you don't explicitly pass the self
argument; Python handles it implicitly. For example, obj.instance_method() is equivalent to
MyClass.instance_method(obj).
Understanding the distinction between class methods and instance methods, as well as the use of
the self argument, is crucial for working effectively with object-oriented programming in Python.
Class Methods:
A class method is a method that is bound to the class and not the instance of the class. It is defined
using the @classmethod decorator, and it takes the class itself as its first parameter, commonly
named cls (though you can use any valid variable name).
class MyClass:
self.instance_variable = instance_variable
@classmethod
def class_method(cls):
print(cls.class_variable)
In the example above, class_method can access and modify class-level attributes but cannot access
instance-level attributes because it does not have access to the instance itself.
In Python, the self argument is a convention used in instance methods to represent the instance of
the class. It is the first parameter in every instance method, though you can use any valid variable
name (e.g., this), but it is highly recommended to use self for clarity.
class MyClass:
self.instance_variable = instance_variable
def instance_method(self):
print(f"Instance variable: {self.instance_variable}")
In the example above, instance_method can access and modify both class-level and instance-level
attributes because it has access to the instance through the self parameter.
It's important to note that when calling an instance method, you don't explicitly pass the self
argument; Python handles it implicitly. For example, obj.instance_method() is equivalent to
MyClass.instance_method(obj).
Class Variables:
Shared by all instances of a class: Class variables are defined within a class but outside any instance
method.
Same value for all instances: They have the same value for every instance of the class.
Defined outside methods using the class name or within class methods using cls:
class MyClass:
self.instance_variable = instance_variable
Instance Variables:
Unique to each instance: Instance variables are defined within the __init__ method and are specific
to each instance of the class.
Different values for different instances: Each instance can have a different value for an instance
variable.
class MyClass:
self.instance_variable = instance_variable
Class Variables: Use class variables when you want a value to be shared among all instances of a
class. For example, a constant value that is the same for all instances.
Instance Variables: Use instance variables when each instance needs to have its own unique data.
It's essential to understand the distinction between class variables and instance variables to
effectively use classes in Python. Class variables are shared among all instances, while instance
variables are specific to each instance.
Accessible from outside the class: Public data members can be accessed and modified from outside
the class.
No special syntax required: They are accessed using the dot notation.
class MyClass:
obj = MyClass()
Not accessible from outside the class: Private data members are intended to be used only within the
class itself.
Prefix with double underscores (__): To make a variable private, prefix its name with double
underscores.
class MyClass:
def get_private_variable(self):
return self.__private_variable
self.__private_variable = new_value
obj = MyClass()
# Accessing and modifying a private variable using getter and setter methods
Note:
While Python does not enforce strict access control, the use of the double underscore prefix is a
convention indicating that a variable is intended to be private.
Private variables can still be accessed with a different name, as the double underscore is mainly a
form of name mangling to make it more difficult but not impossible to access the variable.
In summary, public data members are accessible from outside the class, while private data members
are intended for internal use within the class. The use of private variables is based on convention and
serves as a way to communicate that a variable is not intended for external manipulation.
private methods
In Python, just like data members, methods of a class can also be classified into public and private
based on their visibility and accessibility. Private methods are intended to be used only within the
class and are not accessible from outside the class. To make a method private, you can prefix its
name with double underscores (__). Here's an example:
class MyClass:
self.__private_variable = value
def __private_method(self):
def public_method(self):
self.__private_method()
__private_method is a private method that can only be accessed within the class.
public_method is a public method that can be accessed from outside the class. Inside this method,
the private method __private_method is called.
Python doesn't provide strict access control, and the double underscore is a form of name mangling,
making it more difficult but not impossible to access private methods.
The use of private methods is more about convention and communicating that a method is intended
for internal use.
It's important to carefully consider the design and purpose of your class when deciding which
methods should be public or private to ensure encapsulation and maintainability.
__doc__:
The docstring is a string literal that occurs as the first statement in a module, function, class, or
method definition.
class MyClass:
pass
__name__:
class MyClass:
pass
__module__:
pass
__bases__:
Returns a tuple containing the base classes, in the order they are declared.
python
Copy code
class Parent:
pass
class Child(Parent):
pass
__dict__:
python
Copy code
class MyClass:
These built-in class attributes can be useful for introspection and understanding the structure of
classes at runtime. They provide information about the class, its attributes, and its relationships with
other classes.
static methods
In Python, a static method is a method that belongs to a class rather than an instance of the class.
Unlike instance methods, static methods don't have access to the instance or its attributes. They are
defined using the @staticmethod decorator and take neither self (for instance) nor cls (for class) as
their first parameter. Instead, they behave like regular functions, and you can call them on the class
itself or an instance.
python
Copy code
class MathOperations:
@staticmethod
return x + y
@staticmethod
return x * y
result_add = MathOperations.add(3, 5)
result_multiply = MathOperations.multiply(4, 6)
print(result_add) # Output: 8
print(result_multiply) # Output: 24
In this example:
add and multiply are static methods of the MathOperations class.
They are called on the class itself, not on an instance of the class.
Static methods are useful when a method doesn't depend on the state of an instance or the class.
They provide a way to organize utility functions related to the class but don't rely on instance-specific
or class-specific attributes.
While static methods don't have direct access to instance or class attributes, they can still be called
on instances, and you can use them for operations that are logically related to the class but don't
involve modifying the instance or class state.
LAB
import math
def find_square_root(number):
if number >= 0:
square_root = math.sqrt(number)
return square_root
else:
result = find_square_root(user_input)
output
2. Python Program to Swap Two Variable
temp = var1
var1 = var2
var2 = temp
import random
Output
4. Python Program to Check if a Number is Odd or Even
def check_odd_or_even(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
result = check_odd_or_even(user_input)
Output
5. Python Program to Find the Largest Among Three Numbers
# Using the max function to find the maximum among the three numbers
return largest_number
print(f"The largest number among {num1}, {num2}, and {num3} is: {result}")
output
6. Python Program to Check Prime Number
def is_prime_number(number):
if number <= 1:
return False
elif number == 2:
return True
elif number % 2 == 0:
return False
else:
if number % i == 0:
return False
return True
result = is_prime_number(user_input)
if result:
else:
result = number * i
display_multiplication_table(user_input, table_size)
Output
8. Python Program to Print the Fibonacci sequence
def generate_fibonacci_sequence(terms):
fibonacci_sequence = [0, 1]
fibonacci_sequence.append(next_term)
return fibonacci_sequence
num_terms = int(input("Enter the number of terms for the Fibonacci sequence: "))
fibonacci_result = generate_fibonacci_sequence(num_terms)
Output
9. Python Program to Find the Sum of Natural Numbers
def sum_of_natural_numbers(n):
if n < 0:
else:
sum_result = (n * (n + 1)) // 2
return sum_result
result = sum_of_natural_numbers(user_input)
if isinstance(result, int):
else:
print(result)
Output
10. Python Program to Find Factorial of Number Using Recursion
def factorial_recursive(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial_recursive(n - 1)
if user_input < 0:
else:
result = factorial_recursive(user_input)
Output
11. Python Program to work with string methods
# Sample string
length = len(input_string)
uppercase_string = input_string.upper()
lowercase_string = input_string.lower()
substring_count = input_string.count("o")
substring_index = input_string.find("World")
# 6. Replace a substring
new_string = input_string.replace("Hello", "Hi")
starts_with_hello = input_string.startswith("Hello")
ends_with_programming = input_string.endswith("Programming.")
Output
12. Python Program to create a dictionary and print its content
# Create a dictionary
student_info = {
'age': 20,
'grade': 'A',
print("Dictionary Content:")
print(student_info)
print(f"{key}: {value}")
Output
13. Python Program to create class and objects
class Person:
self.name = name
self.age = age
def display_info(self):
person1.display_info()
person2.display_info()
Output