Python Programming Fundamentals: Identifiers, Memory Management, Arithmetic Precedence, and Math Modules

Python Variable Identifiers and Naming Rules

  • Variable Naming Rules in Python:
    • A variable name is strictly not allowed to start with a numeric digit.
    • Examples of valid variable naming conventions include num_cards, number_of_cards, and number_of_students (e.g., number_of_students = 120).
    • Special Characters and Underscores:
    • Variable names can begin with underscores. Starting a variable name with a double underscore (e.g., __variable) is syntactically valid and does not raise a syntax error.
    • However, by convention, programmers avoid placing two underscores at the beginning of variable names unless utilizing specific internal Python features.
    • Hyphens:
    • Hyphens are not permitted inside variable identifiers (e.g., num-students).
    • The Python interpreter reads a hyphen as the subtraction subtraction operator (-), evaluating the statement as the variable num minus the variable students.

Print Statement Formatting and Output Control

  • Formatting Consecutive Output Lines (end Parameter):
    • By default, each print() function call outputs its contents and terminates with a newline character.
    • To display output from a second print() statement directly adjacent to the output of the first statement on the same line, the end keyword argument is used inside the first print() statement.
    • Example syntax: print("hello passengers", end=" ") followed by print("welcome aboard") displays: hello passengers welcome aboard.
    • If end="" (an empty string) is provided without spaces, the beginning character of the second string attaches directly to the end of the first string without any intervening space.
  • Formatting Separators (sep Parameter):
    • Custom characters can be inserted between multiple elements or statements by specifying separator arguments, such as placing an asterisk (*) between outputs.

Python Object Characteristics and Memory Management

  • Everything in Python is an Object:
    • Every entity in Python—including variables, numbers, strings, and data containers—is an object.
    • Variables act as reference containers pointing to objects stored in memory.
    • An object is defined by three fundamental features:
    • Name: The identifier assigned to reference the object (e.g., age, x).
    • Type: The classification of data stored in the object (e.g., integer int when holding a value like 20).
    • ID: The unique memory address identifier representing where the object resides in hardware memory.
  • Garbage Collection Mechanism:
    • When an unassigned mathematical expression is executed (e.g., print(2 + 2)), the interpreter creates a new object containing the calculated value 4 and stores it at a specific memory location (e.g., location 96).
    • Once the printed value is displayed and no variable reference holds it, the object is no longer required and is thrown away.
    • Definition of Garbage Collection: An automatic Python memory management process that frees memory space by discarding unused objects that are no longer referenced.
    • Persistent Memory Allocation:
    • When an object is explicitly assigned to a variable (e.g., x = 7 stored at memory location 98), printing x displays 7, but the object is retained in memory because the variable reference x exists.
    • Objects remain in memory for as long as they are assigned to an active reference.
    • Explicit Deletion (del Keyword):
    • An object can be manually unreferenced and flagged for immediate garbage collection using the del keyword (e.g., del x).
    • Executing del x removes the reference x. A subsequent call to print(x) results in a runtime error because the variable reference no longer exists in memory.

Object Mutability

  • Concepts of Mutability:
    • Mutable: Describes an object whose contents can be modified in place after creation.
    • Immutable: Describes an object whose value cannot be altered in place once instantiated.
  • Immutable Data Types in Python:
    • Strings: Strings are immutable. Individual character elements within a defined string cannot be reassigned or modified.
    • Integers and Floats: Numeric types are immutable. Modifying specific digits within an existing integer object (e.g., altering individual digits of x = 100) is impossible. Reassigning a variable to a new number creates a completely new object in memory rather than mutating the original object.

Python Arithmetic Operators and Operator Precedence

  • Exponentiation Operator (**):
    • Represented by two consecutive asterisks (**).
    • 3 ** 2 evaluates to 32=93^2 = 9.
    • 3 ** 3 evaluates to 33=273^3 = 27.
  • Modulo Operator (%):
    • Evaluates and returns the remainder of integer division.
    • 10 % 2 evaluates to 00 because 10=(2×5)+010 = (2 \times 5) + 0
    • 29 % 5 evaluates to 44 because 29=(5×5)+429 = (5 \times 5) + 4
    • Extracting the Rightmost Digit:
    • Evaluating any positive integer n % 10 extracts the rightmost (last) digit of n (e.g., 27%10=727 \% 10 = 7).
    • If n % 10 equals 00, the number is evenly divisible by 1010
  • Operator Precedence Rules:
    1. Parentheses (): Expressions inside parentheses are evaluated first.
    2. Multiplication (*), Division (/), and Modulo (%): Share equal precedence and are evaluated from left to right when appearing together in an expression.
    3. Addition (+) and Subtraction (-): Evaluated after multiplication, division, and modulo.

