Skip to content

More Basics

pip installs Python packages from the Python Package Index (PyPI).

  • Install: pip install <package_name>
  • Specific Version: pip install <package_name>=='version_num'
  • Minimum Version: pip install <package_name> >= 'version_num'
  • Upgrade Package: pip install --upgrade <package_name>
  • Upgrade Pip Itself: pip install -U pip
  • Search: pip search "query"
  • Snapshot Environment: pip freeze > requirements.txt (Lists packages case-insensitively).
  • Restore Environment: pip install -r requirements.txt
  • REPL (Interactive Console): Read-Eval-Print Loop. Start by typing python. Executes line-by-line instantly.
  • Script Mode: Run full files via python script.py or ./script.py (Unix executable).
  • IDEs/Editors: PyCharm, Eclipse+PyDev, Sublime Text, Vim. Python only requires a raw text editor, but IDEs provide PEP 8 checks and syntax highlighting.
  • Identifiers (Variables/Functions/Classes): * Allowed: a-z, A-Z, 0-9, _.
    • Rules: Cannot start with a digit. Cannot be only digits. Cannot be a reserved Keyword. Case-sensitive (EPAM != Epam).
  • Keywords: Built-in reserved words (e.g., False, def, if, return). Do not require importing.
  • Quotes: * Single (' ') and double (" ") are identical in function.
    • Triple (""" """ or ''' ''') are used for multiline strings and docstrings.
    • Nest quotes by alternating types: "Here is 'text'"
  • Indentation: * Python uses whitespace (standard is 4 spaces) instead of {} to define code blocks.
    • Inconsistent indentation throws an IndentationError.
  • Multi-line Statements: * Manual break: Use the backslash \ at the end of a line.
    • Automatic break: Statements inside (), [], or {} wrap automatically without a \.
  • Comments: Proceeded by the # symbol.

Python is dynamically typed; you do not declare variable types explicitly. A variable is simply a reference pointing to an object in memory.

  • Integers: Whole numbers. Unlimited length (bound only by system memory).
  • Floats: Decimals.
  • Strings: Text characters.
  • Multiple Assignment: a, b, c = 1, 2, "john" or a = b = c = 1
  • Deletion: del var_name removes the reference. Accessing it afterward throws a NameError.
  • Type Conversion: int(x), float(x), str(x).

5. The Python Data Model: Immutable vs. Mutable

Section titled “5. The Python Data Model: Immutable vs. Mutable”

Every object possesses three core traits:

  1. Identity: Memory address. Evaluated with is. Checked with id(). Unchangeable.
  2. Type: Defines supported operations. Checked with type(). Unchangeable.
  3. Value: The actual data.
CategoryDefinitionCommon Data TypesMemory Logic
ImmutableThe object’s value cannot be changed after creation. Any “modification” actually creates a new object in memory.int, float, bool, str, tuple, frozensetChanging the value results in a new object with a new id().
MutableThe object’s value can be modified after creation without creating a new object.list, dict, set, most custom objectsModifying the object keeps the same id() because it changes in place.

Immutable

x=10
print(id(x))
x=x+5
print(id(x))# Different id

Mutable

x= [1,2,3]
print(id(x))
x.append(4)
print(id(x))# Same id
  • Arithmetic: +, -,* , / (float division), % (modulo/remainder), ** (exponent), // integer dividion
  • Assignment: =, +=, =, etc.
  • Comparison: ==, !=, >, <, >=, <=. Returns boolean True or False.
  • Membership: in and not in (e.g., 'a' in 'apple' -> True).
  • Identity: is and is not (Checks if two variables point to the exact same memory address. x == y tests value equivalence; x is y tests memory equivalence).
  • Conditionals (if/elif/else): Top-down evaluation.
  • Ternary Operator (One-line if): print("even" if a % 2 == 0 else "odd")
  • Input/Output: * input("Prompt: ") strictly returns string data. Wrap in int() to parse numbers.
    • print(a, b) automatically converts arguments to strings and joins them with spaces.
  • Purpose: The official style guide written in 2001 by Guido van Rossum to ensure code consistency and readability.
  • Philosophy: “Code is read much more often than it is written.”
  • Linters/Tools: pycodestyle, flake8 (Install via pip to automatically check scripts against PEP 8 rules).

Loops and Strings

List & Tuples

Set & Dict

Function & Input