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 - 15 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 15 -

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

Text of the Page - 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.
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