Keywords, Variables and Best practices

Keywords, Variables and Best practices

What are Keywords?

Keywords in programming are reserved words that have special meanings and cannot be used as identifiers (names for variables, functions, etc.) in the source code.

Keywords in Python

Here are some keywords which are important from DevOps point of view:
and: It is a logical operator that returns True if both operands are true.

  1. or: It is a logical operator that returns True if at least one of the operands is true.

  2. not: It is a logical operator that returns the opposite of the operand's truth value.

  3. if: It is used to start a conditional statement and is followed by a condition that determines whether the code block is executed.

  4. else: It is used in conjunction with if to define an alternative code block to execute when the if condition is False.

  5. elif: Short for "else if," it is used to check additional conditions after an if statement and is used in combination with if and else.

  6. while: It is used to create a loop that repeatedly executes a block of code as long as a specified condition is true.

  7. for: It is used to create a loop that iterates over a sequence (such as a list, tuple, or string) and executes a block of code for each item in the sequence.

  8. in: Used with for, it checks if a value is present in a sequence.

  9. try: It is the beginning of a block of code that is subject to exception handling. It is followed by except to catch and handle exceptions.

  10. except: Used with try, it defines a block of code to execute when an exception is raised in the corresponding try block.

  11. finally: Used with try, it defines a block of code that is always executed, whether an exception is raised or not.

  12. def: It is used to define a function in Python.

  13. return: It is used within a function to specify the value that the function should return.

  14. class: It is used to define a class, which is a blueprint for creating objects in object-oriented programming.

  15. import: It is used to import modules or libraries to access their functions, classes, or variables.

  16. from: Used with import to specify which specific components from a module should be imported.

  17. as: Used with import to create an alias for a module, making it easier to reference in the code.

  18. True: It represents a boolean value for "true."

  19. False: It represents a boolean value for "false."

  20. None: It represents a special null value or absence of value.

  21. is: It is used for identity comparison, checking if two variables refer to the same object in memory.

  22. lambda: It is used to create small, anonymous functions (lambda functions).

  23. with: It is used for context management, ensuring that certain operations are performed before and after a block of code.

  24. global: It is used to declare a global variable within a function's scope.

  25. nonlocal: It is used to declare a variable as nonlocal, which allows modifying a variable in an enclosing (but non-global) scope.

What are Variables?

In programming, a variable is a named storage location that holds data or a value. It acts as a symbolic representation for a piece of information that can be changed during the program's execution. Variables are used to store and manipulate data in a program.

In Python, you can create a variable by assigning a value to a name. Here's a simple example:

x = 10

In this example, x is a variable, and it has been assigned the value 10. Later in the program, you can use the variable x to reference the stored value or update it with a new value.

Variable naming conventions

Variables in Python have some basic rules:

  • Variable names should be descriptive and indicate their purpose.

  • Use lowercase letters and separate words with underscores (snake_case) for variable names.

  • Avoid using reserved words (keywords) for variable names.

  • Choose meaningful names for variables.

Example:

# Good variable naming
user_name = "John"
total_items = 42

# Avoid using reserved words
class = "Python"  # Not recommended

# Use meaningful names
a = 10  # Less clear
num_of_students = 10  # More descriptive

Variable scope

In Python, variables have different scopes, which determine where in the code the variable can be accessed. There are mainly two types of variable scopes:

Local scope

Variables defined within a function have a local scope. They are accessible only within that specific function.

Example:

def my_function():
    local_variable = "I am local"
    print(local_variable)

my_function()
# Output: I am local

# This would result in an error because local_variable is not accessible outside the function
# print(local_variable)2

Global scope

Variables defined outside of any function or block have a global scope. They can be accessed from any part of the code, including functions.

Example:

global_variable = "I am global"

def my_function():
    print(global_variable)

my_function()
# Output: I am global

# Accessible outside the function
print(global_variable)

If you want to modify a global variable within a function, you need to use the global keyword.

Example:

global_variable = "I am global"

def modify_global():
    global global_variable
    global_variable = "Modified global"

modify_global()
print(global_variable)
# Output: Modified global

What's next?

In the next article in this blog series, we will be talking about functions, modules and packages.

Stay tuned.