ENCMP 100 - COMPUTER PROGRAMMING FOR ENGINEERS Final Review

Part 1: Programming Basics

  • Input and Output Operations

    • Input: Python uses the built-in input() function to accept input from the user via the keyboard. It reads a line of text and returns it as a string.
    • Output: Data is displayed on the screen using the print() function.
    • The sep and end parameters customize the separator between arguments and the ending character, respectively (default uses space as a separator and end="\n" for new line).
  • Operators

    • Arithmetic Operators: (+, -, *, /, %, **) - Highest precedence
    • Bitwise Operators: (&, |, ^, ~, <
    • Comparison Operators: (==, !=,
    • Logical Operators: (and, or, not)
    • Assignment Operators: (=, +=, -=, *=, /=) - Lowest precedence
  • Built-in Mathematical Functions

    • Python has built-in functions that do not require importing any modules, including basic mathematical operations.
  • Importing Modules

    • Entire Module: import math (e.g., math.sqrt(16) yields 4)
    • Specific Functions: from math import sqrt allows direct use without math. prefix.
    • Import Everything: from math import * brings all functions into the current namespace.
    • Import with Alias: import math as m to use m.sqrt() instead of math.sqrt().
  • Functions from the Math Module

    • sqrt(x): Square root of x (x > 0).
    • trunc(x): Truncates value to integer.
    • cos(x): Cosine value in radians.
    • sin(x): Sine value in radians.
    • Other functions include tan(x), exp(x), degrees(x), radians(x), log(x), log(x, base).
  • NumPy Overview

    • NumPy is essential for scientific computing in Python.
    • Key Functions:
    • array(): Creates array from an iterable.
    • arange(): Returns evenly spaced values.
    • shape(), zeros(), full(), random(), cumsum(), reshape(), copy(), dot().
  • NumPy Array Creation

    • numpy.array(object): Converts iterable or other arrays into a NumPy array.
  • NumPy Functions

    • numpy.arange(start, stop, step): Generates values similar to Python's range with specified start, stop, and step.
    • shape returns array dimensions.
    • zeros() creates an array filled with zeros, while ones() creates one filled with ones.
    • full(shape, fill_value): Returns a filled array of a specified shape.
    • Random Functions: random.rand(), random.randint(), random.random() to generate random values.
  • Cumulative Sum:

    • numpy.cumsum(a, axis=None): Computes cumulative sum along specified axis.
  • Array Reshaping

    • numpy.reshape(a, newshape): Changes an array’s shape without altering data.
  • Array Copying

    • numpy.copy(a): Creates an independent copy of an array.
  • Dot Product

    • numpy.dot(a, b): Computes the dot product of two arrays (including scalar multiplication, vector, and matrix multiplication).
  • Matplotlib Basics

    • For data visualization, methods include:
    • plot(), bar(), pie(), axis(), show(), subplot(), grid(), and others for customizing plots.
  • Basic Data Types

    • Integer, Float, String, Boolean:
    • Integers: Whole numbers, useful for counting.
    • Floats: Used for decimals and precise values.
    • Strings: Handle text data.
    • Booleans: Control flow in decision-making.
  • Function Variants

    • Conversions:
    • int(x), float(x), str(x), etc.
    • Formatting Symbols for output:
    • %s, %d, %f, %w.nf, etc.
  • Boolean Operations

    • Values: True and False, often results from relational operations.
    • Conversion:
    • Can convert other types to Boolean using bool(). Non-empty or non-zero values return True.
  • Entry-wise Operations

    • Conducts operations on each element of an array individually.

Part 2: Selection and Repetition

  • Control Flow

    • if…elif…else statement structure to control program flow based on conditions.
    • Nested if statements allow deeper condition checking.
    • New match-case structure in Python 3.10 allows pattern matching.
  • Loop Constructs

    • for Loop: Iterates over a sequence.
    • range(start, stop, step) specifies the range.
    • while Loop: Executes as long as a condition is true.
    • Loops can include else clauses executed when no break occurs.
  • Jump Statements

    • break: Terminates the current loop.
    • continue: Skips to the next iteration.
    • pass: A placeholder that does nothing but maintains syntactic integrity.
  • NumPy Efficiency

    • Vectorization vs. Looping: Vectorization applies operations to entire arrays at once, improving performance.
    • Broadcasting allows different shaped arrays to cooperate in element-wise operations under certain conditions.

Part 3: Functions and Structures

  • Functions

    • Define with def, taking parameters if needed.
    • Call with function_name(arguments).
  • Parameters:

    • Positional (fixed order) and Keyword (can change order).
    • Variable Scope:
    • Local vs. Global variables, controlled with the global keyword.
  • Data Types

    • Lists, Tuples, Sets, Dictionaries utilize various methods for manipulation such as appending, inserting, and counting.

Part 4: Text and File Processing

  • File Operations
    • Opening Files: Utilize modes: