✎ Edit content·DAY 015 · POST 4 OF 5 · Code Example

Python Basics in 8 Slides

Python · 11 slides
DAY 015 · POST 4 OF 5
(REMINDER)
DAY 015
Python Basics in Code
@saurav_dnj_24github.com/SauravDnj · linkedin.com/in/sauravdnj
1 / 11

Theme

Palette

Download

4K — sharpest, slowest
🎬 Video options
Preparing preview…
Live preview · loops the “none” effect
All rendering runs in your browser. No server, no cost, no upload. MP4/WebM = full motion + effects · GIF = lightweight loop · PNG/PDF = static for the Instagram & LinkedIn carousel.

Caption (tap to copy)

📖 Deep dive (full written explanation)

The slides stay clean and scannable. Here's the in-depth explanation behind each one — great for the blog version, show notes, or studying the topic properly.
Slide 1 · Python Basics in Code

This is the hands-on post, and it's deliberately code-heavy. You cannot learn to program by reading about programming any more than you can learn to swim by reading about water. Every snippet here is short, correct, and runnable — your job is to type each one, run it, then change something and rerun.

We walk the essential core in order: variables and types, the four big data structures, loops and conditionals, functions, and finally comprehensions. By the end you'll have touched every fundamental building block of a Python program.

Slide 2 · 1. Variables, types, f-strings

This first snippet covers variables, the four scalar types, and f-strings in one go. Notice again the absence of type declarations — Python infers str, int, float, and bool from the literals. The f-string is the modern, readable way to build text: prefix a string with f and drop expressions inside braces.

The `:.2f` inside the brace is a format specifier — it formats pi as a float with two decimal places. Format specifiers are a small language of their own, but `:.2f` for 'two decimals' and `:,` for thousands separators will cover most of what you need early on. Master f-strings and you'll never go back to clunky string concatenation.

Slide 3 · 2. The four structures

The four data structures are the workhorses of Python and this snippet introduces all of them. A list is an ordered, mutable sequence — your default when you need a collection you'll modify. A tuple is like a list but immutable, ideal for fixed groupings like coordinates. A dict maps keys to values for fast lookup. A set holds unique elements with no order.

The access patterns differ meaningfully: lists and tuples index by position (fruits[0]), dicts index by key (scores["cs"]), and sets don't index at all because they're unordered. Picking the right structure for the job is one of the most consequential decisions in everyday Python, as we argued in post two.

Slide 4 · 3. Loops and conditionals

This snippet combines dict iteration with the full if/elif/else chain, showing how loops and conditionals work together. The .items() method yields key-value pairs, which the for loop unpacks into subject and score in one step — a hugely common pattern in data work.

The conditional ladder demonstrates that Python evaluates branches top to bottom and takes the first match. Order matters: if you checked score >= 80 before score >= 90, a 95 would wrongly match 'solid' first. Run this and trace each score through the branches to confirm the routing, then try reordering the conditions to see the bug appear.

Slide 5 · 4. Functions

Functions are how you package logic for reuse, and this snippet shows the full anatomy: the def keyword, parameters with a default value (round_to=2), a docstring describing what it does, a body, and a return statement handing back the result.

Two things to notice. The default argument means you can call average with one or two arguments — call it without round_to and it uses 2. The docstring in triple quotes is documentation that tools and help() can read; writing one for every function is a habit worth building early. This single example contains nearly everything you need to know about defining functions.

Slide 6 · What a function does

The pipeline diagram captures the essence of every function: inputs flow in as arguments, the body transforms them, and return sends a value out. This input-process-output shape is universal — it describes the average function above and the most complex neural network forward pass alike.

Hold this picture as you write functions. A good function has a clear set of inputs, does one well-defined transformation, and returns a clear output, with no hidden surprises. When your functions match this clean shape, your programs become composable: outputs of one become inputs to the next, like links in a chain.

Slide 7 · 5. Comprehensions

Comprehensions are Python's signature feature for transforming and filtering collections concisely. This snippet shows all three flavors: a list comprehension that transforms (squares), one that filters (evens), and a dict comprehension that builds key-value pairs. Each replaces a multi-line loop with a single readable expression.

The structure reads almost like English: 'n squared, for each n in nums.' Add an if clause and it becomes 'for each n in nums, if n is even.' Comprehensions are everywhere in real Python and ML code, so getting comfortable with this syntax pays off immediately. Just don't over-nest them — past two clauses, a plain loop is clearer.

Slide 8 · Comprehension, unrolled

The trace diagram unrolls a comprehension to demystify it. The expression [n**2 for n in nums] isn't magic — it walks through each value of n, evaluates n**2, and collects the results into a new list. Seeing it expanded as n=1 yields 1, n=2 yields 4, and so on makes the compact syntax click.

Whenever a comprehension confuses you, mentally unroll it like this into the equivalent loop. The comprehension and the loop produce identical results; the comprehension just states the intent more directly. Once the unrolling becomes automatic, you'll read and write comprehensions as fast as plain assignments.

Slide 9 · Runnable takeaways

These takeaways distill the hands-on post into habits. Reach for f-strings over concatenation. Pick the data structure that fits the job rather than defaulting to a list for everything. Remember that for...in iterates anything iterable — strings, lists, dicts, files, generators. Remember that functions return None unless you explicitly return.

And lean on comprehensions to replace simple transform-and-filter loops. None of these are advanced; they're the everyday vocabulary of fluent Python. Practicing them on small examples like the ones above builds the muscle memory you'll use on every real project.

Slide 10 · Off-by-one with range

The range off-by-one is the most common numeric stumble for beginners, and it stems from two design choices that are consistent once you see them. range(n) is half-open — it includes 0 and excludes n, yielding exactly n values. Indexing also starts at 0, so a list of length 3 has valid indices 0, 1, and 2; asking for index 3 raises IndexError.

This half-open convention is actually elegant: range(len(lst)) gives exactly the valid indices, and slicing lst[a:b] gives b minus a elements. The pain is purely in the adjustment period. Count from zero, treat right bounds as exclusive, and the off-by-one errors stop happening.

Slide 11 · Save this. Follow for Day 16.

That closes the hands-on post. You've now written and run the core of Python — variables, structures, control flow, functions, and comprehensions. This is genuinely the bulk of what you'll use day to day; everything else builds on these pieces.

The final post of the day turns to the traps — the specific, repeatable mistakes that cost beginners the most hours. Learning them now means you get the lesson without paying the full tuition in lost debugging time.

🎨 AI image prompt (matches this theme + palette)

Paste into Midjourney, DALL·E, Ideogram, etc. to generate an on-brand image, then upload it on the Edit content page. The prompt updates automatically with the selected theme + palette.