Page - 59 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 59 -
Text of the Page - 59 -
3LoopsandBranching
3.1 TheforLoop
Many computations are repetitive by nature and programming languages have
certain loopstructures to dealwith this. Onesuch loopstructure is the for loop.
3.1.1 Example:Printingthe5TimesTable
Assume the task is to print out the 5 times table. Before having learned about loop
structures inprogramming,mostofuswouldfirst thinkofcoding this like:
# Naively printing the 5 times table
print(β{:d}*5 = {:d}β.format(1, 1*5))
print(β{:d}*5 = {:d}β.format(2, 2*5))
print(β{:d}*5 = {:d}β.format(3, 3*5))
print(β{:d}*5 = {:d}β.format(4, 4*5))
print(β{:d}*5 = {:d}β.format(5, 5*5))
print(β{:d}*5 = {:d}β.format(6, 6*5))
print(β{:d}*5 = {:d}β.format(7, 7*5))
print(β{:d}*5 = {:d}β.format(8, 8*5))
print(β{:d}*5 = {:d}β.format(9, 9*5))
print(β{:d}*5 = {:d}β.format(10, 10*5))
Β©The Author(s) 2020
S.Linge, H.P. Langtangen, Programming forComputations -Python,
Texts in Computational Science and Engineering 15,
https://doi.org/10.1007/978-3-030-16877-3_3 59
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