0% found this document useful (0 votes)
11 views36 pages

Graph-1 (1) - 1

The document provides an overview of Python data types, including numeric, sequence, mapping, boolean, set, and binary types, along with their characteristics. It also covers control flow statements such as if, if-else, and loops, as well as function definitions and calling. Additionally, it discusses graph plotting methods using Matplotlib and various searching algorithms, including uninformed and informed searches, along with their algorithms and features.

Uploaded by

sohamaxpauli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views36 pages

Graph-1 (1) - 1

The document provides an overview of Python data types, including numeric, sequence, mapping, boolean, set, and binary types, along with their characteristics. It also covers control flow statements such as if, if-else, and loops, as well as function definitions and calling. Additionally, it discusses graph plotting methods using Matplotlib and various searching algorithms, including uninformed and informed searches, along with their algorithms and features.

Uploaded by

sohamaxpauli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Name:-Soham mukherjee

reG. No: 2301292114


SeC:a

-:PYTHON:-
DATA Types and variable
Python Data types are the classification or
categorization of data items. It represents the kind of
value that tells what operations can be performed on a
particular data. Since everything is an object in Python
programming, Python data types are classes and
variables are instances (objects) of these classes. The
following are the standard or built-in data types in
Python:
• Numeric – int, float, complex
• Sequence Type – string, list, tuple
• Mapping Type – dict
• Boolean – bool
• Set Type – set, frozenset
• Binary Types – bytes, bytearray, memoryview
Performing arithmetic Operations on int type:
To check the result of arithmetic operations we can
simply print the output using print(res). To check the
type of the object, we can use print(type(res)).
LiSt, tupLe ,DiCtioNary aND Set
1. List
A list is an ordered, mutable (modifiable), and iterable
collection that can contain elements of different data types.
Features:
• Ordered: Elements maintain their insertion order.
• Mutable: You can modify elements (add, remove, or
change).
• Allow duplicates: Lists can have duplicate elements.
Syntax:-

2. Tuple
A tuple is an ordered, immutable (cannot be changed), and iterable
collection that can also contain elements of different data types.
Features:
• Ordered: Maintains insertion order.
• Immutable: Once created, elements cannot be changed.
• Allow duplicates: Tuples can have duplicate elements.
Syntax:

3. Set
A set is an unordered, mutable collection of unique elements. Sets
are useful for removing duplicates and performing set operations.
Features:
• Unordered: No guarantee of element order.
• Mutable: Can add or remove elements.
• Unique elements: Does not allow duplicates.
Syntax:
4. Dictionary
A dictionary is an unordered, mutable collection of key-value pairs.
Keys are unique and immutable, while values can be of any data type.
Features:
• Unordered: No guarantee of order (as of Python 3.7+,
maintains insertion order).
• Mutable: Can add, remove, or modify key-value pairs.
• Keys are unique: Duplicate keys overwrite previous values.
Syntax:

CoNDitioN StatemeNtS
1. if Statement
The if statement is used to execute a block of code only if a specified condition
evaluates to True.
Syntax:
2. if-else Statement
The if-else statement allows you to define an alternate block of code
that runs if the condition is False.
Syntax:
3. if-elif-else Statement
The if-elif-else statement is used for multiple conditions. The first condition
that evaluates to True will execute its block of code. If none of the conditions
are true, the else block is executed.
Syntax:
LoopiNG StatemeNt
1. for Loop
The for loop is used to iterate over a sequence (such as a list, tuple, string, or range).
Syntax:

2. while Loop
The while loop is used to repeatedly execute a block of code as long as a
condition is True.
Syntax:
3. Nested Loops
You can use a loop inside another loop, which is called a nested loop.

Syntax:

FuNCtioN
1. Defining a Function
Functions in Python are defined using the def keyword.
Syntax:

2. Calling a Function
Once defined, a function can be called by using its name followed by
parentheses and passing arguments if required.
Example:

3. Recursive Functions
A recursive function is a function that calls itself to solve a smaller
subproblem of the same type.
Example:

Graph
How to plot a graph in Python?
There are various ways to do this in Python. here we are discussing some
generally used methods for plotting matpotlib in Python. those are the
following.
• Plotting a Line
• Plotting Two or More Lines on the Same Plot
• Customization of Plots
• Plotting Matplotlib Bar Chart
• Plotting Matplotlib Histogram
• Plotting Matplotlib scatter plot
• Plotting Matplotlib Pie-chart
• Plotting Curves of Given Equation

Plotting a line

Customization of Plots
Plotting Matplotlib using barchart

