Skip to content

python

Creating a simple iterators in python with example

The list, tuples and dictionary  in python are iterators. It means, we can iterate over it ie we can run a for loop on it to get its values. These are the default build-in iterators in python.

We can create our own iterators too. Here we will learn to create our very own iterators and iterate over it.

How to create a iterators:

Technically the objects that will implement the method __iter__ and __next__ are iterators. So we need to create a class and make it implement the two methods. Bellow we have a simple example of iterators, where you can find the method being implemented by the class.

Creating our own simple iterators in python3

# trying to create iterators like list , tuples

class MyIterators:
  def __init__(self, maxpoint):
    self.max = maxpoint
  
  #called when the iteration is called
  #pint from which iteration start
  def __iter__(self):
    self.item = 0
    return self

  #to move to the next element
  #until it stops do some action and return
  def __next__(self):
    item = self.item
    if item > self.max:
      raise StopIteration
    
    self.item = item +2
    return item

n=MyIterators(10)
for item in n:
  print(item)

 

Iterating through iterable :

One way will be to use the for loop as in the example above. But we can also make use ot iter method and get the next value as shown bellow

n = MyIterators(2)
item  = iter(n)   #using the iter method as its iterable
print(next(item)) #getting next item with next method  
print(next(item))

 

Using the StopIteration to stop the iteration:

As seen in the iterator example above to stop the iteration from running forever. We use the StopIteration to raise the exception. Which will stop the process when the max limit is reached.

Python IDE Is it compulsory

We often see people wandering around in internet, which IDE should I be using for python. There are so many IDE around. We would not like to compare and say this one is better than other. The choice depends and might differ from users.

As a beginners, We don’t really need IDE. We can simply run the script created in simplest code editor like VIM (linux based code editor). Only requirement is the file must be in .py extension. The IDE make life easier with many features it has like auto-complete, syntax highlighter, debugger and many more. But if you are a beginners first focus on learning the core concept rather than debating and wasting your time on choosing the right IDE.

List of IDE and code editor for python :

  1. PyCharm
  2. Sublime  Text
  3. Atom
  4. Visual Studio
  5. Eclipse with PyDev
  6. Spyder

The list goes on. We personally request beginners to not bother about it and focus more on python.

 

Installing python on linux (ubuntu)

Python normally comes in two version python-2 and python-3. If you are just a beginners ,like many , you might wonder too what is the difference but I would just recommend to get going with python version 3. With time you can explore the difference.

installing python on ubuntu machine

Check if python is installed:

Most of the system mainly linux based system comes with python pre-installed. In such case, we don’t need to install it again. To check if your system already have the python installed or not. Just type python in the terminal. If you see some message with python version then you already have the python installed in your system. Nowadays, the pre-installed version of python is python-3. If you want to install the python-2 follow the steps bellow.

Installing python-2 in ubuntu:

Installing python-2 is very easy in ubuntu machine. Simply type the following command in terminal. To open the terminal type ctrl+alt+t or open it manually.

sudo apt-get update && apt-get install python

Installing python-3 in ubuntu:

To install python-3 in ubuntu machine follow the steps bellow.

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Opening desired python version if both python2 and python3 is installed.

Yes, we can have both version of python installed. In such scenario if we type python, the python version2 interpreter will start. for starting python version3 type python3.

Similarly, if we use python or python3 command to execute the script in respective python.

 

Lazy evaluation in python with simple example

Things become interesting and even the harder concept becomes easier when we have a simple example to showcase the idea. Today I am trying to put the idea of lazy evaluation with a very simple python codes, using a single built in function.

lazy evaluation in python with example

To test the process we need to run the application in python2 and python3

numbers = range(1,10000000000000)
for item in numbers:
    print item

Run it in python-2 and we will run into memory issue. Particularly, will say memory overflow. Which means it tried to put all the numbers in the range in the memory and could not do it.

Solution is lazy evaluation. To get the values only when required. So try running the same line of code and it will run smoothly without any memory issue. So what had exactly happened is. It didn’t put all the values in range into memory and our for loop fetched the value that is required and displayed it.

I hope, I have tried my best to make the concept clear. So try to make your function lazy evaluated as possible when it comes to handling large data.