This book introduces the reader with little or no previous computer-programming experience to the Python programming language of interest for a physicist or a natural-sciences student. The book starts with basic interactive Python in order to acquire an introductory familiarity with the language, than tackle Python scripts (programs) of increasing complexity, that the reader is invited to run on her/his computer. All program listings are discussed in detail, and the reader is invited to experiment on what happens if some code lines are modified. The reader is introduced to Matplotlib graphics for the generation of figures representing data and function plots and, for instance, field lines. Animated function plots are also considered. A chapter is dedicated to the numerical solution of algebraic and transcendental equations, the basic mathematical principles are discussed and the available Python tools for the solution are presented. A further chapter is dedicated to the numerical solution of ordinary differential equations. This is of vital importance for the physicist, since differential equations are at the base of both classical physics (Newton’s equations) and quantum mechanics (Schroedinger’s equation). The shooting method for the numerical solution of ordinary differential equations with boundary conditions at two boundaries is also presented. Python programs for the solution of two quantum-mechanics problems are discussed as examples. Two chapters are dedicated to Tkinter graphics, which gives the user more freedom than Matplotlib, and to Tkinter animation. Programs displaying the animation of physical problems involving the solution of ordinary differential equations (for which in most cases there is no algebraic solution) in real time are presented and discussed. Finally, 3D animation is presented with Vpython.
Author(s): Giovanni Moruzzi
Edition: 1
Publisher: Springer
Year: 2020
Language: English
Pages: 302
Tags: Python, Matplotlib, Numeric, Tkinter, VPython
Preface
Contents
1 Python Basics and the Interactive Mode
1.1 Using the Command Line
1.2 Installing Python
1.2.1 General
1.2.2 Downloading Python for Windows
1.2.3 Downloading Python for macOS
1.3 Using Python in Interactive Mode
1.4 Variables
1.4.1 Variable Types
1.4.2 Variable-Type Conversion
1.5 Arithmetic Operators
1.6 Assignment Operators
1.7 Comparison and Logical Operators
1.8 Python Packages and the import Statement
1.9 Conditional Statements
1.10 Lists and Tuples
1.11 List Methods
1.12 Lists and the = Assignment Operator
1.12.1 Copying Lists
1.12.2 Copying the Elements of Lists and Tuples
1.13 Slicing Lists and Strings
1.14 Dictionaries
1.15 The print() Function and Formatting
1.15.1 Old Style
1.15.2 New Style
1.15.3 Format Types and Flags
1.16 Loops
1.16.1 The for Loop
1.16.2 The while Loop
1.16.3 Breaking a Loop
1.16.4 The continue Statement
1.17 Operations with Matrices and Vectors
1.17.1 Lists and Arrays
1.17.2 Slicing out Rows and Columns from a Matrix
1.17.3 Arrays and Matrix Arithmetics
1.17.4 Further Matrix Operations
1.18 Exiting Interactive Mode
1.19 Exercises
2 Python Scripts
2.1 Scripts
2.1.1 Running Scripts on a Terminal
2.1.2 Running Scripts on Spyder3
2.1.3 A First Script
2.2 Functions
2.2.1 General
2.2.2 Local and Global Variables
2.3 Reading and Writing Files
2.4 Calling External Commands from Python Scripts
2.5 Exercises
3 Plotting with Matplotlib
3.1 Pyplot
3.2 Plotting Lists of Numbers
3.3 Plotting Functions
3.4 Multiple Figures
3.5 Logarithmic Axis Scales
3.6 Plotting Field Lines
3.7 Pyplot Animation
3.8 Exercises
4 Numerical Solution of Equations
4.1 Introduction
4.2 Systems of Linear Equations
4.3 Nonlinear Equations
4.3.1 Common Methods for the Numerical Solution
4.3.2 Bisection Method
4.3.3 The Secant Method
4.4 Root Finding with the scipy.optimize Package
4.5 Algebraic Equations
4.6 Systems of Nonlinear Equations
4.6.1 Equations Involving only Unknowns
4.6.2 Equations Involving Unknowns and Parameters
4.7 Integration of Functions
4.7.1 Introduction
4.7.2 Rectangular and Trapezoidal Rules
4.7.3 The Simpson Rule
4.7.4 The scipy.integrate.simps Function
4.8 Exercises
Reference
5 Numerical Solution of Ordinary Differential Equations (ODE)
5.1 Introduction
5.2 Euler and Runge–Kutta Methods
5.3 The scipy.integrate.odeint Function
5.4 Large-Amplitude Pendulum
5.5 The Shooting Method
5.5.1 The Finite Square Well
5.5.2 The Shooting Method
5.5.3 The Harmonic Oscillator
5.6 Exercises
Reference
6 Tkinter Graphics
6.1 Tkinter
6.2 The Root Window and Tkinter Color Management
6.3 Drawing Geometric Shapes on the Canvas
6.4 Plotting a Function with Tkinter
6.4.1 Plotting a Hyperbola
6.4.2 Saving an Image of the Canvas
6.5 Exercises
7 Tkinter Animation
7.1 Introduction
7.2 Adding Uniform Acceleration
7.3 Adding Interactive Buttons
7.4 Numerical Parameters, Entries, Labels and Mouse Dragging
7.5 Creating Video Files Under Tkinter
7.6 Animation and Ordinary Differential Equations
7.6.1 Euler's Method
7.6.2 The Leapfrog Method
7.6.3 The odeint Integration
7.7 Length and Time Scaling
7.8 Exercises
8 Classes
8.1 The class Statement
8.2 A First Example: Two Colliding Balls
8.3 A ``Classical'' Atom
8.3.1 A ``C-Style'' dfdt() Alternative Function
8.4 Exercises
9 3D Graphics and Animation with VPython
9.1 Introduction and Installation
9.2 The VPython Vector Object
9.3 VPython Displayable Objects
9.3.1 Sphere
9.3.2 Box
9.3.3 Cylinder
9.3.4 Cone
9.3.5 Pyramid
9.3.6 Ellipsoid
9.3.7 Helix
9.3.8 Ring
9.3.9 Curve
9.3.10 Arrow
9.3.11 Compound
9.3.12 Clone
9.3.13 Points
9.3.14 Extrusion
9.3.15 Triangle/quad
9.3.16 Label
9.3.17 Text
9.4 Controlling the View
9.5 VPython Animation
9.5.1 Basics
9.5.2 Interactive Control
9.6 2D Graphics
9.7 Exercises
10 Answers to the Exercises
Appendix A Relevant Mathematical Functions
A.1 The math Module
A.1.1 Number-Theoretic and Representation Functions
A.1.2 Power and Logarithmic Functions
A.1.3 Trigonometric Functions
A.1.4 Angular Conversion
A.1.5 Hyperbolic Functions
A.1.6 Special Functions
A.1.7 Constants
A.2 The cmath Module
A.2.1 Conversions to and from Polar coordinates
A.2.2 Power and Logarithmic Functions
A.2.3 Trigonometric Functions
A.2.4 Hyperbolic Functions
A.2.5 Classification Functions
A.2.6 Constants
Appendix B Building Your Own Library
B.1 Writing a Module
B.2 The PYTHONPATH Environment Variable
Appendix C The integrate Subpackage
C.1 simps()
C.2 odeint()
Appendix D Methods for Drawing and Writing on the Canvas
Appendix E Unicode Characters
Appendix F Tkinter Events
F.1 Mouse Events
F.2 Keyboard Events
Appendix G VPython
G.1 Colors
G.2 Extrusion: Shapes and Paths Library
G.2.1 Rectangle
G.2.2 Circle
G.2.3 Ellipse
G.2.4 Arc
G.2.5 Line
G.2.6 Triangle
G.2.7 Pentagon, Hexagon, Octagon, Ngon
G.2.8 Star
G.2.9 Trapezoid
Index