Notes About Programming in Python
Fundamentals of Programming
Atomic Building Blocks
- Numbers: Represented as integers and floats.
- Strings: Represent textual information associated with primitive values.
Value and Type
- Putting things together: Value and type are combined to form expressions, which consist of operands and operators.
- Interpreters: Programs that execute code inside the machine, evaluating expressions and printing output.
Expression Evaluation
Inside a code block:
- The evaluator uses the script but does not print it back out. Instead, it stores the value for later use.
- There is no explicit
printstatement unless specified.
Operators
- Overloading: Operators can be redefined to combine different data types, requiring type conversions when necessary.
- Syntax: The operator is specified between operands, e.g.,
3 + 'abc'.
Type Checking and Error Handling
- Python checks file syntax and catches errors using type checking early in the process.
- Language features:
- Strong typing (e.g., Python)
- Weak typing (e.g., Lisp)
Variable Binding and Scope
- Binding: Each symbol gets translated to ASCII, creating a binding between the name and its corresponding value.
- Example:
x = 3binds the variablexto the expression3.
Questions about Variables
- Type inheritance:
- The type of a variable is determined by the current value it holds.
- Dynamic typing:
- The type of a variable changes depending on its assigned value.
Style and Variable Naming
- Don’t change types arbitrarily: Use meaningful variable names (e.g.,
x,y,z) to maintain consistency. - Avoid using Python keywords as variable names (e.g.,
print).
Statements and Control Flow
- Statements: Legal commands that Python can interpret, such as assignment (
=) and printing (print). - Conditional statements: Used for branching programs, which change instruction order based on some test.
- Example:
x = 15 if (x/2)*2 == x: print('Even') else: print('Odd')
Branching Programs
- Change instruction order based on some test value.
- Tests are usually values of variables.
Considerations about Logic
- Use meaningful variable names and comments to improve code readability.
- Avoid complex conditional statements without proper testing and debugging.