Trending December 2023 # Python Iterables, Iterators & Generators: A Complete Guide # Suggested January 2024 # Top 18 Popular

You are reading the article Python Iterables, Iterators & Generators: A Complete Guide updated in December 2023 on the website Bellydancehcm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Python Iterables, Iterators & Generators: A Complete Guide

In Python, an iterable object is any object you can loop through using a for loop or while loop.

Common examples of iterables are:

Lists

Strings

Dictionaries

Tuples

Sets

For example, you can loop through a list of numbers:

numbers = [1, 2, 3] for number in numbers: print(number)

Output:

1 2 3

You can use a similar approach to loop through the characters of a string:

word = "Testing" for character in word: print(character)

Output:

T e s t i n g

This comprehensive guide teaches you everything you need to know about iterables, iterators, and generators in Python. After reading this guide, you understand:

What the yield keyword really means

What makes an object iterable

What makes a function iterator

What is a generator

How a for loop works behind the scenes

And much more.

Introduction: Iterables and Iterators in Python

An iterable object means it implements the __iter__ method under the hood. This method returns an iterator that can be used to loop through the iterable.

Let’s take a look at an example of a traditional non-iterable object. Here is a simple Python class called Course:

class Course: participants = ["Alice", "Bob", "Charlie"]

Let’s create a Course object of that class:

course = Course()

Now, let’s try to loop through the Course object and try to print each participant on the course:

for student in course: print(student)

Output:

Traceback (most recent call last): for student in course: TypeError: 'Course' object is not iterable

Of course, this piece of code fails. The error says it all—The Course is not iterable.

Trying to loop through this course object is meaningless. Python has no idea what you’re trying to do.

To make the above code work, you need to make the Course class iterable. To convert the Course class into an iterable, implement the two special methods:

__iter__.

__next__.

Here is the updated version of the Course class:

class Course: participants = ["Alice", "Bob", "Charlie"] def __iter__(self): return iter(self.participants) def __next__(self): while True: try: value = next(self) except StopIteration: break return value

Now you can loop the Course objects with the previously failing for loop syntax:

course = Course() for student in course: print(student)

Output:

Alice Bob Charlie

It works!

At this point, you probably have no idea what the above code does. No worries! That’s what you will be focusing on for the rest of this guide. The above example is just an introduction to what you can do with iterables and iterators in Python.

To understand how this code works, you need to better understand iterators and iterables. Furthermore, you need to learn what the special methods __iter__ and __next__ actually do.

Iterables and Iterators in Python

So, an iterable object in Python is something you can loop through using a for (or while) loop.

But what makes an object iterable in the first place?

To qualify as an iterable, the object has to implement the __iter__() method under the hood.

Let’s think about some common iterable types in Python. For example, a list is clearly iterable as you can loop through it using a for loop syntax, right?

Now, let’s see what special methods a list object implements behind the scenes. To do this, call the dir() function on a list:

numbers = [1, 2, 3, 4, 5] print(dir(numbers))

Here is the output:

This output is a list of all the methods that list objects implement under the hood. You can see the list has the __iter__() method. This verifies that a Python list is indeed iterable.

This __iter__() method is important because it’s what makes looping through a list possible.

For a for loop to work, it calls the __iter__() method of a list. This method returns an iterator. The loop then uses this iterator object to step through all the values.

But what on earth is an iterator then?

An iterator is an object with a state. An iterator object remembers where it is during an iteration. Iterators also know how to get the next value in the collection. They do this by using the __next__() method, which by the way is a method every iterator needs to have.

Let’s continue with the numbers list example and grab the iterator of the list:

numbers = [1, 2, 3, 4, 5] iter_numbers = iter(numbers)

(By the way, calling the iter(numbers) is the same as calling numbers.__iter__() )

Now, let’s call the dir() function on the iterator object to see what methods it has:

print(dir(iter_numbers))

Result:

There’s the __next__() method that I talked about. This is what characterizes an iterator. Without this method, the iterator would not work.

Woah… So much new information!

Let’s recap what you have learned thus far.

A Python list is iterable. In other words, you can call a for loop on it. To be iterable, a list implements the __iter__() method under the hood.

This __iter__() method returns an iterator object. The for loop uses the iterator object to actually step through the list values.

To qualify as an iterator, the iterator must implement the __next__() method for obtaining the next value in the list.

Now, let’s continue exploring the numbers list object. Let’s call the __next__() method on the numbers iterator a bunch of times to see what happens:

>>> iter_numbers = iter(numbers) # get the iterator of the list >>> next(iter_numbers) # get the next value of the list using the iterator 1 >>> next(iter_numbers) 2 >>> next(iter_numbers) 3 >>> next(iter_numbers) 4 >>> next(iter_numbers) 5

(By the way, calling the next(numbers) is the same as calling numbers.__next__() )

Calling next(iter_numbers) always returns the next number in the numbers list. But how on earth is this possible?

This is possible because the iterator object has an internal state. It remembers where it left off when __next__() was called last time. So when the __next__() method is called again, the iterator object knows what value comes next and delivers it to the caller.

As you can see from the above example, the last value you received was 5. This means you have reached the end of the numbers list.

To see what happens if you continue calling the next() function, let’s call it once more:

Traceback (most recent call last): StopIteration >>>

Because there are no more values to access on the list, a StopIteration exception is thrown. At this point, the iterator is exhausted.

The cool part of the above example is you just did what a for loop does under the hood!

When you call a for loop on an iterable, such as a list:

It calls the __iter__() method on a list to retrieve an iterator object.

Then it calls the __next__() method until there are no values left.

When there are no values left, a StopIteration exception is thrown. The for loop handles the exception for you. Thus you never see it.

Let’s simulate the behavior of a for loop again. This time, let’s make it less manual and use a while loop instead:

