Skip to content

Computer Science · Ch 5 — Getting Started with Python

Summary

Summary

  • Python is an open-source, high-level, interpreter-based language usable for a multitude of scientific and non-scientific computing purposes.
  • Python works in two execution modes: interactive mode executes one statement at a time at the >>> prompt, while script mode saves multiple statements in a .py file that can be run and reused.
  • Keywords are reserved words with fixed meanings; Python is case-sensitive, so keywords and identifiers must be written exactly.
  • An identifier is a user-defined name given to a variable or constant in a program. It must start with a letter or underscore, may contain letters, digits and underscores, and cannot be a keyword or contain special symbols.
  • A variable refers to an object stored in memory; variable declaration is implicit — a variable is created the moment it is first assigned a value. Trying to use a variable that has not been assigned a value gives an error.
  • Comments (starting with #) are non-executable statements in a program, added to make the source code easier for humans to understand.
  • Every value in Python is an object with a unique identity (ID), retrievable with id().
  • There are several data types in Python — integer, boolean, float, complex, string, list, tuple, set, None and dictionary.
  • Data types are classified as mutable (lists, sets, dictionaries — values can change after creation) or immutable (integers, floats, booleans, complex, strings, tuples — values cannot change; updating rebuilds the object).
  • Operators are constructs that manipulate the values of operands. Operators may be unary (one operand) or binary (two operands). Python provides arithmetic, relational, assignment, logical, identity (is, is not) and membership (in, not in) operators.
  • An expression is a combination of values, variables and operators, and it always evaluates to a value. Evaluation order follows operator precedence; parentheses override precedence, and operators of equal precedence apply left to right.
  • A statement is a unit of code that the Python interpreter can execute, such as an assignment or a print statement.
  • Python has the input() function for taking user input — it always returns a string, which must be typecast (e.g. with int()) for arithmetic.
  • Python has the print() function to output data to a standard output device; optional sep and end parameters control the separator between values and the string appended after the last value. …