Skip to main content
Engineering LibreTexts

2: Variables, Expressions, and Statements

  • Page ID
    122294
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

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

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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}\)

    • 2.1: Values and Types
      This page explains fundamental programming values, including integers (e.g., 2), strings (e.g., "Hello, World!"), and floats (decimal numbers). It notes how the Python interpreter identifies value types such as str and int. It also highlights common mistakes, such as using commas in large integers, which can lead to errors since Python treats them as separate entities rather than one value.
    • 2.2: Variables
      This page explains variables in programming, emphasizing their function as names for values. It outlines how to create variables with assignment statements, offers examples with strings, integers, and floats, and demonstrates displaying values through print statements. It also notes that a variable's type is determined by the value it contains.
    • 2.3: Variable names and Keywords
      This page discusses the rules for naming variables in Python, emphasizing that variable names must start with a letter, can include letters and numbers, and may contain underscores. It highlights the importance of avoiding names that start with numbers, contain invalid characters, or use reserved keywords. There are 33 reserved keywords in Python that cannot be used, and maintaining a list of these keywords can assist in preventing syntax errors related to illegal names.
    • 2.4: Statements
      This page explains Python statements, which are executable code units like expression and assignment statements. In interactive mode, the interpreter runs statements immediately, showing results as needed. Scripts consist of multiple statements executed sequentially. For example, while the assignment "x = 2" produces no output, "print" commands do display results.
    • 2.5: Operators and Operands
      This page explains operators and operands in computations, highlighting common arithmetic operators like +, -, *, and /. It notes the key difference in the division operator between Python 2.x and 3.x, where Python 3.x produces floating point results, contrasting with Python 2.x's integer truncation. To achieve consistent integer division in Python 3.x, the floored division operator (//) is recommended, aligning with standard calculator functions.
    • 2.6: Expressions
      This page explains that an expression in Python is a mix of values, variables, and operators, which can be simple or complex. In interactive mode, the Python interpreter evaluates and displays the result, while in scripts, writing an expression doesn't automatically execute it, which can confuse beginners. It suggests an exercise for users to input specific statements in the interpreter to see their outcomes.
    • 2.7: Order of Operations
      This page explains that Python's order of evaluation for operators aligns with the PEMDAS convention, where parentheses take the highest precedence. Exponentiation comes next, followed by multiplication and division, which are prioritized over addition and subtraction, sharing equal precedence. Operators of the same precedence are evaluated left to right, and the use of parentheses is recommended to ensure the correct computation order.
    • 2.8: Modulus Operator
      This page explains the modulus operator (%) in Python, which computes the remainder of division between two integers. It is useful for checking divisibility (if x % y is zero, x is divisible by y) and for retrieving the last digits of a number (x % 10 returns the last digit and x % 100 returns the last two digits).
    • 2.9: String Operations
      This page explains that in programming, the + operator concatenates strings instead of performing addition. For example, adding two strings like '100' and '150' results in '100150', whereas adding two integers yields a numerical result.
    • 2.10: Asking the user for input
      This page explains the use of Python's `input()` function for capturing user input, detailing the process of displaying prompts and converting input to integers. It warns that an error will occur if the input is not a valid number and indicates that error handling will be addressed in further discussions.
    • 2.11: Comments
      This page discusses the importance of comments in Python programming for clarifying complex code. It emphasizes that comments should be informative, non-redundant, and provide context, especially for non-obvious features. While good variable names can reduce the need for comments, achieving a balance between clear naming and code readability is essential.
    • 2.12: Choosing Mnemonic Variable Names
      This page emphasizes the significance of variable naming in programming, especially in Python. It notes that while programmers can choose variable names freely, using mnemonic names improves readability and understanding. It warns that beginners might confuse descriptive names with reserved words, which can create confusion.
    • 2.13: Debugging
      This page outlines common Python errors: syntax errors (illegal variable names/spaces), runtime errors (using undefined variables or case sensitivity in names), and semantic errors (incorrect operation order leading to wrong results without error messages).
    • 2.E: Variables, Expressions, and Statements (Exercises)
      This page presents Python programming exercises, including welcoming users by name, calculating gross pay based on hours worked and hourly rate, evaluating expressions with given variables, and converting Celsius to Fahrenheit. It also discusses historical changes in Python and provides mnemonic resources.
    • 2.G: Glossary
      This page defines key programming concepts like variables, data types, operators, and statements. It details fundamental terms such as assignment and concatenation, along with data types including integers and strings. The text explores operands, the modulus operator, mnemonic variable names, and the rules of precedence for evaluating expressions.


    This page titled 2: Variables, Expressions, and Statements is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform.