Prints the multiplication table of 5 i=0
while i&lt;=10:
    i+=1
    if i==5:
        continue
    print i*5


It prints the multiplication table of 5 as well as skips the 5th number in the series
This is the longest possible answer to the shortest problem...

How to make better loops in python


iterables
for loops
range
list comprehensions
Generator expressions 


How to improve this particular code


what is the simplest way possible of writing it
what is the shortest way possible of writing it




How to make better loops in python

1. iterables

For loops can iterate over any iterables. What is an iterable? An iterable is a object we can iterate through.

More seriously, an iterable is an object that is itself a sequence of other objects. A list is an iterable, but it is not the only one as you will discover when you learn more about python.

2. for loops

A for loop can iterate over an iterable (in this case a list) like so:

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(i)


3. range

The previous example is ugly because:
1. Hard-coded values can't be easily changed and are prone to mistakes.
2. We stored the first 10 positive integers in memory which take 144 bytes but it isn't scalable as if I want the first 1000000 positive integer, I would need 9 mo of memory.

That's where range comes in, it is an iterable that doesn't take much space and is often used in loops:

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


note: in earlier versions of python, range created a list and was equivalent to the first example.

What if I only want the numbers between 22 and 42 and skip every odd number:

for i in range(22, 43, 2):
    # range(start, end, step) &lt;- this is a comment
    print(i)


note: the range(22, 43) doesn't actually include 43

4. list comprehensions

But wait, I don't want a 5 in my range, how can I remove it ?

for i in range(10):
    if i==5:
        continue
    print(i)


The technical term for continue is "ugly".

What you can use instead is this:

for i in range(10):
    if i!=5:
        print(i)


Which is better, but isn't what I want to show you. What I want to show you is list comprehensions:

l = [n for n in range(10) if n!=5]
for i in l:
    print(i)


A list is an iterable remember. Well, I've created a list containing the first 10 number except 5.

5. Generator expression

Remember also that I've said that a list was a very bad choice due to the memory inefficiency. Well fortunately for us, we have a better solution, generator expressions:

l = (n for n in range(10) if n!=5)
for i in l:
    print(i)


What changed ? Parentheses ! As easy as that. 

But wait, I want to do the table of 5. How can I do that ?

l = (n for n in range(10) if n!=5)
for i in l:
    print(i*5)


Yes, that works, but what if...

l = (n*5 for n in range(10) if n!=5)
for i in l:
    print(i)


Yep, that also works...

How to improve your particular code

1. what is the simplest way possible of writing it

for i in range(10):
    if i!=5:
        print(i*5)


2. what is the shortest way possible of writing it

print('\n'.join(str(n*5) for n in range(10) if n!=5))
or 
print(*(n*5 for n in range(10) if n!=5), sep='\n')


As you learn more about python, you might be tempted to use the second one. But it isn't pythonic because it violates at least 6 of Tim Peter's commandment:


  Beautiful is better than ugly.&lt;
  Explicit is better than implicit.
  Simple is better than complex.&lt;
  Complex is better than complicated.
  Flat is better than nested.&lt;
  Sparse is better than dense.&lt;
  Readability counts.&lt;
  Special cases aren't special enough to break the rules.
  Although practicality beats purity.
  Errors should never pass silently.
  Unless explicitly silenced.
  In the face of ambiguity, refuse the temptation to guess.
  There should be one-- and preferably only one --obvious way to do it.&lt;
  Although that way may not be obvious at first unless you're Dutch.
  Now is better than never.
  Although never is often better than right now.
  If the implementation is hard to explain, it's a bad idea.
  If the implementation is easy to explain, it may be a good idea.
  Namespaces are one honking great idea -- let's do more of those!


import this to read it at home

Some techniques of looping might be more appropriate in certain cases, but at least now you know what those techniques are.