Google IT Automation with Python study materials
Google IT Automation with Python study resources

Google IT Automation with Python: Python programming fundamentals and scripting for IT tasks

Google IT Automation with Python · Work through your exam, one topic at a time.

All guide sections

Python programming fundamentals and scripting for IT tasks

Python programming fundamentals and scripting for IT tasks

This domain tests whether you can read, predict, and write short Python programs that perform IT work: parsing text, transforming records, defining reusable functions, handling failures, and accepting command-line input. Expect snippet-output questions and 'which approach is safest' questions rather than algorithm puzzles.

Core rules and concepts

  • Names bind to objects; assignment does not copy. Lists and dictionaries are mutable, so passing one into a function lets the function change the caller's object.
  • Indexing starts at 0; slices exclude the stop index. A negative index counts from the end. line.split(maxsplit=2) limits splitting and is useful when the remainder is a message.
  • Prefer for item in collection over index loops. Use enumerate() when you need both position and value.
  • Functions should return values rather than print them when the result will be tested or reused. Avoid mutable default arguments such as def f(items=[]); use None and create a new list inside.
  • Catch only exceptions you can handle. except Exception: pass hides bugs and is a common wrong answer in troubleshooting questions.
  • Scripts that run from a shell need a shebang, a if __name__ == '__main__': guard, and an exit status. sys.exit(1) signals failure; returning 0 means success.

Worked example

A log contains lines like 2024-01-01 ERROR disk full. Count severities and ignore malformed lines.

from collections import Counter

def count_levels(lines):
    counts = Counter()
    for line in lines:
        parts = line.split(maxsplit=2)
        if len(parts) < 3:
            continue
        counts[parts[1]] += 1
    return counts

maxsplit=2 produces at most three fields, so the message stays intact. The length check skips blank or truncated lines. Counter returns 0 for a missing key, so counts[parts[1]] += 1 does not raise KeyError. The function returns a mapping, which is easier to test than a printed report.

Traps that cost marks

Do not modify a list while iterating over it; build a new list or iterate over a copy. Remember that / returns a float in Python 3, while // floors. is compares identity, not equality; use == for values. Opening text files without an encoding can fail on non-ASCII logs. Finally, a script that catches an exception but exits with status 0 tells automation that it succeeded.