I want to create a custom topology using Python API and mininet. It should be such that, if there are n number of hosts, then odd numbered hosts can ping each other and also even numbered hosts can ping each other.
For example, if we have 5 hosts, h1 .. to h5,
then h1 can ping h3 and h5, while h2 can only ping h4.
I have tried writing code, in which I added links between all even hosts and between all odd hosts. But I am not able to get the desired outcome. h1 is able to ping h3, but not h5.
Also, is it correct to define links between hosts? Or should we only have links between hosts and switches and within switches?
Since you are setting the controller to  be remote, --controller=remote, you need to provide a controller explicitly. 
For example, if you are using POX, 
In another terminal, run this:
cd pox
./pox.py openflow.discovery forwarding.l2_learning
Now do a pingall in the mininet console, there should be 0% packet loss
Custom Topology in Mininet