Day 3 || Variables, Rules, and the Global vs. Local Variable reserved keyword in Python

Day 3 || Variables, Rules, and the Global vs. Local Variable reserved keyword in Python

DevOps professionals are tasked with streamlining and automating processes, and Python is an invaluable tool in this domain. On Day 3 of our Python for DevOps journey, we dive into the world of variables, their naming rules, and the distinction between global and local variables. These concepts are fundamental for creating efficient and readable scripts.

1. Variables in DevOps

Variables are crucial in DevOps as they store configuration data, credentials, and other information used in automation scripts. Variables provide flexibility and adaptability to scripts, making them an essential part of a DevOps engineer's toolkit.

2. Rules for Variable Naming

To maintain organized and readable code, it's essential to adhere to proper variable naming conventions:

  • Case-Sensitivity: Variable names are case-sensitive in Python. "myVar" and "myvar" are distinct.

  • Allowed Characters: Variable names can contain letters, numbers, and underscores. They cannot start with a number.

  • Descriptive Names: Use descriptive variable names that convey the purpose of the data. For instance, use "server_address" instead of "addr."

  • Avoiding Keywords: Avoid using Python's reserved keywords as variable names, as they have specific meanings and purposes.

  • Naming Conventions: The "snake_case" convention, using lowercase letters and underscores to separate words, is common in DevOps.

3. Global vs. Local Variables

Understanding variable scope is crucial. In Python, variables can be global or local:

  • Global Variables: Declared outside functions, they are accessible throughout the entire script. Global variables are suitable for settings and configurations that need to be accessed from various parts of the script.

  • Local Variables: Defined within functions, they are accessible only within those functions. Local variables are used for temporary storage or function-specific data.

    Reserved keyword in Python

Reserved keywords, often referred to as "keywords" or "reserved words," are special words in a programming language that are reserved for specific purposes and have predefined meanings. These words cannot be used as identifiers, such as variable names or function names, in the code because they are reserved for specific actions or functionalities within the language.

In Python, there are several reserved keywords that serve various purposes. Some of the common Python-reserved keywords include:

  1. and: Used for logical conjunction (e.g., if x and y:).

  2. as: Used for aliasing when importing modules or working with classes (e.g., import math as m).

  3. assert: Used for debugging and testing assertions in code.

  4. break: Used to exit loops prematurely (e.g., while True: break).

  5. class: Used to define a class in object-oriented programming.

  6. continue: Used to skip the rest of the current loop iteration and move to the next (e.g., for i in range(10): continue).

  7. def: Used to define functions or methods.

  8. elif: Short for "else if," used in conditional statements (e.g., if x == 5: ... elif x == 10: ...).

  9. else: Used in conditional statements (e.g., if x == 5: ... else: ...).

  10. except: Used for handling exceptions in Python's try-except blocks.

  11. False: A Boolean value representing the logical "false" condition.

  12. finally: Used in exception handling to specify a block of code that is executed no matter what.

  13. for: Used to create loops that iterate over a sequence (e.g., for item in sequence:).

  14. if: Used to define conditional statements.

  15. import: Used to import modules and packages.

  16. in: Used to check if a value is found in a sequence (e.g., if item in list:).

  17. is: Used to check if two objects are the same (e.g., if a is b:).

  18. lambda: Used to create anonymous (small, unnamed) functions.

  19. None: A special value representing the absence of a value (similar to null in other languages).

  20. not: Used for logical negation (e.g., if not condition:).

  21. or: Used for logical disjunction (e.g., if x or y:).

  22. pass: Used as a placeholder when no action is desired.

  23. raise: Used to raise exceptions in Python.

  24. return: Used to return a value from a function.

  25. True: A Boolean value representing the logical "true" condition.

  26. try: Used to define blocks where exceptions might occur.

  27. while: Used to create loops that continue as long as a condition is met.

  28. with: Used for resource management and context managers.

  29. yield: Used in generator functions to yield a value.

Did you find this article valuable?

Support Aqib Hafeez(DevOps enthusiast) by becoming a sponsor. Any amount is appreciated!