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

Page - 17 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 17 -

Image of the Page - 17 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 17 -

1.5 MoreBasicConcepts 17 aligned under each other and written with the same precision. The output then becomes Formatting via printf syntax 2.00 1.819 2.55 1.422 3.10 0.129 Weshall frequently use the printf syntax throughout the book so therewill be plentyof furtherexamples. Themodernalternativetoprintfsyntax ModernPythonfavors thenew format string syntaxoverprintf: print ’At t={t:g} s, y={y:.2f} m’.format(t=t, y=y) whichcorresponds to theprintf syntax print ’At t=%g s, y=%.2f m’ % (t, y) The slotswhere variables are inserted are now recognized by curly braces, and in formatwe list the variable names inside curly braces and their equivalent variables in theprogram. Since the printf syntax is sowidely used inmany programming languages, westick to that in thepresentbook,butPythonprogrammerswill frequentlyalso meet thenewer format string syntax, so it is important tobeaware its existence. 1.5.6 Arrays In the program ball_plot.py fromChapter 1.4we saw how1001 height com- putationswere executed and stored in the variabley, and then displayed in a plot showing y versus t, i.e., height versus time. The collection of numbers in y (or t, respectively)was stored inwhat is called anarray, a construction also found in most other programming languages. Sucharrays are createdand treated according tocertainrules,andasaprogrammer,youmaydirectPythontocomputeandhandle arrays as awhole, or as individualarray elements. Let usbriefly lookat a smaller suchcollectionofnumbers. Assume that the heights of four family members have been collected. These heightsmaybegeneratedandstored inanarray,e.g., namedh, bywriting h = zeros(4) h[0] = 1.60 h[1] = 1.85 h[2] = 1.75 h[3] = 1.80 where the array elements appear ash[0], h[1], etc. Generally, whenwe read or talkabout thearrayelementsofsomearraya,werefer to thembyreadingorsaying
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
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