Skip to main content
Mathematics LibreTexts

1.4: Logic and Loops

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

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Logic Operations

    We will often need to determine whether two numbers are the equal, whether one is greater, lesser, etc. To do this, we use logic operations. Here are some basic logic operations.

    Equality:

    print(2 == 3)
    

    output: False

    print(2 == 2)
    

    output: True

    We can check if a number is even by using the modulo operation:

    print(53 % 2 == 0)
    

    output: False

    Inequality:

    print(2 <= 3)
    

    output: True

    print(2 > 3)
    

    output: False

    An exclamation point with an equals means "Not Equal". It will return True if the quantities are not equal. It will return False if the quantities are equal.

    print(2 != 3)
    

    output: True

    And/Or/Not:

    The and operation will return true if and only if both statements return true.

    print((2 == 2) and (2 < 3))
    

    output: True

    The following will return False, since it is False that "2 does NOT equal 2" AND "3 equals 3". The statements are not both true.

    print((2 != 2) and (3 == 3))
    

    output: False

    The or operation will return true if at least one of the statements return true.

    The following will return True, since at least one of them is true.

    print((2 != 2) or (3 == 3))
    

    output: True

    If/Else

    Sometimes we wish to take an action only if a certain statement is true. To do this, we use If statements. The following snippet of code checks whether the number 37 is even or not. If it is, we print "37 is even". Otherwise, we print "37 is odd".

    if (37 % 2 == 0):
      print("37 is even")
    else:
      print("37 is odd")
    

    output: odd

    For Loops

    Sometimes we wish to perform the same operations to several numbers or objects in a row. One way to do this is with for loops. The following code defines an array, and then prints each number one by one with the message "is a cool number!" after each.

    import numpy as np
    
    coolNumbers=np.array([2,3,5,8,9,14])
    for x in coolNumbers:
      print(x, " is a cool number!")
    

    output: 2 is a cool number!
    3 is a cool number!
    5 is a cool number!
    8 is a cool number!
    9 is a cool number!
    14 is a cool number!

    We can combine the program from the if statement section to check if several numbers are even.

    import numpy as np
    
    coolNumbers=np.array([2,3,5,8,9,14])
    for x in coolNumbers:
      if (x % 2 == 0):
        print(x," is even")
      else:
        print(x," is odd")
    

    output: 2 is even
    3 is odd
    5 is odd
    8 is even
    9 is odd
    14 is even

    If we wish to perform the above on every number from 1 to 7, we can use Python's range function. The range function has syntax range(start,stop,step), and returns all integers in \([start,stop)\) and counts the numbers up by step. Note that the number entered for stop is not included.

    import numpy as np
    
    for x in range(1,8,1):
      if (x % 2 == 0):
        print(x," is even")
      else:
        print(x," is odd")
    

    output: 1 is odd
    2 is even
    3 is odd
    4 is even
    5 is odd
    6 is even
    7 is odd

    While loops

    Another way to construct loops is with the While construct. Instead of going through a list, While loops continues until certain conditions are met. For example, if we want to keep counting through numbers until we get to 20, we can use the following.

    n=1
    while n<=8:
      print(n) 
      n=n+1
    

    output: 1
    2
    3
    4
    5
    6
    7
    8


    1.4: Logic and Loops is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?