numbers = [1, 2, 3, 4, 5] # Get the iterator from numbers list iter_numbers = iter(numbers) # Start retrieving the next values indefinitely while True: try: # Try to get the next value from the iterator and print it number = next(iter_numbers) print(number) # If the iterator has no more values, escape the loop except StopIteration: break

Output:

1 2 3 4 5

The above code construct works exactly the same way as a for…in loop operates on a list.

Awesome! Now you understand what are iterables and iterators in Python. Also, you now know how a for loop truly works when iterating over a list.

Next, let’s take a look at how you can implement custom iterables and iterators.

How To Create Iterators and Iterables

There is nothing special about Python’s native iterables, such as a list or tuple. You can convert any custom classes to iterables too!

To do this, you need to implement the __iter__() and __next__() methods in your class.

Example

I’m sure you’re familiar with the built-in range() function in Python. You can use it like this:

for i in range(4): print(i)

Output:

0 1 2 3

To demonstrate iterables and iterators, let’s create a custom implementation of range(). Here is a class RangeValues that mimics the behavior of the range() function:

class RangeValues: def __init__(self, start_value, end_value): self.current_value = start_value self.end_value = end_value def __iter__(self): return self def __next__(self): raise StopIteration value = self.current_value self.current_value += 1 return value

Now, let’s go through this implementation line by line to understand what’s happening.

Lines 2–4

The __init__() method makes it possible to initialize a RangeValues object with start and end values. For example: RangeValues(0, 10).

Lines 6–7

The __iter__() method makes the class iterable. In other words, it is possible to call for i in RangeValues(0,10). This method returns an iterator. In this case, it returns the class itself, because the class is an iterator as it implements the __next__() method.

Lines 9–14

These lines specify how the iterator behaves when someone calls a for loop on the RangeValues object.

The __next__() method is responsible for going through the values from start to end. It raises a StopIteration exception when it reaches the end of the range.

If the iterator hasn’t reached the end yet, it continues returning the current value (and increments it for the next round).

Now that you understand how the code works, let’s finally test the RangeValues class:

for i in RangeValues(1,5): print(i)

Output:

1 2 3 4

As you can see, this function now works just like the range() function in Python.

Building an example iterable/iterator like this is a great way to get a better understanding of how the iterables work in Python.

Make sure to tweak the code and see what happens. Also, feel free to come up with your own iterable concepts and put them to test. If you only read this article without experimenting with the code, chances are you won’t understand the concepts well enough.

Now, let’s move on to generators that offer a more readable way to write iterators.

Generators—Readable Iterators in Python

If you take a look at the above example of RangeValues class, you see it’s daunting to read.

Luckily, Python provides you with generators to remedy this problem.

Because a generator is also an iterator, it doesn’t return a single value. Instead, it yields (delivers) values one at a time. Besides, the generator keeps track of the state of the iteration to know what value to deliver next.

For example, let’s turn the RangeValues class from the earlier example into a generator:

def range_values(start, end): current = start while current < end: yield current current += 1

Let’s test the function:

for i in range_values(0,5): print(i)

Output:

0 1 2 3 4

The range_values works exactly like the RangeValues class but the implementation is way cleaner.

First of all, you don’t need to specify a class. Instead, you can use a generator function like the above. Then, you don’t need to implement the __next__() and __iter__() methods.

How Does ‘Yield’ Work?

In the previous example, you turned iterable into a generator to make it more readable.

But in the above example, you saw a new keyword yield. If you have never seen it before, there is no way for you to tell how it works.

When you write a generator function, you don’t return values. This is because, as you might recall, a generator only knows the current value and how to get the next value. Thus, a generator doesn’t store values in memory. This is what makes generators memory efficient.

To create a generator, you cannot return values. Instead, you need to yield them.

When Python encounters the yield keyword, it delivers a value and pauses the execution until the next call.

For example, if you have a huge text file with a lot of words, you cannot store the words in a Python list. This is because there is not enough memory for you to do that. To iterate over the words in the file, you cannot store them in memory. This is where a generator comes in handy. A generator picks the first word, yields it to you, and moves to the next one. It does this until there are no words in the list. This way, you don’t need to store the words in your Python program to go through them.

Make sure to read Yield vs Return to get a better understanding.

Infinite Stream of Elements with Generators

Iterators and generators only care about the current value and how to get the next one. It’s thus possible to create an infinite stream of values because you don’t need to store them anywhere.

Example

Let’s create an infinite iterator that produces all the numbers after a starting value. Let’s use a generator function to keep it readable:

def infinite_values(start): current = start while True: yield current current += 1

(If you want to see how this could be done with a class, here it is.)

This iterator produces values from start to infinity.

Let’s run it. (Warning: An infinite loop):

infinite_nums = infinite_values(0) for num in infinite_nums: print(num)

Output:

0 1 2 3 4 5 . . .

Syntactically it looks as if the infinite_nums really was an infinite list of numbers after 0.

In reality, it’s nothing but an iterator that stores the current value and knows how to get the next one.

Conclusion

Today you learned what iterable means in Python.

An iterable is something that can be looped over in Python.

On a low level, an iterable is an object that implements the __iter__() method which returns an iterator.

An iterator is an object with a state. It remembers where it’s at during an iteration. To qualify as an iterator, an object must implement the __next__() method for obtaining the next value in the iteration process.

Thanks for reading. Happy coding!

Further Reading

Python Tips and Tricks

You're reading Python Iterables, Iterators & Generators: A Complete Guide

Python Not Equal Operator (!=)

What is Python Not equal to Operator?

Python is identified as a programming language that is very dynamic, and it is generally regarded as a strongly typed language. This statement can be explained by understanding the significance of not equal operator. In not equal operator, if the values of the two operands on either side of the operator are not equal, then the operator provides true value, else it provides false.

In not equal operator, if two variables are of different types but hold the same values in themselves, then the not equal operator returns a true. Not many programming languages can classify it as true if the variable type is of a different type, which makes python a very dynamic language. In python, not equal operators can be classified as one of the comparison operators.

