The first programming encounter: a formula
Using a program as a calculator
About programs and programming
Tools for writing programs
Writing and running your first Python program
Warning about typing program text
Verifying the result
Using variables
Names of variables
Reserved words in Python
Comments
Formatting text and numbers
Computer science glossary
Another formula: Celsius-Fahrenheit conversion
Potential error: integer division
Objects in Python
Avoiding integer division
Arithmetic operators and precedence
Evaluating standard mathematical functions
Example: Using the square root function
Example: Computing with \( \sinh x \)
A first glimpse of rounding errors
Interactive computing
Using the Python shell
Type conversion
IPython
Complex numbers
Complex arithmetics in Python
Complex functions in Python
Unified treatment of complex and real functions
Symbolic computing
Basic differentiation and integration
Equation solving
Taylor series and more
Summary
Chapter topics
Example: Trajectory of a ball
About typesetting conventions in this book
Exercises
Exercise 1: Compute 1+1
Exercise 2: Write a Hello World program
Exercise 3: Derive and compute a formula
Exercise 4: Convert from meters to British length units
Exercise 5: Compute the mass of various substances
Exercise 6: Compute the growth of money in a bank
Exercise 7: Find error(s) in a program
Exercise 8: Type in program text
Exercise 9: Type in programs and debug them
Exercise 10: Evaluate a Gaussian function
Exercise 11: Compute the air resistance on a football
Exercise 12: How to cook the perfect egg
Exercise 13: Derive the trajectory of a ball
Exercise 14: Find errors in the coding of formulas
Exercise 15: Explain why a program does not work
Exercise 16: Find errors in Python statements
Exercise 17: Find errors in the coding of a formula
Exercise 18: Find errors in a program
References
Our first examples on computer programming involve programs that evaluate mathematical formulas. You will learn how to write and run a Python program, how to work with variables, how to compute with mathematical functions such as \( e^x \) and \( \sin x \), and how to use Python for interactive calculations.
We assume that you are somewhat familiar with computers so that you know what files and folders are (another frequent word for folder is directory), how you move between folders, how you change file and folder names, and how you write text and save it in a file.
All the program examples associated with this document can be
downloaded as a tarfile or zipfile from the web page http://hplgit.github.com/scipro-primer. I strongly recommend you to
visit this page, download and pack out the files. The examples are
organized in a folder tree with src
as root. Each subfolder
corresponds to a particular chapter. For example, the subfolder
formulas
contains the program examples associated with this first
chapter. The relevant subfolder name is listed at the beginning of
every chapter.
The folder structure with example programs can also be directly
accessed in a GitHub repository on the web.
You can click on the formulas
folder to see all the examples from
the present chapter. Clicking on a filename shows a nicely
typeset version of the file. The file can be downloaded by first
clicking Raw to get the plain text version of the file,
and then right-clicking in the web page and choosing Save As....
This chapter is taken from the book A Primer on Scientific Programming with Python by H. P. Langtangen, 5th edition, Springer, 2016.