.py File:
π A .py file is a Python source code file used to write and store Python programs.
β¨ It allows you to:
π Write multiple lines of Python code in one file
πΎ Save your code permanently for future use
βΆοΈ Run the entire program whenever needed
π Reuse and modify the same code multiple times
π Debug and fix errors more easily in an organized way
ποΈ Build small to large Python applications
π Unlike the Python Shell, where code runs temporarily line by line, a .py file keeps your code saved and structured.
π οΈ You can create .py files using editors like VS Code, PyCharm, Sublime Text, or any text editor.
How to Open Python Shell
Open Command Prompt or Terminal
Type:
python(windows) orpython3(mac/linux)
If Python starts, you will see:
>>>This means Python Shell is ready.
Example
>>> 5 + 3
8
>>> print("Hello")
HelloMulti-line Example
>>> for i in range(3):
... print(i)Output:
0
1
2Exit Python Shell
exit() or quit()
<div class="infographic-pyshell">
<style>
.infographic-pyshell {
--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: 720px;
margin: 0 auto;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-pyshell[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
}
.infographic-pyshell *, .infographic-pyshell *::before, .infographic-pyshell *::after { box-sizing: border-box; }
.infographic-pyshell .ps-wrap { padding: 0 clamp(1rem, 4vw, 2rem) 1.5rem; }
.infographic-pyshell h1 {
font-family: var(--display-font);
font-weight: 600;
font-size: clamp(1.6rem, 5vw, 2.2rem);
text-align: center;
margin: 1.5rem 0 0.25rem;
letter-spacing: -0.01em;
}
.infographic-pyshell .ps-sub {
text-align: center;
color: var(--ink-soft);
font-size: 0.9rem;
margin: 0 0 1.5rem;
}
.infographic-pyshell .ps-caption {
text-align: center;
font-size: 0.92rem;
font-weight: 600;
color: var(--accent);
min-height: 1.3em;
margin-bottom: 0.7rem;
}
/* terminal β always a dark editor, independent of page theme */
.infographic-pyshell .device {
--ed-bg: #1e1e1e; --ed-bar: #2d2d2d; --ed-text: #d4d4d4; --ed-soft: #8a8a8a; --ed-ok: #6fdc8c;
background: var(--ed-bg); border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.infographic-pyshell .device-bar {
display: flex; align-items: center; gap: 0.4rem;
background: var(--ed-bar); padding: 0.55rem 0.75rem; border-bottom: 1px solid #3a3a3a;
font-size: 0.75rem; color: var(--ed-soft); font-weight: 600;
}
.infographic-pyshell .device-bar .dot { width: 10px; height: 10px; border-radius: 50%; background: #5a5a5a; }
.infographic-pyshell .device-bar .dot:nth-child(1) { background: #ff5f56; }
.infographic-pyshell .device-bar .title { flex: 1; text-align: center; margin-right: 26px; }
.infographic-pyshell .device-body {
padding: 1rem 1.1rem; font-family: var(--mono-font); font-size: 0.95rem;
color: var(--ed-text); min-height: 220px;
}
.infographic-pyshell .repl-line { display: flex; gap: 0.55rem; white-space: pre-wrap; line-height: 1.7; opacity: 0; animation: ps-fadein 0.3s ease forwards; }
@keyframes ps-fadein { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: translateY(0); } }
.infographic-pyshell .repl-line .p { color: var(--ed-ok); font-weight: 700; flex: none; }
.infographic-pyshell .repl-line.out { color: #ffffff; }
.infographic-pyshell .cursor-blink {
display: inline-block; width: 7px; height: 1.1em; background: var(--ed-ok);
vertical-align: text-bottom; animation: ps-blink 1s step-end infinite;
}
@keyframes ps-blink { 50% { opacity: 0; } }
.infographic-pyshell .ps-controls {
display: flex; align-items: center; justify-content: center; gap: 0.9rem; margin-top: 1.1rem;
}
.infographic-pyshell .ps-btn {
font: inherit; font-size: 0.85rem; font-weight: 700;
border: 1px solid var(--accent); background: var(--accent); color: #fff;
padding: 0.5rem 1.1rem; cursor: pointer; border-radius: 4px;
}
.infographic-pyshell .ps-btn.secondary {
background: var(--paper); color: var(--accent);
}
.infographic-pyshell .ps-btn:disabled {
opacity: 0.4; cursor: default; border-color: var(--line); background: var(--paper-alt); color: var(--ink-soft);
}
.infographic-pyshell .ps-progress {
text-align: center; font-size: 0.75rem; color: var(--ink-soft); margin-top: 0.6rem;
}
@media (prefers-reduced-motion: reduce) {
.infographic-pyshell *, .infographic-pyshell *::before, .infographic-pyshell *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ps-wrap">
<h1>The Python Shell</h1>
<p class="ps-sub">Type a line, get an answer instantly. Step through a real session.</p>
<p class="ps-caption" id="psCaption">Open your terminal to begin.</p>
<div class="device">
<div class="device-bar">
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="title">Terminal</span>
</div>
<div class="device-body" id="psLog"></div>
</div>
<div class="ps-controls">
<button class="ps-btn secondary" id="psBack">Back</button>
<button class="ps-btn" id="psNext">Next β</button>
<button class="ps-btn secondary" id="psRestart">Restart</button>
</div>
<p class="ps-progress" id="psProgress"></p>
</div>
<script>
(function () {
var root = document.currentScript.closest('.infographic-pyshell')
|| document.querySelector('.infographic-pyshell');
// page-theme detection (the terminal itself always stays dark)
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 log = root.querySelector('#psLog');
var caption = root.querySelector('#psCaption');
var progress = root.querySelector('#psProgress');
var nextBtn = root.querySelector('#psNext');
var backBtn = root.querySelector('#psBack');
var restartBtn = root.querySelector('#psRestart');
var steps = [
{ lines: [{ p: '$', t: 'python3' }], caption: 'Open your terminal and start Python.' },
{ lines: [{ p: '', t: 'Python 3.10.11 ...' }], caption: 'Python is ready β the prompt appears.' },
{ lines: [{ p: '>>>', t: '5 + 3' }, { p: '', t: '8', out: true }], caption: 'Math runs instantly, no file needed.' },
{ lines: [{ p: '>>>', t: 'print("Hello, World!")' }, { p: '', t: 'Hello, World!', out: true }], caption: 'print() shows text on screen.' },
{ lines: [
{ p: '>>>', t: 'for i in range(3):' },
{ p: '...', t: ' print(i)' },
{ p: '...', t: '' },
{ p: '', t: '0', out: true },
{ p: '', t: '1', out: true },
{ p: '', t: '2', out: true }
], caption: 'Loops run line by line too.' },
{ lines: [{ p: '>>>', t: 'exit()' }], caption: 'Type exit() when you\'re done.' }
];
var current = -1; // index of last revealed step
function renderUpTo(index) {
log.innerHTML = '';
for (var i = 0; i <= index; i++) {
steps[i].lines.forEach(function (l) {
var line = document.createElement('div');
line.className = 'repl-line' + (l.out ? ' out' : '');
var html = '';
if (l.p) html += '<span class="p">' + l.p + '</span>';
html += '<span>' + l.t + '</span>';
line.innerHTML = html;
log.appendChild(line);
});
}
// trailing blinking cursor, unless the session just exited
var sessionEnded = index === steps.length - 1;
if (!sessionEnded) {
var cursorPrompt = index < 1 ? '$' : '>>>';
var cursorLine = document.createElement('div');
cursorLine.className = 'repl-line';
cursorLine.innerHTML = '<span class="p">' + cursorPrompt + '</span><span class="cursor-blink"></span>';
log.appendChild(cursorLine);
}
caption.textContent = index >= 0 ? steps[index].caption : 'Open your terminal to begin.';
progress.textContent = index >= 0
? ('Step ' + (index + 1) + ' of ' + steps.length)
: ('Step 0 of ' + steps.length);
backBtn.disabled = index < 0;
nextBtn.disabled = index >= steps.length - 1;
nextBtn.textContent = index >= steps.length - 1 ? 'Done' : 'Next β';
log.scrollTop = log.scrollHeight;
}
nextBtn.addEventListener('click', function () {
if (current < steps.length - 1) {
current++;
renderUpTo(current);
}
});
backBtn.addEventListener('click', function () {
if (current > -1) {
current--;
renderUpTo(current);
}
});
restartBtn.addEventListener('click', function () {
current = -1;
renderUpTo(current);
});
renderUpTo(current);
})();
</script>
</div>Mode | Use Case | Example |
|---|---|---|
Python Shell | Quick testing, tryouts |
|
| Full applications, scripts | Save as |
Example:
Just like using a calculator for one-time math is quick (Python shell), but you write equations in a notebook when you want to keep them for later (Python file).