Plotting Matplotlib Using Histogram


Plotting Matplotlib Using Scatter Plot

Plotting Matplotlib Using Pie-chart


Searching Algorithms
Uninformed Searches:
1. Breadth First Search (BFS)
2. Depth First Search (DFS)

1. Breadth First Search (BFS) :


Algorithm:
1. Initialize:
o Create a queue and add the starting node to it.

o Mark the starting node as visited to prevent reprocessing.

2. Traversal:
o While the queue is not empty:
1. Dequeue a node from the queue.
2. Process the node (e.g., print its value or record it).
3. Enqueue all unvisited neighbours of the node and
mark them as visited.

Flowchart:
Code:

2. Depth First Search (DFS):


Algorithm:
1. Initialization:

o Start from a given node.

o Mark it as visited.
2. Exploration:

o Recursively visit all unvisited neighbours of the current


node.

o Backtrack when all neighbours are visited.

Flowchart:
Code:

Informed Searches:
1. Best First Search
2. A-star Search (A*)
1. Best First Search:
Algorithm:
1. Initialize:
o Start with the initial node and add it to the priority
queue.
2. Loop:
o Remove the node with the smallest h(n)h(n)h(n)
from the priority queue.
o If the node is the goal, terminate and return the
solution.
o Otherwise, expand the node and add its neighbours
to the priority queue, evaluating each neighbour
using the heuristic.
3. Repeat:
o Continue until the goal is found or the priority
queue is empty (failure).

Flowchart:
Code:
2. A-star Search (A*):
Algorithm:
1. Initialize:
• Place the start node in a priority queue (commonly called the
open list). Initialize its g(n)=0g(n) = 0g(n)=0.

2. Expand Nodes:
• At each step, select the node nnn from the priority queue with
the lowest f(n)f(n)f(n) value.

• If nnn is the goal, terminate and reconstruct the path.

• Otherwise, move nnn to the closed list (visited nodes) and


evaluate its neighbours.

3. Update Costs:
• For each neighbour of nnn:

o Compute g(neighbour)g(neighbour)g(neighbour).

o If the neighbour is not in the open list or has a lower ggg-


cost, update its g(n)g(n)g(n) and calculate f(n)f(n)f(n).

o Add the neighbour to the priority queue if not already


present.

4. Repeat:
• Continue until the goal is found or the priority queue is empty
(failure).
Features:
1. Optimal: Finds the shortest path if h(n)h(n)h(n) is admissible (never overestimates
the actual cost).

2. Complete: Always finds a solution if one exists.

3. Efficient: Reduces the number of nodes explored by guiding the search using
h(n)h(n)h(n).

Flowchart:
Code:

CSP(Constraint Satisfaction Problem):


Algorithm:
Input: Graph (adjacency matrix), number of colors m, number of vertices V.
Initialize:
• colors list with -1 for all vertices (no color assigned).
Recursive Function:
• Define graph_coloring(graph, m, colors, vertex):
1. Base Case:
▪ If all vertices are assigned a color (vertex == V), return True
(solution found).
2. Color Assignment:
▪ Loop through all colors (0 to m-1) for the current vertex:
▪ Check if the color is safe using is_safe(vertex, color,
colors, graph):
▪ Ensure no adjacent vertex has the same color.
▪ If safe:
▪ Assign the color to the vertex.
▪ Recur for the next vertex (vertex + 1).
▪ If the recursion fails, backtrack and remove the
assigned color (colors[vertex] = -1).
Start the Algorithm:
• Call graph_coloring(graph, m, colors, 0) to solve.
Output:
• If a solution is found, print the color assigned to each vertex.
• Otherwise, print "No solution exists."

Flowchart:
Code & Output:

MEA (Mean-Ends Analysis):


Algorithm:
1. Identify the Goal: Define the desired outcome.
2. Analyze Current State: Assess your current situation.
3. Identify Differences: Note the gaps between the current state
and the goal.
4. Generate Subgoals: Break the main goal into smaller,
manageable subgoals.
5. Select Means: Identify actions that can help achieve each
subgoal.
6. Evaluate Options: Assess the feasibility of the identified actions
and choose the best one.
7. Implement Action: Execute the chosen action to progress
toward the subgoal.
8. Reassess: Check if the subgoal is achieved and if the overall goal
is closer.
9. Iterate: Repeat the process, adjusting as necessary until the
main goal is reached.
10.Terminate: Conclude when the goal is achieved or deemed
unattainable.
Flowchart:

Code & Output:


Code:
Output:

You might also like