Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

Variables, comments/shaare/x85Yzw

  • python
  • python

Variables: Naming Values

  • Naming Guidelines:
    • Must start with a letter or underscore (_) and can contain letters, numbers, and underscores.
    • Use snake_case for readability (e.g., max_retries).
  • Purpose: Store data like file paths, server counts, status messages, API keys, configurations.
  • Typing: Python uses dynamic typing, which means we don't need to explicitly declare the variable type, and we can assign values with different types to the same variable (not recommended!)
var1 = "hello"
item = 101
print(type(item))
# Never do that! Don't assign a value of a different type to the same variable!
item = "Code 101"
print(type(item))

Comments

Python code may be readable, but comments and docstrings explain intent, rationale, and usage. Comments (#) are ignored by the interpreter; docstrings ("""...""") are accessible via __doc__ (we'll come back to docstrings later, when we discuss functions).

Single-Line Comments (#)

Use # to comment single lines or inline code. Best for explaining why, adding TODO/FIXME markers, or temporarily disabling code.

# Example of a single-line comment
error_code = 0

# TODO: handle case when argument is None 

Multi-Line / Block Comments

Prefix each line with # to comment out blocks of code. Useful for disabling sections or annotating complex logic. It's also possible to wrap multiline comments between triple single-quotes ('''...''') or between triple double-quotes ("""..."""), but this is not their original intended usage.

# if True:
#     print("I will execute")
2 months ago Permalink
cluster icon
  • Classes and Objects : Classes and Objects Beyond Built-ins: Python lets you define your own data types using class. Class: A blueprint or template for creating objects. De...
  • Python Functions Are First‑Class Citizens : Python Functions Are First‑Class Citizens In Python, functions behave like any other object (strings, ints, lists). Because they are "first‑clas...
  • Logging to Files : Logging to Files Basic File Logging with FileHandler Use logging.FileHandler to write log records to a file. mode='a' (append) preserves existing log...
  • Functions: return vs yield : Functions: return vs yield Regular functions execute immediately, run to completion, and return a single value (or None). Generator functions retur...
  • Running External Commands with subprocess.run : Running External Commands with subprocess.run DevOps automation often requires invoking existing CLI tools or scripts to leverage their functionality...


(97)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community