if
", "elif
", and "else
")for
" and "while
" and the accompanying "break
", "continue
", and "pass
").A basic example of a Python conditional statement is this (we need to use a colon to specify an 'if, elif or else' statement:
In [1]:
foo = 'bar'
# The 'if' statment will set a criteria, and 'if' the condition is met, it will print 'Condition satisfied'
if foo == 'bar':
print('Condition satisfied')
In [2]:
x = -15
# If x is equal to zero print x, "is zero"
if x == 0:
print(x, "is zero")
# Other wise if x is greater than zero print x, "is positive"
elif x > 0:
print(x, "is positive")
# If all else fails print x, "is negative"
else:
print(x, "is negative")
Note especially the use of colons (:
) and whitespace to denote separate blocks of code.
So, for example, if we'd like to print each of the items in a list, we can use a for
loop:
In [3]:
for N in [2, 3, 5, 7]:
print(N)
Now lets print it on the same line for ease:
A 'for loop' is a basic and extremely helpful tool in python.
What we are doing here is replace the 'N' with each number within the list ie. [2,3,5,7] sequentionally, and printing the output.
For each number you printed, you can see it has been placed on a new line. This signifies the loop has restarted on the next value in the list.
In [4]:
for N in [2, 3, 5, 7]: # you can replace 'N' with anything you like, but make sure to replace it below too!
print(N, end=' ') # print all on same line
In [5]:
for i in range(10): # Here we have replaced 'N' with 'i'
print(i) #print(i, end=' ')
Range objects can also have more complicated values:
In [6]:
# range from 5 to 10
list(range(5, 10))
Out[6]:
In [7]:
# range from 0 to 10 by 2
list(range(0, 10, 2))
Out[7]:
In [8]:
i = 0
while i < 10:
print(i, end=' ')
i += 1
The argument of the while
loop is evaluated as a boolean statement, and the loop is executed until the statement evaluates to False.
Let's print a string of odd numbers.
In [9]:
for n in range(20):
# check if n is even
if n % 2 == 0:
continue
print(n,end=" ")
In this case, the result could be accomplished just as well with an if-else
statement, but sometimes the continue
statement can be a more convenient way to express the idea you have in mind:
Here is an example of a break
statement used for a less trivial task.
This loop will fill a list with all Fibonacci numbers up to a certain value:
In [10]:
a, b = 0, 1
amax = 100
L = []
while True:
(a, b) = (b, a + b)
if a > amax:
break
L.append(a)
print(L)
Notice that we use a while True
loop, which will loop forever unless we have a break statement! Above we set a value of 100 to break if the value 'L' becomes greater than 100.
Write a program that can print the following pattern for a given triangle size N.
For N=5:
*
* *
* * *
* * * *
* * * * *
print('*',end=' ') # to print '*' without changing line print('') # change line </code>
In [11]:
import sys
if sys.version_info < (3,0):
range = xrange
range
, like a list, exposes an iterator:
In [12]:
iter(range(10))
Out[12]:
So Python knows to treat it as if it's a list:
In [13]:
for i in range(2):
print(i)
In this indirect iterator the full list is never explicitly created!
Let's try something sily:
In [14]:
N = 10 ** 9
for i in range(N): # this would take > 4 GBytes to store in memory
if i >= 10: break
print(i) # print(i, end=', ') in python 3
sys.getsizeof(range(N))
Out[14]:
If range were to actually create that list of one trillion values, it would occupy tens of terabytes of machine memory: a waste, given the fact that we're ignoring all but the first 10 values!
In fact, there's no reason that iterators ever have to end at all! Python's itertools library contains a count function that acts as an infinite range:
In [15]:
from itertools import count
for i in count():
if i >= 10:
break
print(i, end=', ')
Had we not thrown-in a loop break here, it would go on happily counting until the process is manually interrupted or killed (using, for example, ctrl-C
).
If range
were to actually create that list of one trillion values, it would occupy tens of GB of machine memory: a waste, given the fact that we're ignoring all but the first 10 values!
In [16]:
L = [2, 4, 6, 8, 10]
for i in range(len(L)):
print(i, L[i])
Although this does work, Python provides a cleaner syntax using the enumerate
iterator:
In [17]:
for i, val in enumerate(L):
print(i, val)
In [18]:
L = [3, 3, 6, 8, 15]
M = [2, 6, 8, 10, 12]
R = [1, 4, 5, 9, 10]
for lval, mval, rval in zip(L, M, R):
print(max(lval, mval, rval))
#Alternative to
for i in range(len(L)):
max(L[i], M[i], R[i])
Any number of iterables can be zipped together, and if they are different lengths, the shortest will determine the length of the zip
.
In [19]:
list(map(max, L, M, R))
Out[19]:
More specialised iterators are available in a built-in itertools
module. Example:
In [20]:
from itertools import permutations
p = permutations(range(3))
print(*p)
These things happen... Mistakes come in three basic flavors:
Here we're going to focus on how to deal cleanly with runtime errors.
In [21]:
import sys
### Handling exceptions
try:
open('myfile')
except IOError:
err = sys.exc_info()[1]
print(err)
print('Cannot read file so will use default values')
# Do alternate action
else:
print('Values were read from myfile')
In [22]:
N=5
#Solution
for i in range(N):
for j in range(i+1):
print ('*', end=' ')
print('')
print('')
In [23]:
#Solution using break
for i in range(N):
for j in range(N):
if (j > i):
break
print('*', end=' ')
print('')