1026 Final

Cards (432)

  • Variables
    Named storage location in a computer program
  • Variables
    • There are many different types of variables, each type used to store different things
    • You 'define' a variable by telling the compiler: what name you will use to refer to it, the initial value of the variable
  • Defining a variable
    Specify an initial value
  • Assignment statement

    Use the '=' sign to place a new value into a variable
  • The '=' sign is NOT used for comparison, it copies the value on the right side into the variable on the left side
  • Assignment syntax
    The value on the right of the '=' sign is assigned to the variable on the left
  • Types of numbers

    • Whole number (integer or int)
    • Number with fraction part (float)
    • Sequence of characters (string)
  • The data type is associated with the value, not the variable
  • Updating a variable (assigning a new value)
    The new value replaces the previous contents of the variable
  • Updating a variable (computed)
    Calculate the right hand side, store the result in the variable on the left
  • A variable can be assigned different values of different types at different places in a program
  • Number literals in Python
    • Integer
    • Floating point
    • Exponential notation
    • Imaginary numbers
  • Naming variables
    • Variable names must start with a letter or '_', continue with letters, digits or '_', no other symbols or spaces, use camelCase, don't use reserved Python words
  • You must define a variable before you use it
  • Constants
    Variables whose value should not be changed after initial assignment, use UPPER_CASE naming convention
  • Python will let you change the value of a constant, but you shouldn't do it
  • Arithmetic operations
    • Addition (+), Subtraction (-), Multiplication (*), Division (/), Exponents (**)
  • If you mix integer and floating-point values in an expression, the result is a floating-point value
  • Floor division
    The '//' operator computes the quotient and discards the fractional part
  • Modulus
    The '%' operator calculates the remainder of dividing two integers
  • Calling functions
    Provide the correct number of arguments when calling a function
  • Most functions return a value
  • Integer Division
    Calculates the number of whole units (e.g. dollars)
  • Handy to use integer division and remainder for making change
  • Function
    A collection of programming instructions that carry out a particular task
  • When calling a function you must provide the correct number of arguments, otherwise the program will generate an error
  • Functions that return a value
    The function completes its task and passes a value back to the point where the function was called
  • Built-in functions
    Functions that are defined as part of the Python language and can be used without importing any modules
  • Built-in mathematical functions
    • sqrt(), sin(), cos(), etc.
  • Python libraries (modules)
    Collections of code written and compiled by others, ready for use in your program
  • Use functions from the Math module
    1. Import the module
    2. Call the function
  • Floating point values are not exact due to limitations of binary representation
  • Roundoff errors can be dealt with by rounding to the nearest integer or displaying a fixed number of digits after the decimal
  • Unbalanced parentheses
    At any point in an expression the count of "(" must be greater than or equal to the count of ")", and at the end of the expression the two counts must be the same
  • Using spaces in expressions makes them easier to read
  • There are different ways to import modules in Python
  • String
    A sequence of characters
  • if statement
    Allows a program to carry out different actions depending on the nature of the data to be processed
  • String length

    The number of characters in a string
  • Relational operators
    • Used to compare numbers and Strings
    • Strings are compared in lexicographic order