Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 110 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 110 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 110 -

Image of the Page - 110 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 110 -

110 5 SomeMorePythonEssentials Ifwe try touseanuninitializedvariable, aNameErrorexception is raised: In [1]: print(x) # x is uninitialized ... ... NameError: name ’x’ is not defined Whendivisionbyzero is attempted, it results inaZeroDivisionErrorexception: In [2]: 1.0/0 ... ... ZeroDivisionError: float division by zero Using illegal indicescausesPython to raise anIndexErrorexception.: In [3]: x = [7, 8, 9] In [4]: x[2] Out[4]: 9 In [5]: x[3] # legal indices are 0, 1 and 2 ... ... IndexError: list index out of range WrongPythongrammar,orwrong typingof reservedwords,givesaSyntaxError exception: In [6]: impor numpy as np # typo... missing t in import ... impor numpy as np ^ SyntaxError: invalid syntax Ifobject typesdonotmatch,PythonraisesaTypeErrorexception: In [7]: ’a string’ + 1 # attempt to add string and integer ... ... TypeError: must be str, not int (We might add that, in the last example here, two strings could have been straight forwardlyconcatenatedwith+.) AbortExecutionwithsys.exit In some cases, it is desirable to stop execution there and then. This may be done effectively by use of the exit function in the sys modulea (a module with functions and parameters that are specific to the system). For anapplicationofsys.exit, see Sect. 7.2.2. a https://docs.python.org/3/library/sys.html. We have been careful to check code behavior in a step-wise fashion while developing our program. Still, testing should be done also with (what, for now, is regardedas) the β€œfinal” version.To test our times tables program,we should check
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python