Comprehensive Python Programming and Programming Fundamentals Study Guide
Python Data Structures: Strings
- Definition and Composition: A string is defined as a sequence consisting of 0 or more characters. In Python, these can be enclosed in single or double quotes.
- Immutability of Strings: Strings in Python are immutable data structures. This means that once a string object has been created, its contents cannot be altered. To change a string, one must create an entirely new string object and assign it to the variable.
- String Structure and Indexing:
- The index of the first character in a Python string is always 0.
- Accessing an index equal to the string's length (e.g., s[extlen(s)]) results in an error because indices go from 0 to extlen(s)−1. The specific error is an "index out of range" error.
- Negative indexing allows access from the end of the string, with −1 representing the last character.
- String Methods and Functions:
-
extlen(): Calculates the length of a string based on the total number of characters.
- extisnumeric(): Returns extTrue if the string contains only digits and extFalse otherwise (this excludes Roman numerals or fractions in some contexts).
- extisalpha(): Returns extTrue if the string contains only letters and extFalse otherwise.
- exts.find("substring"): Locates the lowest (starting) index where the specified substring appears within the string s.
- extin Operator: Used to check for the existence of a substring. The left operand is the target substring being searched for, and the right operand is the string being searched. (e.g., ext"apple"in"pineapple" returns extTrue).
Control Flow: Selection Statements
- General Purpose: Selection statements allow a computer to make choices and selectively execute blocks of code based on specific test conditions.
- Types of Selection:
- One-Way Selection: A single
extif statement without an extelse block. It tests a condition, executes the internal statements if the condition is extTrue, and proceeds to the next statement in the program text regardless.
- Multiway Selection: Used when there are more than two paths. A prime logical scenario for multiway selection is converting numeric grades (e.g., 95) into letter grades (e.g., "A", "B", "C"). - Conditions: A condition in a selection statement expresses a hypothesis about the state of its world at that specific point in time.
- Indentation Rules: In Python, the minimum number of spaces required for a statement under an
extif or extelse clause is one space, though standard style guides (PEP 8) recommend four spaces.
Control Flow: Iteration and Loops
- Functionality: Loops allow the repetition of code blocks. They do not themselves make choices; selection statements within or around them handle the logic of choices.
- Types of Loops:
- Count-Controlled Loop: A loop that counts through a specific range of numbers (typically using the
extrange() function).
- Conditional Iteration / While Loop: Often called an "entry-control" loop because the condition is checked at the entry point before the loop body executes. - Sentinel Values: A sentinel value (also known as a flag, trip, or signal value) is a special value provided by the user or the program to terminate a conditional loop (like a
extwhile loop). - Loop Termination: The
extbreak statement is used to immediately exit a loop and proceed to the remainder of the script after the termination condition or a manual trigger is met. - While Loop Structure: Typically requires three steps: 1. Initialize a counter or variable; 2. Check the condition in the loop header; 3. Manually update/increment the variable within the body.
Python Function Mechanics and Variables
- Function Call Evaluation: When a Python function is called, the first thing that happens is that any expressions supplied as arguments are evaluated.
- Default Arguments: If a function is called without providing values for parameters that have default arguments, Python automatically assigns the specified default values to them.
- Prototype: In programming, a prototype is defined as a rough draft of a program or a partial implementation used for testing concepts.
- Scope: Scope describes the area of the program text in which a name (variable) refers to a given value. This is distinct from "lifetime," which refers to how long a variable exists in memory.
- Variable Creation:
- Module Variables: Module-level (global) variables come into existence when they are introduced via an assignment statement outside of functions.
- Modification: When a function attempts to change a module-level variable without the
extglobal keyword, Python creates a temporary local variable with the same name that exists only within the function's scope. The module-level variable remains unchanged.
File Handling and the OS Module
- Reading Files: When reading text from a file, once the end-of-file (EOF) is reached, subsequent calls to the
extread() method return an empty string (""). - File Modes: Opening a file with the mode string
ext′r′ indicates that the file is being opened for read access only. - Writing Files: The file method
extwrite() expects a single string as its argument. - Metadata and OS Module:
-
extos.remove(path): This function removes the specific file passed to it.
- extos.path: Used for retrieving specific file metadata, such as the size of a file in bytes. - Text Files: Basic text editors like Notepad (Windows) or TextEdit (macOS) are used to create, view, and save data in plain text format.
Operators and Logic
- Logical Operators: Python uses
extand, extor, and extnot. The operator extxor is not a standard logical/Boolean operator in the base Python language in the same sense as the others. - Comparison Operators:
-
ext!=: Tests if an expression does NOT equal another.
- ext==: Tests for equality. - Augmented Assignment Operators: Examples include
ext+=, ext−=, ext∗=, and ext/=. These update the value of a variable in place (e.g., extvalue+=1 increases the variable's value by 1). - Operator Logic Example: Given
x=4 and y=5, the expression (x > y) == ext{False} evaluates to extTrue because 4 > 5 is ext{False}$, and ext{False} == ext{False}</code>is<code> ext{True}.
- Format Operator: The modulo symbol
ext{%} acts as the format operator. - Field Width: Represents the total number of data characters plus any additional spaces (padding) used to display a datum.
- Format Specifiers:
-
ext{%s}: Placeholder for a string.
- ext{%d}</code>:Placeholderforaninteger(e.g.,<code> ext{"%4d" % var1}</code>requires<code> ext{var1} to be an integer).
Security and Number Systems
- Encryption Terms:
- Plain Text: The original, readable information.
- Cipher Text: The resulting unreadable text after an encryption algorithm and key have been applied.
- Encryption Algorithms:
- Caesar Cipher: A substitution cipher where each letter is shifted by a specific distance. Example: Encrypting "data" with a shift of 2resultsin"fcvc"(d
ightarrow f, a
ightarrow c, t
ightarrow v, a
ightarrow c).
- Block Cipher: Utilizes mathematical structures such as an invertible matrix to transform blocks of plaintext into ciphertext.
- Number Base Conversion: To convert a binary number to octal, one must group the bits into sets of three starting from the right (not four, which is used for hexadecimal). Each 3-bit group matches an octal digit (0-7$$).
Error Classification
- Logic Errors: An "off-by-one" error in a loop (iterating one time too many or too few) is a logic error because the code runs without crashing but produces incorrect results.
- Syntax Errors: Errors in the grammar or structure of the code that prevent it from running.
- Runtime Errors: Errors such as "index out of range" that occur while the program is executing.