Understanding Optics with Python
CRC Series on MULTIDISCIPLINARY AND APPLIED OPTICSSERIES EDITORVasudevan Lakshminarayanan University of Waterloo, Ontario, CanadaRECENT TITLESUnderstanding Optics with PythonVasudevan Lakshminarayanan, Hassen Ghalila, Ahmed Ammar, and L. Srinivasa VaradharajanIntroduction to Adaptive Optics for Vision ScienceKaren Hampson
Understanding Optics with PythonVasudevan LakshminarayananHassen Ghalila Ahmed AmmarL. Srinivasa Varadharajan
CRC PressTaylor & Francis Group6000 Broken Sound Parkway NW, Suite 300Boca Raton, FL 33487-2742© 2018 by Taylor & Francis Group, LLCCRC Press is an imprint of Taylor & Francis Group, an Informa businessNo claim to original U.S. Government worksPrinted on acid-free paperVersion Date: 20180118International Standard Book Number-13: 978-1-4987-5504-7 (Hardback)This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint.Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers.For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe.Visit the eResources: https://www.crcpress.com/971498755047Visit the Taylor & Francis Web site athttp://www.taylorandfrancis.comand the CRC Press Web site athttp://www.crcpress.com
Dedication
Vasudevan Lakshminarayanan dedicates this book to Mike Maiten, a genius
software engineer and a very close friend for over 40 years starting from
freshman year at University High School.
Hassen Ghalila dedicates this book to Jean-Max Buzzi, who introduced him
to the world of digital simulation and showed him how to understand
complex physical concepts in an otherwise simpler way. He also dedicates
this book to Zohra BenLakhdar for her love for optics, her generosity, and
her eternal optimism.
Ahmed Ammar dedicates this book to all his friends at the Astronomical
Society of Tunisia (SAT) and at the Optical Society of Tunisia (STO).
Srinivasa Varadharajan dedicates this book to Geetha and Saranya for their
understanding and patience during the preparation of this book.
Contents
Preface........................................................................................................ xiii
Chapter 1
Introduction to Python..........................................................1
1.1 Why Python?.................................................................1
1.2 Python Setup .................................................................2
1.2.1 Which Distribution Do We Need? ......................2
1.2.2
Installing Anaconda............................................3
1.2.3 The Anaconda Navigator ...................................3
1.2.3.1 How to Start Anaconda Navigator ......4
Jupyter/IPython QtConsole ................4
1.2.3.2
1.2.3.3
Spyder..................................................5
1.3 Coding with Jupyter/IPython QtConsole......................6
1.3.1 Comments...........................................................6
1.3.2 Hello World! .......................................................7
1.3.3 Use Python As a Calculator...............................7
1.3.3.1 Numbers ..............................................7
1.3.3.2 Values and Types.................................8
1.3.4 Variables and Reserved Keywords ......................9
1.3.4.1 Variables ..............................................9
1.3.4.2 Keywords ...........................................10
1.3.5 Container Types ...............................................10
1.3.5.1
Strings................................................11
1.3.5.2
Lists ...................................................15
1.3.5.3 Tuples ................................................17
1.3.5.4 Dictionaries........................................18
1.3.6 Control Structures............................................19
1.3.6.1 Condition Checking ...........................19
1.3.6.2 The if/elif/else Construction .............20
1.3.6.3 The for/range Loop ...........................20
1.3.6.4 The while Loop..................................21
1.3.6.5 Continue and Break...........................21
1.4 Modules and Scripts.....................................................22
1.4.1 Modules ............................................................22
1.4.2 Write and Run Python Scripts with Spyder.....23
1.4.3 Defining Functions............................................23
1.4.4 Classes ..............................................................25
1.5 Widely Used Python Libraries for Science and
Engineering ..................................................................26
vii