Printing Your First Python Program – Hello World
Let’s begin with the traditional first program: Hello World!
Using VS Code or any code editor:
Create a Python file in your project folder.
Example
test.pyhello.pyyourfilename.py
Write the following code:
print("Hello, World!") # Output: Hello, World!Need help creating the file or setting up the editor?
Check the previous chapter’s reference video.
Running the Program
Method 1: Using Terminal
Open the terminal and run:
python yourfilename.pyMethod 2: Using VS Code Run Button
Click the Run button at the top-right corner of VS Code.
<div class="infographic-runpy">
<style>
.infographic-runpy {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--mono-font: 'IBM Plex Mono', 'SFMono-Regular', Menlo, monospace;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', system-ui, sans-serif;
box-sizing: border-box;
max-width: 880px;
margin: 0 auto;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-runpy[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
}
.infographic-runpy *, .infographic-runpy *::before, .infographic-runpy *::after { box-sizing: border-box; }
.infographic-runpy .rp-wrap { padding: 0 clamp(1rem, 4vw, 2rem) 1.5rem; }
.infographic-runpy h1 {
font-family: var(--display-font);
font-weight: 600;
font-size: clamp(1.7rem, 5vw, 2.4rem);
text-align: center;
margin: 1.5rem 0 0.25rem;
letter-spacing: -0.01em;
}
.infographic-runpy .rp-sub {
text-align: center;
color: var(--ink-soft);
font-size: 0.92rem;
margin: 0 0 1.75rem;
}
/* flow */
.infographic-runpy .flow { display: flex; gap: 0; flex-wrap: wrap; align-items: stretch; }
.infographic-runpy .flow-step {
flex: 1 1 220px;
background: var(--paper-alt);
border: 1px solid var(--line);
padding: 1.1rem 1.1rem 1.25rem;
position: relative;
cursor: pointer;
font: inherit;
color: var(--ink);
text-align: left;
z-index: 1;
}
.infographic-runpy .flow-arrow {
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: center;
width: 2.2rem;
color: var(--accent);
font-size: 1.5rem;
line-height: 1;
background: var(--paper);
}
.infographic-runpy .flow-arrow svg { width: 22px; height: 22px; stroke: var(--accent); fill: none; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.infographic-runpy .flow-step .num {
font-family: var(--display-font); color: var(--accent); font-size: 1.4rem; line-height: 1;
}
.infographic-runpy .flow-step .step-title { font-weight: 700; margin: 0.35rem 0 0.6rem; font-size: 0.98rem; }
.infographic-runpy .flow-step .step-icon { width: 56px; height: 46px; }
.infographic-runpy .flow-step .step-icon svg { width: 100%; height: 100%; stroke: var(--ink-soft); fill: none; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.infographic-runpy .flow-step.is-open .step-icon svg { stroke: var(--accent); }
.infographic-runpy .flow-step .step-detail { display: none; margin-top: 0.7rem; font-size: 0.82rem; color: var(--ink-soft); line-height: 1.4; }
.infographic-runpy .flow-step.is-open .step-detail { display: block; }
.infographic-runpy .flow-step.is-open { border-color: var(--accent); }
@media (max-width: 700px) {
.infographic-runpy .flow { flex-direction: column; }
.infographic-runpy .flow-arrow { width: 100%; height: 1.8rem; }
.infographic-runpy .flow-arrow svg { transform: rotate(90deg); }
}
/* code snippet chip inside step 2 */
.infographic-runpy .code-chip {
font-family: var(--mono-font);
background: var(--paper);
border: 1px solid var(--line);
padding: 0.5rem 0.65rem;
font-size: 0.78rem;
margin-top: 0.6rem;
color: var(--ink);
}
.infographic-runpy .code-chip .kw { color: var(--accent); }
/* run demo (inside step 3) */
.infographic-runpy .run-demo { margin-top: 0.8rem; }
.infographic-runpy .method-toggle { display: flex; gap: 0.4rem; margin-bottom: 0.7rem; }
.infographic-runpy .method-btn {
font: inherit; font-size: 0.75rem; font-weight: 600;
background: var(--paper); border: 1px solid var(--line); color: var(--ink-soft);
padding: 0.35rem 0.65rem; cursor: pointer;
}
.infographic-runpy .method-btn.is-selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
/* editor/terminal chrome is intentionally always dark, like a real code editor,
independent of the page's light/dark theme */
.infographic-runpy .device {
--ed-bg: #1e1e1e;
--ed-bar: #2d2d2d;
--ed-text: #d4d4d4;
--ed-soft: #8a8a8a;
--ed-kw: #6fa8f5;
--ed-str: #ce9178;
--ed-line: #3a3a3a;
background: var(--ed-bg); border: 1px solid var(--line); overflow: hidden;
border-radius: 6px;
}
.infographic-runpy .device-bar {
display: flex; align-items: center; gap: 0.4rem;
background: var(--ed-bar); padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--ed-line);
font-size: 0.72rem; color: var(--ed-soft); font-weight: 600;
}
.infographic-runpy .device-bar .dot { width: 9px; height: 9px; border-radius: 50%; background: #5a5a5a; }
.infographic-runpy .device-bar .dot:nth-child(1) { background: #ff5f56; }
.infographic-runpy .device-body { padding: 0.8rem 0.9rem; font-family: var(--mono-font); font-size: 0.82rem; min-height: 74px; color: var(--ed-text); }
.infographic-runpy .device[data-method="vscode"] { display: none; }
.infographic-runpy .device[data-method="vscode"].is-visible { display: block; }
.infographic-runpy .device[data-method="terminal"].is-hidden { display: none; }
.infographic-runpy .prompt-line { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }
.infographic-runpy .prompt-line .caret { color: #6fdc8c; font-weight: 700; }
.infographic-runpy .run-trigger {
font: inherit; font-size: 0.72rem; font-weight: 700;
background: var(--accent); color: #fff; border: none;
border-radius: 3px;
padding: 0.3rem 0.6rem; cursor: pointer; margin-left: 0.3rem;
}
.infographic-runpy .vscode-topbar { display: flex; justify-content: flex-end; margin-bottom: 0.6rem; }
.infographic-runpy .run-play {
display: inline-flex; align-items: center; gap: 0.3rem;
background: #2a3a2f; color: #6fdc8c; border: 1px solid #3d5a44;
border-radius: 3px;
font: inherit; font-size: 0.72rem; font-weight: 700; padding: 0.25rem 0.55rem; cursor: pointer;
}
.infographic-runpy .vscode-code { color: var(--ed-text); }
.infographic-runpy .vscode-code .kw { color: var(--ed-kw); }
.infographic-runpy .vscode-code .str { color: var(--ed-str); }
.infographic-runpy .code-chip.term-str { color: var(--ce9178, #ce9178); }
.infographic-runpy .output-line {
margin-top: 0.65rem; padding-top: 0.6rem; border-top: 1px dashed var(--ed-line);
color: var(--ed-soft); min-height: 1.1em;
}
.infographic-runpy .output-line .out-text { color: #ffffff; font-weight: 600; }
.infographic-runpy .output-line .cursor-blink {
display: inline-block; width: 6px; height: 1em; background: #6fdc8c;
vertical-align: text-bottom; animation: rp-blink 1s step-end infinite;
}
@keyframes rp-blink { 50% { opacity: 0; } }
.infographic-runpy .rp-hint { text-align: center; font-size: 0.78rem; color: var(--ink-soft); margin-top: 1.75rem; }
@media (prefers-reduced-motion: reduce) {
.infographic-runpy *, .infographic-runpy *::before, .infographic-runpy *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="rp-wrap">
<p class="rp-sub">Three steps. Tap each one to open it.</p>
<div class="flow">
<!-- Step 1 -->
<button class="flow-step" data-step="1">
<div class="num">01</div>
<div class="step-icon">
<svg viewBox="0 0 56 46"><path d="M2,10 L2,40 C2,42 4,44 6,44 L50,44 C52,44 54,42 54,40 L54,16 C54,14 52,12 50,12 L26,12 L22,6 L6,6 C4,6 2,8 2,10 Z"/><path d="M28,20 L28,32 M22,26 L34,26"/></svg>
</div>
<div class="step-title">Create the file</div>
<div class="step-detail">
New file in your project folder, saved with a <strong>.py</strong> extension.
<div class="code-chip">hello.py</div>
</div>
</button>
<div class="flow-arrow" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M4,12 H20 M13,5 L20,12 L13,19"/></svg></div>
<!-- Step 2 -->
<button class="flow-step" data-step="2">
<div class="num">02</div>
<div class="step-icon">
<svg viewBox="0 0 56 46"><rect x="2" y="4" width="52" height="38" rx="3"/><line x1="2" y1="14" x2="54" y2="14"/><line x1="10" y1="22" x2="32" y2="22"/><line x1="10" y1="30" x2="24" y2="30"/></svg>
</div>
<div class="step-title">Write the code</div>
<div class="step-detail">
One line tells Python what to say.
<div class="code-chip"><span class="kw">print</span>(<span style="color:#8a6d3b">"Hello, World!"</span>)</div>
</div>
</button>
<div class="flow-arrow" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M4,12 H20 M13,5 L20,12 L13,19"/></svg></div>
<!-- Step 3 -->
<button class="flow-step" data-step="3">
<div class="num">03</div>
<div class="step-icon">
<svg viewBox="0 0 56 46"><rect x="4" y="6" width="48" height="34" rx="3"/><polygon points="24,16 24,30 36,23" fill="currentColor" stroke="none"/></svg>
</div>
<div class="step-title">Run it</div>
<div class="step-detail">
Two ways to launch the same file — try both.
<div class="run-demo">
<div class="method-toggle">
<button class="method-btn is-selected" data-method="terminal">Terminal</button>
<button class="method-btn" data-method="vscode">VS Code button</button>
</div>
<div class="device" data-method="terminal">
<div class="device-bar"><span class="dot"></span><span class="dot"></span><span class="dot"></span> Terminal</div>
<div class="device-body">
<div class="prompt-line">
<span class="caret">$</span>
<span>python hello.py</span>
<button class="run-trigger" data-trigger="1">Enter ⏎</button>
</div>
<div class="output-line" data-output="1"><span class="cursor-blink"></span></div>
</div>
</div>
<div class="device" data-method="vscode">
<div class="device-bar"><span class="dot"></span><span class="dot"></span><span class="dot"></span> hello.py — VS Code</div>
<div class="device-body">
<div class="vscode-topbar">
<button class="run-play" data-trigger="2">▶ Run</button>
</div>
<div class="vscode-code"><span class="kw">print</span>(<span class="str">"Hello, World!"</span>)</div>
<div class="output-line" data-output="2"><span class="cursor-blink"></span></div>
</div>
</div>
</div>
</div>
</button>
</div>
<p class="rp-hint">Same file, same output — pick whichever fits your workflow.</p>
</div>
<script>
(function () {
var root = document.currentScript.closest('.infographic-runpy')
|| document.querySelector('.infographic-runpy');
// dark mode detection
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
var match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) root.setAttribute('data-theme', 'dark');
var reduceMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// flow step open/close
root.querySelectorAll('.flow-step').forEach(function (step) {
step.addEventListener('click', function (e) {
if (e.target.closest('.run-demo')) return; // don't collapse when interacting inside step 3
var wasOpen = step.classList.contains('is-open');
root.querySelectorAll('.flow-step').forEach(function (s) { s.classList.remove('is-open'); });
if (!wasOpen) step.classList.add('is-open');
});
});
// method toggle inside step 3
root.querySelectorAll('.method-btn').forEach(function (btn) {
btn.addEventListener('click', function (e) {
e.stopPropagation();
var method = btn.dataset.method;
root.querySelectorAll('.method-btn').forEach(function (b) { b.classList.toggle('is-selected', b === btn); });
root.querySelectorAll('.device').forEach(function (d) {
d.classList.toggle('is-visible', d.dataset.method === method);
d.classList.toggle('is-hidden', d.dataset.method !== method);
});
});
});
// set initial visibility (terminal shown, vscode hidden by default CSS already handles it)
function typeOutput(el, text) {
el.innerHTML = '';
var span = document.createElement('span');
span.className = 'out-text';
el.appendChild(span);
var cursor = document.createElement('span');
cursor.className = 'cursor-blink';
el.appendChild(cursor);
if (reduceMotion) {
span.textContent = text;
return;
}
var i = 0;
var timer = setInterval(function () {
span.textContent = text.slice(0, i + 1);
i++;
if (i >= text.length) clearInterval(timer);
}, 45);
}
root.querySelectorAll('[data-trigger]').forEach(function (btn) {
btn.addEventListener('click', function (e) {
e.stopPropagation();
var id = btn.dataset.trigger;
var out = root.querySelector('[data-output="' + id + '"]');
typeOutput(out, 'Hello, World!');
});
});
})();
</script>
</div>