Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 15 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 15 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 15 -

Bild der Seite - 15 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 15 -

1.4 ImportingfromModulesandPackages 15 Importing Individual Items With ball_angle.py, we just learned that the import statement from math import atan, pi made the atan function and pi available to the program. To bring in even more functionality from math, the import statement could simply have been extended with the relevant items, say from math import atan, pi, sin, cos, log andso on. Having Several Import Statements Very often, we need to import functionality from several libraries. This is straight forward, as we may show by combining imports from math with imports from the useful Numerical Python (or NumPy) library,11 namednumpy inPython: from math import atan, pi, sin, cos, log from numpy import zeros, linspace Right now, do not worry what the functions zeros and linspace do, we will explainanduse themsoon. ImportingAllItemswith“Import*” Theapproachofimportingindividualitems (atan, pi, etc.) might appear less attractive if you need many of them in your program.There is anotherway, though,but it should be used with care, for reasons to be explained. In fact, many programmers will advice you not to use it at all, unlessyou know very well what youare doing.With this import technique, the list of itemsin the importstatement isexchangedwithsimplyastar (i.e.,*).Theimport statement thenappearsas from some_library import * # import all items from some_library whichwith themath library reads from math import * # import all items from math This will cause all items from math to be imported, however, also the ones you do not need! So, with this “lazy” import technique, Python has to deal with a lot of names that are not used. Like when importing individual items, items are used withoutprefix. Disadvantage:No Prefix Allows Name Conflicts! When importingso that items are written without prefix, there is a potential problem with name conflicts. Let us illustrate the point with a simple example. Assume that, being new to Python, we want to write a little program for checking out some of the functions that the languagehasgot to offer. Our first candidate could be the exponential function and we might like to computeandprintoutet for t=0,1,2.Afellowstudentexplainsushowafunction exp in thenumpy library allows our calculations to be done with a single function 11 The NumPy library (http://www.numpy.org/) is included in Anaconda. If you have not installed Anaconda, you may have to installNumPy separately.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python