More Basics
1. Package Management (PIP)
Section titled “1. Package Management (PIP)”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
2. Execution Environments & Tooling
Section titled “2. Execution Environments & Tooling”- REPL (Interactive Console): Read-Eval-Print Loop. Start by typing
python. Executes line-by-line instantly. - Script Mode: Run full files via
python script.pyor./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.
3. Syntax Rules & Structure
Section titled “3. Syntax Rules & Structure”- 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).
- Rules: Cannot start with a digit. Cannot be only digits. Cannot be a reserved Keyword. Case-sensitive (
- 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'"
- Triple (
- Indentation: * Python uses whitespace (standard is 4 spaces) instead of
{}to define code blocks.- Inconsistent indentation throws an
IndentationError.
- Inconsistent indentation throws an
- Multi-line Statements: * Manual break: Use the backslash
\at the end of a line.- Automatic break: Statements inside
(),[], or{}wrap automatically without a\.
- Automatic break: Statements inside
- Comments: Proceeded by the
#symbol.
4. Variables & Dynamic Typing
Section titled “4. Variables & Dynamic Typing”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"ora = b = c = 1 - Deletion:
del var_nameremoves the reference. Accessing it afterward throws aNameError. - 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:
- Identity: Memory address. Evaluated with
is. Checked withid(). Unchangeable. - Type: Defines supported operations. Checked with
type(). Unchangeable. - Value: The actual data.
| Category | Definition | Common Data Types | Memory Logic |
|---|---|---|---|
| Immutable | The object’s value cannot be changed after creation. Any “modification” actually creates a new object in memory. | int, float, bool, str, tuple, frozenset | Changing the value results in a new object with a new id(). |
| Mutable | The object’s value can be modified after creation without creating a new object. | list, dict, set, most custom objects | Modifying the object keeps the same id() because it changes in place. |
Example
Section titled “Example”Immutable
x=10print(id(x))
x=x+5print(id(x))# Different idMutable
x= [1,2,3]print(id(x))
x.append(4)print(id(x))# Same id6. Operators
Section titled “6. Operators”- Arithmetic:
+,-,*,/(float division),%(modulo/remainder),**(exponent),//integer dividion - Assignment:
=,+=,=, etc. - Comparison:
==,!=,>,<,>=,<=. Returns booleanTrueorFalse. - Membership:
inandnot in(e.g.,'a' in 'apple'->True). - Identity:
isandis not(Checks if two variables point to the exact same memory address.x == ytests value equivalence;x is ytests memory equivalence).
7. Control Flow & I/O
Section titled “7. Control Flow & I/O”- 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 inint()to parse numbers.print(a, b)automatically converts arguments to strings and joins them with spaces.
8. PEP 8 (Python Enhancement Proposal 8)
Section titled “8. PEP 8 (Python Enhancement Proposal 8)”- 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).