π Key Concepts
Concept One-Line Definitionprint()
Built-in function to output data to the terminal
Variable
A named container in memory that holds a value
Dynamic Typing
Python figures out the data type automatically at runtime
Identifier
The name you give to a variable, function, or class
Keyword
Reserved words Python uses internally β cannot be used as identifiers
type()
Built-in function that tells you the data type of a value
id()
Returns the memory address of an object
π€ Syntax
# print() β basic output
print("Hello DevOps")
print(variable_name)
print(value, type(value)) # print value AND its type together
# Variable assignment
port = 8080 # int
service_name = "nginx" # str
cpu_threshold = 85.5 # float
# type() β check data type
print(type(port)) # <class 'int'>
# id() β check memory address
print(id(service_name))
# keyword list
import keyword
print(keyword.kwlist)
# input() β take user input
name = input("Enter your name: ")
print("Hello", name)
Enter fullscreen mode Exit fullscreen mode
π Core Concepts Explained
1. print() Function
The most basic output function. In DevOps scripts, you’ll use it constantly to log statuses, debug values, and show results.
print("Deployment started")
print("Server IP:", server_ip)
print("Status:", status, "| Code:", code) # multiple values with comma
Enter fullscreen mode Exit fullscreen mode
2. Variables
A variable is just a label pointing to a value in memory. Think of it like naming a config value so you can reuse it.
# BAD β hardcoded everywhere
# connect("192.168.1.10", 22)
# connect("192.168.1.10", 22)
# GOOD β use a variable
server_ip = "192.168.1.10"
ssh_port = 22
connect(server_ip, ssh_port)
Enter fullscreen mode Exit fullscreen mode
3. Dynamic Typing
Python doesn’t require you to declare the type. It detects it automatically. The same variable can hold different types at different times.
a = 10 # int
a = "hello" # now str β Python is fine with this
a = 3.14 # now float
Enter fullscreen mode Exit fullscreen mode
β οΈ Common Mistake: Reassigning a variable to a different type accidentally can cause bugs in automation scripts.
4. Data Types (from class)
Type Example DevOps Use Caseint
port = 22
Port numbers, exit codes
float
cpu = 85.5
CPU/memory thresholds
str
env = "prod"
Environment names, server IPs
bool
is_healthy = True
Health check flags
port = 22 # int
threshold = 85.5 # float
environment = "prod" # str
is_active = True # bool
Enter fullscreen mode Exit fullscreen mode
5. Identifiers β Naming Rules
β
VALID β INVALID
server_ip 5server (starts with number)
_private server-ip (hyphen = minus operator)
cpu2core server ip (space not allowed)
myVar if (reserved keyword)
EC2Instance print = 100 (shadows built-in β avoid!)
Enter fullscreen mode Exit fullscreen mode
Golden Rule for DevOps Scripts: Use snake_case (lowercase with underscores) β server_ip, max_retries, aws_region. This matches Python convention and is easy to read.
6. Keywords β Off-Limits Names
import keyword
print(keyword.kwlist)
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
# 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
# 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
# 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
# 'try', 'while', 'with', 'yield']
Enter fullscreen mode Exit fullscreen mode
7. Don’t Shadow Built-ins β οΈ
The class demonstrated this critical mistake:
# β NEVER do this
print = 100 # You just killed the print function!
print("Python") # TypeError: 'int' object is not callable
# β
Restart kernel or use del to recover
del print
print("Python") # Works again
Enter fullscreen mode Exit fullscreen mode
Common built-ins to never overwrite: print, input, list, dict, str, int, len, open, type, id
βοΈ DevOps / Cloud Use Cases
# AWS EC2 config variables
instance_type = "t2.micro"
region = "ap-south-1" # Mumbai
ami_id = "ami-0abcdef1234567890"
max_instances = 5
spot_price = 0.012 # float β price in USD
# Docker/K8s config
container_port = 8080
replica_count = 3
image_tag = "nginx:1.25"
is_rolling_update = True
# CI/CD pipeline variable
build_status = "SUCCESS"
exit_code = 0 # 0 = success in Linux/bash
artifact_size_mb = 145.7
# Log line output (you'll do this in every script)
print(f"Deploying {image_tag} to {region} with {replica_count} replicas")
print(f"Build: {build_status} | Exit Code: {exit_code}")
Enter fullscreen mode Exit fullscreen mode
β Common Mistakes (from class + real world)
Mistake Example Fix Typo in function nameprin("Python")
Check spelling β Python gives NameError
Shadow a built-in
print = 100
Never use built-in names as variables
Use keywords as names
if = 5
Use keyword.kwlist to check
Start var with number
5server = "aws"
Start with letter or underscore
Space in variable name
server ip = "x"
Use server_ip
Wrong case
Print("hi")
Python is case-sensitive β print β Print
π― Interview Points
“Is Python statically or dynamically typed?”
β Dynamically typed. Types are determined at runtime, not declared. Usetype()to check.“What’s the difference between
=and==in Python?”
β=assigns a value.==compares two values. Critical distinction inifconditions.“What is
id()used for?”
β Returns memory address. Useful to understand if two variables point to the same object in memory.“Can a variable name start with a number?”
β No. It can start with a letter (A-Z, a-z) or underscore_.“What happens if you name a variable
listorprint?”
β You shadow the built-in. The original function becomes inaccessible in that scope β a common bug.“What is dynamic typing and why does it matter in DevOps scripts?”
β Python auto-detects types, so you must be careful when reading user input or config values β they come in asstrby default, even if they look like numbers.
π Knowledge Base (Quick Revision)
# ββ PRINT ββββββββββββββββββββββββββββββββββββββββββ
print("text") # basic
print(var, type(var)) # value + type
print("Label:", value) # label + value
# ββ VARIABLES & TYPES ββββββββββββββββββββββββββββββ
x = 10 # int
x = 3.14 # float
x = "hello" # str
x = True # bool
# ββ USEFUL BUILT-INS βββββββββββββββββββββββββββββββ
type(x) # what type is x?
id(x) # memory address of x
input("Enter: ") # get user input (returns str)
# ββ KEYWORDS βββββββββββββββββββββββββββββββββββββββ
import keyword
keyword.kwlist # list of all reserved words
# ββ IDENTIFIER RULES βββββββββββββββββββββββββββββββ
# β
letters, digits (not first), underscore
# β spaces, hyphens, special chars, keywords, starts with digit
# ββ BUILT-IN SAFETY ββββββββββββββββββββββββββββββββ
dir(__builtins__) # list ALL built-in names
# Never use these as variable names!
Enter fullscreen mode Exit fullscreen mode
ποΈ Practice Questions
Easy
- Write a Python script that stores your name, age, and city in variables and prints them with their data types.
- Try assigning
if = 10and run it. What error do you get? Now look upkeyword.kwlistand list 5 keywords you didn’t know. - What is the output of this code? Explain why:
a = "8080"
print(a, type(a))
Enter fullscreen mode Exit fullscreen mode
Medium
- Create variables for: EC2 instance type, region, number of replicas, and whether auto-scaling is enabled. Print each with its type. Now reassign
replicasfromintto astrβ what changes? - Write a script that takes a server name as input and prints:
"Connecting to: <server_name>". What type doesinput()always return? - Explain with code why
print = 100breaks things and how you would fix it without restarting Python.
DevOps-Focused
-
Config Validator Script: Write a script that stores 5 deployment config values (instance type, port, region, replica count, environment name). Print each value, its type, and its memory
id. Then changeportfrominttostr(e.g., from8080to"8080") and explain the real-world implication in a comment. -
Build Status Logger: Write a script that uses
input()to ask for a build status (SUCCESS/FAILED), stores it in a variable, and prints:"Pipeline result: <status> | Type: <type>". Add a comment explaining why you’d never name this variableinputorprint.
Python for DevOps & Cloud*
λ΅κΈ λ¨κΈ°κΈ°