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

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

Image of the Page - 57 -

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

Text of the Page - 57 -

2.5 Exercises 57 In [1]: x = 1; print(x + 1); x = x + 1; x += 1; x = x - 1; x -= 1 In [2]: print(x) Then type it inandconfirmthatyourpredictionsarecorrect. b) Repeat the previous task, but this time without statements number 3 and 4 (counted from left) of the first input line. To confirm your prediction this time, use the command history facility to first bring up the previous command line, whichyou thenedit appropriatelybeforepressingenter. Remarks With our statements here, it would clearly have been more readable to write them on separate lines (also making the semi-colon superfluous). Generally, this isalso thecase,althoughthepossibilityofwritingseveralstatementsonasingle linecomes inhandyfromtime to time. Exercise2.5:BooleanExpression—Even orOdd Number? Letxbean integer.Use the modulooperatorand suggest a booleanexpression that can be used to check whether that integer is an even number (or odd, since if not even, the integer isodd).Thencheckyoursuggestion interactively,e.g., forxbeing 1,2,3and4. Exercise2.6:PlottingArrayData Assumefourmembersofa familywithheights1.60m,1.85m,1.75m,and1.80m. Nextdoor,thereisanotherfamily,alsowithfourmembers.Theirheightsare0.50m, 0.70m,1.90m,and1.75m. a) Writeaprogramthatcreatesasingleplotwithall theheightsfrombothfamilies. In your code, use thezeros function fromnumpy to generate three arrays, two for keeping the heights from each family and one for “numbering” of family members (e.g., as 1, 2, 3 and 4). Let the code overwrite the zeros in the arrays with the heightsgiven,usingappropriateassignment statements.Let the heights bepresentedas two continuouscurves in redandblue (solid lines), respectively, when theheightsofeach familyareplottedagainst familymembernumber.Use also theaxis functionfor theplot, so that theaxiswith familymembernumbers run from0to5,while theaxiswith heightscover0 to2. b) Suggest a minor modification of your code, so that only data points are plotted. Also, confirmthat yoursuggestionworksasplanned. Filename:plot_heights.py. Exercise2.7:SwitchingValues In an interactive session, use linspace from numpy to generate an array xwith the numbers 1.0, 2.0 and 3.0. Then, switch the content ofx[0] and x[1], before checkingx to see thatyourswitchingworkedasplanned. Filename:switching_values.py.
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