Division Types and Negative Modulo Calculations

  • Standard Division (/) vs. Floor Division (//):
    • Standard division (/) produces a floating-point number (e.g., 20/5=4.020 / 5 = 4.0, 11/2=5.511 / 2 = 5.5, and −11/2=−5.5-11 / 2 = -5.5).
    • Floor division (//) rounds the result down to the nearest lower integer to the left on the number line.
    • 11 // 2 evaluates to 55
    • -11 // 2 evaluates to −6-6 because rounding −5.5-5.5 down to the lower integer to the left yields −6-6
  • Modulo Arithmetic with Negative Numerators:
    • Mathematical relation for integer division:   Dividend=(Quotient×Divisor)+Remainder\text{Dividend} = (\text{Quotient} \times \text{Divisor}) + \text{Remainder}
    • Strict Constraint: The remainder of integer division must strictly be a non-negative integer:   Remainder≥0\text{Remainder} \ge 0
    • Positive Modulo Calculation Example (27%427 \% 4):
    • 27=(6×4)+3  ⟹  Remainder=327 = (6 \times 4) + 3 \implies \text{Remainder} = 3
    • Negative Modulo Calculation Example (−23%4-23 \% 4):
    • If Quotient is assumed to be −6-6: −23=(−6×4)+(−3)-23 = (-6 \times 4) + (-3). This gives a remainder of −3-3, which violates the non-negative requirement.
    • If Quotient is chosen as −7-7: −23=(−7×4)+1-23 = (-7 \times 4) + 1. This yields a valid positive remainder of 11
    • Therefore, −23%4=1-23 \% 4 = 1
  • Range of Acceptable Modulo Remainders:
    • When dividing an integer by a divisor nn, the remainder strictly lies in the range:   {0,1,2,…,n−1}\{0, 1, 2, \dots, n - 1\}
    • For divisor 44, acceptable remainders are {0,1,2,3}\{0, 1, 2, 3\}.
    • For divisor 55, acceptable remainders are {0,1,2,3,4}\{0, 1, 2, 3, 4\}.

Parity Testing and the Math Module

  • Determining Even and Odd Parity:
    • Even numbers evaluated modulo 22 yield a remainder of 00 (number % 2 == 0).
    • Odd numbers evaluated modulo 22 yield a remainder of 11 (number % 2 == 1).
    • Any integer ending in an even digit has a modulo 22 value of 00; any integer ending in an odd digit (such as 33) has a modulo 22 value of 11
  • Overview of Python Modules:
    • A module is a external file containing Python code, functions, and variable definitions.
    • Importing a module into a script allows the main code to reuse the functions and definitions contained within that file.
  • The math Module:
    • The math module provides standard mathematical functions, such as math.sqrt() (square root), math.factorial(), and math.ceil().
    • Functions inside an imported module must be accessed using dot notation (e.g., math.sqrt(36) evaluates to 6.06.0).
    • Ceiling Function (math.ceil()):
    • Rounds a floating-point number up to the next highest integer.
    • If passed an integer, math.ceil() returns that integer unchanged.

Questions & Discussion

  • Soccer Coaching Guidance:
    • Experience outlined in youth soccer coaching includes coaching girls' and boys' competitive teams at levels including DPO, CCO, NPL, and UPS.
    • Recommended approach for breaking into soccer coaching: Reach out directly via email to local competitive soccer clubs (such as those near Northville) expressing interest; clubs frequently offer entry-level coaching practice opportunities.
  • Student Profile & Background:
    • Name: Ethan (introduced alongside friend A.J.).
    • Academic Year: Junior standing.
    • Origin & Relocation History: Born in Nigeria, relocated to the United States on December 27, and attended Central High School.
    • Field of Study & Career Goals: Focuses on Computer Science / Cybersecurity, specifically interested in identifying system vulnerabilities, solving server problems, implementing server protection, and practicing simulated server attacks.
    • Communication Preference: Prefers mobile text communication over Instagram (account is inactive/logged out).