Skip to main content
Physics LibreTexts

28.1: A quick intro to programming

  • Page ID
    19588
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In Python, as in other programming languages, the equal sign is called the assignment operator. Its role is to assign the value on its right to the variable on its left. The following code does the following:

    • assigns the value of 2 to the variable a
    • assigns the values of 2*a to the variable b
    • prints out the value of the variable b
    python code \(\PageIndex{1}\)

    Declaring variables in Python

    #This is a comment, and is ignored by Python
    a = 2
    b = 2*a
    print(b)

    Output

    4

    Note that any text that follows a pound sign (#) is intended as a comment and will be ignored by Python. Inserting comments in your code is very important for being able to understand your computer program in the future or if you are sharing your code with someone who would like to understand it. In the above example, we called the print() function and passed to it the variable b as an argument; this allowed us to print (display) the value of the variable b and verify that it was indeed equal to the number \(4\).

    In Python, if you want to have access to “functions”, which are a more complex series of operations, then you typically need to load the module that defines those operations.

    A large number of functions are provided in Python. Most of these functions need to be “imported” from “modules”. For example, if you want to be able to take the square root of a number, then you need to load (import) the “math module” which contains the square root function, as in the following example:

    python code \(\PageIndex{1}\)

    Using functions from modules

    #First, we load (import) the math module
    import math as m
    a = 9
    b = m.sqrt(a)
    print (b)
    

    Output

    3

    In the above code, we loaded the math module (and renamed it m); this then allows us to use the functions that are part of that module, including the square root function (m.sqrt()).


    This page titled 28.1: A quick intro to programming is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Ryan D. Martin, Emma Neary, Joshua Rinaldo, and Olivia Woodman via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.