Graph-1 (1) - 1
Graph-1 (1) - 1
-: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
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:
o Mark it as visited.
2. Exploration:
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.
3. Update Costs:
• For each neighbour of nnn:
o Compute g(neighbour)g(neighbour)g(neighbour).
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).
3. Efficient: Reduces the number of nodes explored by guiding the search using
h(n)h(n)h(n).
Flowchart:
Code:
Flowchart:
Code & Output: