0% found this document useful (0 votes)
21 views

2.4.2 For/Range: Python Scientific Lecture Notes, Release 2012.3 (Euroscipy 2012)

This document contains lecture notes about Python scientific programming. It discusses Python concepts like indentation, for/range loops to iterate with indexes or over values, while loops with features like break and continue, and conditional expressions. Examples are provided like iterating from 0 to 3 with a for loop, printing strings with different words using for, and using while and break/continue in a Mandelbrot problem and to continue or break out of loops.

Uploaded by

claudyane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

2.4.2 For/Range: Python Scientific Lecture Notes, Release 2012.3 (Euroscipy 2012)

This document contains lecture notes about Python scientific programming. It discusses Python concepts like indentation, for/range loops to iterate with indexes or over values, while loops with features like break and continue, and conditional expressions. Examples are provided like iterating from 0 to 3 with a for loop, printing strings with different words using for, and using while and break/continue in a Mandelbrot problem and to continue or break out of loops.

Uploaded by

claudyane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Scientific lecture notes, Release 2012.

3 (EuroScipy 2012)

...:
A lot

Indentation is compulsory in scripts as well. As an exercise, re-type the previous lines with the same indentation
in a script condition.py, and execute the script with run condition.py in Ipython.

2.4.2 for/range

Iterating with an index:


>>> for i in range(4):
... print(i)
0
1
2
3

But most often, it is more readable to iterate over values:


>>> for word in (’cool’, ’powerful’, ’readable’):
... print(’Python is %s’ % word)
Python is cool
Python is powerful
Python is readable

2.4.3 while/break/continue

Typical C-style while loop (Mandelbrot problem):


>>> z = 1 + 1j
>>> while abs(z) < 100:
... z = z**2 + 1
>>> z
(-134+352j)

More advanced features


break out of enclosing for/while loop:
>>> z = 1 + 1j

>>> while abs(z) < 100:


... if z.imag == 0:
... break
... z = z**2 + 1

continue the next iteration of a loop.:


>>> a = [1, 0, 2, 4]
>>> for element in a:
... if element == 0:
... continue
... print 1. / element
1.0
0.5
0.25

2.4.4 Conditional Expressions

if object

2.4. Control Flow 17

You might also like