The Way Of The Program
A program is a sequence of instructions that specifies how to perform a computation. This can range from mathematical calculations to symbolic processing like text replacement or program compilation.
🔑 Basic Instructions in Every Language
Input: Get data from the keyboard, a file, or other devices.
Output: Display data on the screen or send it to a file/device.
Math: Perform basic mathematical operations like addition and multiplication.
Conditional Execution: Check for specific conditions and execute appropriate statements.
Repetition: Perform actions repeatedly, usually with variation.
🗣️ Natural vs. Formal Languages
Natural languages are spoken by people (English, Spanish, French). They evolved naturally over time.
Formal languages are designed by people for specific applications (Mathematics, Chemistry, Programming).
Key Differences
Ambiguity: Natural languages are full of it; formal languages are designed to be nearly or completely unambiguous.
Redundancy: Natural languages use redundancy to reduce misunderstandings; formal languages are concise.
Literalness: Natural languages use idiom and metaphor; formal languages mean exactly what they say.
Python is an interpreted language executed by an interpreter in two ways: interactive mode and script mode.
⚠️ Types of Errors (Debugging)
1. Syntax Errors
Refers to the structure and rules of the program. Python can only run if the syntax is 100% correct.
Example:
(1 + 2)is legal, but1 + 2)is a syntax error.
2. Runtime Errors (Exceptions)
These do not appear until the program starts running. They indicate that something "exceptional" and bad occurred during execution.
3. Semantic Errors
The program runs without error messages, but it does not do what you intended. It does what you told it to do, but the meaning (semantics) is wrong.
📏 Python Code Layout Requirements (PEP 8)
Indentation: Use 4 spaces per level. Avoid mixing tabs and spaces.
Line Length: Limit to 79 characters for code, 72 for docstrings/comments.
Binary Operators: Breaking before a binary operator is suggested for readability (Knuth's style).
# Yes: easy to match operators with operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
🛠️ Algorithm Representation
✅ Flowcharts: Graphical symbols showing the flow of steps.
✅ Natural Language: Everyday language (can be ambiguous).
✅ Pseudocode: Structured, human-readable logic that bridges the gap between flowcharts and real code.
🐍 Python Types and Variables
Basic Types
String: A sequence of characters. Immutable.
Integer: Whole numbers.
Boolean:
True(1) orFalse(0).Tuples: A comma-separated sequence of values. Immutable.
Lists: A mutable sequence of values defined by
[].
Variable Naming Rules
Must begin with a letter or underscore (
_).Should be lowercase with underscores (
snake_case).Cannot use constants (e.g.,
5 = xis invalid).
🔢 Operators & Priority
Arithmetic:
+,-,*,/,%(modulo),//(floor division).Comparison:
==,!=,>,<,>=,<=.Logical:
and,or,not.Assignment:
=,+=,-=,*=, etc.
# Example of List Mutation numbers = [17, 123] numbers[1] = 5 print(numbers) # Output: [17, 5]
📖 Glossary
Problem solving: Formulating a problem and finding a solution.
High-level language: Easy for humans (Python).
Low-level language: Easy for computers (Machine code).
Source code: Program in high-level language before processing.
Token: Basic element of program structure.
Parse: To analyze the syntactic structure of code.
Next Steps: Would you like me to create a summary table comparing Lists and Tuples for a quick-reference guide?