Types of Not equal to operators with Syntax in Python

The syntax of both types is shown below: –

X!=Y

There are two types of not equal operators in python:-

!=

The first type, != is used in python versions 2 and 3.

Example of Python Not Equal Operator

Let us consider two scenarios to illustrate not equal to in python. Following is the example of not equal operator for same data type but different values:-

A = 44 B = 284 C = 284 print(B!=A) print(B!=C)

Output:

True False

Following is the example of not equal in python for different data type but same values

C = 12222 X = 12222.0 Y = "12222" print(C!=X) print(X!=Y) print(C!=Y)

Output:

False True True

How to use Not Equal Operator with IF Statement

Let us take a basic example of using if statement and does not equal to operator as shown below: –

X = 5 Y = 5 if ( X != Y ): print("X is not equal to Y") else: print("X is equal to Y")

Output:

X is equal to Y

Here, not equal to != is utilized along with the if statement.

How to use equal to (==) operator with while loop

In python, while-loop iterates block of code as long as a condition is true or false. Let us take a case of printing odd numbers using while loop and equal to operator as shown below: –

m = 300 while m <= 305: m = m + 1 if m%2 == 0: continue print (m)

Output:

301 303 305

Here, equal to == is utilized along with the if statement.

Example: Finding even numbers by using not equal operator

In python, while loop can also be used with not equal to operator. Let us take a case of printing even numbers using while loop and not equal to operator as shown below: –

m = 300 while m <= 305: m = m + 1 if m%2 != 0: continue print (m)

Output:

302 304 306

Here, not equal to != is utilized along with the if statement.

How to use Python not equal Operator with custom object

Custom objects enable the user or a developer to create their custom implementations. This enables the developers to change the actual output than what is usually anticipated.

Let us take an example of a custom object that utilizes not equal to operator as shown below: –

Example:

class G9Example: s_n='' def __init__(self, name): self.s_n = name def __ne__(self, x): if type(x) != type(self): return True # return True for different values if self.s_n != x.s_n: return True else: return False G1 = G9Example("Guru99") G2 = G9Example("HipHop99") G3 = G9Example("Guru99") print(G1 != G2) print(G2 != G3) print(G1 != G3)

Output:

True True False Comparison operators in Python

Following table describes the list of comparison operators in python: –

Operator Meaning Example

!= Not equal to-gives true if operands do not have the same values A!=B

== Equal to-Gives true if operands have the same values A==B

Greater than or equal to- gives true as value if the first operand is greater than or equal with the second operand

<= Less than or equal to- gives true as value if the first operand is Less than or equal with the second operand A<=B

Greater than – gives true as value if the first operand is greater than the second operand

< Less than – gives true as value if the first operand is Less than the second operand A<B

Useful Tips on Usage of Not Equal Operator

Here are some useful tips

The not equal to operator can be used in formatted strings.

This feature is relatively new and is part of python version 3.6.

The developer should ensure that syntax should be != and not ≠ because some fonts or interpreters change syntax from != to ≠.

How To Use Python Xlrd?

Definition of Python xlrd What is Python xlrd?

Furthermore, the user may be required to navigate through several sheets and extract data based on certain criteria.

The attributes can be used to determine rows in the sheet. By default, all rows are padded with same size, but if we want to skip the empty columns at the end, use the ragged rows parameter when calling the open workbook function.

We need to install xlrd package by using the pip command. Without installing the xlrd package we cannot use the same in our code. It is very useful and important in python.

How to Use Python xlrd?

We need to install xlrd package by using the pip command. Xlrd module is not coming by default at the time of installing the python package in our system.

1. In the first step, we are installing the xlrd module by using the pip command. We can install the xlrd module in any operating system on which python is installed. In the below example, we are installing the xlrd module on UNIX systems.

Code:

pip install xlrd  

Output:

2. After installing all the modules we are opening the python shell by using the python3 command.

Code:

python3

Output:

3. After login into the python shell in this step, we are checking xlrd package is installed in our system. To use the xlrd module in our program we need to import the same in our code by using the import keyword. Without importing the xlrd module we cannot use the same in our code.

Code:

import xlrd print (xlrd)

Output:

Code:

import xlrd file_loc = ("test.xlsx") py_xl = xlrd.open_workbook (file_loc) py_sheet = py_xl.sheet_by_index (0) print (py_sheet.cell_value (0, 0))

Output:

How to Install XLRD in Python?

We are installing by using the pip command. In the below example, we are installing it on the windows system as follows. The below steps show how to install the xlrd module on the windows system.

1. In the first step, we are installing the xlrd module by using the pip command. We are installing the xlrd version as xlrd==1.2.0

Code:

pip install xlrd==1.2.0

Output:

2. After installing the modules, we are opening the python shell by using the python command to check xlrd package is installed on our system.

Code:

python

Output:

3. After login into the python shell in this step, we are checking xlrd package is installed in our windows system.

Code:

import xlrd print (xlrd)

Output:

In the below example, we are installing it on the Ubuntu system as follows. The below steps shows how to install the xlrd module on the ubuntu system.

1. In the first step, we are installing the xlrd module by using the pip command. We are installing the xlrd version as 2.0.1.

Code:

pip install xlrd

Output:

2. After installing the xlrd modules we are opening the python shell by using the python3 command as follows.

Code:

python3

Output:

After login into the python shell in this step, we are checking xlrd package is installed in our Ubuntu system.

Code:

import xlrd

Output:

Python xlrd in Excel Files

We can also extract the data from an excel file by using python xlrd modules. The below example shows extracting the number of rows from the excel file as follows. In the below example, we are using the excel file name test.xlsx.

Code:

import xlrd py_file = ("test.xlsx") py_xlrd = xlrd.open_workbook (py_file) py_sheet = py_xlrd.sheet_by_index (0) py_sheet.cell_value (0, 0) print (py_sheet.nrows)

Output:

The below example shows extract of the number of columns from the excel file as follows. In the below example, we are using the excel file name test.xlsx.

Code:

import xlrd py_file = ("test.xlsx") py_xlrd = xlrd.open_workbook (py_file) py_sheet = py_xlrd.sheet_by_index (0) py_sheet.cell_value (0, 0) print (py_sheet.ncols)

Output:

The below example shows extract all column names from the excel file as follows.

Code:

import xlrd py_file = ("test.xlsx") py_xlrd = xlrd.open_workbook (py_file) py_sheet = py_xlrd.sheet_by_index (0) py_sheet.cell_value (0, 0) for i in range (py_sheet.ncols): print (py_sheet.cell_value(0, i))

Output:

The below example shows extract the first column from the excel file as follows.

Code:

import xlrd py_file = ("test.xlsx") py_xlrd = xlrd.open_workbook(py_file) py_sheet = py_xlrd.sheet_by_index(0) py_sheet.cell_value(0, 0) for i in range(py_sheet.nrows): print(py_sheet.cell_value(i, 0))

Output:

Conclusion Recommended Article

This is a guide to Python xlrd. Here we discuss the definition, What is python xlrd, How to use python xlrd, examples along with code implementation and output. You may also have a look at the following articles to learn more –

Python Vs C++: Difference Between Them

Key Difference Between Python and C++

Python code runs through an interpreter while C++ code is pre-compiled

Python supports Garbage Collection whereas C++ does not support Garbage Collection

Python is slower, on the other hand, C++ is faster than Python

In Python, Rapid Prototyping is possible because of the small size of the code while in C++, Rapid Prototyping not possible because of larger code size

Python is easy to learn language whereas C++ has a stiff learning curve as it has lots of predefined syntaxes and structure

What is C++?

C++ is widely used in general-purpose programming languages. The language allows you to encapsulates high and low-level language features. So, it is seen as an intermediate-level language. It also used to develop complex systems where the hardware level coding requires.

What is Python?

Python is a high-level object-oriented programming language. It has built-in data structures, combined with dynamic binding and typing, which makes it an ideal choice for rapid application development. Python also offers support for modules and packages, which allows system modularity and code reuse.

It is one of the fastest programming languages as it requires very few lines of code. Its emphasis is on readability and simplicity, which make it a great choice for beginners.

Stack Overflow Questions c++ VS. Python

Why Python?

Here, are reasons for using Python language:

Very simple syntax compared to Java, C, and C++ languages.

It is used for Machine Learning, Deep Learning, and the general overarching AI field.

Very useful in data analysis and visualization.

Extensive library and handy tools for developers/programmer

Python is cross-compatible

Python has its auto-installed shell

Compared with the code of other languages, python code is easy to write and debug. Therefore, its source code is relatively easy to maintain.

Python is a portable language so that it can run on a wide variety of Operating systems and platforms.

Python comes with many prebuilt libraries, which makes your development task easy.

Python helps you to make complex programming simpler. As it internally deals with memory addresses, garbage collection.

Python provides an interactive shell that helps you to test the things before it’s actual implementation.

Python offers database interfaces to all major commercial DBMS systems.

Supports imperative and functional programming

Python is famous for its use in IoT.

Why C++?

Here, are reasons for using C++

C++ is multi-paradigm means it follows three paradigms Generic, Imperative, and Object-Oriented.

C++ provides performance and memory efficiency.

It provides high-level abstraction.

C++ is compatible with C.

The language allows the reusability of code.

Features of C++

Here, are important features of C++

The program should be simple, object-oriented and easy to understand

Development should be conducted in a robust and secure environment.

Code should follow the specific architecture and must be portable.

Code should be easily “interpreted and dynamic “

Features of Python

Here, are important features of Python

Easy to learn, read, and maintain

It can run on various hardware platforms using the same interface.

You can include low-level modules to the Python interpreter.

Python offers an ideal structure and support for large programs.

Python offers support for automatic garbage collection.

It supports an interactive mode of testing and debugging.

It offers high-level dynamic data types and also supports dynamic type checking.

Python language can be integrated with Java, C, and C++ programming code

Applications of C++

Here, are important applications of C++:

C++ is used to develop all kinds of embedded systems like smartwatches, multimedia systems in automobiles, lot devices, etc.

C++ also allows you to develop the servers and the high-performance microcontroller programs

Game development is the key to C++. That’s why C++ is becoming more popular among game developers.

Applications of Python

Here, are some important Applications of Python

Python is widely used in machine learning

The language allows you to manage a huge amount of data with an easy and cost-effective way.

Data analysts use Python to analyze the data and statistical information.

It is also useful in big data technologies. In fact, most of the significant data functions can be performed using python programming.

Web developers use python language for developing the complex web application; that’s because Python offers the Django framework, which helps you to create the entire sites using Python.

Python vs. C++: Differences Between Python and C++

Here, are the major difference between Python and C++

Python C++

Supports Garbage Collection Does not support Garbage Collection

Python programs are easier to write Not easy in contrast to Python because of its complex syntax.

Run through interpreter C++ is pre-compiled

Rapid Prototyping is possible because of the small size of the code Rapid Prototyping not possible because of larger code size

Python is difficult to be installed on a windows box Not have an issue while installing in the windows system.

Python is nearer to plain English language. Therefore, it is easy to learn language. C++ has a stiff learning curve as it has lots of predefined syntaxes and structure

Python is slower. C++ is faster than Python

Python has more English like syntax, so readability is very high. C++ code readability is weak when compared with Python code.

In Python, variables are accessible outside the loop. The scope of the C++ variables is limited within the loops.

Famous companies using Python are Google, Lyft, Twitch, Telegram. Famous companies using C++ are Uber technologies, Netflix, Spotify, Instagram.

TIOBE rating is 3 TIOBE rating is 4

The average salary for a Python Developer is $120,359 per year in the United States of America. The average salary for a C++ Developer is $108,809 per year in the United States.

Here, are cons/drawbacks of using C++ language

It offers no security for your code

Complex language to use in a very large high-level program.

It is used for platform-specific applications commonly.

When C++ used for web applications it is complex and difficult to debug.

C++ can’t support garbage collection.

C++ is not as portable as other high-level programming languages. So, when you want to compile the C++ code, you need to run it on another machine.

If the same operation has to be executed more than one time, the same sequence has to copy at some places, which increases code redundancy.

Here, are cons/drawbacks of using Python language

Used in fewer platforms.

Weak in mobile computing, hence not used in app development

As Python is dynamic, so it shows more errors at run-time

Under-developed and primitive database access layer

Absence of commercial support

Google Trends C++ vs. Python

Topological Sort: Python, C++ Algorithm Example

What is Topological Sort Algorithm?

Topological Sorting is also known as Kahn’s algorithm and is a popular Sorting Algorithm. Using a directed graph as input, Topological Sort sorts the nodes so that each appears before the one it points to.

This algorithm is applied on a DAG (Directed Acyclic Graph) so that each node appears in the ordered array before all other nodes are pointed to it. This algorithm follows some rules repeatedly until the sort is completed.

To simplify, look at the following example:

Directed Graph

Here, we can see that “A” has no indegree. It means the edge that points to a node. “B” and “C” have a pre-requisite of “A”, then “E” has a pre-requisite of “D” and “F” nodes. Some of the nodes are dependent on other nodes.

Here’s another representation of the above Graph:

Dependency of each node (Linear Ordering)

So, when we pass the DAG (Directed Acyclic Graph) to the topological sort, it will give us an array with linear ordering, where the first element has no dependency.

This example shows a graph with a cycle:

Here’re the steps to do this:

Step 1) Find the node with zero incoming edges, a node with zero degrees.

Step 2) Store that zeroes in-degree node in a Queue or Stack and removes the node from the Graph.

Step 3) Then delete the outgoing edge from that node.

This will decrement the in-degree count for the next node.

Topological ordering requires that the graph data structure will not have any cycle.

A graph will be considered a DAG if it follows these requirements:

One or more nodes with an indegree value of zero.

Graph doesn’t contain any cycle

As long as there’re nodes in the Graph and the Graph is still DAG, we will run the above three steps. Otherwise, the algorithm will fall into the cyclic dependency, and Kahn’s Algorithm won’t be able to find a node with zero in-degree.

How Topological Sort Works

Here, we will use “Kahn’s Algorithm” for the topological sort. Let’s say we have the following Graph:

Here’re the steps for Kahn’s Algorithm:

Step 1) Calculate the indegree or incoming edge of all nodes in the Graph.

Note:

Indegree means the directed edges pointing to the node.

Outdegree means the directed edges that come from a node.

Here’s the indegree and outdegree of the above Graph:

Step 2) Find the node with zero indegrees or zero incoming edges.

The node with zero indegree means no edges are coming toward that node. Node “A” has zero indegrees, meaning there’s no edge pointing to node “A”.

So, we will do the following actions:

Remove this node and its outdegree edges (outgoing edges)

Place the node in the Queue for ordering.

Update the in-degree count of the neighbor node of “A.”

Step 3) We need to find a node with an indegree value of zero. In this example, “B” and “C” have zero indegree.

Here, we can take either of these two. Let’s take “B” and delete it from the Graph.

Then update the indegree values of other nodes.

After performing these operations, our Graph and Queue will look like the following:

Step 4) Node “C” has no incoming edge. So, we will remove node “C” from the Graph and push it into the Queue.

We can also delete the edge that is outgoing from “C”.

Now, our Graph will look like this:

Step 5) We can see that nodes “D” and “F” have the indegree of zero. We will take a node and put it in the Queue.

Let’s take out “D” first. Then the indegree count for node “E” will be 1. Now, there’ll be no node from D to E.

We need to do the same for node “F”, our result will be like the following:

Step 6) The indegree (ingoing edges) and outdegree (outgoing edges) of node “E” became zero. So, we have met all the pre-requisite for node “E”.

Here, we l put “E” at the end of the Queue. So, we don’t have any nodes left, so the algorithm ends here.

Pseudo Code for Topological Sorting

Here’s the pseudo-code for the topological sort while using Kahn’s Algorithm.

function TopologicalSort( Graph G ): for each node in G: calculate the indegree start = Node with 0 indegree G.remove(start) topological_list = [start] While node with O indegree present: topological_list.append(node) G.remove(node) Update Indegree of present nodes Return topological_list

Topological sort can also be implemented using the DFS (Depth First Search) method. However, that approach is the recursive method. Kahn’s algorithm is more efficient than the DFS approach.

C++ Implementation of Topological Sorting

using namespace std; class graph{ int vertices; public: graph(int vertices){ } void createEdge(int u, int v){ adjecentList[u].push_back(v); } void TopologicalSort(){

for(int i=0;i<vertices;i++){ for(itr=adjecentList[i].begin(); itr!=adjecentList[i].end();itr++){ indegree_count[*itr]++; } } for(int i=0; i<vertices;i++){ if(indegree_count[i]==0){ Q.push(i); } } int visited_node = 0; while(!Q.empty()){ int u = Q.front(); Q.pop(); order.push_back(u);

for(itr=adjecentList[u].begin(); itr!=adjecentList[u].end();itr++){ if(–indegree_count[*itr]==0){ Q.push(*itr); } } visited_node++; } if(visited_node!=vertices){ cout<<“There’s a cycle present in the Graph.nGiven graph is not DAG”<<endl; return; } for(int i=0; i<order.size();i++){ cout<<order[i]<<“t”; } } }; int main(){ graph G(6); G.createEdge(0,1); G.createEdge(0,2); G.createEdge(1,3); G.createEdge(1,5); G.createEdge(2,3); G.createEdge(2,5); G.createEdge(3,4); G.createEdge(5,4); G.TopologicalSort(); }

