Python Programming Basics: Data Types, Variables, String Formatting, and User Input

Python Data Structures and Overview

  • Sequences in Python:

    • Lists: Ordered, mutable sequences used to store collections of data.
    • Tuples: Immutable sequences used to hold fixed collections of items.
    • Ranges: Sequence types representing an immutable sequence of numbers, commonly used in loops.
  • Dictionaries:

    • Data structure patterned after concepts used in lossless compression.
    • Composed of key-value pairs.
    • A single key and its associated value together form an item.
  • Sets:

    • Identical to the mathematical definition of sets (from Set Theory / Math 2).
    • Must contain unique elements only; strictly prohibits duplicate values.
  • Booleans:

    • Logical data type representing exactly one of two values: True or False.
  • Numeric Types:

    • Integers (int): Whole numbers without decimal points (e.g., 3535, 4040).
    • Floating-point numbers (float): Real numbers containing decimal points (e.g., 3.143.14).

Syntax Rules, Comments, and Data Type Inspection

  • Code Cells:

    • Executable blocks containing actual Python code.
  • The print() Function:

    • Used to output text or object values directly to the screen.
    • Requires the function name print followed immediately by an opening parenthesis (, parameters inside, and a closing parenthesis ) as the final character on the line.
  • Data Type Inspection with type():

    • Wrapping an object or variable inside type() returns its exact data type.
    • Passing a string literal to type() and printing it outputs <class 'str'> to the screen.
  • Comments in Python:

    • A comment begins with a pound symbol / hashtag # as the first character of a line.
    • Comments are human-readable notes completely ignored by the Python interpreter during code execution.
    • If the # symbol is removed from a line containing plain English text (e.g., removing # from # explore data types with type), execution fails and Python raises a syntax error because it does not recognize the unformatted plain text as valid code.
    • Selective uncommenting (removing #) allows step-by-step execution line by line to inspect multiple data types sequentialy.

Code Output, Strings, and Syntax Errors

  • String Literals:

    • Text values must be enclosed inside quotation marks (e.g., "hello").
    • Removing quotation marks from string text (e.g., writing hello instead of "hello") causes Python to search for a variable or identifier named hello.
    • If no such variable exists, code editors flag it with a red squiggly error indicator and Python raises a NameError upon execution.
  • Numbers vs. String Representations of Numbers:

    • A bare integer like 42 is of data type int.
    • Wrapping a number in quotation marks (e.g., "42") changes its data type from int to string (str).
  • Mathematical Operations vs. String Concatenation:

    • Standard Addition: Evaluating numeric addition outputs standard mathematical sums, such as 4+4=84 + 4 = 8.
    • Standard Multiplication: Evaluating numeric multiplication outputs standard mathematical products, such as 2×3=62 \times 3 = 6.
    • String Concatenation with +:
      • When the + operator is applied to two strings, Python joins or "smushes" them together end-to-end.
      • Evaluating "5" + "3" treats `