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

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

Bild der Seite - 17 -

Bild der Seite - 17 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 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
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
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