Python Basics: Algorithms, Pseudocode, IO, Variables, and Data Types

Algorithms, Pseudocode, and Circle Formulas

  • Algorithm: a defined set of logical steps to perform a task.
  • Circle formulas:
    • Area: A = \pi r^{2}
    • Circumference: C = 2 \pi r
  • Radius is needed to compute area or circumference.

Pseudocode

  • Pseudocode is a human-readable description of steps; not executable by a computer.

Program Structure: Input, Processing, Output

  • A program typically has three parts:
    • Input: gather data from keyboard or data files
    • Processing: compute using formulas or logic
    • Output: display results
  • Consider these three parts before designing and writing a program.

Print function and Python basics

  • Print is a predefined Python function to display information on the screen.
  • Function name: print; Parameter/argument: the data passed inside the parentheses.
  • Strings must be enclosed in quotes; numbers can be printed directly.
  • Execution order: statements run from top to bottom.
  • Memory is cleared after the program ends unless you save results to a file.

Comments and Variables

  • Comments:
    • Single-line: start with #; text after # is ignored by Python.
    • Multiline: enclosed by triple quotes (''') or ("""), and are not executed.
  • Variables store information; assignment: name = value.
  • Reassignment: you can change a variable's value (and even its type).
  • Data types: integer, float, string.

Variable naming rules and style

  • Variable names cannot be Python keywords; cannot contain spaces; first character must be a letter or underscore.
  • Variables are case sensitive.
  • Use descriptive names; underscores can be used to represent spaces (e.g., first_name).

Multiple variables and reassignment

  • Multiple assignment: a, b = 1, 2
  • You can reassign variables, and you can change the data type over time (dynamic typing).

Input and type conversion

  • input() returns a string.
  • To use numeric input in calculations, convert with int() or float():
    • age_str = input("Please enter your age: ")
    • age = int(age_str)
    • or: age = float(age_str)
  • If input is not numeric, conversions raise ValueError.
  • Decimals: int(85.5) -> 85 (decimal part lost); float("85.5") -> 85.5.
  • Always convert the input string to the proper type before arithmetic.

Arithmetic and power

  • Exponent in Python: a ** b corresponds to a^b in math.
  • Example: to compute area with radius r: A = \pi r^{2}, and in Python: A = math.pi * r ** 2

Output formatting and separators

  • print(a, b, c) prints values separated by a single space by default.
  • To customize: print(a, b, sep=", ")
  • End of line can be controlled with end=, but not required for the key ideas here.

Saving results and memory

  • Program memory is cleared after execution; to persist results, write to a file (covered later).

Common pitfalls and quick checks

  • Accessing an undefined variable raises an error (NameError).
  • Mixing strings and numbers in arithmetic requires explicit conversion.
  • The first character of a variable must be a letter or underscore; starting with a digit is invalid.
  • Variable names should reflect their use to avoid confusion.