Python Programming Fundamentals: Variables, Data Types, Expressions, and Operators

Variables and Assignment Statements

  • Purpose of a Variable: A variable is used to store data in memory so that a program can reference, manipulate, and reuse that stored information at a later point in execution.
  • Assignment Statement Definition: An assignment statement creates a variable and binds it to reference a specific value or the evaluated output of an expression.
  • Assignment Operator: Python uses the equal sign == as the assignment operator.
  • Structural Rules of Assignment Statements:
    • Left Side: Must exclusively be a target variable name being created or updated.
    • Right Side: Must be a value, string literal, existing variable, or expression that produces a value.
    • Flow of Operation: Evaluates the right-hand side first, then assigns the resulting value to the variable specified on the left-hand side.
  • Concrete Assignment Examples:
    • num=10num = 10: Creates a new variable named numnum and assigns it the integer value 1010.
    • A=10.23A = 10.23: Creates a new variable named AA and assigns it the floating-point decimal value 10.2310.23.
    • hello_world=’hello world’hello\_world = \text{'hello world'}: Creates a variable named hello_worldhello\_world and assigns it the text string literal 'hello world' wrapped in quotation marks.
    • name=’willy’name = \text{'willy'}: Creates a variable named namename and assigns it the string literal 'willy'.
    • age=18age = 18: Creates a variable named ageage and assigns it the integer value 1818.
  • Analysis of Invalid Assignment Statements:
    • Invalid Syntax Example: 4=b4 = b
    • Reason for Failure: The left side of an assignment statement must be a valid variable identifier. An integer literal like 44 cannot act as a variable target.
    • Distinguishing Mathematics from Programming: While 4=b4 = b is a valid symmetric equality statement in mathematics, it is completely invalid in Python, Java, and other imperative programming languages because the assignment operator == represents a directional right-to-left data assignment.
    • Correct Formulation: b=4b = 4 assigns the value 44 to the variable bb.

Variable Naming Rules and Rules Evaluation

  • Rule 1 (Reserved Keywords): A variable name cannot be a Python keyword. Keywords (such as TrueTrue or FalseFalse) are reserved words with explicit built-in operational meanings in the language.
  • Rule 2 (Whitespace Exclusion): Variable names cannot contain spaces under any circumstances.
  • Rule 3 (First Character Constraints): The first character of a variable name must be an alphabetic letter (A-Z, a-z) or an underscore character _\_. It cannot start with a numeric digit.
  • Rule 4 (Subsequent Character Constraints): After the initial character, variable names may consist of letters, numeric digits, or underscores.
    • Valid Example: A5A5 (starts with an alphabetic letter, followed by a digit).
    • Invalid Example: 5A5A (starts with a numeric digit, violating Rule 3).
  • Rule 5 (Case Sensitivity): Variable identifiers are case-sensitive. For instance, AA and aa are recognized as two distinct variables in memory.
  • Naming Conventions and Best Practices:
    • Functional Meaning: Variable names should clearly describe their intended purpose or stored state (e.g., is_runningis\_running to indicate whether a vehicle is operating).
    • Contextual Use: Simple identifiers like aa or xx may be acceptable for basic classroom demonstrations, but real-world industry practice mandates descriptive names over non-descriptive single-letter names.
  • Evaluation of Specific Identifier Examples:
    • b_4=5b\_4 = 5: Valid. Starts with a letter, contains an underscore, and ends with a digit without spaces.
    • _b4=5\_b4 = 5: Valid. Starts with an allowed underscore _\_, followed by a letter and a digit.
    • my variable=5my\ variable = 5: Invalid. Contains a space between mymy and variablevariable.
    • 2a=52a = 5: Invalid. Begins with the numeric digit 22.
    • a2=5a2 = 5: Valid. Starts with a letter aa before using the digit 22.
    • my_underscore_8=5my\_underscore\_8 = 5: Valid. Starts with a letter and incorporates underscores and a trailing digit without spaces.

