Python Basics β `print()`, Variables, Identifiers, Data Types
Source: Dev.to
π Key Concepts
Concept One-Line Definition
print() 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
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)) #
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)
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
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)
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
β οΈ Common Mistake: Reassigning a variable to a different type accidentally can cause bugs in automation scripts.
Type Example DevOps Use Case
int 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
β 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!)
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. 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β]
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
Common built-ins to never overwrite: print, input, list, dict, str, int, len, open, type, id
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}β)
Mistake Example Fix
Typo in function name prin(β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
βIs Python statically or dynamically typed?β type() to check.
βWhatβs the difference between = and == in Python?β = assigns a value. == compares two values. Critical distinction in if conditions.
βWhat is id() used for?β
βCan a variable name start with a number?β _.
βWhat happens if you name a variable list or print?β
βWhat is dynamic typing and why does it matter in DevOps scripts?β str by 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!
Write a Python script that stores your name, age, and city in variables and prints them with their data types. Try assigning if = 10 and run it. What error do you get? Now look up keyword.kwlist and list 5 keywords you didnβt know. What is the output of this code? Explain why:
a = β8080β print(a, type(a))
Create variables for: EC2 instance type, region, number of replicas, and whether auto-scaling is enabled. Print each with its type. Now reassign replicas from int to a str β what changes? Write a script that takes a server name as input and prints: βConnecting to: β. What type does input() always return? Explain with code why print = 100 breaks things and how you would fix it without restarting Python. 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 change port from int to str (e.g., from 8080 to β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: | Type: β. Add a comment explaining why youβd never name this variable input or print. Python for DevOps & Cloud*