Variables, comments/shaare/x85Yzw
Variables: Naming Values
- Naming Guidelines:
- Must start with a letter or underscore (
_) and can contain letters, numbers, and underscores. - Use
snake_casefor readability (e.g.,max_retries).
- Must start with a letter or underscore (
- 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")
(97)