Output: 0 1 2 3 5 4 Python Implementation of Topological Sorting from collections import defaultdict class graph: def __init__(self, vertices): self.adjacencyList = defaultdict(list) self.Vertices = vertices # No. of vertices # function to add an edge to adjacencyList def createEdge(self, u, v): self.adjacencyList[u].append(v) # The function to do Topological Sort. def topologicalSort(self): total_indegree = [0]*(self.Vertices) for i in self.adjacencyList: for j in self.adjacencyList[i]: total_indegree[j] += 1 queue = [] for i in range(self.Vertices): if total_indegree[i] == 0: queue.append(i) visited_node = 0 order = [] while queue: u = queue.pop(0) order.append(u) for i in self.adjacencyList[u]: total_indegree[i] -= 1 if total_indegree[i] == 0: queue.append(i) visited_node += 1 if visited_node != self.Vertices: print("There's a cycle present in the Graph.nGiven graph is not DAG") else: print(order) G = graph(6) G.createEdge(0,1) G.createEdge(0,2) G.createEdge(1,3) G.createEdge(1,5) G.createEdge(2,3) G.createEdge(2,5) G.createEdge(3,4) G.createEdge(5,4) G.topologicalSort() Output: [0, 1, 2, 3, 5, 4] Cyclic Graphs of Topological Sort Algorithm

A graph containing a cycle can’t be topologically ordered. As the cyclic Graph has the dependency in a cyclic manner.

For example, check this Graph:

This Graph is not DAG (Directed Acyclic Graph) because A, B, and C create a cycle. If you notice, there’s no node with zero in-degree value.

According to Kahn’s Algorithm, if we analyze the above Graph:

Find a node with zero indegrees (no incoming edges).

However, in the above Graph, there’s no node with zero in degrees. Every node has an in-degree value greater than 0.

Return an empty queue, as it could not find any node with zero in degrees.

We can detect cycles using the topological ordering with the following steps:

Step 1) Perform topological Sorting.

Step 2) Calculate the total number of elements in the topologically sorted list.

Step 3) If the number of elements equals the total number of vertex, then there’s no cycle.

Step 4) If it’s not equal to the number of vertices, then there’s at least one cycle in the given graph data structure.

Complexity Analysis of Topological Sort

There are two types of complexity in algorithms. They’re

Time Complexity

Space Complexity

These complexities are represented with a function that provides a general complexity.

Time Complexity: All time complexity is the same for Topological Sorting. There are worst, average, and best-case scenarios for time complexity.

The time complexity for topological Sorting is O(E + V), here, E means the number of Edges in the Graph, and V means the number of vertices in the Graph.

Let’s break through this complexity:

Step 1) At the beginning, we will calculate all the indegrees. To do that, we need to go through all the edges, and initially, we will assign all V vertex indegrees to zero. So, the incremental steps we complete will be O(V+E).

Step 2) We will find the node with zero indegree value. We need to search from the V number of the vertex. So, the steps completed will be O(V).

Step 3) For each node with zero indegrees, we will remove that node and decrement the indegree. Performing this operation for all the nodes will take O(E).

Step 4) Finally, we will check if there is any cycle or not. We will check whether the total number of elements in the sorted array is equal to the total number of nodes. It will take O(1).

So, these were the individual time complexity for each step of the topological Sorting or topological ordering. We can say that the time complexity from the above calculation will be O( V + E ); here, O means the complexity function.

Space Complexity: We needed O(V) spaces for running the topological sorting algorithm.

Here are the steps where we needed the space for the program:

We had to calculate all the indegrees of nodes present in the Graph. As the Graph has a total of V nodes, we need to create an array of size V. So, the space required was O(V).

A Queue data structure was used to store the node with zero indegree. We removed the nodes with zero indegree from the original Graph and placed them in the Queue. For this, the required space was O(V).

The array is named “order.” That stored the nodes in topological order. That also required O(V) spaces.

These were the individual space complexity. So, we need to maximize these spaces in the run time.

Space complexity stands for O(V), where V means the number of the vertex in the Graph.

Application of Topological Sort

There’s a huge use for Topological Sorting. Here are some of them:

It is used when Operating system needs to perform the resource allocation.

Finding a cycle in the Graph. We can validate if the Graph is DAG or not with topological sort.

Sentence ordering in the auto-completion apps.

It is use for detecting deadlocks.

Different type of Scheduling or course scheduling uses the topological sort.

Resolving dependencies. For example, if you try to install a package, that package might also need other packages. Topological ordering finds out all the necessary packages to install the current package.

Linux uses the topological sort in the “apt” to check the dependency of the packages.

Import Module In Python With Examples

What are the modules in Python?

A module is a file with python code. The code can be in the form of variables, functions, or class defined. The filename becomes the module name.

For example, if your filename is chúng tôi the module name will be guru99. With module functionality, you can break your code into different files instead of writing everything inside one file.

In this tutorial, you will learn:

What is the Python import module?

A file is considered as a module in python. To use the module, you have to import it using the import keyword. The function or variables present inside the file can be used in another file by importing the module. This functionality is available in other languages, like typescript, JavaScript, java, ruby, etc.

How to create and import a module in Python?

Now we will create a module and import it in another file.

Here is the flow to create and import the module as shown in the screenshot:

Follow the steps given to create a module in python.

The folder structure used to test the code is as follows:

modtest/ test.py display.py

Step 1) Create a file and name it test.py

Step 2) Inside chúng tôi create a function called display_message()

Def display_message(): return "Welcome to Guru99 Tutorials!"

Step 3) Now create another file display.py.

Step 4) Inside chúng tôi import the chúng tôi file, as shown below:

import test

While importing, you don’t have to mention the chúng tôi but just the name of the file.

Step5)

Then you can call the function display_message() from chúng tôi inside chúng tôi you need to make use of module_name.function_name.

For example test.display_message().

Import test print(test.display_message())

Step 6)

When you execute chúng tôi you will get the following output:

Welcome to Guru99 Tutorials! Importing a Class in Python

Earlier, we have seen a simple module with a function. Here will create a class and refer the class inside another file.

The folder structure to test the code is as follows:

myproj/ Car.py display.py

Create a file called chúng tôi with the following code:

Filename : Car.py

class Car: brand_name = "BMW" model = "Z4" manu_year = "2023" def __init__(self, brand_name, model, manu_year): self.brand_name = brand_name self.model = model self.manu_year = manu_year def car_details(self): print("Car brand is ", self.brand_name) print("Car model is ", self.model) print("Car manufacture year is ", self.manu_year) def get_Car_brand(self): print("Car brand is ", self.brand_name) def get_Car_model(self): print("Car model is ", self.model)

In the file chúng tôi , there are attributes brand_name, model and manu_year. The functions defined inside the class are car_details(), get_Car_brand(), get_Car_model().

Let us now use the file chúng tôi as a module in another file called display.py.

Filename : display.py

import Car car_det = Car.Car("BMW","Z5", 2023) print(car_det.brand_name) print(car_det.car_details()) print(car_det.get_Car_brand()) print(car_det.get_Car_model())

Output:

BMW Car brand is BMW Car model is Z5 Car manufacture year is 2023 Car brand is BMW Car model is Z5

So we can access all the variables and functions from chúng tôi using Car module.

Using from to import module

You can import only a small part of the module i.e., only the required functions and variable names from the module instead of importing full code.

When you want only specific things to be imported, you can make use of “from” keyword to import what you want.

So the syntax is

from module import your function_name , variables,... etc.

The folder structure used to test the code is as follows:

modtest/ test.py display.py

In chúng tôi there are 2 functions as shown :

Filename : test.py

defdisplay_message(): return "Welcome to Guru99 Tutorials!" def display_message1(): return "All about Python!"

Now you want display_message() function. The function or variable that you are importing can be directly accessed as shown below:

File Name : display.py

from test import display_message print(display_message())

Output:

Welcome to Guru99 Tutorials!

Now if you happen to use the function display_message1() ,it will throw an error that the function is not defined as shown below:

from test import display_message print(display_message1())

Output:

Traceback (most recent call last): print(display_message1()) Name Error: name 'display_message1' is not defined Importing everything from the module

Import allows you to import the full module by using import followed by module name, i.e., the filename or the library to be used.

Syntax:

Import module

Or by using

from module import *

The folder structure used to test the code is as follows:

modtest/ test.py display.py

Following are the code details inside test.py

my_name = "Guru99" my_address = "Mumbai" defdisplay_message(): return "Welcome to Guru99 Tutorials!" def display_message1(): return "All about Python!" Using the import module

Using just import module name, to refer to the variables and functions inside the module, it has to prefix with module name.

Example

Filename : display.py

Import test print(test.display_message()) print(test.display_message1()) print(test.my_name) print(test.my_address)

The module name test is used to refer to the function and variables inside module test.

Output:

Welcome to Guru99 Tutorials! All about Python! Guru99 Mumbai Using import *

Let us see an example using import *. Using import *, the functions and variables are directly accessible, as shown in the example below:

from test import * print(display_message()) print(display_message1()) print(my_name) print(my_address)

Output:

Welcome to Guru99 Tutorials! All about Python! Guru99 Mumbai The dir( ) function

The dir() is a built-in-function in python. The dir() returns all the properties and methods, including the given object’s built-in properties.

So when dir() is used on the module, it will give you the variables, functions that are present inside the module.

Here is a working example of dir() on a module. We have a class called chúng tôi let us import Car and assign to dir() to see the output.

The folder structure to test the code will be as follows:

test prop/ Car.py test.py

Filename: Car.py

class Car: brand_name = "BMW" model = "Z4" manu_year = "2023" def __init__(self, brand_name, model, manu_year): self.brand_name = brand_name self.model = model self.manu_year = manu_year def car_details(self): print("Car brand is ", self.brand_name) print("Car model is ", self.model) print("Car manufacture year is ", self.manu_year) def get_Car_brand(self): print("Car brand is ", self.brand_name) def get_Car_model(self): print("Car model is ", self.model)

Filename: test.py

import Car class_contents = dir(Car) print(class_contents)

The output gives us the name of the class and all the functions defined in Car.py.

You can also try using dir() on a built-in module available in Python. Let us try the same on json module as shown in the example below. It will display all the properties and methods available in json module.

Import json json_details = dir(json) print(json_details)

Output:

['JSONDecodeError', 'JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__bu iltins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__pac kage__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_en coder', 'codecs', 'decoder', 'detect_encoding', 'dump', 'dumps', 'encoder', 'loa Packages

A package is a directory with all modules defined inside it. To make a Python interpreter treat it as a package, your directory should have the init.pyfile. The chúng tôi makes the directory as a package. Here is the layout of the package that we are going to work on.

The name of the package is my package. To start working with the package, create a directory called package/. Inside the directory, create an empty file called __init__.py. Create 3 more files chúng tôi chúng tôi and chúng tôi and define the functions as shown in the screenshot. Here are the details of module1.py,module2.py and module3.py

chúng tôi

def mod1_func1(): print("Welcome to Module1 function1") def mod1_func2(): print("Welcome to Module1 function2") def mod1_func3(): print("Welcome to Module1 function3")

chúng tôi

def mod2_func1(): print("Welcome to Module2 function1") def mod2_func2(): print("Welcome to Module2 function2") def mod2_func3(): print("Welcome to Module2 function3")

chúng tôi

def mod3_func1(): print("Welcome to Module3 function1") def mod3_func2(): print("Welcome to Module3 function2") def mod3_func3(): print("Welcome to Module3 function3")

The packages ready for use. Now call the package inside any of your file as shown below :test.py:

Here, the mypackage.module1 is imported and given an alias name as mod1. Similarly, you can use other modules chúng tôi and chúng tôi from my package.

import mypackage.module1 as mod1 print(mod1.mod1_func1()) print(mod1.mod1_func2()) print(mod1.mod1_func2())

Output:

Welcome to Module1 function1 None Welcome to Module1 function2 None Welcome to Module1 function2 None

We have just demonstrated the package with a simple module with functions inside it. As per your project, you can also package with have sub-packages. Sub-folders/ having modules with classes defined.

Python Module Search Path

During execution, when python comes across import module name, the interpreter tries to locate the module. It searches the module in the build-in module list. Later in all, the directories defined inside sys.path.

To sum up, the interpreter does the following search to locate the module:

In your current directory.

In in the build-in module list

Inside the chúng tôi directories

You can get the details of chúng tôi by importing sys module and print the sys.path. It will give you the list of directories as shown below:

importsys print(sys.path)

Output:

['Python Latest\task2', 'Users\AppData\Local\Programs\Python Python37\python37.zip', 'Users\AppData\Local\Programs\Python\P ython37\DLLs']

You can also modify the path and keep the directories as per your requirements.

Using module alias in the import

You can also convert the module name to a shorter form by giving an alias name to it. The alias can be done using the keyword.

Syntax:

import filename as alias name

The folder structure to test the code will be as follows:

Mod test/ test.py display.py

Following is the code inside test.py

my_name = "Guru99" my_address = "Mumbai" def display_message(): return "Welcome to Guru99 Tutorials!" def display_message1(): return "All about Python!"

Now will use an alias for chúng tôi in display.py

Import test as t print(t.display_message()) print(t.display_message1()) print(t.my_name) print(t.my_address)

The alias that is used for test module is t . So the function and variables from chúng tôi can be referred using the alias t.

Output:

Welcome to Guru99 Tutorials! All about Python! Guru99 Mumbai Absolute and Relative Imports in Python

You now know how to import a file as a module inside another file. Let us now see how to manage the files available in folders. The files in the folders can be imported either by using absolute or relative imports.

Consider you have your project folder structure, as shown below:

The root folder is my project/. It has two subfolders package1 and package2.

The folder package1 has two modules, chúng tôi and module2.py.

The folder package2 has one class chúng tôi a sub-package subpkg with chúng tôi and last module4.py.

In chúng tôi there is a functioncalledmyfunc1.

In chúng tôi there is a functioncalledmyfunc2.

In chúng tôi there is a functioncalledmyfunc3.

In chúng tôi there is a functioncalledmyfunc4.

Using Absolute Imports

For Absolute imports, you need to add the entire path of your module right from the project root folder.

Let us now see how to make use of absolute imports to refer to the functions present in each of the module.

To work with the functionmyfunc1, you will need to import as follows:

from package1.module1 import myfunc1 or from package1 import module1 module1.myfunc1()

To work with the function myfunc3 you will need to import as follows:

from package1.subpkg.module3 import myfunc3 or from package1.subpkg import module3 module3.myfunc3()

It becomes easy to trace back the modules for code check.

Easy to use and very straightforward.

If the project is moved to a different path, still the imports will remain the same.

The import path can get very long in-case, the modules are nested, and if the name of the modules is lengthy.

Using Relative Imports

Considering the same folder structure mentioned below, we will see how to import the same using relative imports.

In relative import, the module to be imported is relative to the current location that is the location where the import statement is present.

Syntax:

In relative imports, you need to add a period (.) before the module name when importing using from.

It will be 2 periods (..) before the module name if the module is in the one level up from the current location.

Referring to the folder structure figure mentioned above, we have the following modules with their function, which we need to refer to.

In chúng tôi there is a functioncalledmyfunc1.

In chúng tôi there is a functioncalledmyfunc2.

In chúng tôi there is a functioncalledmyfunc3.

In chúng tôi there is a functioncalledmyfunc4.

To work with the functionmyfunc1 you will need to import as follows:

from .module1 import myfunc1

To work with the function myfunc3, you will need to import as follows:

from .subpkg.module3 import myfunc3 Advantages of Relative Imports

Advantages:

It is easy to work with relative imports.

From the current location, the imports can be shortened in comparison to absolute imports.

Using relative imports, it is difficult to trace back where the code resides

Summary:

Import in Python helps you to refer to the code, i.e., .functions/objects that are written in another file. It is also used to import python libraries/packages that are installed using pip(python package manager), and you need then to use in your code.

Import functionality is available in other languages like typescript, JavaScript, java, ruby, etc.

A module is python is the code written inside the file, for example (test.py). Inside your file, you can have your variables, functions, or your class defined. The entire file becomes a module and can be imported inside another file to refer to the code.

With module functionality, you can break your code into different files instead of writing everything inside one file. Later, using import, you can refer to the code inside the file you need.

Python has its built-in modules, and also external libraries/packages installed using a python package manager (pip), e.g., pandas, NumPy, etc. are referred to as modules.

You can import only a small part of the module, i.e., only the required functions and variable names from the module instead of importing full code.

You can also convert the module name to a shorter form by giving an alias name to it. The alias can be done using the keyword.

A package is a directory with all modules defined inside it. To make a Python interpreter treat it as a package, your directory should have the __init.pyfile. The chúng tôi makes the directory as a package. Here is the layout of the package that we are going to work on.

During execution, when python comes across import module name, the interpreter tries to locate the module. It searches the module in the build-in module list. Later in all, the directories defined inside sys.path.

For Absolute imports, you need to add the entire path of your module right from the project root folder.

In relative import, the module to be imported is relative to the current location that is the location where the import statement is present.

Update the detailed information about Python Iterables, Iterators & Generators: A Complete Guide on the Bellydancehcm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!