Right-Side Referencing and Variable Reassignment

  • Variables on the Right-Hand Side:
    • Statement: a=numa = num
    • Execution Details: The interpreter reads the variable numnum on the right side, retrieves the stored value referenced by numnum (such as 1010), and assigns that retrieved value to variable aa on the left side.
    • Equivalence: If numnum stores 1010, executing a=numa = num is functionally identical to executing a=10a = 10.
    • Variable Target: Only the variable on the left side (aa) is updated or created. The value of numnum remains unchanged.
  • Self-Referencing Variable Reassignment:
    • Statement: num=num+1num = num + 1
    • Operational Flow:
    1. Evaluates the right-hand side first by reading the current value of numnum (e.g., 1010).
    2. Performs the addition operation 10+110 + 1, resulting in 1111.
    3. Updates numnum on the left side, replacing its old value 1010 with 1111.
    • Distinction from Algebra: Mathematically, num=num+1num = num + 1 has no solution. In programming, it represents a state update where the variable's new value becomes its previous value incremented by 11.

Printing Variables vs. String Literals

  • Evaluation of Print Statements:
    • print(num)print(num): Evaluates the variable numnum and outputs its underlying value (e.g., outputs 1010).
    • print(num+1)print(num + 1): Evaluates the expression num+1num + 1 and outputs the computed result (e.g., outputs 1111).
    • print(’num’)print(\text{'num'}): Enclosing num in single quotation marks defines it as a string literal. The interpreter outputs the literal text characters num rather than evaluating a variable.
  • Demonstration of Print Outputs:
    • Setup: num=10num = 10
    • Running print(num)print(num) displays 10.
    • Running print(’num’)print(\text{'num'}) displays num.
  • Numeric Content inside String Literals:
    • Any characters inside quotation marks, such as '10.23', are classified as string literals (strstr), not numeric data types, regardless of whether the content consists of numbers.
  • Complex Text Reference Example:
    • Variable Assignment: name=’Mercer University’name = \text{'Mercer University'}
    • Executing print(name)print(name) evaluates the variable namename and outputs Mercer University.
    • Executing print(’name’)print(\text{'name'}) evaluates the string literal and outputs name.

Variable Lifecycle and Reassignment Rules

  • Prerequisite Creation Rule: A variable must be created via an assignment statement before it can be referenced or used in any program expression.
  • Invalid Uninitialized Usage: The statement num=1+bnum = 1 + b produces an error if bb has not been previously instantiated.
  • Correct Instantiation Steps:
    1. Create variable bb via assignment: b=0b = 0 or b=numb = num.
    2. Reference bb in subsequent updates: num=num+bnum = num + b.
  • Overwriting Variable Values: Python variables are dynamic and can hold updated values across sequential assignment statements.
    • Initial assignment: num=10num = 10 binds numnum to 1010.
    • Subsequent assignment: num=100num = 100 overwrites the reference, updating numnum to 100100.

Python Built-in Data Types

  • Significance of Data Types: Data types inform the interpreter about the structure of stored data, governing which specific operational methods and functions can be applied to them.
  • Integer (intint):
    • Represents whole numbers without fractional components (e.g., −5-5, 00, 1010).
    • Supports standard mathematical and arithmetic operations.
  • Floating-Point (floatfloat):
    • Represents continuous real numbers containing decimal points (e.g., 10.2310.23, 34.534.5).
    • Supports decimal arithmetic operations.
  • String (strstr):
    • Represents an ordered sequence of text characters bounded by quotation marks (e.g., 'willy', 'Mercer University').
    • Supports textual operations such as string concatenation (joining strings together).
  • Boolean (boolbool):
    • Represents truth values consisting of exactly two possible outcomes: TrueTrue and FalseFalse.
    • Formatting Requirement: Must begin with capitalized letters TT and FF as keywords.
    • Binary State: Corresponds directly to a single binary bit state (e.g., on/off, positive/negative charge).

