Python En Toc.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Revision September 12, 2013 About this Textbook This textbook is provided as a courtesy to NCLab users. Python is a modern high-level dynamic programming language that is used in many areas of business, engineering, and science today. After taking this course, you will have solid theoretical knowledge and vast practical experience with computer programming in Python. Acknowledgments The author Dr. Pavel Solin would like to thank the following people for their valuable suggestions and help: – Martin Novak, Czech Technical University, Prague, Czech Republic. – Joel Landsteiner, Cray Inc, USA. – William Mitchell, NIST, USA. – Venkata Rama Rao Mallela, Hyderabad, India. – Steven Lamb, Philadelphia, USA. – Norman Dunbar, Leeds, West Yorkshire, England. – Samuel Marks, Sydney, Australia. Graphics Design: TR-Design http://tr-design.cz Table of Contents I Textbook 1 Introduction . .5 1.1 Objectives . .5 1.2 Compiled and interpreted programming languages . .5 1.3 Basic facts about Python . .5 1.4 Python programming in NCLab . .6 1.5 Downloading Python programs from NCLab’s database . .6 1.6 Launching the Python module . .6 1.7 Code, output, and descriptive cells . .7 2 Using Python as a Calculator . .8 2.1 Objectives . .8 2.2 Addition and subtraction . .8 2.3 Multiplication . .9 2.4 Division . .9 2.5 Modulo . 11 2.6 Powers . 11 2.7 Priority of operators . 12 2.8 Using empty characters makes your code more readable . 13 2.9 Using mathematical functions . 13 2.10 Fractions . 14 2.11 Random numbers . 15 2.12 Complex numbers . 16 3 Functions . 17 3.1 Objectives . 17 3.2 Defining new functions . 18 3.3 Passing arbitrary arguments . 19 3.4 Returning multiple values . 19 3.5 Using default arguments . 20 4 Colors and Plotting . 23 4.1 Objectives . 23 4.2 RGB colors . 24 4.3 Plotting polylines and polygons . 25 4.4 Plotting functions of one variable . 27 4.5 Labels, colors, and styles . 28 4.6 Scaling axes and showing grid . 30 4.7 Adjusting plot limits . 31 4.8 Plotting multiple functions at once . 32 4.9 Plotting parametric 2D curves . 34 4.10 Plotting parametric 3D curves . 35 4.11 Plotting functions of two variables . 36 4.12 Plotting functions of two variables with WebGL . 39 4.13 Plotting pie charts . 41 4.14 Plotting bar charts . 43 5 Variables . 46 5.1 Objectives . 46 5.2 Creating variables . 47 5.3 Initializing multiple variables at once . 48 5.4 Changing values of variables . 49 5.5 Local and global variables . 50 5.6 How to get in trouble . 51 5.7 Variable shadowing . 52 5.8 Overriding constants . 53 6 Logic and Probability . 53 6.1 Objectives . 53 6.2 True and False ..................................................... 53 6.3 Boolean variables . 55 6.4 Boolean operations . 55 6.5 Monte Carlo methods . 57 7 Conditional Loop . 60 7.1 Objectives . 60 7.2 Counting and conditional loops . 61 7.3 Conditions and the elif statement. 61 7.4 The while loop...................................................... 63 7.5 The break statement . 64 7.6 The continue statement . 65 7.7 Abusing the while loop.............................................. 66 7.8 Next trip into scientific computing . 67 8 Strings .................................................................. 69 8.1 Objectives . 69 8.2 Using quotes . 69 8.3 Multiline strings and backslashes . 69 8.4 Concatenation and repetition . ..