Programming Fundamentals-Python Chap 1-4
High-level language vs Low level language
- high-level language is a programming language such as Python, that enables a programmer to write programs that are more or less independent of a particular type of computer. ... In contrast, assembly languages are considered low-level because they are very close to machine languages.
Operators
// integer division ○ C = A // B # divides A by B and puts the result in C as a whole (truncated) number ● % modulo, gives the remainder of a division ○ C = A % B # divides A by B and puts the remainder in C ● ** exponent ○ C = A ** B # raises A to the power B and puts the result in C
While loops
A While loop permits code to execute repeatedly until a certain condition is met. This is useful if the number of iterations required to complete a task is unknown prior to flow entering the loop.
Algorithm
A step-by-step procedure for solving a problem, by taking us from a Problem Statement to a Problem Solution that meets the following conditions: The steps are ordered. The steps are executable. The steps are unambiguous. There exists halting or reset criteria. It is repeatable and predictable in its repeatability. The exact same input always produces the exact same output.
Computational Thinking techniques
Abstraction Decomposition and Analysis Pattern Recognition Algorithm Design
Computer Science versus Programming
All computer scientists are programmers. Not all all programmers are computer scientists. (- Computer science is the study of all areas of computation, computer engineering, and software engineering, and is highly theoretical. -Programming is the implementation of one specific aspect of computer science, writing software, and is more concerned with practice than theory. -The best programmers are also good computer scientists. This is the difference between a "coder" or "developer" and a software engineer.)
Loose Coupling
An approach, interconnecting components in a system, so that those components depend on each other to the least extent practicable. Loose coupling - relies on well defined interfaces that change very little over time. - assumes you don't know, and don't want to know, what's inside other components. You only care about the interfaces. - Usually, the more loose coupling the better.
Programming Tools
At a minimum you need: Text Editor, Interpreter or Compiler. - You will also need to become familiar with the Command Prompt. ●Optionally, you may use Integrated Development Environments (IDEs) which generally include the following: ○text editor ○interpreter / compiler ○debugger ○other supporting utilities (version control, source repository, etc.) ○a user interface that ties it all together ●IDLE: Simple, free, open source IDE (not really a professional tool) ●PyCharm: Jetbrains' professional Python IDE
Compiled Language
CODE > COMPILER > Compiled versus Interpreted
Interpreted Language
CODE > INTERPRETER > Application runs anywhere there is an Interpreter
if Statement in flowchart
Diamond represents true/false condition that must be tested •Actions can be conditionally executed •Performed only when a condition is true •Single alternative decision structure: provides only one alternative path of execution •If condition is not true, exit the structure Statements must be consistently indented
if-else statement
Dual alternative decision structure : two possible paths of execution - One is taken if the condition is true, and the other if the condition is false •Syntax: if condition: statements else: other statements • if clause and else clause must be aligned Statements must be consistently indented
Python
High level language, One to many relationship. Meaning, one statement in a high level language equals many statement in a low level representation. Requires very sophisticated "translator" which in Python's case is an interpreter. (- Interpreted, Strongly, Dynamically typed, Procedural and OO. - Relatively new language & easy to learn - General purpose language that has found a niche in Data Science. - Common uses: Data Science, Mathematics, Statistics, General Purpose Scripting, General Purpose Programming, Learning.)
Variables
Hold values. Think of them as containers where you can store data. More accurately, a variable is a memory space that holds a value. ●Example: age = 25 ●The variable "age" is now equal to the value 25
Variable Types
Integers= -4, 0, 1, 4000 Floats= -104.54, 0.01, 3.14 Strings= hold character data ("hello", "3.14",) Once a variable is assigned, it's type is declared and remains the same until you re-assign it to a new type with a new declaration.
For loop
Loops that have a predetermined beginning, end, and increment (step interval).
Relational Operator
One of the operators that compares its operands: ==, !=, >, <, >=, and <=.
The "or" Operator
Only False when: false "or" false (F and F = F T and F = T F and T= T T and T = T)
The "and" Operator
Only True is when: true "and" true all others are always false (T and F = F F and T= F F and F = F)
Short circuit evaluation
Performed by the "or" and "and" operators • For or operator: If left operand is true, compound expression is true. Otherwise, evaluate right operand • For and operator: If left operand is false, compound expression is false. Otherwise, evaluate right operand
Computational Thinking (3 stages)
Problem formulation Solution expression Execution & evaluation
Procedural vs Object Oriented
Procedural ○Code is organized into procedures (functions) that do something. ○ If data is stored, it is local to the procedure and not available outside the procedure. ○ Data is stored separately and in some fashion "passed" from procedure to procedure. ○ Procedural programming is about "verbs." function do_something(x); ●Object Oriented ○Code is organized into objects that are something. ○Data stored is local to the object but is available outside the object according to the objects rules. ○The object owns and strictly controls access to its data and operations on its data. ○Object Oriented Programming (OOP) is about "nouns." class someThing( ); ●Python is both procedural and Object Oriented.
Recursive Thinking
Process of solving large problems by breaking them down into smaller, simpler problems that have identical (or very similar*) forms. (Each sub problem might then be broken down further into smaller, simpler problems that have identical (or very similar*) forms, etc. Solving the smallest breakdown of the simplest problem, then solves all previous levels of the larger problem. - It is a divide-and-conquer approach, one of the most important concepts in computer science. Example: Searching an order group of things, like in order of operations when given an if statement)
if Statement
Programming structure that implements "conditional statements". Control structure or selection structure: logical design that controls order in which set of statements execute • Sequence structure: statements executed in order they appear • Decision structure: specific action(s) performed only if condition exists
Coupling
Refers to the degree of direct knowledge that one element has of another.
Basic problem-solving techniques
Separation of interface from implementation. Interface: the conceptual form of the input and output of a system or subsystem. Implementation: the actual construction of a functioning input and output system
Static Versus Dynamically Typed
Static and Dynamic Typing is about when type information is acquired (Either at compile time or at runtime). ●Static Typing required types are bound to objects at compile time and remain fixed. ●Dynamic Typing allows types to bound to objects at runtime, allowing for implicit conversions. ●In simple terms, static/dynamic typing refers to the time when type checking occurs: compile time for static typing, and run time for dynamic languages. Likewise, strong/weak typing refers to how aggressive a language is in enforcing its type system
Strong Versus Weakly Typed
Strong versus Weak is about how strictly types are distinguished (i.e. whether the language tries to do an implicit conversion from strings to numbers). ●Strongly Typed ○Has strict typing rules at compile time. Variables (and other things) must declare their type and location prior to compiling and may not change. ○Errors and exceptions are more likely to happen during compilation. ○Most of these rules affect variable assignment, return values and function calling. ○C/C++ is strongly typed as are most compiled languages. ○Python is strongly typed. ●Weakly Typed (aka Loosely Typed) ○Has looser typing rules. Types are interpreted by implication of the statement. ○May produce unpredictable results or may perform implicit type conversion at runtime. ○Many interpreted languages are weakly typed. ○PHP is an example of a very weakly typed language
Operator
The = sign is called an assignment operator. Not equals.
Type Conversion example
a = 3.14 print(a) - 3.14 print(int(a)) - 3 print(str(a)) - 3.14
Boolean Expressions
expression tested by if statement to determine if it is true or false •Example: a > b >true if a is greater than b; >false otherwise
commenting
good practice to comment your code. ●This means adding "notes". The comment notation for Python is the # symbol
Fundamental Counting Principle
if an event can happen in N ways, and another, independent event can happen in M ways, then both events together can happen N x M ways. (Menu example: 4 different types of sandwich, 3 different types of side, 2 different types of desserts and five different types of drink. The number of meal combos possible is 4 * 3 * 2 * 5 = 120)
Tight Coupling
occurs when methods excessively depend on each other; it makes programs more prone to errors An approach, interconnecting components in a system where those depend highly on each other (they usually cannot work alone). Tight coupling -relies on system level knowledge. -Systems that are very tightly coupled are also referred to as monolithic. -assumes you know what's inside other components and how they work. -Interfaces are usually highly customized. Changing the internal workings of an element usually means changing interfaces and other elements.
Output: The print function
print('hello world') or print("hello world") #notice single quotes, or double quotes works print('hello "big" world') or print("hello 'big' world") #notice mixed quotes, works SAME quotes do not work: print("hello "big" world") or print('This is my 'first' program')
repetition structures
will repeat a block of code if a a condition is true. i.e. While / for loops