Structure of Expressions and Arithmetic Operators

  • Definition of an Expression: A combination of variables, numeric values, and operators that evaluates to a single output value.
  • Operational Evaluation: The interpreter replaces any variables within an expression with their current stored values and executes the calculations to compute a final single value.
  • Basic Expression Examples:
    • 5+65 + 6: Combines two literal integer values using addition to evaluate to 1111.
    • a+b34.5\frac{a + b}{34.5}: If a=10a = 10 and b=20b = 20, the numerator evaluates to 3030, resulting in 3034.5≈0.869565\frac{30}{34.5} \approx 0.869565.
  • Standard Arithmetic Operators:
    • Addition: ++
    • Subtraction: −-
    • Multiplication: ∗* (Asterisk. Symbolic multiplication sign; letters like x must not be used).
    • Division: // (Forward slash. Performs division, returning a floating-point output).
    • Exponentiation: ∗∗** (Two consecutive asterisks. Computes base raised to a power, e.g., x∗∗yx ** y represents xyx^y).
    • Floor Division: //// (Divides and truncates the decimal portion to return an integer quotient).

Modulus (Remainder) Operator Mechanics and Applications

  • Modulus Operator Symbol: %\%
  • Mathematical Definition: Calculates the integer remainder resulting from division between two operands.
  • Step-by-Step Remainder Calculation for A(modB)A \pmod B:
    1. Determine the largest integer multiple of BB that is less than or equal to AA.
    2. Subtract that multiple from AA to obtain the remaining value.
  • Step-by-Step Numerical Examples:
    • 4(mod3)=14 \pmod 3 = 1: The largest multiple of 33 below 44 is 33. Remaining: 4−3=14 - 3 = 1.
    • 5(mod3)=25 \pmod 3 = 2: The largest multiple of 33 below 55 is 33. Remaining: 5−3=25 - 3 = 2.
    • 6(mod3)=06 \pmod 3 = 0: 66 is fully divisible by 33. Remaining: 6−6=06 - 6 = 0.
    • 7(mod3)=17 \pmod 3 = 1: The largest multiple of 33 below 77 is 66. Remaining: 7−6=17 - 6 = 1.
    • 8(mod3)=28 \pmod 3 = 2: The largest multiple of 33 below 88 is 66. Remaining: 8−6=28 - 6 = 2.
    • 10(mod4)=210 \pmod 4 = 2: The largest multiple of 44 below 1010 is 88. Remaining: 10−8=210 - 8 = 2.
  • Remainder Limit Law: For any modulus operation A(modB)A \pmod B, the resulting remainder RR is strictly bounded by 0≤R<B0 \le R < B.
  • Parity Testing Algorithm (Odd vs. Even Checking):
    • Executing N(mod2)N \pmod 2 evaluates whether an integer NN is odd or even.
    • If N(mod2)=0N \pmod 2 = 0: The number is even (divisible by 22 with zero remainder).
    • If N(mod2)=1N \pmod 2 = 1: The number is odd (leaves a remainder of 11 when divided by 22).

Operator Precedence Rules

  • Precedence Hierarchy:
    1. Parentheses ()(): Expressions enclosed within parentheses evaluate first.
    2. Exponentiation ∗∗**: Computed prior to multiplication, division, or addition.
    3. Multiplication ∗*, Division //, and Modulus %\%: Equal priority tier; computed left-to-right.
    4. Addition ++ and Subtraction −-: Lowest priority tier; computed left-to-right.
  • Step-by-Step Walkthrough Example:
    • Statement: (3+5)×32(3 + 5) \times 3^2 (written in code as (3 + 5) * 3 ** 2)
    • Step 1 (Parentheses): Evaluate (3+5)=8(3 + 5) = 8. Expression simplifies to 8×328 \times 3^2.
    • Step 2 (Exponentiation): Evaluate 32=93^2 = 9. Expression simplifies to 8×98 \times 9.
    • Step 3 (Multiplication): Evaluate 8×9=728 \times 9 = 72.
    • Final Evaluated Result: 7272.

Foundational Learning Methodology and Course Logistics

  • Three-Question Analytical Framework for Programming Concepts:
    1. What is it? (Define the syntax and structural mechanics of the concept).
    2. Why do we need it? (Understand the underlying purpose and computational necessity).
    3. How do we use it? (Apply correct syntax to implement the concept in code).
  • Assignment Logistics:
    • Posting Schedule: First programming assignment posted by Friday at the latest.
    • Submission Window: Students receive a 2 week2\text{ week} submission period from the date of posting (due on Friday two weeks after release).