Day 3: Keywords and Variables in Python for DevOps

Day 3: Keywords and Variables in Python for DevOps

·

3 min read

Keywords in Python

Keywords are reserved words in Python that have special meaning in the language. They cannot be used as variables, functions, or any other identifiers.

Some of the main keywords in Python are:

  • False - Represents Boolean false value.

  • None - Represents null value.

  • True - Represents Boolean true value.

  • and, or, not - Logical operators.

  • as - Used to create an alias.

  • assert - Used for debugging.

  • async, await - Used for asynchronous programming.

  • break, continue - Used in loops.

  • class - Used to define a class.

  • def - Used to define a function.

  • del - Used to delete objects.

  • elif - Used in if...else conditional statements.

  • else - Used in if...else conditional statements.

  • except - Used to handle exceptions.

  • finally - Used to execute cleanup code.

  • for - Used for loops.

  • from, import - Used to import modules.

  • global - Used to declare global variables.

  • if - Used in if..else conditional statements.

  • in - Used to check if an item is present in a sequence.

  • is - Used for identity checking.

  • lambda - Used to define an anonymous function.

  • nonlocal - Used to modify variables in the enclosing scope.

  • pass - The null statement.

  • return - Used to return a value from a function.

  • try - Used to handle exceptions.

  • while - Used for loops.

  • with - Used as a context manager.

  • yield - Used to define a generator function.

Variables in Python

In Python, a variable is a named location that stores values. Variables allow us to symbolically name values so that we can manipulate them.

Creating Variables

Variables are created when we assign a value to them:

name = "John"
age = 30

Python does not require variables to be declared with a type. The type is determined by the value assigned.

age = 30   # age is an int
name = "John" # name is a string

Variable Names

Variable names must start with a letter or underscore. They can contain letters, numbers and underscores.

Some rules for variable names:

  • Names can contain alphabetic letters, digits and underscores.

  • Names cannot start with a digit.

  • Names cannot contain spaces.

  • Names are case-sensitive (age and Age are different variables).

Object References

In Python, variables do not directly contain values. Instead, variables are names that refer to objects. This means when we assign a value to a variable, we create a reference to that object.

For example:

a = 5
b = a

Here, a and b refer to the same object - the integer 5. Changes to a will be reflected in b.

Variable Types

Python does not have separate variable types for integers, floats, etc. A variable can reference objects of different types during its lifetime.

a = 5 # a is an int
a = "Hello" # now a is a str

Scope

Variables exist in a particular namespace called a scope. In Python, we have:

  • Local scope: Variables defined inside a function belong to the local scope of that function.

  • Global scope: Variables defined outside of all functions belong to the global scope.

To use a global variable inside a function, we use the global keyword:

total = 0 # global variable

def count():
    global total
    total += 1

That's a wrap.............................