Supprimer Rendre public Rendre privé Add tags Delete tags
  Ajouter un tag   Annuler
  Supprimer le tag   Annuler
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Connexion

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")
3 months ago Permalien
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...
  • Functions, Docstrings : Functions Functions package reusable code into named blocks, improving modularity, readability, and testability. They prevent duplication (DRY) and ma...
  • Typing classes : Introduction As our Python automation projects grow, defining custom classes helps model complex objects and should be reflected in type hints for cl...
  • Dictionaries : Dictionaries (dict) Dictionaries are mutable, insertion-ordered collections of key-value pairs. Keys must be unique and immutable; values can be of an...
  • If / Elif / Else Logic : If / Elif / Else Logic Control the flow of scripts based on conditions using if, elif, and else. The if Statement An if statement executes a block of ...


(110)
Filtrer par liens sans tag
Replier Replier tout Déplier Déplier tout Êtes-vous sûr de vouloir supprimer ce lien ? Êtes-vous sûr de vouloir supprimer ce tag ? Le gestionnaire de marque-pages personnel, minimaliste, et sans base de données par la communauté Shaarli