Seite - 28 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 28 -
Text der Seite - 28 -
28 1 TheFirstFewSteps
Exercise1.7: Peculiarresults fromdivision
Consider the following interactivePythonsession:
In [1]: x=2; y=4
In [2]: x/y
Out[2]: 0
What is theproblemandhowcanyoufix it?
Exercise1.8:Updatevariableatcommandprompt
InvokePython interactivelyandperformthe followingsteps.
1. Initializeavariablex to2.
2. Add3 tox. Printout the result.
3. Print out the result ofx + 1*2and(x+1)*2. (Observehowparenthesesmake
adifference).
4. Whatvariable type isx?
Exercise1.9: Formattedprint toscreen
Write a program that defines two variables as x = pi and y = 2. Then let the
program compute the product z of these two variables and print the result to the
screenas
Multiplying 3.14159 and 2 gives 6.283
Filename:formatted_print.py.
Exercise1.10:Pythondocumentationandrandomnumbers
Writeaprogramthatprintsfourrandomnumberstothescreen.Thenumbersshould
bedrawnfromauniformdistributionoverthe interval Œ0;10/ (0inclusive,10exclu-
sive). Find the informationneededfor the task, see forexamplehttp://docs.python.
org.
Hint Pythonhasamodulerandom that containsa functionby thenameuniform.
Filename:drawing_random_numbers.py.
OpenAccess This chapter is distributedunder the termsof theCreativeCommonsAttribution-
NonCommercial 4.0 International License (http://creativecommons.org/licenses/by-nc/4.0/),
which permits any noncommercial use, duplication, adaptation, distribution and reproduction
in anymediumor format, as long as you give appropriate credit to the original author(s) and the
source, a link isprovided to theCreativeCommons licenseandanychangesmadeare indicated.
The images or other third partymaterial in this chapter are included in the work’s Creative
Commons license, unless indicated otherwise in the credit line; if suchmaterial is not included
in thework’s Creative Commons license and the respective action is not permitted by statutory
regulation, users will need to obtain permission from the license holder to duplicate, adapt or
reproduce thematerial.
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