Skip to main content
Mathematics LibreTexts

1.2: Python Basics

  • Page ID
    122054
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Computations

    Let's get started with some basic computations.

    The following code will produce no output:

    2+3
    

    output:

    This is because here, you are asking Python to add 2 and 3 together, but you aren't specifying what to do with the output. Instead, we will add a print command, so that Python knows that we want to actually see what the computation yields.

    
    print(2+3)
    
    

    output: 5

    Note that, unlike our Hello World! program from the previous section, we do not include quotes here. That's because we do not want to literally print the string "2+3", but instead we want Python to add the numbers together and then display the result.

    Most arithmetic is intuitive, though exponents are a bit different in Python than in other languages. For example, to calculate \(3^7\), we use two asterisks **:

    
    1  print(3**7)
    
    

    output: 2187

    Modulo Operator

    Modulo in python is performed using the percent symbol %. The modulus of two numbers is the remainder after dividing the two numbers. For example,

    
    1  print(7 % 2)
    
    

    output: 1

    As can be seen above, 7 mod 2 = 1 since \(7\div 2 = \) 3 with a remainder of 1.

    Math Package

    There are several mathematical functions that python does not have built in, so we must "import" the package. Here, we will import all functions (the wildcard *) from the math package. This gives us access to both the sin function and pi. We use them here.

    from math import *
    print(sin(pi/4))
    

    output: 0.7071067811865475

    One way to learn about a function is to search for it online in the Python documentation https://docs.python.org/3/. For example, the syntax for the logarithm function in the
    math package is log(x[; b]) where b is the optional base. If b is not provided, the natural logarithm of x (to base e) is computed.

    from math import *
    print(log(4,2))
    

    output: 2.0

    NumPy arrays

    NumPy is a useful Python package for array data structure, random number generation, linear algebra algorithms, and so on. A NumPy array is a data structure that can be used to represent vectors and matrices, for which the computations are also made easier. Import the NumPy package and define an alias (np) for it.

    import numpy as np
    

    Importing a package like this means that you will need to use the prefix np. for any functions. For example, here is the basic syntax to create a 1-dimensional NumPy array using the array function from the NumPy package.

    import numpy as np
    x=np.array([10,20,30])
    print(x.dtype)
    

    output: [10 20 30]

    Note that Python automatically chooses the variable types. For example, we can check that the variable types for the array about are integers of 64 bits.

    import numpy as np
    x=np.array([10,20,30])
    print(x.dtype)
    

    output: int64

    If we input a real number that is not an integer, Python will change the type accordingly:

    import numpy as np
    x=np.array([10,20,30,0.1])
    print(x.dtype)
    

    output: float64

    Here, float64 means the variable type is a 64-bit floating point number. Floating point numbers are decimal values with an undefined number of digits before and after the decimal point (so the decimal point can "float" as needed).

    A 1D NumPy array does not assume a particular row or column arrangement of the data, and hence taking transpose for a 1D NumPy array is not valid. Here is another way to construct a 1D array, and some array operations:

    import numpy as np
    x = np.array([10*i for i in range(1, 6)])
    print(x)
    

    output: [10 20 30 40 50]

    If we wish to print a certain element of an array, we use the bracket notation x[index], where the element in the first position is index 0.

    import numpy as np
    x = np.array([10*i for i in range(1, 6)])
    print(x[2])
    

    output: 30

    For the last entry, we use the index -1. Note that we omit the first two lines for the following examples.

    ...
    print(x[-1])
    

    output: 50

    To find the minimum entry:

    ...
    print(min(x))
    

    output: 10

    To add them together:

    ...
    print(np.sum(x))
    

    output: 150

    To append a new entry:

    import numpy as np
    x = np.array([10*i for i in range(1, 6)])
    print(x)
    x=np.append(x,99)
    print(x)
    

    output: [10 20 30 40 50]
    [10 20 30 40 50 99]

    To find the number of entries:

    import numpy as np
    x = np.array([10*i for i in range(1, 6)])
    print(x.size)
    

    output: 6

    The NumPy package has a wide range of mathematical functions such as sin, log, etc., which can be applied elementwise to an array:

    import numpy as np
    x = np.array([1,2,3])
    x = np.sin(x)
    print(x)
    

    output: [0.84147098 0.90929743 0.14112001]


    1.2: Python Basics is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?