Skip to main content

Part of the book series: Undergraduate Lecture Notes in Physics ((ULNP))

  • 3013 Accesses

Abstract

This chapter explains basic programming concepts. After an overview of common Python distributions, we show how to use Python as a simple calculator. As a first step toward programming, variables and expressions are introduced. The arithmetic series and Fibonacci numbers illustrate the concepts of iteration and branching. We conclude this chapter with a program for the computation of a planet’s orbital velocity around the Sun, using constants and functions from libraries and giving a small glimpse at objects in Python.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 44.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 59.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    How to execute Python code depends on the software you are using (consult the documentation). In a notebook, for example, all you need to do is to simultaneously press the enter and shift keys of your keyboard in the cell containing the code.

  2. 2.

    Enclosing the string in parentheses is obligatory in Python 3. You may find versions of “Hello, World!” without parentheses on the web, which work only with Python 2.

  3. 3.

    Strictly speaking, the time needed by Earth to complete one revolution around the Sun is the sidereal year,  which has about 365.256 d. One day has 86400 s.

  4. 4.

    In many programming languages, integers such as 2 are treated differently than floating point numbers. For example, using 2.0 instead of the integer 2 in a division might produce a different result. In Python 3, it is usually not necessary to make this distinction. Alas, Python 2 behaves differently in this respect.

  5. 5.

    The concept of a variable in Python is different from variables in programming languages such as C, where variables have a fixed data type and can be declared without assigning a value. Basically, a variable in C is a placeholder in memory whose size is determined by the data type. Python variables are objects that are much more versatile.

  6. 6.

    In interactive Python, just writing the variable name in the final line of a cell would also result in its value being displayed in the output.

  7. 7.

    Here it is assumed that the mass of the planet is negligible compared to the mass of the Sun. For the general formulation of Kepler’s third law see Sect. 2.2.

  8. 8.

    See mars.jpl.nasa.gov/msp98/orbiter/.

  9. 9.

    The term algorithm derives from the astronomer and mathematician al-Khwarizmi whose name was transcribed to Algoritmi in Latin (cf. [2] if you are interested in the historical background). al-Khwarizmi worked at the House of Wisdom, a famous library in Bagdad in the early 9th century. Not only was he the founder of the branch of mathematics that became later known as algebra, he also introduced the decimal system including the digit 0 in a book which was preserved until the modern era only in a Latin translation under the title Algoritmi de numero Indorum (this refers to the origin of the number zero in India). The digit 0 is quintessential to the binary system used on all modern computers.

  10. 10.

    There is a reason for the stop value being excluded. The default start value is 0 and simply spans the n integers \(0,1,2,\ldots ,n-1\).

  11. 11.

    German mathematician, physicist, and astronomer who is known for the Gauss theorem, the normal distribution, and many other import contributions to algebra, number theory, and geometry.

  12. 12.

    Named after Leonardo de Pisa, also known as Fibonacci, who introduced the sequence to European mathematics in the early 13th century. However, the Fibonacci sequence was already known to ancient Greeks. It was used to describe growth processes and there is a remarkable relation to the golden ratio.

  13. 13.

    As you can see from Fig. 1.2, \(F_{11}\) is computed as final value of . But it is not used. You can try to modify the program such that only 9 iterations are needed to print the Fibonacci sequence up to \(F_{10}\).

  14. 14.

    This feature was introduced with Python 3.6.

  15. 15.

    Another application in our Fibonacci program would be the merging of lines 13 and 14 into the multiple assignment .

  16. 16.

    See, for example, packaging.python.org/tutorials/installing-packages

    and docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html.

  17. 17.

    Since strings are immutable objects, the method does not change the original string with placeholders. It creates a new string object with the formatted numbers inserted.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Wolfram Schmidt .

Rights and permissions

Reprints and permissions

Copyright information

© 2021 Springer Nature Switzerland AG

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Schmidt, W., Völschow, M. (2021). Python Basics. In: Numerical Python in Astronomy and Astrophysics. Undergraduate Lecture Notes in Physics. Springer, Cham. https://doi.org/10.1007/978-3-030-70347-9_1

Download citation

Publish with us

Policies and ethics