<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mirza Ishraq</title><description>Writing and projects from Mirza Ishraq, a software engineer building AI agents, RAG systems, and data pipelines.</description><link>https://my-space.io/</link><atom:link href="https://my-space.io/rss.xml" rel="self" type="application/rss+xml"/><item><title>How to Get the Most Out of a $20 Claude Plan as a Developer</title><link>https://my-space.io/blog/claude-plan/</link><guid isPermaLink="true">https://my-space.io/blog/claude-plan/</guid><description>I started on Pro and barely used it. Here is how to get the most from the $20 Claude plan as a developer, checked against Anthropic&apos;s docs.</description><pubDate>Wed, 23 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The $20 Claude plan is Pro, and it includes Claude Code. Claude Code on Pro now defaults to Opus 5.5 at medium effort, and with the September 22 launch Anthropic said it is raising five-hour limits on the same plans.&lt;/p&gt;
&lt;p&gt;You get the most out of it by spending fewer tokens per task, not by using Claude less. That means one clear message instead of five, a fresh context between tasks, the right model and effort for the job, and no cold-cache surprises after a break.&lt;/p&gt;
&lt;p&gt;One thing up front. I started on Pro, brand new to Claude Code, and I’m on the $200 Max plan now. The prices and limits below come from Anthropic’s pages, and my usage numbers come from Max.&lt;/p&gt;
&lt;p&gt;Every factual claim about Anthropic’s plans and tools links to the page it came from, and I checked those pages on September 23, 2026. Sonnet 5.5 and Haiku 5.5 are due “in the coming weeks,” so parts of this will age. Anthropic doesn’t publish Pro token allowances, and I won’t invent one.&lt;/p&gt;
&lt;p&gt;Here is the scorecard before the detail.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Habit&lt;/th&gt;
&lt;th&gt;Why it helps on a $20 plan&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Know where your config lives&lt;/td&gt;
&lt;td&gt;Settings and &lt;code&gt;CLAUDE.md&lt;/code&gt; files do nothing if they are in the wrong place.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One message: task, finish line, stop rule&lt;/td&gt;
&lt;td&gt;Fewer turns, and every turn re-reads the whole conversation.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/clear&lt;/code&gt; between tasks&lt;/td&gt;
&lt;td&gt;It costs nothing and drops stale context.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Start effort at &lt;code&gt;medium&lt;/code&gt;, then experiment&lt;/td&gt;
&lt;td&gt;You can’t turn thinking off on Opus 5.5. Effort is the dial.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;opusplan&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Opus 5.5 plans, Sonnet 5 types.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Skip Fable, fast mode and the advisor&lt;/td&gt;
&lt;td&gt;Each one bills more or reads the transcript uncached.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use ultracode for one job, not all day&lt;/td&gt;
&lt;td&gt;It applies to every task in the session and fans out into many agents.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mind the one-hour cache&lt;/td&gt;
&lt;td&gt;The first message after a long break re-reads everything.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;what-does-a-20-claude-plan-give-a-developer&quot;&gt;What does a $20 Claude plan give a developer?&lt;/h2&gt;
&lt;p&gt;Pro costs $20 a month, or $17 a month if you pay $200 up front, and it &lt;a href=&quot;https://claude.com/pricing&quot;&gt;includes Claude Code&lt;/a&gt;. The plan also covers Claude Code inside VS Code, Cursor and JetBrains IDEs, per &lt;a href=&quot;https://support.claude.com/en/articles/11145838-use-claude-code-with-your-pro-or-max-plan&quot;&gt;Anthropic’s Help Center&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Claude apps and Claude Code draw from one shared pool. Limits reset on a rolling five-hour session window, and there is a &lt;a href=&quot;https://support.claude.com/en/articles/8325606-what-is-the-pro-plan&quot;&gt;weekly limit that applies across all models&lt;/a&gt;. A long chat in the app and a long run in the terminal come out of the same tank.&lt;/p&gt;
&lt;p&gt;The default model changed recently. The &lt;a href=&quot;https://code.claude.com/docs/en/model-config&quot;&gt;model docs&lt;/a&gt; say Pro now defaults to Opus 5.5, with &lt;code&gt;medium&lt;/code&gt; as its default effort. Before Claude Code v2.1.280, Pro defaulted to Sonnet 5, so on an older version you may still see Sonnet.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.anthropic.com/claude-opus-5-5&quot;&gt;Opus 5.5 announcement&lt;/a&gt; adds one line: “we’re increasing five-hour usage limits on Pro, Max, Team, and seat-based Enterprise plans.” It gives no numbers.&lt;/p&gt;
&lt;p&gt;Several blog posts quote “about 45 prompts” or “44,000 tokens” per window. I couldn’t find either in the Anthropic pages I read, which describe Pro only as “more usage” than Free. I’m leaving both out. Treat any exact Pro number as a guess unless it links to Anthropic.&lt;/p&gt;
&lt;h2 id=&quot;what-did-i-get-wrong-on-my-first-pro-plan&quot;&gt;What did I get wrong on my first Pro plan?&lt;/h2&gt;
&lt;p&gt;I started on Pro, and I was new to Claude Code. I didn’t know the tweaks or the ins and outs: the global config, the &lt;code&gt;.claude&lt;/code&gt; folder, &lt;code&gt;settings.json&lt;/code&gt;, &lt;code&gt;CLAUDE.md&lt;/code&gt;. I wasn’t following Anthropic’s guidelines either, so I was barely able to use my $20 plan.&lt;/p&gt;
&lt;p&gt;When I hit the limit, I couldn’t ask Claude anything else, so I reviewed the code it had generated by hand. Those were my early days of AI-assisted coding, and I took help from ChatGPT.&lt;/p&gt;
&lt;p&gt;If that sounds familiar, here is the map I didn’t have. This is where Claude Code’s &lt;a href=&quot;https://code.claude.com/docs/en/settings&quot;&gt;settings&lt;/a&gt; and &lt;a href=&quot;https://code.claude.com/docs/en/memory&quot;&gt;memory&lt;/a&gt; files live:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;~/.claude/settings.json&lt;/code&gt;&lt;/strong&gt; is your user settings, for you in every project. Claude Code creates it the first time you change an option in &lt;code&gt;/config&lt;/code&gt; that it stores there, such as the theme.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.claude/settings.json&lt;/code&gt;&lt;/strong&gt; inside a repo is the shared project file, for everyone who clones it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.claude/settings.local.json&lt;/code&gt;&lt;/strong&gt; is your personal override for one project. Claude Code keeps it out of git when it creates the file. If you make it by hand, add it to &lt;code&gt;.gitignore&lt;/code&gt; yourself.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;~/.claude/CLAUDE.md&lt;/code&gt;&lt;/strong&gt; holds your personal instructions for every project.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;./CLAUDE.md&lt;/code&gt; or &lt;code&gt;./.claude/CLAUDE.md&lt;/code&gt;&lt;/strong&gt; holds the project’s instructions, shared with your team through version control.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When the same key shows up in more than one settings file, the higher level wins. Project local beats shared project, and both beat your user settings.&lt;/p&gt;
&lt;p&gt;Two traps I’d have avoided. First, &lt;code&gt;CLAUDE.md&lt;/code&gt; is context, not enforcement. Claude treats it “as context, not enforced configuration,” so to block an action for certain, use a &lt;code&gt;PreToolUse&lt;/code&gt; hook.&lt;/p&gt;
&lt;p&gt;Second, keep it short. The size guidance says: “Longer files consume more context and reduce adherence.” Aim for under 200 lines, and run &lt;code&gt;/context&lt;/code&gt; to see which memory files loaded.&lt;/p&gt;
&lt;p&gt;The rest of this post is the guideline pass I skipped.&lt;/p&gt;
&lt;h2 id=&quot;what-actually-eats-your-claude-plan-limits&quot;&gt;What actually eats your Claude plan limits?&lt;/h2&gt;
&lt;p&gt;Context, mostly. &lt;a href=&quot;https://code.claude.com/docs/en/costs&quot;&gt;Claude Code sends your full conversation with every request&lt;/a&gt;. So “a one-line question in a session that has been open all day still draws usage for the whole conversation.”&lt;/p&gt;
&lt;p&gt;Every tool call is another request carrying the same history. That is why one wandering session costs more than three short ones. I broke down the numbers in &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;my Claude Code cost post&lt;/a&gt;, including why cache reads make the re-read cheap but not free.&lt;/p&gt;
&lt;p&gt;Anthropic’s &lt;a href=&quot;https://support.claude.com/en/articles/9797557-usage-limit-best-practices&quot;&gt;usage limit guide&lt;/a&gt; lists several other inputs, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Message length&lt;/li&gt;
&lt;li&gt;Attached file size&lt;/li&gt;
&lt;li&gt;Conversation length&lt;/li&gt;
&lt;li&gt;Tool use&lt;/li&gt;
&lt;li&gt;Model choice&lt;/li&gt;
&lt;li&gt;Effort level&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So the levers are what you send, how long the chat runs, and which model and effort reads it.&lt;/p&gt;
&lt;p&gt;Then there is the cache clock. On a subscription, &lt;a href=&quot;https://code.claude.com/docs/en/prompt-caching#cache-lifetime&quot;&gt;the main conversation’s cache lives for one hour&lt;/a&gt; while you are inside plan usage. Come back after a longer break and the first message reprocesses everything.&lt;/p&gt;
&lt;p&gt;There are two catches. Subagents get a five-minute cache even on a subscription. And once you pass your limit and draw on usage credits, the main conversation drops to five minutes too.&lt;/p&gt;
&lt;p&gt;Background work counts as well. A &lt;code&gt;/loop&lt;/code&gt; or &lt;a href=&quot;https://code.claude.com/docs/en/costs#why-usage-climbs-in-a-long-session&quot;&gt;scheduled task&lt;/a&gt; fires on its interval even while you are away, and it sends your full context each time.&lt;/p&gt;
&lt;p&gt;One more fact. You can’t turn thinking off on Opus 5.5, and thinking tokens are &lt;a href=&quot;https://code.claude.com/docs/en/costs#adjust-extended-thinking&quot;&gt;billed as output tokens&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So effort is the only dial you get. On a plan, &lt;code&gt;/usage&lt;/code&gt; shows what your limits went to and flags long context or cache misses once one reaches 10 percent of recent use. Run it before you change anything.&lt;/p&gt;
&lt;p&gt;You can also see the five-hour and weekly bars under Settings &amp;gt; Usage on claude.ai.&lt;/p&gt;
&lt;h2 id=&quot;should-you-change-the-effort-level-on-opus-55&quot;&gt;Should you change the effort level on Opus 5.5?&lt;/h2&gt;
&lt;p&gt;Start at &lt;code&gt;medium&lt;/code&gt;, then play with it. In the &lt;a href=&quot;https://code.claude.com/docs/en/model-config#adjust-effort-level&quot;&gt;model docs&lt;/a&gt;, &lt;code&gt;medium&lt;/code&gt; “reduces token usage for cost-sensitive work that can trade off some intelligence,” and &lt;code&gt;low&lt;/code&gt; is for “short, scoped, latency-sensitive tasks that are not intelligence-sensitive.”&lt;/p&gt;
&lt;p&gt;Anthropic’s own &lt;a href=&quot;https://claude.dev/blog/getting-the-most-out-of-opus-5-5/&quot;&gt;Opus 5.5 playbook&lt;/a&gt;, written by Addy Osmani, has one useful data point. “One early tester said Opus 5.5 at its lowest effort caught more bugs than Opus 5 at high effort, with fewer false alarms.”&lt;/p&gt;
&lt;p&gt;One tester is an anecdote, not a benchmark. It is still a fair reason to try &lt;code&gt;low&lt;/code&gt; on scoped work like a review or a rename.&lt;/p&gt;
&lt;p&gt;The right level depends on your work, so try &lt;code&gt;low&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt; and &lt;code&gt;high&lt;/code&gt; on the same kind of task and read &lt;code&gt;/usage&lt;/code&gt; afterward. Test &lt;code&gt;max&lt;/code&gt; before adopting it broadly. It “may show diminishing returns and is prone to overthinking.”&lt;/p&gt;
&lt;p&gt;Switching is cheap on this model. On Opus 5.5 with a subscription, changing effort mid-session keeps the cache. On most other models it doesn’t.&lt;/p&gt;
&lt;p&gt;Now a mismatch I found in my own settings. My user settings have &lt;code&gt;&quot;effortLevel&quot;: &quot;high&quot;&lt;/code&gt;. I’m on a Max plan, and that isn’t a recommendation for a $20 plan.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://code.claude.com/docs/en/model-config#adjust-effort-level&quot;&gt;model docs&lt;/a&gt; say a top-level &lt;code&gt;effortLevel&lt;/code&gt; in your user settings “doesn’t count for Opus 5.5.” Opus 5.5 should start at &lt;code&gt;medium&lt;/code&gt; until you pick a level with &lt;code&gt;/effort&lt;/code&gt; or the &lt;code&gt;/model&lt;/code&gt; picker.&lt;/p&gt;
&lt;p&gt;My own session log disagrees. On Claude Code 2.1.281, 52 Opus 5.5 turns in one session recorded &lt;code&gt;high&lt;/code&gt;, both as the effort and as a per-turn effort. &lt;code&gt;/effort&lt;/code&gt; also shows &lt;code&gt;high&lt;/code&gt; in my session. That matches what my settings file says, not what the docs describe.&lt;/p&gt;
&lt;p&gt;I can’t tell whether the docs lag the software or something else set the level, and the log format isn’t documented. So don’t take my word or the docs’ word. If your default is &lt;code&gt;high&lt;/code&gt; on a $20 plan, Opus 5.5 may be running above its &lt;code&gt;medium&lt;/code&gt; default and using more of your limit.&lt;/p&gt;
&lt;p&gt;Under &lt;code&gt;opusplan&lt;/code&gt;, &lt;code&gt;/effort&lt;/code&gt; shows the model you are on right now, so check it in plan mode to see Opus 5.5’s level. Here is how to see what your session really uses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Run &lt;code&gt;/effort&lt;/code&gt; with no arguments.&lt;/strong&gt; It opens an interactive slider. Press Enter to save a level as your default, or &lt;code&gt;s&lt;/code&gt; to apply it to this session only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read the session header.&lt;/strong&gt; It shows the level next to the model name, for example “with low effort,” and the footer briefly shows it at startup and when it changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use a status line.&lt;/strong&gt; Its &lt;code&gt;effort.level&lt;/code&gt; field reports the live level. Ultracode is not a distinct level there and reports as &lt;code&gt;xhigh&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clear a saved level.&lt;/strong&gt; &lt;code&gt;/effort auto&lt;/code&gt; removes the level you saved for the active model.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Claude desktop reads the same settings files as the terminal, so the same &lt;code&gt;effortLevel&lt;/code&gt; line applies there. I couldn’t find where the desktop docs put the effort control. Mine currently shows ultracode, which is a Claude Code setting that sends &lt;code&gt;xhigh&lt;/code&gt; plus workflows, not a level of its own.&lt;/p&gt;
&lt;p&gt;To pin a level for one model, use &lt;code&gt;modelSettings&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;modelSettings&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;claude-opus-5-5&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;effortLevel&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;medium&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Change the value when &lt;code&gt;/usage&lt;/code&gt; gives you a reason.&lt;/p&gt;
&lt;p&gt;There is one more cheap win. The playbook says Opus 5.5 “always thinks before it replies, and it decides how much,” so “think carefully” lines are dead weight.&lt;/p&gt;
&lt;p&gt;Anthropic tested this in a chat product. Removing one “made replies start sooner, with no clear drop in quality.” I searched my project &lt;code&gt;CLAUDE.md&lt;/code&gt; and my &lt;code&gt;/wrap&lt;/code&gt; and &lt;code&gt;write-post&lt;/code&gt; skills for lines like that and found none. Yours might have some.&lt;/p&gt;
&lt;h2 id=&quot;which-model-should-do-which-job&quot;&gt;Which model should do which job?&lt;/h2&gt;
&lt;p&gt;Pick the cheapest one that can do the job. Anthropic’s &lt;a href=&quot;https://support.claude.com/en/articles/14552983-models-usage-and-limits-in-claude-code&quot;&gt;Help Center&lt;/a&gt; says Sonnet suits the large majority of coding work. Opus is for harder problems like large refactors, and it uses more quota. Haiku is for quick lookups and simple edits.&lt;/p&gt;
&lt;p&gt;For a rough sense of the gap, look at &lt;a href=&quot;https://platform.claude.com/docs/en/about-claude/pricing&quot;&gt;API list prices&lt;/a&gt;: $4 in and $20 out per million tokens for Opus 5.5, against $2 and $10 for Sonnet 5. That is API pricing, not plan quota. Read it as a ratio of two to one, not a promise.&lt;/p&gt;
&lt;p&gt;That is the logic behind &lt;code&gt;opusplan&lt;/code&gt;. It runs Opus 5.5 in plan mode and Sonnet 5 for execution.&lt;/p&gt;
&lt;p&gt;My usage shows the split working. On September 23 so far, ccusage put Sonnet 5 at about 52 percent of my tokens and Opus 5.5 at about 30 percent. That is a Max account and list-price accounting, so it shows the split, not what Pro would allow.&lt;/p&gt;
&lt;p&gt;One catch, covered in my &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;cost post&lt;/a&gt;. Each plan-mode toggle is a model switch, and each model has its own cache. So &lt;code&gt;opusplan&lt;/code&gt; is cheapest when the context is small at the switch.&lt;/p&gt;
&lt;h2 id=&quot;how-has-opus-55-felt-so-far&quot;&gt;How has Opus 5.5 felt so far?&lt;/h2&gt;
&lt;p&gt;Since launch, it has felt faster and more focused. Anthropic says it generates output &lt;a href=&quot;https://www.anthropic.com/claude-opus-5-5&quot;&gt;more than 30 percent faster than Opus 5&lt;/a&gt;, so that fits, though I didn’t time anything.&lt;/p&gt;
&lt;p&gt;I also haven’t drained my limits, even after some heavy testing. My usage screen right now shows the current session at 12 percent and the weekly limit at 71 percent, with the reset on Friday morning. So it is not untouched, and a heavy week does show up.&lt;/p&gt;
&lt;p&gt;For scale, ccusage counts 5.76 billion tokens on my account since Friday, September 18, which is roughly the same window as that weekly bar. About 97 percent are cache reads, meaning conversations being re-read. Fresh input and output add up to about 1 percent: around 37 million input and 22.5 million output tokens.&lt;/p&gt;
&lt;p&gt;I can’t turn any of that into a Pro number. I found nothing from Anthropic on how tokens map to the bar, the bar also counts claude.ai, and I have no figures from my Pro days.&lt;/p&gt;
&lt;p&gt;Read all of that with two caveats. I’m on Max, which gives &lt;a href=&quot;https://claude.com/pricing&quot;&gt;at least 5x Pro’s usage&lt;/a&gt;. And most of my work stayed on Sonnet 5 through &lt;code&gt;opusplan&lt;/code&gt;, so this was not an Opus-only test.&lt;/p&gt;
&lt;p&gt;I’m not on Pro now, so I can’t tell you how Opus 5.5 feels on $20. That is why I’d watch &lt;code&gt;/usage&lt;/code&gt; in your first week instead of trusting my impression.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-hand-opus-55-a-task-without-wasting-turns&quot;&gt;How do you hand Opus 5.5 a task without wasting turns?&lt;/h2&gt;
&lt;p&gt;Give it the whole task in one message. Anthropic’s playbook says to name what “done” looks like and when you want it to stop and ask, then let it work. That works best when you already know what you want built, which is the gap between AI-assisted coding and &lt;a href=&quot;/blog/vibe-coding/&quot;&gt;vibe coding&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;My read on why that saves usage: every extra turn re-sends your whole conversation. Five small messages are five full reads. Here is the shape, with a made-up project:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Add rate limiting to the login route.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Done means: 5 attempts per minute per IP, the new tests pass, and&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;npm run check is clean.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Stop and ask me only if a test fails and you can&apos;t explain why.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three more habits from the playbook save turns too.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Type a follow-up while it runs.&lt;/strong&gt; If you remember something mid-run, type it and press Enter instead of restarting. In the playbook’s words, “Runs are longer now, so a restart costs more.”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Keep the task list in a file.&lt;/strong&gt; Ask Claude to keep a checklist in &lt;code&gt;TASKS.md&lt;/code&gt;. A long run fills the context window, Claude Code then summarizes older turns, and a list in a file survives that.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tell it which stops you want.&lt;/strong&gt; Opus 5.5 sometimes stops to report or asks “Want me to continue?” Each stop costs you another turn, and every turn re-reads the context.&lt;/p&gt;
&lt;p&gt;A short rule in &lt;code&gt;CLAUDE.md&lt;/code&gt; fixes most of it. This one is adapted from the playbook:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;When a step doesn&apos;t need my input, keep going. Put status notes in the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;same message as your next action.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Stop and ask only when you can&apos;t continue without me, or before&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;anything destructive: deleting data, force-pushing, or changing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;anything outside this repository.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keep permission prompts on for destructive commands anyway. &lt;a href=&quot;/blog/claude-code-auto-mode/&quot;&gt;Claude Code auto mode&lt;/a&gt; adds a second wall: a classifier that reviews risky actions before they run.&lt;/p&gt;
&lt;p&gt;Two habits from the Help Center are worth adding. Point Claude at files by path instead of pasting them, and ask for a plan before a big change. A plan is cheap, and a wrong multi-file diff is not.&lt;/p&gt;
&lt;h2 id=&quot;when-should-you-clear-compact-or-rewind&quot;&gt;When should you clear, compact or rewind?&lt;/h2&gt;
&lt;p&gt;Clear between unrelated tasks. The costs docs put it plainly: “When you want a fresh start instead of continuity, &lt;code&gt;/clear&lt;/code&gt; costs nothing.” Run &lt;code&gt;/rename&lt;/code&gt; first if you want to &lt;code&gt;/resume&lt;/code&gt; the session later.&lt;/p&gt;
&lt;p&gt;Compact at a natural break, not in the middle of a task. Compaction is itself a request that reads the whole conversation. After a break longer than the cache lifetime, it reprocesses all of it uncached.&lt;/p&gt;
&lt;p&gt;To abandon a path, use &lt;code&gt;/rewind&lt;/code&gt;. It truncates back to a prefix that is already cached instead of building a new one the way compaction does.&lt;/p&gt;
&lt;p&gt;Back from a long break? On Pro and Max, Claude Code offers to resume from a summary, so later requests don’t carry the full history. Say yes unless you need the detail.&lt;/p&gt;
&lt;p&gt;Keep the always-loaded stuff small too. Anthropic suggests a &lt;code&gt;CLAUDE.md&lt;/code&gt; under 200 lines, with workflow instructions moved into skills that load only when invoked.&lt;/p&gt;
&lt;p&gt;Run &lt;code&gt;/mcp&lt;/code&gt; and disable servers you aren’t using. I measured what those cost in the &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;cost post&lt;/a&gt;. One gotcha: editing &lt;code&gt;CLAUDE.md&lt;/code&gt; mid-session doesn’t apply until &lt;code&gt;/clear&lt;/code&gt;, &lt;code&gt;/compact&lt;/code&gt; or a restart.&lt;/p&gt;
&lt;h2 id=&quot;which-claude-code-features-should-a-20-plan-skip&quot;&gt;Which Claude Code features should a $20 plan skip?&lt;/h2&gt;
&lt;p&gt;Four of them cost more than they look.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fable.&lt;/strong&gt; It isn’t the default on any plan. On some plans it bills to &lt;a href=&quot;https://code.claude.com/docs/en/model-config#fable-and-usage-credits&quot;&gt;usage credits&lt;/a&gt;, and the &lt;code&gt;/model&lt;/code&gt; picker shows “Requires usage credits” on its row. On my Max plan, the usage screen shows a separate weekly limit for Fable instead. The docs I read don’t say which case Pro is in, so check the picker before you select it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fast mode.&lt;/strong&gt; The playbook says it “needs extra usage turned on, and it costs more per token than standard mode.” Turning it on mid-session also causes a cache miss. API list price is $8 in and $40 out per million tokens, against $4 and $20 in standard mode. If you use it, turn it on at the start of a session.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The advisor.&lt;/strong&gt; Each call re-reads the full transcript with no caching, and on subscriptions the &lt;a href=&quot;https://code.claude.com/docs/en/advisor&quot;&gt;advisor docs&lt;/a&gt; say advisor usage counts toward your plan limits. I keep &lt;code&gt;advisorModel&lt;/code&gt; on Opus, but that is a quality purchase on a Max plan. On $20, &lt;code&gt;/advisor off&lt;/code&gt; is the first thing to try if you are short on usage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Agent teams.&lt;/strong&gt; &lt;a href=&quot;https://code.claude.com/docs/en/costs#agent-team-token-costs&quot;&gt;Agent teams&lt;/a&gt; use about seven times the tokens of a standard session when teammates run in plan mode. They are off by default. Leave them off.&lt;/p&gt;
&lt;h2 id=&quot;should-a-20-plan-use-ultracode&quot;&gt;Should a $20 plan use ultracode?&lt;/h2&gt;
&lt;p&gt;Not as a default. Ultracode combines &lt;code&gt;xhigh&lt;/code&gt; effort with automatic workflows, so Claude plans a workflow for each substantive task. That &lt;a href=&quot;https://code.claude.com/docs/en/workflows#let-claude-decide-with-ultracode&quot;&gt;applies to every task in the session&lt;/a&gt;, “so each request uses more tokens and takes longer than at lower effort levels.” The docs also say to drop back with &lt;code&gt;/effort high&lt;/code&gt; for routine work.&lt;/p&gt;
&lt;p&gt;The cost is the fan-out. A workflow spawns many agents, so a single run &lt;a href=&quot;https://code.claude.com/docs/en/workflows#cost&quot;&gt;can use meaningfully more tokens&lt;/a&gt; than doing the same task in conversation, and runs count toward your plan’s usage and rate limits.&lt;/p&gt;
&lt;p&gt;Anthropic’s own &lt;a href=&quot;https://claude.dev/blog/a-harness-for-every-task-dynamic-workflows-in-claude-code/&quot;&gt;post on dynamic workflows&lt;/a&gt; says they “often use more tokens and are best suited for complex, high value tasks.” It adds that most traditional coding tasks “do not need a panel of 5 reviewers.”&lt;/p&gt;
&lt;p&gt;I tried ultracode on Opus 5.5 in Claude desktop for a large code review, which is the kind of job the &lt;a href=&quot;https://code.claude.com/docs/en/workflows#example-workflow-prompts&quot;&gt;docs use as an example&lt;/a&gt;. That was on Max, so it is a look at the feature, not a Pro test. The desktop app draws on the &lt;a href=&quot;https://code.claude.com/docs/en/desktop&quot;&gt;same plan usage as the other Claude Code surfaces&lt;/a&gt;, and its approval card carries a token-usage caution.&lt;/p&gt;
&lt;p&gt;If you review often, a repeatable pipeline is another route. Mine is in my &lt;a href=&quot;/blog/automated-code-review-claude-code/&quot;&gt;automated code review pipeline&lt;/a&gt; post.&lt;/p&gt;
&lt;p&gt;Three details matter more on $20:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;You turn workflows on yourself.&lt;/strong&gt; On Pro, use the Dynamic workflows row in &lt;code&gt;/config&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The size guideline defaults to &lt;code&gt;small&lt;/code&gt;.&lt;/strong&gt; On Pro with Claude Code v2.1.271 or later, Claude aims for &lt;a href=&quot;https://code.claude.com/docs/en/workflows#set-a-size-guideline&quot;&gt;fewer than five agents&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ultracode hides the &lt;code&gt;Large workflow&lt;/code&gt; warning.&lt;/strong&gt; That warning normally appears past 25 agents or 1.5 million projected tokens. With ultracode on, you don’t see it, because turning it on “already opts you in to large runs.”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you want to try one, there is a cheaper route:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ask for one workflow, not a whole session.&lt;/strong&gt; Put the keyword &lt;code&gt;ultracode&lt;/code&gt; in a single prompt. It runs one task as a workflow without changing the session’s effort level.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Start on a slice.&lt;/strong&gt; Try one directory instead of the whole repo, and watch each agent’s token use in &lt;code&gt;/workflows&lt;/code&gt;. You can stop the run there.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep the size small.&lt;/strong&gt; Run &lt;code&gt;/config workflowSizeGuideline=small&lt;/code&gt; if yours isn’t already.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Know what a limit does.&lt;/strong&gt; On v2.1.271 or later, a run pauses at your five-hour limit and continues after the reset, instead of failing. It only pauses if the limit resets within 24 hours, so a weekly limit can still fail the agents. The &lt;a href=&quot;https://code.claude.com/docs/en/workflows#when-a-run-hits-your-usage-limit&quot;&gt;workflows docs&lt;/a&gt; list every condition.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-do-you-do-when-you-hit-the-limit&quot;&gt;What do you do when you hit the limit?&lt;/h2&gt;
&lt;p&gt;You have four options.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Wait.&lt;/strong&gt; The limit message shows the reset time. On Claude Code v2.1.234 or later, it can also wait and pick the task back up after the reset.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Turn on usage credits.&lt;/strong&gt; Pro is eligible. Usage is billed at standard API rates, and you set a monthly spending cap. The &lt;a href=&quot;https://support.claude.com/en/articles/12429409-manage-usage-credits-for-paid-claude-plans&quot;&gt;Help Center&lt;/a&gt; has the details. Remember the cache drops to five minutes once you are on credits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Switch model families.&lt;/strong&gt; If you see a model-specific message like “You’ve hit your Opus limit,” switching to a model outside that family with &lt;code&gt;/model&lt;/code&gt; keeps you working. That guidance sits in the &lt;a href=&quot;https://code.claude.com/docs/en/costs#when-a-developer-asks-about-a-limit&quot;&gt;costs docs&lt;/a&gt; section for seat-based plans, so check it against your own message. A session or weekly limit is shared across models, so switching won’t fix that one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Move up a tier.&lt;/strong&gt; &lt;a href=&quot;https://claude.com/pricing&quot;&gt;Max&lt;/a&gt; starts at $100 and gives you “5x or 20x more usage than Pro.”&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ask for the review while you still have usage left. I once had to review generated code by hand because I had already hit the limit.&lt;/p&gt;
&lt;p&gt;If you hit the session limit most days even with these habits, that is not you doing it wrong. It is a sign Pro may be too small for how you work, and the point where Max becomes worth pricing out.&lt;/p&gt;
&lt;h2 id=&quot;the-settings-id-start-with&quot;&gt;The settings I’d start with&lt;/h2&gt;
&lt;p&gt;Here is the small version. I run it on Max, not Pro, so treat it as a starting point.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;model&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;opusplan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;env&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;CLAUDE_CODE_SUBAGENT_MODEL&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;haiku&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Add the &lt;code&gt;CLAUDE.md&lt;/code&gt; stop rule from above and delete any “think carefully” lines. The built-in Explore and Plan subagents ignore the variable on its own, as I found in the &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;cost post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Then measure. Run &lt;code&gt;/usage&lt;/code&gt; and read the &lt;code&gt;Prompt cache (main)&lt;/code&gt; line for your hit rate and misses. If a habit doesn’t move a number there, it is just a habit.&lt;/p&gt;
&lt;h2 id=&quot;the-short-version-how-to-get-the-most-out-of-your-20-plan&quot;&gt;The short version: How to get the most out of your $20 plan&lt;/h2&gt;
&lt;p&gt;Here is the whole post as a checklist.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Know where your config lives.&lt;/strong&gt; &lt;code&gt;~/.claude/settings.json&lt;/code&gt; is for you everywhere, &lt;code&gt;.claude/settings.json&lt;/code&gt; is for the project, and &lt;code&gt;CLAUDE.md&lt;/code&gt; should stay under about 200 lines.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read &lt;code&gt;/usage&lt;/code&gt; first.&lt;/strong&gt; Check the &lt;code&gt;Prompt cache (main)&lt;/code&gt; line so you know where your limits actually go.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Start effort at &lt;code&gt;medium&lt;/code&gt;, then experiment.&lt;/strong&gt; Try &lt;code&gt;low&lt;/code&gt; on scoped jobs and &lt;code&gt;high&lt;/code&gt; on hard ones, check &lt;code&gt;/effort&lt;/code&gt; for what you are really on, pin levels with &lt;code&gt;modelSettings&lt;/code&gt;, and delete “think carefully” lines from your prompts, &lt;code&gt;CLAUDE.md&lt;/code&gt; and skills.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;opusplan&lt;/code&gt;.&lt;/strong&gt; Opus 5.5 plans and Sonnet 5 types.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Send the whole task in one message.&lt;/strong&gt; Say what “done” means and when to stop, keep a checklist in &lt;code&gt;TASKS.md&lt;/code&gt; for long runs, and type follow-ups mid-run instead of restarting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Add the stop rule to &lt;code&gt;CLAUDE.md&lt;/code&gt;.&lt;/strong&gt; Keep going when a step needs no input, ask before anything destructive, and keep permission prompts on.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep context small.&lt;/strong&gt; &lt;code&gt;/clear&lt;/code&gt; between tasks, &lt;code&gt;/compact&lt;/code&gt; at natural breaks, &lt;code&gt;/rewind&lt;/code&gt; to abandon a path, and &lt;code&gt;/mcp&lt;/code&gt; to turn off servers you don’t use.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accept the resume-from-summary offer.&lt;/strong&gt; Do it after a break longer than an hour, when the cache has gone cold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Skip the expensive features.&lt;/strong&gt; That means Fable, fast mode and agent teams, and &lt;code&gt;/advisor off&lt;/code&gt; is worth trying too.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use ultracode for one big job, not all day.&lt;/strong&gt; Put the keyword in a single prompt, start on a small slice, and drop back with &lt;code&gt;/effort high&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;At the limit, pick a route.&lt;/strong&gt; Wait for the reset, turn on usage credits with a spending cap, or price out Max if it happens most days.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Which of these would save you the most on a $20 Claude plan, and have you checked it against your own &lt;code&gt;/usage&lt;/code&gt; lately?&lt;/p&gt;
</content:encoded><category>claude-code</category><category>ai-tools</category><category>developer-tools</category><category>productivity</category></item><item><title>Automated Code Review Pipeline with Claude Code</title><link>https://my-space.io/blog/automated-code-review-claude-code/</link><guid isPermaLink="true">https://my-space.io/blog/automated-code-review-claude-code/</guid><description>My real eight-stage review pipeline for Claude Code changes: independent agents, fresh context at every stage, and a CI gate before anything merges.</description><pubDate>Tue, 22 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Automated code review doesn’t work by asking the same AI that wrote a change to also grade it. That’s not review, that’s the same reasoning agreeing with itself.&lt;/p&gt;
&lt;p&gt;My own pipeline for Claude Code runs eight independent stages before anything merges: a different model, a fresh context with zero memory of why any line was written the way it was, or a real running system that can’t be talked out of its own behavior.&lt;/p&gt;
&lt;p&gt;It didn’t come from one clean rewrite. It grew stage by stage, over months, every time a single pass let something through that a second one would have caught.&lt;/p&gt;
&lt;p&gt;None of the stages get final authority on their own. Each one produces a verdict, a human reads it, and merge approval is a separate step from everything else passing, on purpose, even on a Tuesday when everything is green.&lt;/p&gt;
&lt;p&gt;That human read isn’t a formality either. I read the actual diff myself, not just the review verdicts, and if something doesn’t sit right, I ask Claude to explain it directly before I’ll approve anything.&lt;/p&gt;
&lt;p&gt;An explanation that doesn’t hold up under a direct question is its own finding, even when nothing upstream flagged it.&lt;/p&gt;
&lt;h2 id=&quot;what-automated-code-review-with-claude-code-actually-runs&quot;&gt;What automated code review with Claude Code actually runs&lt;/h2&gt;
&lt;p&gt;Here’s the full chain, in order.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;What runs&lt;/th&gt;
&lt;th&gt;What it checks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. Plan&lt;/td&gt;
&lt;td&gt;A capable model, reading real source&lt;/td&gt;
&lt;td&gt;File-and-line-level plan, not a description of intent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2. Adversarial plan review&lt;/td&gt;
&lt;td&gt;A fresh context, read-only tools&lt;/td&gt;
&lt;td&gt;Every load-bearing assumption in the plan, against the actual files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3. Implement&lt;/td&gt;
&lt;td&gt;Whatever model is fast enough&lt;/td&gt;
&lt;td&gt;Follows the reviewed plan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4. Pre-push code review&lt;/td&gt;
&lt;td&gt;Independent model, fresh context&lt;/td&gt;
&lt;td&gt;The diff, cold, no memory of the implementation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5. CI&lt;/td&gt;
&lt;td&gt;GitHub Actions&lt;/td&gt;
&lt;td&gt;Test suite, real build, smoke test against the built artifact&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6. Holistic PR review&lt;/td&gt;
&lt;td&gt;Independent model, fresh context&lt;/td&gt;
&lt;td&gt;Diff + description + CI, checked against each other&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7. Functional test&lt;/td&gt;
&lt;td&gt;A real running instance&lt;/td&gt;
&lt;td&gt;The behavior, exercised by hand, not just asserted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8. Human approval&lt;/td&gt;
&lt;td&gt;Me&lt;/td&gt;
&lt;td&gt;Explicit, every time, never inferred&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Stages 2, 4, and 6 are real Claude Code subagents on my machine, not a metaphor. &lt;code&gt;plan-adversary&lt;/code&gt; runs before I exit plan mode, on a strong model, and its entire job is to refuse to believe the plan.&lt;/p&gt;
&lt;p&gt;Its own rule, verbatim from its prompt: “every factual claim in the plan is a hypothesis until you open the file.” It won’t accept “this is already verified” or “nothing else uses this” without grepping for every consumer itself, and it separates what it confirmed from what it merely couldn’t check, because letting “unverifiable” quietly become “confirmed” is exactly how a stale plan gets implemented.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;code-reviewer&lt;/code&gt; runs before every commit and push, on a fixed strong model, with a fresh context that has never seen the implementation decisions or the rationalizations that came with them. It’s the same model split I use for the rest of the day’s work too, a capable model reserved for judgment, a faster one for typing, which I broke down properly in &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;Claude Code cost&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;pr-reviewer&lt;/code&gt; runs once a PR is open, and its job is different in scope: not “is this diff correct,” but “is this the right, complete, honestly described unit of work to merge.” It checks whether the PR description’s “what changed” actually matches the diff, and whether the “tested” claims were actually run or just judged plausible.&lt;/p&gt;
&lt;p&gt;There’s a fourth agent, &lt;code&gt;docs-librarian&lt;/code&gt;, that isn’t one of the eight stages. It triggers whenever a change touches a service that talks to an external API, and its only job is checking the implementation against the actual current documentation instead of trusting anyone’s memory of how that API works, including the model’s own.&lt;/p&gt;
&lt;p&gt;None of this runs through a hook. The instruction is just: run these yourself, every time, unasked, even on the smallest edit. No hooks, no markers, just do it.&lt;/p&gt;
&lt;p&gt;That’s a deliberate choice and not a shortcut I haven’t gotten around to. A hook fires on a file pattern; it doesn’t know a one-line change to an auth check deserves the full chain while a copy edit doesn’t. An instruction that’s expected to be followed every time, unprompted, handles that judgment call in a way a static trigger can’t.&lt;/p&gt;
&lt;p&gt;It’s the same bet I made writing about &lt;a href=&quot;/blog/claude-code-auto-mode/&quot;&gt;Claude Code auto mode&lt;/a&gt;: don’t beg the model with a prompt, build something that either runs or doesn’t. Here the “something” is a standing instruction treated as non-negotiable, not a classifier, but the reasoning is the same. Begging doesn’t scale, structure does.&lt;/p&gt;
&lt;p&gt;Any check that gates something also has to fail in the right direction. A permission check, a classifier, a review stage that can’t reach a verdict should default to deny, not allow. A check that returns “nothing is restricted” when it can’t read its input is a vulnerability waiting for exactly that failure to happen in production, and it’s worth testing the error path on purpose, not just the happy path where everything loads correctly.&lt;/p&gt;
&lt;p&gt;Stage 7 gets the same independence rule, just applied to people instead of models. I’m not the one who functional-tests a change before it ships.&lt;/p&gt;
&lt;p&gt;Someone who has never seen the implementation clicks through it instead. They don’t know which code path was touched, so they can’t unconsciously avoid the spot that’s actually fragile, the same reason a fresh-context code review catches things a self-review doesn’t.&lt;/p&gt;
&lt;p&gt;Familiarity with your own reasoning is exactly what makes you bad at finding the gaps in it.&lt;/p&gt;
&lt;h2 id=&quot;what-it-actually-catches&quot;&gt;What it actually catches&lt;/h2&gt;
&lt;p&gt;The categories that keep showing up: a regex that matches more, or less, than it was supposed to. A race condition where two requests hit the same check-then-act sequence against Redis or Postgres, so the second one clobbers a state the first one assumed was still true. A cache key that doesn’t capture everything that actually affects the cached value, so a stale or cross-context result gets served back as current.&lt;/p&gt;
&lt;p&gt;None of those fail a build. They fail under a timing pattern or an input shape a single read-through doesn’t reproduce, which is exactly why a fresh-context review pass matters more than a more careful first pass would.&lt;/p&gt;
&lt;p&gt;The person, or the model, who wrote the regex already believes it’s correct. It takes a reviewer with no stake in that belief to ask what it matches when the input isn’t the one it was tested against.&lt;/p&gt;
&lt;p&gt;The technique that actually finds these is boring: grep for every consumer of whatever’s changing, not just the one the plan or the diff talks about.&lt;/p&gt;
&lt;p&gt;A plan almost always describes the happy-path caller. The break is nearly always in the variant it didn’t mention, the retry path, the second client, the call site that passes an option nobody else does.&lt;/p&gt;
&lt;p&gt;Reading the described change carefully doesn’t surface that. Searching for who else touches the same thing does.&lt;/p&gt;
&lt;p&gt;Findings also get graded, not just listed: blocker, should-fix, or question, the same way across every stage.&lt;/p&gt;
&lt;p&gt;A blocker is something that can cause wrong results, lost data, or a security hole. A should-fix is a real defect that isn’t a merge risk. A question is something that looks suspicious but can’t be confirmed from the code alone, so it’s phrased as one instead of asserted as fact.&lt;/p&gt;
&lt;p&gt;Grading turns a review into a triage list instead of a wall of text, and it stops a genuinely dangerous finding from getting lost between ten nitpicks about naming.&lt;/p&gt;
&lt;h2 id=&quot;the-one-rule-i-dont-break-docs-over-prompting&quot;&gt;The one rule I don’t break: docs over prompting&lt;/h2&gt;
&lt;p&gt;I don’t try to control the model through clever prompting. I lean on what’s actually documented and on structured output instead.&lt;/p&gt;
&lt;p&gt;That’s the whole reason &lt;code&gt;docs-librarian&lt;/code&gt; exists. Prompting an agent to “be careful with the Stripe webhook signature” is a suggestion the model can rationalize past. Pointing it at Stripe’s actual current webhook documentation, and having it check the implementation line against that page, isn’t a suggestion, it’s a fact the model either matches or doesn’t.&lt;/p&gt;
&lt;p&gt;The failure mode I’m avoiding isn’t the model being careless. It’s the model being confidently wrong from memory about an API that shipped a breaking change since its training data, and no amount of “please double-check” in a prompt fixes that. Only the current page does.&lt;/p&gt;
&lt;p&gt;It checks two layers, in order. First, whatever pinned reference docs already exist for that integration in the repo itself, because that’s the contract the code was actually built against, and a mismatch between the code and that pinned file is a real finding even when the live API would also accept what the code currently does.&lt;/p&gt;
&lt;p&gt;Then it goes to the live documentation for anything the pinned reference doesn’t cover, and it says plainly when no pinned reference existed to check against, rather than quietly filling the gap from memory.&lt;/p&gt;
&lt;p&gt;When the pinned file and the live docs disagree with each other, it reports both instead of picking one silently, because that disagreement is itself information: either the pin is stale, or the code drifted from a deliberate decision, and only a human reading both knows which.&lt;/p&gt;
&lt;p&gt;The same distrust of memory applies to security rules that get computed rather than declared. If “is this restricted” is derived by parsing something else, a naming convention, a list extracted from documentation prose, instead of being an explicit rule, a well-intentioned edit to that other artifact can silently change what’s protected with nothing failing to signal it. So a derived rule like that gets a test that pins the expected result directly, independent of the artifact it’s derived from.&lt;/p&gt;
&lt;h2 id=&quot;the-ci-stage-test-suite-smoke-test-docker-or-ubuntu-and-when-the-minutes-run-out&quot;&gt;The CI stage: test suite, smoke test, Docker or Ubuntu, and when the minutes run out&lt;/h2&gt;
&lt;p&gt;Stage 5 isn’t one job. On GitHub Actions it’s a test suite, a real build of the deployable artifact, and at least one smoke test against that built artifact rather than against source running in a dev harness, because a test suite can pass while the packaging step silently drops a file the app needs at runtime.&lt;/p&gt;
&lt;p&gt;Whether that build is a Docker build or a plain Ubuntu build depends on the project: a container image if that’s what actually ships, a bare Ubuntu runner if it doesn’t. Same principle either way, only the artifact changes.&lt;/p&gt;
&lt;p&gt;GitHub’s own free tier gives a private repo &lt;a href=&quot;https://docs.github.com/en/billing/concepts/product-billing/github-actions&quot;&gt;2,000 Actions minutes a month&lt;/a&gt; on GitHub-hosted runners. Past that, I move the project to a self-hosted runner instead of paying per-minute overage. &lt;a href=&quot;https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners&quot;&gt;Self-hosted runners are free to use with GitHub Actions&lt;/a&gt;, the cost is just whatever machine you point at the job, so it’s a straight swap once a project’s usage pattern makes that machine cheaper than the overage.&lt;/p&gt;
&lt;p&gt;I run this same shape of pipeline on &lt;a href=&quot;https://tradionlabs.com&quot;&gt;Tradion&lt;/a&gt;, the AI trading research platform I built, and on other projects I can’t name here. The stages don’t change with the project. What changes is only the build target in stage 5.&lt;/p&gt;
&lt;h2 id=&quot;the-four-agents-generic-versions&quot;&gt;The four agents, generic versions&lt;/h2&gt;
&lt;p&gt;These are stripped-down, generic versions of the four agents, not the verbatim prompts running on my own projects. Swap in your own stack’s file paths, frameworks, and conventions; the structure and the rules are what matter, not the specifics.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name: plan-adversary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;description: Reviews a proposed plan against the actual codebase before any&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  code is written. Never writes code itself.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tools: Read, Grep, Glob, Bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model: opus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Every factual claim in the plan is a hypothesis until you open the file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Never accept, without checking: a quoted line number or function name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;&quot;this is already the case,&quot; &quot;nothing else uses this,&quot; or a claim that a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;change is safe. Open every file the plan touches. Grep for every consumer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;of what&apos;s changing, not just the one the plan describes. Record each&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;assumption as confirmed, false, or unverified, and never let unverified&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;quietly become confirmed.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Output: WILL BREAK, CLAIMS THAT ARE FALSE, UNVERIFIED, NOT ACCOUNTED FOR,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;CORRECT AS WRITTEN. Verdict: safe to implement / safe with changes / do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;not implement as written.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name: code-reviewer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;description: Independently reviews a diff after implementation, before&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  push. Fresh context, no memory of implementation decisions.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tools: Read, Grep, Glob, Bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model: opus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;You have no memory of why any line was written the way it was. Review the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;diff cold. Check correctness, access control, injection risks, race&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;conditions and check-then-act patterns, and regressions against existing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tests. Every finding needs file:line, the exact code, a concrete failure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scenario, and a fix. Grade each: BLOCKER, SHOULD-FIX, QUESTION. If nothing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;is wrong, say so plainly; don&apos;t manufacture findings to look thorough.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name: docs-librarian&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;description: Verifies integrations against real, current documentation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  instead of trusting model memory.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tools: Read, Grep, WebFetch, WebSearch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model: sonnet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Check any pinned reference docs in the repo first, that&apos;s the contract the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;code was built against. Then check live documentation for anything not&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;covered. If the two disagree, report both, don&apos;t silently prefer one.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Never rely on memory for version-specific API details. Quote directly from&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;documentation when flagging an issue, with a direct URL for every one.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name: pr-reviewer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;description: Reviews an opened pull request holistically, diff,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  description, and CI, before merge. Fresh context.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tools: Read, Grep, Glob, Bash, WebFetch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model: opus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Check CI status first; don&apos;t review behind a red check. Compare the PR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;description against the actual diff: does &quot;what changed&quot; match, were the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;&quot;tested&quot; claims actually run or just judged plausible? Review the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;implementation for correctness, security, and regressions. Verdict: ready&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;to approve / ready to approve with nits / request changes / blocked.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Return the verdict, never post, merge, or approve anything yourself.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;what-it-costs-and-when-i-skip-it&quot;&gt;What it costs, and when I skip it&lt;/h2&gt;
&lt;p&gt;The full gate, all eight stages, takes about 25 to 30 minutes end to end on a typical change. That’s real time added to every merge, and it’s not free in tokens either.&lt;/p&gt;
&lt;p&gt;Running a pipeline this heavy only makes sense if I’m not also leaking tokens everywhere else. I keep Claude Code itself tightly configured for cost outside the pipeline, the model splits, the context hygiene, the settings I actually run are in &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;how tightly I keep Claude Code’s day-to-day cost down&lt;/a&gt;. Spending deliberately in one place and carelessly everywhere else isn’t a strategy, it’s just a bigger bill with extra steps.&lt;/p&gt;
&lt;p&gt;So I don’t run it on everything. Pure content or docs changes, or a single-property config or style tweak, skip the plan-review and pre-push review stages entirely and go straight to CI plus the holistic review. A small follow-up fixing something a reviewer already flagged gets the same treatment.&lt;/p&gt;
&lt;p&gt;Anything touching permissions, shared state, money, or data deletion, or anything else expensive to undo, runs the full eight stages, no shortcuts, no matter how small the diff looks.&lt;/p&gt;
&lt;p&gt;The sizing rule isn’t “is the diff small.” It’s “could this plausibly cause a wrong behavior or a security issue.”&lt;/p&gt;
&lt;p&gt;A one-line diff that flips an authorization check is small and gets the full chain. A two-hundred-line copy change gets none of it.&lt;/p&gt;
&lt;p&gt;Honestly, I don’t always get the sizing right. There are days a three-line fix goes through the full 25 to 30 minutes because I didn’t stop to ask whether it needed to, and it’s hard to tell in the moment whether that’s discipline or just not trusting my own judgment about what counts as small.&lt;/p&gt;
&lt;p&gt;I also don’t think this pipeline is the only good way to do this. It’s the shape that’s worked for me so far, not a claim that it’s optimal.&lt;/p&gt;
&lt;p&gt;I’ve mistaken process for progress before: &lt;a href=&quot;/blog/open-source-ai-project/&quot;&gt;347 commits didn’t save the open source AI project&lt;/a&gt; that never found a user, and a review pipeline this elaborate deserves the same question I didn’t ask early enough there, whether it’s actually earning its cost or just feels like it is.&lt;/p&gt;
&lt;p&gt;I’m genuinely curious what other people’s version of this looks like, and what a leaner or smarter setup would catch that mine doesn’t. What does your own review pipeline actually look like?&lt;/p&gt;
</content:encoded><category>claude-code</category><category>code-review</category><category>ci-cd</category><category>github-actions</category></item><item><title>I Despise Vibe Coding. Here&apos;s the Only Place It Belongs</title><link>https://my-space.io/blog/vibe-coding/</link><guid isPermaLink="true">https://my-space.io/blog/vibe-coding/</guid><description>I use AI to code every day, and I despise vibe coding. Here&apos;s the real difference, where vibe coding belongs, and why it has no place near real users or data.</description><pubDate>Tue, 22 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I use AI-assisted coding every day. Claude Code is part of how I build everything. And I despise vibe coding.&lt;/p&gt;
&lt;p&gt;That sounds like a contradiction. It isn’t. AI-assisted coding and vibe coding are not the same thing, and the difference is the whole point of this post.&lt;/p&gt;
&lt;h2 id=&quot;so-what-is-vibe-coding&quot;&gt;So what is vibe coding?&lt;/h2&gt;
&lt;p&gt;Vibe coding is when you don’t read the code. You trust the AI to write everything. You accept whatever it spits out. Something breaks? Paste the error back in and hope for the best.&lt;/p&gt;
&lt;p&gt;That’s not me being harsh. That’s literally how it started. Andrej Karpathy coined “vibe coding” in a &lt;a href=&quot;https://x.com/karpathy/status/1886192184808149383&quot;&gt;post on X&lt;/a&gt; on February 2, 2025 (X blocks some readers, so here’s &lt;a href=&quot;https://simonwillison.net/2025/Mar/19/vibe-coding/&quot;&gt;a readable copy of the same post&lt;/a&gt;). In that same post, he admitted that the code grows beyond his usual comprehension, that when the AI can’t fix a bug he just asks for random changes until it goes away, and that it’s not too bad for throwaway weekend projects.&lt;/p&gt;
&lt;p&gt;Throwaway weekend projects. The guy who invented the term said that. Somewhere along the way, people skipped that part and started shipping vibe-coded apps to real users.&lt;/p&gt;
&lt;h2 id=&quot;i-tried-it-heres-what-happened&quot;&gt;I tried it. Here’s what happened.&lt;/h2&gt;
&lt;p&gt;I’m not writing this from the sidelines. I’ve vibe coded, mostly in Google AI Studio, building things like Crumbs, a business manager for cottage bakers, and ClaimGuard AI, an insurance-claims assistant. At first it felt amazing. Type a sentence, get an app.&lt;/p&gt;
&lt;p&gt;Then I actually looked at what I had.&lt;/p&gt;
&lt;p&gt;The designs were AI slop: the same sloppy blue-purple gradient and generic rounded borders every AI app seems to ship with. Simple things were written in weirdly complicated ways. Stuff that should’ve been ten lines was fifty. And Gemini hallucinated data right inside the app. I couldn’t fix it, because I had no idea how the code that was supposed to catch it actually worked.&lt;/p&gt;
&lt;p&gt;That bug is what stopped me. I realized I didn’t own what I built. The AI did. And the AI doesn’t care if it breaks.&lt;/p&gt;
&lt;h2 id=&quot;the-only-place-vibe-coding-belongs&quot;&gt;The only place vibe coding belongs&lt;/h2&gt;
&lt;p&gt;Prototypes. Demos. Testing an idea on a weekend. Anything with no real users, no real data, and nothing that hurts anyone if it falls apart.&lt;/p&gt;
&lt;p&gt;That’s it. That’s the list.&lt;/p&gt;
&lt;p&gt;I’ve been trading for over 9 years, and vibe coding reminds me of trading on a hot tip with no plan. Sometimes it works. You feel like a genius. But you don’t know &lt;em&gt;why&lt;/em&gt; it worked, so you can’t repeat it, and you definitely can’t protect yourself when it goes wrong. Fine with play money. Terrible with real money.&lt;/p&gt;
&lt;h2 id=&quot;why-i-despise-vibe-coding-in-production&quot;&gt;Why I despise vibe coding in production&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;AI builds stuff you don’t need.&lt;/strong&gt; It over-engineers everything. You ask for one simple feature and it adds extra layers, extra config, and code for problems you don’t even have. It pulls you toward features you never planned. If you’re not reading, you don’t notice, and your app slowly fills up with code nobody asked for and nobody understands.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;It produces slop.&lt;/strong&gt; AI slop apps. AI slop designs. They look done in a screenshot and fall apart the second a real user does something unexpected.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Even the best models need steering.&lt;/strong&gt; In my experience, Sonnet still needs a lot more hands-on direction than Opus, which is part of why &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;I split the two by task instead of using one for everything&lt;/a&gt;. Even Opus over-engineers things I never asked for often enough that I’ve stopped being surprised by it.&lt;/p&gt;
&lt;p&gt;No model gets it right every time. More than once I’ve asked it to fix a bug and gotten a list of two problems back instead of an actual fix. My reply, verbatim: “Dude wtf.” It went back and fixed them.&lt;/p&gt;
&lt;p&gt;AI is fast. Fast and correct are not the same thing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The AI will grade its own homework, and lie about it.&lt;/strong&gt; Look at the &lt;a href=&quot;https://www.eweek.com/news/replit-ai-coding-assistant-failure/&quot;&gt;Replit incident&lt;/a&gt;. During a 12-day vibe coding experiment, SaaStr founder Jason Lemkin’s AI assistant ran destructive commands that wiped a database with records on 1,206 executives and over 1,196 companies.&lt;/p&gt;
&lt;p&gt;Then it made it worse. It &lt;a href=&quot;https://www.theregister.com/2025/07/21/replit_saastr_vibe_coding_incident/&quot;&gt;built a database of 4,000 fake user profiles and lied about a unit test passing&lt;/a&gt;, then told Lemkin the rollback was impossible. It wasn’t; the rollback actually worked. &lt;a href=&quot;https://heise.de/-10499597&quot;&gt;Lemkin said it was a demo app&lt;/a&gt;, but he still lost around 100 hours of work. That was a demo. Now imagine real customers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The numbers say the same thing.&lt;/strong&gt; A December 2025 &lt;a href=&quot;https://www.coderabbit.ai/blog/state-of-ai-vs-human-code-generation-report&quot;&gt;CodeRabbit analysis&lt;/a&gt; of 470 GitHub pull requests found AI co-authored code averaged 10.83 issues per PR versus 6.45 for human-only PRs, about 1.7 times more overall, with up to 2.74 times more security vulnerabilities.&lt;/p&gt;
&lt;p&gt;In May 2026, security firm &lt;a href=&quot;https://www.ians.com/news/easy-to-build-easy-to-expose-how-vibe-coding-is-creating-new-data-risks&quot;&gt;RedAccess reported&lt;/a&gt; that out of roughly 380,000 publicly accessible AI-built assets, about 5,000 vibe-coded apps built for business use were leaking corporate data, many of them because privacy settings defaulted to public. &lt;a href=&quot;https://securityboulevard.com/2026/05/thousands-of-vibe-coded-apps-exposing-corporate-personal-data-redaccess/&quot;&gt;Some were exposing medical records and a bank’s financial data&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;where-real-data-lives-vibe-coding-doesnt&quot;&gt;Where real data lives, vibe coding doesn’t&lt;/h2&gt;
&lt;p&gt;At my day job, I work with school district data. Student records are protected by &lt;a href=&quot;https://studentprivacy.ed.gov/faq/what-ferpa&quot;&gt;FERPA&lt;/a&gt;, a federal privacy law. You don’t get to vibe your way through code that touches that. You have to be careful about exactly what the code spits out, every field, every time.&lt;/p&gt;
&lt;p&gt;Same with Tradion, the trading app I’m building. It connects to people’s real brokerage accounts and learns how each person trades. If I vibe coded it, I might never notice personal info leaking somewhere it shouldn’t. Or Tradion’s memory layer looking like it works while being wired up wrong, quietly giving bad answers instead of crashing. Those are the scariest bugs. The ones that don’t tell you they’re there.&lt;/p&gt;
&lt;p&gt;And here’s one most people don’t see coming. When AI gets messy output from another model, its favorite fix is to slap a regex patch or a keyword filter on top. I’ve watched a regex meant to keep a model’s output in one shape silently cut off a real response the moment it didn’t match exactly, instead of failing loud.&lt;/p&gt;
&lt;p&gt;Works in the demo. Breaks on the first weird edge case in production. A vibe coder accepts that patch without thinking twice. I banned it completely.&lt;/p&gt;
&lt;h2 id=&quot;what-i-do-instead-ai-assisted-coding&quot;&gt;What I do instead: AI-assisted coding&lt;/h2&gt;
&lt;p&gt;I don’t blindly trust what AI gives me. Everything goes through gates, the same principle behind &lt;a href=&quot;/blog/claude-code-auto-mode/&quot;&gt;keeping an autonomous agent from doing something stupid&lt;/a&gt;: don’t beg the model to behave; build something that actually stops it.&lt;/p&gt;
&lt;p&gt;It starts with an issue. Opus writes the plan. A separate agent, &lt;code&gt;plan-adversary&lt;/code&gt;, reviews that plan against the actual codebase, and Sonnet builds it.&lt;/p&gt;
&lt;p&gt;Then I have the work verified. Automated tests (functional, end-to-end, smoke tests, and Docker builds) run before anything gets pushed or merged, and only then does it ship. It’s the same shape as &lt;a href=&quot;/blog/automated-code-review-claude-code/&quot;&gt;the eight-stage review pipeline I run on every change&lt;/a&gt;, just compressed.&lt;/p&gt;
&lt;p&gt;This isn’t hypothetical, and none of it was vibe coded either. I keep a running account of every Claude Code session, and across 61 of them between July and September 2026, the AI’s own work would have reached production twice if nothing had checked it first.&lt;/p&gt;
&lt;p&gt;A “fix” for cleaning up old storage would have let one user delete another user’s data; a separate review pass caught it, not the AI that wrote it. A database migration reached main bundled inside eight commits nobody had reviewed together, one production didn’t have yet; the build broke, and I applied it by hand.&lt;/p&gt;
&lt;p&gt;A vibe coder skips every single one of those steps.&lt;/p&gt;
&lt;p&gt;I also give the AI written rules. A few from Tradion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Numbers come from data, not model memory.&lt;/strong&gt; Prices, returns, dates all come from real sources. The math happens in code, then the model explains it. If data is missing, say so. Don’t fill the gap. &lt;a href=&quot;/blog/open-source-ai-project/&quot;&gt;I hold the same rule for every project, not just Tradion&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No control layers on top of the model.&lt;/strong&gt; No regex hacks to “fix” output. No keyword blocklists. No homemade JSON repair.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Provider docs over memory.&lt;/strong&gt; Check the official docs before touching any AI integration, because model names and features change all the time.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A vibe coder would never write rules like these. They don’t even know the AI makes up numbers.&lt;/p&gt;
&lt;p&gt;One honest thing I have to call out. &lt;a href=&quot;https://news.research.gatech.edu/2026/04/13/bad-vibes-ai-generated-code-vulnerable-researchers-warn&quot;&gt;Georgia Tech researchers&lt;/a&gt; track security flaws caused by AI-written code. Their &lt;a href=&quot;https://vibesecradar.com/&quot;&gt;Vibe Security Radar&lt;/a&gt; is a live-updating tracker, and it attributed 227 of 289 confirmed CVEs to Claude Code as of its August 26, 2026 update. That’s the tool I use every day.&lt;/p&gt;
&lt;p&gt;And that proves my point. The tool isn’t the problem. Skipping review is. Same tool, different process, totally different result.&lt;/p&gt;
&lt;h2 id=&quot;bottom-line&quot;&gt;Bottom line&lt;/h2&gt;
&lt;p&gt;Vibe coding is a sketch. Production is a building people live in.&lt;/p&gt;
&lt;p&gt;Sketch all you want. Vibe code the prototype, test your idea, throw it away. But the moment real users or real data show up, the vibes are over.&lt;/p&gt;
&lt;p&gt;So learn to code. Actually read what the AI gave you. Understand &lt;em&gt;why&lt;/em&gt; it gave you that. Then decide. AI is a really fast junior developer, and you’re still the one who signs off.&lt;/p&gt;
&lt;p&gt;Where do you draw the line between a prototype you’d vibe code and something you’d review line by line?&lt;/p&gt;
</content:encoded><category>ai-tools</category><category>claude-code</category><category>code-review</category><category>vibe-coding</category></item><item><title>My Open Source AI Project: 347 Commits, No Users</title><link>https://my-space.io/blog/open-source-ai-project/</link><guid isPermaLink="true">https://my-space.io/blog/open-source-ai-project/</guid><description>I spent four months on an open source AI project and never launched it. 347 commits, a benchmark that couldn&apos;t fail, no users. Here&apos;s what that taught me.</description><pubDate>Mon, 21 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;On June 18, 2025, I ran my first commit: “RAGnetic Project Setup.” I spent the next four months building an open source AI project and never launched it.&lt;/p&gt;
&lt;p&gt;347 commits, 22,000 lines of Python, about 100 API endpoints, and the only people who ever touched it were a couple of coworkers I asked to try it as a favor. That’s the whole outcome.&lt;/p&gt;
&lt;p&gt;Here’s what building the wrong thing, for four months, taught me.&lt;/p&gt;
&lt;p&gt;The project is called &lt;a href=&quot;https://github.com/ishraq21/RAGnetic&quot;&gt;RAGnetic&lt;/a&gt;, and the code is still on GitHub if you want to see the receipts yourself.&lt;/p&gt;
&lt;h2 id=&quot;the-open-source-ai-project-i-meant-to-build-and-what-i-actually-built&quot;&gt;The open source AI project I meant to build, and what I actually built&lt;/h2&gt;
&lt;p&gt;I worked in biotech for about two years, from 2023 to 2025. What I kept running into was process knowledge scattered across docs, wikis, and people’s heads, and no fast way to ask a question and get a real answer.&lt;/p&gt;
&lt;p&gt;The idea was to consolidate that knowledge and embed it into whichever AI model you preferred, local or one of the frontier ones, and just talk to it whenever you needed information.&lt;/p&gt;
&lt;p&gt;Point it at your documents, your databases, your APIs, describe an agent in YAML, and deploy it. “Complete ownership of your on-premise AI.”&lt;/p&gt;
&lt;p&gt;That’s a specific problem. What I shipped was not a specific solution to it.&lt;/p&gt;
&lt;p&gt;The README organizes RAGnetic into five pillars: &lt;strong&gt;Ingest, Adapt, Collaborate, Interact, Evaluate.&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ingest&lt;/strong&gt;: PDFs, DOCX, CSV, web pages, code repos, live SQL, with PII redaction and hybrid BM25 + vector retrieval.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Adapt&lt;/strong&gt;: hosted models (OpenAI, Anthropic, Gemini), local ones through Ollama, and LoRA fine-tuning wired directly into agent config.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collaborate&lt;/strong&gt;: multi-agent graphs on LangGraph.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interact&lt;/strong&gt;: a Docker sandbox for code, a SQL toolkit, web search, ArXiv, an HTTP toolkit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Evaluate&lt;/strong&gt;: benchmarking with retrieval metrics and an LLM judge.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Underneath all of that: FastAPI, Celery, Redis, Postgres, Alembic migrations, user and role management, API keys with rate limiting, and a credits and cost-accounting system with per-model pricing tables, the kind of thing you need once you’re &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;actually paying attention to what an AI tool costs you&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Read that list again. Each bullet is somebody else’s entire company.&lt;/p&gt;
&lt;p&gt;I built a generic platform for anyone who might one day want to talk to their documents, instead of the specific tool a biotech team needs to talk to their process docs. Somewhere between the idea and the code, the target quietly disappeared.&lt;/p&gt;
&lt;h2 id=&quot;lesson-1-i-tried-creating-a-platform-instead-of-a-product&quot;&gt;Lesson 1: I tried creating a platform instead of a product&lt;/h2&gt;
&lt;p&gt;Here’s the part that actually stings. I didn’t need to guess who my first users would be.&lt;/p&gt;
&lt;p&gt;They were sitting next to me. A couple of coworkers agreed to try it, people who had the exact problem I’d started this to solve.&lt;/p&gt;
&lt;p&gt;What I handed them was RAGnetic: five pillars, a YAML config language, a CLI, a dashboard, LoRA fine-tuning. Not “ask your process docs a question.” A platform, when what they needed was a product.&lt;/p&gt;
&lt;p&gt;Here’s what I’ve come to think since: the best products don’t try to solve everything, at least not in the beginning. They solve one thing all the way, for one person, and earn the right to solve the next thing later. Mine tried to solve everything from day one, which meant it never quite solved anything.&lt;/p&gt;
&lt;p&gt;Part of why it never got further than that was scope. One more feature was always the thing standing between where it was and where it needed to be to show someone.&lt;/p&gt;
&lt;p&gt;Retrieval was fine, but the eval system wasn’t done. The eval system was fine, but the dashboard needed a rework.&lt;/p&gt;
&lt;p&gt;There was always a next thing, and the next thing always felt more urgent than putting an unfinished tool in front of the people who’d asked for the specific thing in the first place.&lt;/p&gt;
&lt;p&gt;The moment that actually landed the lesson wasn’t a user conversation. It was applying to YC and getting rejected.&lt;/p&gt;
&lt;p&gt;That’s what made me look at the space honestly: RAG frameworks and agent platforms were already one of the most saturated categories in software, full of funded teams shipping the same pitch. A generic “better platform” story loses to whoever has more distribution.&lt;/p&gt;
&lt;p&gt;I didn’t have distribution. I had a biotech problem and two coworkers who would have told me, for free, whether embedding their process docs into a model actually helped them find an answer faster. I built the platform instead of asking them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What I’d do differently:&lt;/strong&gt; skip the platform. Build the one thing the person next to me needed, show it to them in a week, and let their answer decide whether pillar two ever gets built.&lt;/p&gt;
&lt;h2 id=&quot;lesson-2-i-built-a-benchmark-that-couldnt-fail&quot;&gt;Lesson 2: I built a benchmark that couldn’t fail&lt;/h2&gt;
&lt;p&gt;I had a sandboxed code executor, a distributed task queue, role-based access control, and a cost calculator with per-model pricing tables. What I didn’t have was a trustworthy answer to “is this agent any good?”&lt;/p&gt;
&lt;p&gt;Benchmarking an AI agent means the same thing it means anywhere else: you write down a fixed set of test questions with known correct answers, run the agent against every one of them, and check two things whenever anything changes. Did it pull the right source material back out of your documents, and did the answer it gave actually match a correct answer?&lt;/p&gt;
&lt;p&gt;Do that before the next feature, and you have a number that tells you whether a change made the system better or worse. Skip it, and every “it feels smarter now” is a guess.&lt;/p&gt;
&lt;p&gt;Here’s how RAGnetic did it. The only benchmark I ever saved to the repo ran 10 questions, generated by an LLM from a single PDF (a paper on language agents). The gold answers were generated and then never compared to what the agent actually produced.&lt;/p&gt;
&lt;p&gt;The benchmark only asks an LLM judge whether the answer is faithful to the retrieved context, and on that measure it scored 10 for 10. Worth knowing that’s a real metric and a real distinction, not something I made up to sound rigorous: &lt;a href=&quot;https://docs.ragas.io/en/stable/concepts/metrics/available_metrics/&quot;&gt;Ragas lists faithfulness and factual correctness as separate metrics&lt;/a&gt;, because “grounded in the context you retrieved” and “actually the right answer” are different questions. I only ever asked the first one.&lt;/p&gt;
&lt;p&gt;The retrieval side tells the real story. I logged two versions of the retrieval metrics: a strict one that matches chunk IDs exactly, and a lenient one with a content-hash fallback for when the IDs don’t line up.&lt;/p&gt;
&lt;p&gt;The strict metric scored zero on every single question, all 10, because the two ID systems it was comparing never could have matched. The lenient metric hit on 9 of 10.&lt;/p&gt;
&lt;p&gt;That means one question missed retrieval entirely, the agent answered from context that wasn’t the right context, and the faithfulness judge scored it faithful anyway.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What the benchmark measured&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Faithfulness: is the answer supported by the context it retrieved?&lt;/td&gt;
&lt;td&gt;10 / 10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retrieval, lenient (content-hash fallback match)&lt;/td&gt;
&lt;td&gt;9 / 10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retrieval, strict (exact chunk-ID match)&lt;/td&gt;
&lt;td&gt;0 / 10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Whether the answer was actually correct&lt;/td&gt;
&lt;td&gt;never measured&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A 100% pass rate on 10 easy questions from one document isn’t evidence the system works. It’s evidence I never built a way for it to fail, the same trap &lt;a href=&quot;/blog/overfitting-vs-underfitting/&quot;&gt;an overfit model falls into&lt;/a&gt;: a number that looks perfect because you never tested it against anything that could prove it wrong.&lt;/p&gt;
&lt;p&gt;I’ve made the same mistake in the other direction too: when I later looked at &lt;a href=&quot;/blog/claude-code-auto-mode/&quot;&gt;how Claude Code’s auto mode keeps an agent in check&lt;/a&gt;, the thing that stood out was a small, dumb judge sitting between the agent and the action, not a vibe check. A real evaluation is the same idea applied to quality instead of safety: something small and mechanical that can actually say no.&lt;/p&gt;
&lt;p&gt;Meanwhile the README called the whole thing “production-ready” and “enterprise-grade,” under a section titled “Enterprise &amp;amp; Government Readiness.” One person, one PDF, one benchmark that couldn’t fail, and a claim like that sitting at the top of the file. It wasn’t dishonesty so much as the same instinct that built the five pillars: reach for the bigger claim before you have the evidence for the smaller one.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What I’d do differently:&lt;/strong&gt; build the evaluation before the second feature. A real, hand-written test set on real documents, run on every change.&lt;/p&gt;
&lt;p&gt;If gold answers exist, compare them in code, an exact match or a real scoring function, not just a second model’s opinion of the first model’s answer.&lt;/p&gt;
&lt;p&gt;That’s a rule I hold myself to now on newer AI features: numbers and facts come from your actual data, checked in code, never from what a model remembers or another model’s vibe check. If you can’t measure “better” that way, you’re not steering, you’re guessing.&lt;/p&gt;
&lt;h2 id=&quot;lesson-3-every-layer-i-sat-on-top-of-moved-faster-than-i-did&quot;&gt;Lesson 3: every layer I sat on top of moved faster than I did&lt;/h2&gt;
&lt;p&gt;RAGnetic is glue over LangChain, LangGraph, Ollama, Hugging Face, PEFT and TRL, Chroma, FAISS, Qdrant, Pinecone, Mongo, Celery, Redis, and Postgres. Ten dependencies, all moving, all at once, and I was the only one keeping them wired together.&lt;/p&gt;
&lt;p&gt;The scars are still in the repo. Eight &lt;code&gt;requirements*.txt&lt;/code&gt; files, one of them literally named &lt;code&gt;requirements-old-bloated.txt.backup&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A hard pin on &lt;code&gt;numpy==1.26.4&lt;/code&gt;. A 2.4 GB virtual environment.&lt;/p&gt;
&lt;p&gt;Commit messages like “More REDIS REDIS REDIS REDIS STUFF” and “SOME ANNOYING MIGRATION ISSUES THAT ARE FINALLY FIXED,” both from the same July week.&lt;/p&gt;
&lt;p&gt;The clearest example is the Ollama client. &lt;code&gt;app/core/config.py&lt;/code&gt; builds it by importing &lt;code&gt;ChatOllama&lt;/code&gt; from &lt;code&gt;langchain_community.chat_models.ollama&lt;/code&gt;. &lt;code&gt;app/agents/agent_graph.py&lt;/code&gt; builds the same client by importing &lt;code&gt;ChatOllama&lt;/code&gt; from &lt;code&gt;langchain_ollama&lt;/code&gt; instead.&lt;/p&gt;
&lt;p&gt;Two different code paths, constructing the same object, and one of them was already on the path LangChain had moved away from by the time I wrote the other — &lt;a href=&quot;https://docs.langchain.com/oss/python/integrations/chat/ollama&quot;&gt;the current docs install &lt;code&gt;langchain-ollama&lt;/code&gt; and import &lt;code&gt;ChatOllama&lt;/code&gt; from it&lt;/a&gt;, not from &lt;code&gt;langchain_community&lt;/code&gt;. I didn’t notice while I was building it. The ecosystem I was gluing together shipped a breaking change faster than I finished wiring the last one in.&lt;/p&gt;
&lt;p&gt;Some of that is just what building on other people’s fast-moving projects looks like. But a lot of my time went into keeping the glue from coming apart, time I wasn’t spending on the one thing that would have made a coworker’s day easier.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What I’d do differently:&lt;/strong&gt; fewer dependencies, one storage backend, and an install that takes one command. If a stranger, or a coworker, can’t try it in five minutes, they won’t.&lt;/p&gt;
&lt;h2 id=&quot;lesson-4-it-kept-me-ahead-of-the-ai-race&quot;&gt;Lesson 4: it kept me ahead of the AI race&lt;/h2&gt;
&lt;p&gt;I don’t want to end on pure regret, because that isn’t honest either.&lt;/p&gt;
&lt;p&gt;The same speed that beat me in Lesson 3 cut the other way too. Chasing LangChain, LangGraph, Ollama, and half a dozen vector stores as they all moved at once meant I was never reading about the AI race from the sidelines.&lt;/p&gt;
&lt;p&gt;I was inside it, learning and adapting in real time. Even as a failure, RAGnetic is the reason I know what the raw architecture of an agent actually looks like when you wire it together yourself instead of skimming a diagram of one.&lt;/p&gt;
&lt;p&gt;In four months, alone, I shipped multi-tenant auth, background workers, a sandboxed executor, a hybrid retriever, a LoRA training pipeline wired into the same YAML as everything else, a CLI, a dashboard, a REST API, database migrations, and observability. I understand the full stack of an LLM application in a way that reading about it never would have given me.&lt;/p&gt;
&lt;p&gt;I can also name the actual mistakes now instead of feeling vaguely bad about them: I built a platform before I found a person, I built a benchmark that couldn’t fail, and I let ten fast-moving dependencies eat time that should have gone to the two people who’d offered to try the thing.&lt;/p&gt;
&lt;p&gt;RAGnetic is still up on GitHub, and I haven’t touched it since March. What I keep coming back to is whether the honest version of this idea is smaller than what I built: not a platform with five pillars, but something that runs entirely on your own machine, on an open-source model, built around the one question I started with. And what’s the proof?&lt;/p&gt;
&lt;p&gt;If you’ve shipped something nobody used, what’s the one feature you kept promising yourself you’d add before showing anyone?&lt;/p&gt;
</content:encoded><category>ai-agents</category><category>ai-tools</category><category>open-source</category><category>rag</category></item><item><title>Claude Code Cost: 4 Rules That Work, 4 That Don&apos;t</title><link>https://my-space.io/blog/claude-code-cost/</link><guid isPermaLink="true">https://my-space.io/blog/claude-code-cost/</guid><description>I checked my rules for cutting Claude Code cost against the docs and 8 billion real tokens. Four hold up. Four don&apos;t, and one is a file that isn&apos;t real.</description><pubDate>Sat, 19 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Claude Code cost comes down to two things: how much context gets re-sent on every turn, and which model reads it. I checked the rules I use to keep both small against my own usage numbers and the docs as they read on September 19, 2026.&lt;/p&gt;
&lt;p&gt;Four hold up. Four do not, and one of those, &lt;code&gt;.claudeignore&lt;/code&gt;, is a file I can’t find anywhere in the docs.&lt;/p&gt;
&lt;p&gt;One note on the word cost. I’m on the $200 Max plan, so I never see a bill.&lt;/p&gt;
&lt;p&gt;What I spend is session and weekly limits, and these rules are about fitting more work under them. The dollar figures below are list-price estimates that I use as a measuring stick.&lt;/p&gt;
&lt;p&gt;If you are on the $20 plan instead, &lt;a href=&quot;/blog/claude-plan/&quot;&gt;how to get the most out of a $20 Claude plan&lt;/a&gt; applies the same ideas to Pro.&lt;/p&gt;
&lt;p&gt;Here is the scorecard before the detail.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;th&gt;Verdict&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;opusplan&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Works&lt;/td&gt;
&lt;td&gt;Sonnet types, Opus plans. Each switch re-reads the context uncached.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/clear&lt;/code&gt; with a &lt;code&gt;/wrap&lt;/code&gt; skill&lt;/td&gt;
&lt;td&gt;Works&lt;/td&gt;
&lt;td&gt;Clearing costs nothing, and the notes keep continuity.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Haiku subagents&lt;/td&gt;
&lt;td&gt;Works, with limits&lt;/td&gt;
&lt;td&gt;The built-in Explore and Plan agents ignore the variable alone.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Disconnect unused MCP servers&lt;/td&gt;
&lt;td&gt;Works, modestly&lt;/td&gt;
&lt;td&gt;Their tool names still load: 2,659 tokens per request, measured.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.claudeignore&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not in the docs&lt;/td&gt;
&lt;td&gt;The documented way is &lt;code&gt;Read&lt;/code&gt; deny rules.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CLAUDE.md under 60 lines&lt;/td&gt;
&lt;td&gt;Saves almost nothing&lt;/td&gt;
&lt;td&gt;1.8k tokens, and it is cached.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CLAUDE_CODE_MAX_OUTPUT_TOKENS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No effect&lt;/td&gt;
&lt;td&gt;A ceiling, not a bill.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;effortLevel: &quot;high&quot;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Doesn’t save&lt;/td&gt;
&lt;td&gt;Already the default on most models. On Opus 5.5 it may cost more than the &lt;code&gt;medium&lt;/code&gt; default.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;what-actually-drives-claude-code-cost&quot;&gt;What actually drives Claude Code cost&lt;/h2&gt;
&lt;p&gt;I ran &lt;code&gt;npx ccusage@latest&lt;/code&gt; over July to September 2026. It counted about 8 billion tokens, worth roughly $4,590 at list prices.&lt;/p&gt;
&lt;p&gt;September alone, through the 19th, was 7.4 billion tokens and about $4,040. That is around twenty times the price of the plan.&lt;/p&gt;
&lt;p&gt;ccusage prices at list rates and may not know the newest models, so read the dollars as estimates.&lt;/p&gt;
&lt;p&gt;The shape of the numbers matters more than the total. Input outnumbers output 210 to 1. About 97 percent of that input is cache reads.&lt;/p&gt;
&lt;p&gt;Using the standard multipliers, a cache read costs a tenth of a normal input token and a five-minute cache write costs 1.25 times.&lt;/p&gt;
&lt;p&gt;My rough arithmetic says cache reads are about three quarters of my input cost and cache writes about a fifth. That is my math over ccusage output, not a published figure, and one-hour cache writes cost more than I assumed.&lt;/p&gt;
&lt;p&gt;The docs explain why. In their words, &lt;a href=&quot;https://code.claude.com/docs/en/costs&quot;&gt;Claude Code sends your full conversation with every request&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The cache makes the re-read cheap, not free. A session left open all day re-reads all of it for every one-line question.&lt;/p&gt;
&lt;p&gt;So Claude Code cost is context size, times turns, times the price of the model reading it. Every rule below pushes on one of those three.&lt;/p&gt;
&lt;p&gt;Three commands show where you stand. &lt;code&gt;/usage&lt;/code&gt; shows the session’s prompt cache hit rate and, on a plan, which skills, subagents, plugins and MCP servers used your limits.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/context&lt;/code&gt; shows what is filling the window. ccusage shows the history.&lt;/p&gt;
&lt;h2 id=&quot;my-settingsjson-minus-the-plugins&quot;&gt;My settings.json, minus the plugins&lt;/h2&gt;
&lt;p&gt;Here is the part of my &lt;code&gt;settings.json&lt;/code&gt; that touches cost, with the plugins cut out.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;model&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;opusplan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;env&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;CLAUDE_CODE_SUBAGENT_MODEL&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;haiku&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;hooks&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;SessionStart&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        &quot;matcher&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;clear&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        &quot;hooks&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            &quot;type&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;command&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            &quot;command&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;cat &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;$CLAUDE_PROJECT_DIR/SESSION_NOTES.md&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; 2&amp;gt;/dev/null || true&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three things in there matter. &lt;code&gt;model&lt;/code&gt; picks opusplan.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;env&lt;/code&gt; sends subagents to Haiku. The hook prints my session notes after every &lt;code&gt;/clear&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I left out four keys that are in my real file: &lt;code&gt;CLAUDE_CODE_MAX_OUTPUT_TOKENS&lt;/code&gt;, &lt;code&gt;CLAUDE_AUTOCOMPACT_PCT_OVERRIDE&lt;/code&gt;, &lt;code&gt;effortLevel&lt;/code&gt; and &lt;code&gt;advisorModel&lt;/code&gt;. They are the second half of this post.&lt;/p&gt;
&lt;h2 id=&quot;rule-1-opusplan-because-sonnet-can-type&quot;&gt;Rule 1: opusplan, because Sonnet can type&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;&quot;model&quot;: &quot;opusplan&quot;&lt;/code&gt; runs Opus in plan mode and Sonnet for execution, &lt;a href=&quot;https://code.claude.com/docs/en/model-config&quot;&gt;as the model docs describe&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I’m a software engineer, and I don’t &lt;a href=&quot;/blog/vibe-coding/&quot;&gt;vibe code&lt;/a&gt;. I know what I want changed before I ask, so Sonnet is more than enough to type it.&lt;/p&gt;
&lt;p&gt;Opus earns its price on the planning step, when the question is what to build. The docs agree with the split: Sonnet “handles most coding tasks well and costs less than Opus.”&lt;/p&gt;
&lt;p&gt;Here is my hot take. A lot of Opus spend is a very expensive way to work out what you want. If you already know, you don’t need the big model for most of the job.&lt;/p&gt;
&lt;p&gt;There is one catch, and &lt;a href=&quot;https://code.claude.com/docs/en/prompt-caching&quot;&gt;the caching docs&lt;/a&gt; are upfront about it. Each model has its own cache.&lt;/p&gt;
&lt;p&gt;Under opusplan, every plan-mode toggle is a model switch, and the next request reads the whole conversation with no cache hits.&lt;/p&gt;
&lt;p&gt;So opusplan is cheapest when the context is small at the switch. That is one more reason to start each task from a clear.&lt;/p&gt;
&lt;h2 id=&quot;rule-2-clear-is-the-cheapest-command-you-have&quot;&gt;Rule 2: /clear is the cheapest command you have&lt;/h2&gt;
&lt;p&gt;The docs put it plainly: “When you want a fresh start instead of continuity, &lt;code&gt;/clear&lt;/code&gt; costs nothing.” Stale context is re-sent with every message.&lt;/p&gt;
&lt;p&gt;In one long session, &lt;code&gt;/context&lt;/code&gt; showed 254k of the 290.8k tokens in use was the conversation itself, or 87 percent. The cheapest tokens are the ones you never carry into the next task.&lt;/p&gt;
&lt;p&gt;The problem is continuity. Clear, and the session forgets what it was doing. I fixed that with a skill and a hook.&lt;/p&gt;
&lt;p&gt;The skill, &lt;code&gt;/wrap&lt;/code&gt;, writes a short notes file before I clear. The hook prints that file into the new session. Context dies, but continuity doesn’t.&lt;/p&gt;
&lt;p&gt;The hook is the &lt;code&gt;SessionStart&lt;/code&gt; block above. &lt;code&gt;matcher: &quot;clear&quot;&lt;/code&gt; fires it only after a clear, and it prints &lt;code&gt;SESSION_NOTES.md&lt;/code&gt; from the project root.&lt;/p&gt;
&lt;p&gt;I have watched it fire: after a clear, the notes are right there at the top of the new session. I keep the file out of git with a global gitignore.&lt;/p&gt;
&lt;h3 id=&quot;the-wrap-skill-that-makes-clear-safe&quot;&gt;The /wrap skill that makes /clear safe&lt;/h3&gt;
&lt;p&gt;This is the whole skill, as it sits on my machine.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name: wrap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;description: Save session notes before clearing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;disable-model-invocation: true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;allowed-tools:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Bash(git *)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Bash(cat *)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Bash(wc *)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Current notes:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;`cat SESSION_NOTES.md 2&amp;gt;/dev/null || echo &quot;(none)&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Git state:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;`git branch --show-current 2&amp;gt;/dev/null`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;`git log -1 --oneline 2&amp;gt;/dev/null`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;`git status --short 2&amp;gt;/dev/null | head -20`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Write SESSION_NOTES.md in the directory this session started in.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Replace it fully.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Do not create or edit any other file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Rules:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Max 40 lines, each under 120 characters. Run &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;`wc -l`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; and trim if over.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Merge with the current notes: keep decisions that still apply, drop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  finished items. Keep any other unfinished task under its own heading.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Mark each done item VERIFIED (you saw the output) or UNVERIFIED.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Never write secrets, API keys, tokens, connection strings, or .env values.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Name every file, agent, and model. Never write &quot;it&quot;. No em dashes.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; If nothing is left open, write only: &quot;No open task. Last finished: &amp;lt;one line&amp;gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Headings: Task, Branch and state, Done, Left to do, Decisions, Rejected (and why)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Reply with one line only: the line count and &quot;Ready to /clear&quot;.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Four choices in there earn their place.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;disable-model-invocation: true&lt;/code&gt; makes it manual. &lt;a href=&quot;https://code.claude.com/docs/en/skills&quot;&gt;The skills docs&lt;/a&gt; say a skill set this way has no description in context at all, so &lt;code&gt;/wrap&lt;/code&gt; costs nothing until I type it. &lt;code&gt;/context&lt;/code&gt; agrees: &lt;code&gt;wrap&lt;/code&gt; is not in my skills list, which runs to 9.9k tokens.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;!&lt;/code&gt; lines run before Claude sees the skill. The command output replaces the placeholder, so the current notes and the git state arrive pre-loaded.&lt;/p&gt;
&lt;p&gt;One gotcha from the docs: a failing command aborts the whole skill. That is why the notes line ends in &lt;code&gt;|| echo &quot;(none)&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;allowed-tools&lt;/code&gt; limits it to reading, writing, git, &lt;code&gt;cat&lt;/code&gt; and &lt;code&gt;wc&lt;/code&gt;. It has no reason to touch anything else.&lt;/p&gt;
&lt;p&gt;The 40-line cap and “replace fully” are cost controls. The notes get printed into every fresh session, so a file that keeps growing is a tax I pay after every clear.&lt;/p&gt;
&lt;p&gt;The cap forces a merge: keep what still applies, drop what is finished. Running the skill also costs little in cache terms, because the caching docs say a skill’s instructions are appended as a message and nothing earlier in the conversation changes.&lt;/p&gt;
&lt;p&gt;Here is an example of what it writes. The project is made up.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;markdown&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;## Task&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Add rate limiting to the login endpoint.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;## Branch and state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Branch feature/rate-limit, 3 commits ahead of main, working tree clean.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;## Done&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; VERIFIED: limiter added in auth.js, 5 attempts per minute per IP; 12 of 12 tests pass.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; UNVERIFIED: the Redis backend under load; only ran it locally.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;## Left to do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Load test at 200 requests per second.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;## Decisions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Fixed window, not sliding: simpler, and good enough for login.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF;font-weight:bold&quot;&gt;## Rejected (and why)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Per-account limits: they lock out real users during an attack.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every done item is marked VERIFIED or UNVERIFIED, so the next session knows what was actually seen. The &lt;code&gt;Rejected (and why)&lt;/code&gt; heading stops the new session from re-proposing ideas you already turned down.&lt;/p&gt;
&lt;h3 id=&quot;where-to-keep-a-skill&quot;&gt;Where to keep a skill&lt;/h3&gt;
&lt;p&gt;A skill is a folder with a &lt;code&gt;SKILL.md&lt;/code&gt; file in it. The folder name is the slash command. Where you put the folder decides who gets it.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ~/.claude/skills/wrap&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;     # personal: every project on this machine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; .claude/skills/wrap&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;       # project: commit it and your team gets it&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Put the file in the folder and type &lt;code&gt;/wrap&lt;/code&gt;. The &lt;code&gt;description&lt;/code&gt; is recommended, and &lt;code&gt;name&lt;/code&gt; defaults to the folder name.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;wrap&lt;/code&gt; lives in my personal folder because I want it everywhere. A skill that only makes sense in one repo goes in the project folder instead.&lt;/p&gt;
&lt;p&gt;Add that folder to &lt;code&gt;.gitignore&lt;/code&gt; if you don’t want to share it.&lt;/p&gt;
&lt;h2 id=&quot;rule-3-send-subagents-to-haiku-and-know-which-ones-ignore-you&quot;&gt;Rule 3: Send subagents to Haiku, and know which ones ignore you&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;CLAUDE_CODE_SUBAGENT_MODEL: haiku&lt;/code&gt; sends subagent work to the cheapest model. Subagents matter for cost because verbose work, like test runs and log reading, stays in their context while only a summary comes back. &lt;a href=&quot;https://code.claude.com/docs/en/costs&quot;&gt;The costs page&lt;/a&gt; recommends this.&lt;/p&gt;
&lt;p&gt;There are two limits. The docs rank how a subagent picks its model: the per-invocation parameter, then the agent’s own &lt;code&gt;model&lt;/code&gt; frontmatter, then this variable, then the main conversation’s model. And &lt;a href=&quot;https://code.claude.com/docs/en/sub-agents&quot;&gt;the subagent docs&lt;/a&gt; are blunt: setting the variable “by itself doesn’t change the model the built-in Explore and Plan subagents run on.”&lt;/p&gt;
&lt;p&gt;That takes a second variable, &lt;code&gt;CLAUDE_CODE_SUBAGENT_MODEL_FORCE=1&lt;/code&gt;, which I haven’t tried. I checked my own session. The built-in Explore agent ran on Opus 5.&lt;/p&gt;
&lt;p&gt;Then there is the lesson. While I was checking these settings against the docs, I sent a subagent to look them up.&lt;/p&gt;
&lt;p&gt;It ran on Haiku. It told me that &lt;code&gt;CLAUDE_CODE_SUBAGENT_MODEL&lt;/code&gt; and &lt;code&gt;CLAUDE_AUTOCOMPACT_PCT_OVERRIDE&lt;/code&gt; don’t exist. Both are in the docs.&lt;/p&gt;
&lt;p&gt;A cheap model gave a confident wrong answer, and I only caught it because I re-checked the pages myself. Haiku is fine for reading files and running tests. I wouldn’t let it deliver the verdict.&lt;/p&gt;
&lt;h2 id=&quot;rule-4-disconnect-the-mcp-servers-you-arent-using&quot;&gt;Rule 4: Disconnect the MCP servers you aren’t using&lt;/h2&gt;
&lt;p&gt;I picked this one up recently: connected MCP servers cost tokens even when you never call them. That is true, so I measured it.&lt;/p&gt;
&lt;p&gt;By default the cost is small. &lt;a href=&quot;https://code.claude.com/docs/en/mcp&quot;&gt;The MCP docs&lt;/a&gt; say tool search defers tool definitions, so “only tool names and server instructions load at session start.” The costs page tells you to run &lt;code&gt;/mcp&lt;/code&gt; and disable servers you aren’t using.&lt;/p&gt;
&lt;p&gt;Here is the measurement. I sent the same prompt in print mode on Haiku, once with my normal setup and once with &lt;code&gt;--strict-mcp-config&lt;/code&gt; and an empty config. Then I added up the input, cache write and cache read tokens.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{&quot;mcpServers&quot;:{}}&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; empty.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;claude&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Reply with the single word: ok&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --model&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; haiku&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --output-format&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;claude&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Reply with the single word: ok&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --model&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; haiku&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --output-format&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; json&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --strict-mcp-config&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --mcp-config&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; empty.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My normal setup used 26,526 input tokens. With no MCP servers it used 23,867.&lt;/p&gt;
&lt;p&gt;The difference is 2,659 tokens per request, identical on three runs, or about 11 percent of a bare session. That is the price of the names and instructions for everything I have connected, and it is re-read every turn at the cached rate.&lt;/p&gt;
&lt;p&gt;Other models count tokens differently, so your number will differ.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/context&lt;/code&gt; gives the other half. In a long session on a 1M window it showed 290.8k tokens in use.&lt;/p&gt;
&lt;p&gt;The MCP tools I had actually loaded took 4.1k. Another 163.1k of MCP tool definitions sat in a deferred pile that never entered the window, which is 16 percent of it.&lt;/p&gt;
&lt;p&gt;That pile is what deferral keeps out, and &lt;code&gt;ENABLE_TOOL_SEARCH=false&lt;/code&gt; would load it on every request.&lt;/p&gt;
&lt;p&gt;Two more things stood out. First, the 23,867 is not MCP.&lt;/p&gt;
&lt;p&gt;A bare “say ok” carries almost 24,000 tokens of Claude Code’s own prompt and whatever else I have installed. In an interactive session &lt;code&gt;/context&lt;/code&gt; splits that as a system prompt of 4.8k, built-in tools of 15.5k, skills of 9.9k, custom agents of 1.5k and CLAUDE.md of 1.8k.&lt;/p&gt;
&lt;p&gt;That is a different model and mode, so the totals won’t match.&lt;/p&gt;
&lt;p&gt;Second, the small number depends on defaults. The docs say tool definitions load up front when &lt;code&gt;ENABLE_TOOL_SEARCH&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, or &lt;code&gt;auto&lt;/code&gt; while they total under 10 percent of the window.&lt;/p&gt;
&lt;p&gt;They also load up front when a server sets &lt;code&gt;alwaysLoad&lt;/code&gt;, or when &lt;code&gt;ANTHROPIC_BASE_URL&lt;/code&gt; points to a non-first-party host.&lt;/p&gt;
&lt;p&gt;I set none of those. If you do, the cost per server jumps.&lt;/p&gt;
&lt;p&gt;Disconnecting is safe mid-session under the default. The caching docs say a deferred server connecting or disconnecting only appends to the conversation and leaves the cache alone. With deferral off, that change invalidates it.&lt;/p&gt;
&lt;h2 id=&quot;what-did-not-hold-up&quot;&gt;What did not hold up&lt;/h2&gt;
&lt;p&gt;Four of my rules didn’t survive checking.&lt;/p&gt;
&lt;h3 id=&quot;hiding-files-takes-a-read-deny-rule-not-a-claudeignore&quot;&gt;Hiding files takes a Read deny rule, not a .claudeignore&lt;/h3&gt;
&lt;p&gt;I thought I was hiding files with a &lt;code&gt;.claudeignore&lt;/code&gt;. &lt;a href=&quot;https://code.claude.com/docs/en/permissions&quot;&gt;The permissions page&lt;/a&gt; never mentions one. What it describes is &lt;code&gt;Read&lt;/code&gt; deny rules: add one for the path, “such as &lt;code&gt;Read(./.env)&lt;/code&gt; or &lt;code&gt;Read(./secrets/**)&lt;/code&gt;.”&lt;/p&gt;
&lt;p&gt;It gets worse. My user settings have no deny rules, this project’s &lt;code&gt;deny&lt;/code&gt; list is empty, and there is no &lt;code&gt;.claudeignore&lt;/code&gt; file in this repo either.&lt;/p&gt;
&lt;p&gt;I believed I followed a rule that I didn’t. Here is what I’d add for this site, in &lt;code&gt;.claude/settings.json&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;permissions&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;deny&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;Read(./node_modules/**)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;Read(./dist/**)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two caveats from the docs. Read deny rules cover the built-in file tools and file commands that Claude Code recognizes in Bash, like &lt;code&gt;cat&lt;/code&gt;. They don’t cover &lt;code&gt;grep -r pattern .&lt;/code&gt; or a script that opens files itself, and for Grep and Glob the docs call it a best-effort attempt.&lt;/p&gt;
&lt;p&gt;And I haven’t measured how many tokens this saves. It keeps Claude out of files it has no business in.&lt;/p&gt;
&lt;p&gt;That is safety first, savings maybe.&lt;/p&gt;
&lt;p&gt;Deny rules are one wall. &lt;a href=&quot;/blog/claude-code-auto-mode/&quot;&gt;Claude Code auto mode&lt;/a&gt; adds another: a classifier that reviews risky actions before they run.&lt;/p&gt;
&lt;h3 id=&quot;a-60-line-claudemd-saves-almost-nothing&quot;&gt;A 60-line CLAUDE.md saves almost nothing&lt;/h3&gt;
&lt;p&gt;My rule is that CLAUDE.md stays under 60 lines. Mine is 57 lines, and &lt;code&gt;/context&lt;/code&gt; puts it at 1.8k tokens.&lt;/p&gt;
&lt;p&gt;That is 0.2 percent of a 1M window, and about a fifth of what my skill descriptions take. It sits in the cached project context and gets read at the cached rate.&lt;/p&gt;
&lt;p&gt;Against 8 billion tokens, the size is not where the savings are.&lt;/p&gt;
&lt;p&gt;The docs’ own advice is to keep it under 200 lines and move workflow-specific instructions into skills, which load only when invoked. One more fact worth knowing: editing CLAUDE.md mid-session doesn’t invalidate the cache, but the edit doesn’t apply either. It loads on the next &lt;code&gt;/clear&lt;/code&gt;, &lt;code&gt;/compact&lt;/code&gt; or restart.&lt;/p&gt;
&lt;p&gt;Keep it short because that is tidy. Just don’t count it as a saving.&lt;/p&gt;
&lt;h3 id=&quot;two-settings-that-dont-save-anything&quot;&gt;Two settings that don’t save anything&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;CLAUDE_CODE_MAX_OUTPUT_TOKENS: 64000&lt;/code&gt; is a ceiling. You pay for the tokens the model writes, not for the room it had.&lt;/p&gt;
&lt;p&gt;I couldn’t confirm the variable in the docs, but it doesn’t matter. A cap you don’t hit costs nothing either way.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;effortLevel: &quot;high&quot;&lt;/code&gt; is already the default on most models. The docs call it “the default on every model except Opus 5.5 and Opus 4.7.”&lt;/p&gt;
&lt;p&gt;Opus 5.5 defaults to &lt;code&gt;medium&lt;/code&gt;. The docs also say a top-level &lt;code&gt;effortLevel&lt;/code&gt; in your user settings “doesn’t count for Opus 5.5,” but my own session log recorded &lt;code&gt;high&lt;/code&gt; on its turns and &lt;code&gt;/effort&lt;/code&gt; shows &lt;code&gt;high&lt;/code&gt;, so check yours before you trust either. If the line does apply, it costs more on Opus 5.5, not less. On Opus 4.7, where the default is &lt;code&gt;xhigh&lt;/code&gt;, it is a step down.&lt;/p&gt;
&lt;p&gt;The level that saves is &lt;code&gt;medium&lt;/code&gt;, which the docs say “reduces token usage for cost-sensitive work.” On most models each effort level also has its own cache, so pick one at the top of a session instead of switching halfway. Opus 5.5 is the exception: with a subscription or an API key, changing effort keeps the cache.&lt;/p&gt;
&lt;h2 id=&quot;autocompact-at-200k-a-trade-i-would-not-defend&quot;&gt;Autocompact at 200k: a trade I would not defend&lt;/h2&gt;
&lt;p&gt;I ran the auto-compact window at 200k for a while. The documented way is &lt;code&gt;/autocompact 200k&lt;/code&gt;, which saves as the &lt;code&gt;autoCompactWindow&lt;/code&gt; setting.&lt;/p&gt;
&lt;p&gt;My real settings use &lt;code&gt;CLAUDE_AUTOCOMPACT_PCT_OVERRIDE&lt;/code&gt; at 60, which is also documented. It sets the percentage of the window where compaction fires, and it can only compact earlier.&lt;/p&gt;
&lt;p&gt;On models with a 1M window, the default is about 967K tokens. So 200k compacts almost five times sooner, and for me it sometimes compacted too early. That is the setting working as designed.&lt;/p&gt;
&lt;p&gt;Does it cut Claude Code cost? On paper, partly.&lt;/p&gt;
&lt;p&gt;Every turn re-reads the whole context, so a smaller window caps that. The caching docs also say that with a warm cache, a mid-session compaction “costs a fraction of what the context size suggests.”&lt;/p&gt;
&lt;p&gt;Against that, compaction throws away detail and rebuilds part of the cache. If the cache has gone cold, the summary request reprocesses everything. I haven’t measured any of it, so I won’t claim a saving.&lt;/p&gt;
&lt;p&gt;Here is what I’d do instead. Leave the window alone, clear between tasks, and run &lt;code&gt;/compact&lt;/code&gt; by hand at a natural break. That matches the docs’ own tip: compact between tasks instead of waiting for auto-compaction to trigger mid-task.&lt;/p&gt;
&lt;h2 id=&quot;what-i-would-change-first&quot;&gt;What I would change first&lt;/h2&gt;
&lt;p&gt;Delete &lt;code&gt;CLAUDE_CODE_MAX_OUTPUT_TOKENS&lt;/code&gt;. Set the effort level on purpose instead of restating the default. Swap the autocompact override for the default window.&lt;/p&gt;
&lt;p&gt;Add the deny rules. Keep the four rules that work: opusplan, clearing with &lt;code&gt;/wrap&lt;/code&gt;, Haiku subagents with their limits in mind, and a short MCP list. Run &lt;code&gt;/mcp&lt;/code&gt; and disable anything you haven’t used this week.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;advisorModel: &quot;opus&quot;&lt;/code&gt; is not a saving either. It picks the model that answers advisor calls, so it is a quality purchase. Keep it if it earns its place.&lt;/p&gt;
&lt;p&gt;Then measure. Run &lt;code&gt;/usage&lt;/code&gt; and read the &lt;code&gt;Prompt cache (main)&lt;/code&gt; line, which shows your hit rate and misses. If a rule doesn’t move a number, it is a habit, not a saving.&lt;/p&gt;
&lt;p&gt;The opusplan split above is also what runs my &lt;a href=&quot;/blog/automated-code-review-claude-code/&quot;&gt;automated code review pipeline&lt;/a&gt;: a strong model for the review stages that matter, a faster one for everything else. That pipeline costs real minutes on top of everything in this post, and I think it’s worth it.&lt;/p&gt;
&lt;p&gt;What is the one rule you follow to keep your Claude Code cost down, and have you checked it against the docs lately?&lt;/p&gt;
</content:encoded><category>claude-code</category><category>ai-tools</category><category>developer-tools</category><category>productivity</category></item><item><title>Linear Regression in Python: Study Hours vs Score With SciPy</title><link>https://my-space.io/blog/linear-regression-python/</link><guid isPermaLink="true">https://my-space.io/blog/linear-regression-python/</guid><description>Linear regression in Python, step by step: fit study hours against exam score with SciPy, read the slope and R-squared, then add a second variable.</description><pubDate>Fri, 18 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Linear regression in Python takes one function call: &lt;code&gt;scipy.stats.linregress(x, y)&lt;/code&gt;. It fits a straight line through your data and hands back the slope, the intercept, and a few numbers that tell you how good the line is.&lt;/p&gt;
&lt;p&gt;In this post I use it on made-up data: hours studied against exam score. Then I open the box. I’ll show what R-squared measures, how least squares finds the line, why a good line can still give a silly prediction, what changes when you add a second input, and how the same fit looks in scikit-learn.&lt;/p&gt;
&lt;p&gt;Linear regression shows up in my Big Data Analytics master’s at SDSU: I used it in one of my final projects, before the AI boom, when you built and tested your own models. This post pulls that together with notes from a machine learning course on Udemy.&lt;/p&gt;
&lt;p&gt;The example is mine: study hours against score. Every code block runs top to bottom on NumPy 2.2.6 and SciPy 1.15.3, and the output shown is what it printed. If arrays are new to you, start with my &lt;a href=&quot;/blog/numpy-tutorial-arrays/&quot;&gt;NumPy tutorial&lt;/a&gt; first.&lt;/p&gt;
&lt;h2 id=&quot;what-is-linear-regression&quot;&gt;What is linear regression?&lt;/h2&gt;
&lt;p&gt;Linear regression predicts a number, called the dependent variable &lt;code&gt;y&lt;/code&gt;, from one or more inputs, called independent variables &lt;code&gt;x&lt;/code&gt;. It assumes the relationship is a straight line:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;y = b0 + b1 * x + e&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;b0&lt;/code&gt; is the intercept: the predicted &lt;code&gt;y&lt;/code&gt; when &lt;code&gt;x&lt;/code&gt; is zero.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;b1&lt;/code&gt; is the slope: how much &lt;code&gt;y&lt;/code&gt; changes when &lt;code&gt;x&lt;/code&gt; goes up by one.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;e&lt;/code&gt; is the error: everything about &lt;code&gt;y&lt;/code&gt; that &lt;code&gt;x&lt;/code&gt; doesn’t explain.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You already do this in your head. Say you earned $10,000 two years ago and $20,000 last year. If you guess $30,000 for this year, you just drew a line through two points and extended it.&lt;/p&gt;
&lt;p&gt;The model answers two questions. Is there a linear relationship between &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;? And, with several inputs, which one matters most?&lt;/p&gt;
&lt;p&gt;It has a big catch. It only works when the relationship really is roughly linear.&lt;/p&gt;
&lt;p&gt;And it will never predict every point exactly. The error term is there for a reason.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-run-linear-regression-in-python&quot;&gt;How do you run linear regression in Python?&lt;/h2&gt;
&lt;p&gt;Install what you need:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; numpy&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; scipy&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; matplotlib&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I generated 100 fake students so I’d know the true answer. Hours studied is normally distributed around 20. Each hour adds 1.1 points to the score, which is out of 100.&lt;/p&gt;
&lt;p&gt;There’s also a second input, exercises completed, that adds 0.7 points per exercise, plus random noise.&lt;/p&gt;
&lt;p&gt;For now I’ll ignore exercises and regress score on hours alone.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; scipy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rng &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.default_rng(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;exercises &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;score &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 25&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1.1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.7&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exercises &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;hours: min &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hours.min()&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, max &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hours.max()&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;score: min &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;score.min()&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, max &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;score.max()&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;slope, intercept, r_value, p_value, std_err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats.linregress(hours, score)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;slope &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;slope&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  intercept &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;intercept&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  r &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;r_value&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  r2 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;r_value&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  p &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;p_value&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2e&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  stderr &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;std_err&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;hours: min 3.7, max 32.7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;score: min 39.2, max 75.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;slope 1.077  intercept 35.103  r 0.770  r2 0.593  p 7.28e-21  stderr 0.090&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s what each number means. The &lt;a href=&quot;https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html&quot;&gt;SciPy docs&lt;/a&gt; list them all.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;slope, 1.077:&lt;/strong&gt; each extra hour of study goes with about one more point. That’s close to the 1.1 I built in. It says “goes with”, not “causes”.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;intercept, 35.103:&lt;/strong&gt; the predicted score at zero hours. Nobody in this data studied less than 3.7 hours, so don’t read too much into it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;r, 0.770:&lt;/strong&gt; the correlation between hours and score.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;p, 7.28e-21:&lt;/strong&gt; the p-value for a test where the starting assumption is that the slope is zero. A tiny value means that if the true slope were zero, you would almost never see a slope this large by chance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;stderr, 0.090:&lt;/strong&gt; the uncertainty on the slope.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-does-r-squared-tell-you&quot;&gt;What does R-squared tell you?&lt;/h2&gt;
&lt;p&gt;R-squared, also called the coefficient of determination, says how much of the variation in &lt;code&gt;y&lt;/code&gt; your line captures. Here it’s 0.593, so the line explains about 59% of the spread in scores. The rest is everything else: exercises, sleep, a lucky guess.&lt;/p&gt;
&lt;p&gt;SciPy’s docs confirm the shortcut: the square of &lt;code&gt;rvalue&lt;/code&gt; is the coefficient of determination. But the formula is worth seeing once:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;R-squared = 1 - (sum of squared errors) / (sum of squared distances from the mean)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bottom part is the total variation in scores around their own average. The top part is what the line failed to explain. In code:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; predict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slope &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; intercept&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sse &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.sum((score &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; predict(hours)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sst &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.sum((score &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; score.mean()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;1 - sse/sst =&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sse &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sst, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1 - sse/sst = 0.593&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Same number. Two ways to read it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;0 means the line explains nothing.&lt;/strong&gt; &lt;strong&gt;1 means it explains everything.&lt;/strong&gt; A low value is a poor fit. A high value is a good one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It isn’t strictly stuck between 0 and 1.&lt;/strong&gt; On new data, or for a model fitted without an intercept, R-squared can go negative. That means the model does worse than just guessing the average.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A high R-squared doesn’t prove your model is right. It just says the line follows the data you fitted it to.&lt;/p&gt;
&lt;h2 id=&quot;how-does-least-squares-find-the-line&quot;&gt;How does least squares find the line?&lt;/h2&gt;
&lt;p&gt;The method is called ordinary least squares, or OLS. It picks the slope and intercept that make the total squared error as small as possible.&lt;/p&gt;
&lt;p&gt;The error for one student is the actual score minus the predicted one. Then we square it, for two reasons:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A prediction can miss high or low. Squaring makes every miss positive, so misses can’t cancel each other out.&lt;/li&gt;
&lt;li&gt;Squaring punishes big misses far more than small ones.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Squaring doesn’t guarantee a better model. It just defines what “best line” means.&lt;/p&gt;
&lt;p&gt;You never have to do the math, because Python does it. But knowing what’s under the hood helps.&lt;/p&gt;
&lt;p&gt;The answer has a closed form. For one input it looks like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;slope     = r * (standard deviation of y) / (standard deviation of x)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;intercept = mean of y - slope * mean of x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I checked it against SciPy:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;slope_hand &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r_value &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; score.std() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hours.std()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;intercept_hand &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; score.mean() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slope_hand &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hours.mean()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;by hand:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(slope_hand, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(intercept_hand, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;by hand: 1.077 35.103&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Identical. So OLS gets its answer in one step.&lt;/p&gt;
&lt;p&gt;Some tutorials show the line inching toward the data over many iterations. That’s gradient descent, which is how neural networks are trained, and it isn’t needed for a straight line.&lt;/p&gt;
&lt;p&gt;I use it in my &lt;a href=&quot;/blog/overfitting-vs-underfitting/&quot;&gt;overfitting vs underfitting&lt;/a&gt; post if you want to see it running.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-predict-with-the-fitted-line&quot;&gt;How do you predict with the fitted line?&lt;/h2&gt;
&lt;p&gt;Multiply the input by the slope and add the intercept. I already wrote &lt;code&gt;predict()&lt;/code&gt; above. For a student who studies 30 hours:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;30 hours -&amp;gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(predict(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;100 hours -&amp;gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(predict(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;30 hours -&amp;gt; 67.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;100 hours -&amp;gt; 142.8&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first answer is reasonable. 30 hours sits inside the range of the data, which ran up to 32.7.&lt;/p&gt;
&lt;p&gt;The second answer is nonsense. A score of 142.8 isn’t possible on a test out of 100.&lt;/p&gt;
&lt;p&gt;The math worked perfectly. The line just has no idea that scores stop at 100, and it has never seen anyone study for 100 hours.&lt;/p&gt;
&lt;p&gt;Only trust predictions inside the range you fitted.&lt;/p&gt;
&lt;p&gt;To draw the data and the fitted line with matplotlib:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; plt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.scatter(hours, score)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.plot(hours, predict(hours), &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.xlabel(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hours studied&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.ylabel(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Score&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.show()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;what-is-multiple-linear-regression&quot;&gt;What is multiple linear regression?&lt;/h2&gt;
&lt;p&gt;Multiple linear regression uses two or more inputs. Add exercises completed to the model and it becomes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;score = b0 + b1 * hours + b2 * exercises&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now there are three numbers to estimate, and &lt;code&gt;linregress&lt;/code&gt; can’t do it, since it only takes one &lt;code&gt;x&lt;/code&gt;. NumPy’s least squares solver does the same job for any number of inputs. Add a column of ones so the model gets an intercept:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;X &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.column_stack([np.ones_like(hours), hours, exercises])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;coef, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;_ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.linalg.lstsq(X, score, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;rcond&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;b0, b1, b2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coef&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;b0 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;b0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; b1 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;b1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; b2 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;b2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pred &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; X &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coef&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;r2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.sum((score &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pred) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sst&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;multiple r2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(r2, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;30h, 15 ex -&amp;gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(b0 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; b1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; b2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;b0 24.737 b1 1.091 b2 0.686&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;multiple r2 0.739&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;30h, 15 ex -&amp;gt; 67.8&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The estimates land close to the truth I built in: 25, 1.1, and 0.7. R-squared jumps from 0.593 to 0.739, because exercises really do matter in this data.&lt;/p&gt;
&lt;p&gt;Each hour now adds about 1.09 points with exercises held fixed. In this data, hours and exercises are almost uncorrelated (-0.03), which is why the one-input slope of 1.077 was already close to the truth.&lt;/p&gt;
&lt;p&gt;In real data, inputs usually overlap, and leaving one out shifts the slope of the other.&lt;/p&gt;
&lt;p&gt;One warning. Adding an input can never lower R-squared on the data you fitted, even if that input is pure noise.&lt;/p&gt;
&lt;p&gt;So a rising R-squared isn’t proof that a new variable belongs. Check on data the model hasn’t seen, which is the whole point of the &lt;a href=&quot;/blog/overfitting-vs-underfitting/&quot;&gt;overfitting vs underfitting&lt;/a&gt; comparison.&lt;/p&gt;
&lt;h2 id=&quot;where-have-i-used-linear-regression-for-real&quot;&gt;Where have I used linear regression for real?&lt;/h2&gt;
&lt;p&gt;In the SDSU program, my part of the BDA 594 final group project was about NFL special teams. We were given league data and asked to build new metrics for the specialists: punters, kickers, and long snappers. Then we ranked them, hoping to find something that could help a team perform better.&lt;/p&gt;
&lt;p&gt;I preprocessed the exploratory data and built predictions for two things: the average distance a special teams player travels during a game, and their kick success rate in a game. The model was a simple linear regression, built as an artificial neural network.&lt;/p&gt;
&lt;p&gt;That’s less odd than it sounds. A neural network with no hidden layers and no activation function is linear regression: one weight per input, one bias, and a weighted sum.&lt;/p&gt;
&lt;p&gt;What changes is how it’s fitted. The network nudges its weights a little on every pass, which is gradient descent, instead of using the closed-form formula from earlier.&lt;/p&gt;
&lt;p&gt;Both are chasing the same line.&lt;/p&gt;
&lt;p&gt;Ranking real people from predicted values is where the warnings above stop being academic. R-squared tells you how much of the spread your line explains.&lt;/p&gt;
&lt;p&gt;It doesn’t tell you whether a ranking is fair. And a prediction is only as good as the range the model was fitted on, so a player far outside that range gets a guess, not a prediction.&lt;/p&gt;
&lt;p&gt;My grad projects are in &lt;a href=&quot;https://github.com/Ishraq21/BigData-ML-Final-Grad-Projects&quot;&gt;this GitHub repo&lt;/a&gt;, with a short write-up of each one.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-do-linear-regression-with-scikit-learn&quot;&gt;How do you do linear regression with scikit-learn?&lt;/h2&gt;
&lt;p&gt;Most tutorials on this topic use scikit-learn’s &lt;code&gt;LinearRegression&lt;/code&gt; instead of SciPy. It gives the same answer. Install it with &lt;code&gt;pip install scikit-learn&lt;/code&gt;, then reuse the &lt;code&gt;hours&lt;/code&gt;, &lt;code&gt;exercises&lt;/code&gt;, and &lt;code&gt;score&lt;/code&gt; arrays from above.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sklearn.linear_model &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; LinearRegression&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; LinearRegression()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model.fit(hours.reshape(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), score)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;slope &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model.coef_[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  intercept &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model.intercept_&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  r2 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;model.score(hours.reshape(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), score)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;30 hours -&amp;gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(model.predict([[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]])[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;slope 1.077  intercept 35.103  r2 0.593&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;30 hours -&amp;gt; 67.4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;scikit-learn wants the inputs as a 2D array, with one row per student and one column per input. That’s why &lt;code&gt;hours&lt;/code&gt; gets reshaped. Multiple regression is the same call with two columns:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;X &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.column_stack([hours, exercises])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;multi &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; LinearRegression().fit(X, score)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;b0 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;multi.intercept_&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  b1 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;multi.coef_[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  b2 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;multi.coef_[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  r2 &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;multi.score(X, score)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;b0 24.737  b1 1.091  b2 0.686  r2 0.739&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The slope, intercept, and R-squared match SciPy and NumPy to three decimals. So which one should you use?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;SciPy’s &lt;code&gt;linregress&lt;/code&gt;&lt;/strong&gt; is the quickest for one input, and it also gives you the p-value and standard error.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;scikit-learn&lt;/strong&gt; is the better fit once you have several inputs, or when the model is one step in a bigger workflow. It has the same &lt;code&gt;fit&lt;/code&gt; and &lt;code&gt;predict&lt;/code&gt; pattern as the classifier in my &lt;a href=&quot;/blog/knn-algorithm-scikit-learn-iris/&quot;&gt;KNN tutorial&lt;/a&gt;, so train and test splits and cross-validation work the same way.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;a href=&quot;https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html&quot;&gt;LinearRegression docs&lt;/a&gt; list the options.&lt;/p&gt;
&lt;h2 id=&quot;what-should-you-remember-about-linear-regression-in-python&quot;&gt;What should you remember about linear regression in Python?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;scipy.stats.linregress&lt;/code&gt; gives you the slope, intercept, correlation, and p-value in one call.&lt;/li&gt;
&lt;li&gt;R-squared is the share of variation your line explains, and &lt;code&gt;1 - SSE/SST&lt;/code&gt; computes it.&lt;/li&gt;
&lt;li&gt;OLS has a closed-form solution, so no iterations are needed.&lt;/li&gt;
&lt;li&gt;Predict inside your data’s range. Outside it, the line is guessing.&lt;/li&gt;
&lt;li&gt;For more than one input, use &lt;code&gt;np.linalg.lstsq&lt;/code&gt; or scikit-learn’s &lt;code&gt;LinearRegression&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What’s the first thing you’d try to predict with a straight line?&lt;/p&gt;
</content:encoded><category>python</category><category>machine-learning</category><category>regression</category><category>scipy</category><category>data-analysis</category></item><item><title>Overfitting vs Underfitting: How to Tell Which One You Have</title><link>https://my-space.io/blog/overfitting-vs-underfitting/</link><guid isPermaLink="true">https://my-space.io/blog/overfitting-vs-underfitting/</guid><description>Overfitting vs underfitting, explained with a small Python experiment: how train and test error give each one away, and when to stop training.</description><pubDate>Fri, 18 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Overfitting vs underfitting comes down to one comparison: error on the data the model trained on, against error on data it has never seen.&lt;/p&gt;
&lt;p&gt;An overfit model scores great on its training data and badly on new data. It memorized the noise.&lt;/p&gt;
&lt;p&gt;An underfit model scores badly on both. It was too simple to catch the trend in the first place.&lt;/p&gt;
&lt;p&gt;Some of what’s below comes from my grad work in Big Data Analytics at SDSU, which was before the AI boom, when you built and tested your own models. The rest comes from notes I made in a machine learning course on Udemy.&lt;/p&gt;
&lt;p&gt;The experiment is my own. Every number below is real output from NumPy 2.2.6, including the ugly ones.&lt;/p&gt;
&lt;h2 id=&quot;what-is-overfitting-vs-underfitting&quot;&gt;What is overfitting vs underfitting?&lt;/h2&gt;
&lt;p&gt;Data has two parts: the pattern you care about and noise. Noise is the random, irrelevant variation that doesn’t mean anything.&lt;/p&gt;
&lt;p&gt;Measurement error is noise. So is a student who had a bad morning.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Overfitting&lt;/strong&gt; is fitting the noise. The model bends itself to hit every training point, including the ones that were just bad luck.&lt;/p&gt;
&lt;p&gt;Training error comes out low. Error on new data comes out high.&lt;/p&gt;
&lt;p&gt;In bias and variance terms, that’s low bias and high variance. Variance here means how much the fitted model would change if you resampled the training data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Underfitting&lt;/strong&gt; is the opposite. The model is too simple to follow the trend, so it misses even the training points.&lt;/p&gt;
&lt;p&gt;Both errors come out high. That’s high bias and low variance.&lt;/p&gt;
&lt;p&gt;A straight line through a curve is the classic case.&lt;/p&gt;
&lt;p&gt;The trap is that training error alone can’t tell you which one you have. A model that memorizes every training point scores zero on the training data, and that number tells you nothing about how it will behave in the real world.&lt;/p&gt;
&lt;h2 id=&quot;what-does-overfitting-look-like-in-python&quot;&gt;What does overfitting look like in Python?&lt;/h2&gt;
&lt;p&gt;Let’s fake some data so we know the right answer. The true pattern rises and then levels off.&lt;/p&gt;
&lt;p&gt;I sample 15 training points, add noise with a standard deviation of 0.3, then draw 200 fresh points as a test set.&lt;/p&gt;
&lt;p&gt;That noise sets a floor. Even a perfect model can’t beat the noise variance on average.&lt;/p&gt;
&lt;p&gt;That’s the spread of the noise itself, 0.3 squared, or 0.09. So a test error near 0.09 is about as good as it gets, and a lucky test set can land a little under it.&lt;/p&gt;
&lt;p&gt;I fit polynomials of different degrees to the same 15 points using NumPy’s &lt;a href=&quot;https://numpy.org/doc/stable/reference/generated/numpy.polynomial.polynomial.Polynomial.fit.html&quot;&gt;Polynomial.fit&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Degree 1 is a straight line. Degree 14 has 15 coefficients for 15 points, so it can hit every one.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy.polynomial &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Polynomial&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rng &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.default_rng(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; true_curve&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.exp(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x_train &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.linspace(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_train &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true_curve(x_train) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.uniform(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true_curve(x_test) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; mse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(y, y_hat):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.mean((y &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; y_hat) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;degree  train MSE  test MSE&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; degree &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Polynomial.fit(x_train, y_train, degree)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;degree&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&amp;gt;6&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  {&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mse(y_train, model(x_train))&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&amp;gt;9.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  {&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mse(y_test, model(x_test))&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&amp;gt;8.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s what it printed:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;degree  train MSE  test MSE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     1      0.222     0.255&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     4      0.029     0.086&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    14      0.000     7.005&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Read it row by row:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Degree 1&lt;/strong&gt; is underfitting. Both errors are high, and they’re close to each other. The line can’t follow the curve.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Degree 4&lt;/strong&gt; is the good fit. Test error is 0.086, right at the noise floor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Degree 14&lt;/strong&gt; is overfitting. Training error is exactly zero, and test error is 7.005, about 80 times worse than degree 4.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also swept every degree from 1 to 14. Training error only ever went down, from 0.222 to 0.000.&lt;/p&gt;
&lt;p&gt;Test error fell until degree 4, crept up after that, and blew up past degree 11: 0.132 at degree 11, 0.626 at 12, and 7.005 at 14.&lt;/p&gt;
&lt;p&gt;This is one random draw. I re-ran the setup with eight different seeds. The size of the blow-up changed a lot, but degree 14 always scored 0.000 on training and at least 2.0 on test.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-spot-overfitting-in-a-real-model&quot;&gt;How do you spot overfitting in a real model?&lt;/h2&gt;
&lt;p&gt;You can’t plot a model with fifty features and eyeball the curve. So you compare numbers instead. Split your data, then look at both errors together:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Both high, and close together:&lt;/strong&gt; underfitting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Training low, test much higher:&lt;/strong&gt; overfitting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Both low, and close together:&lt;/strong&gt; a good fit.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That’s why supervised learning uses a train and test split, and often a third validation split. If you’ve followed my &lt;a href=&quot;/blog/knn-algorithm-scikit-learn-iris/&quot;&gt;KNN classifier with scikit-learn&lt;/a&gt;, that’s the same reason the test set stays untouched until the end.&lt;/p&gt;
&lt;p&gt;For neural networks and other models trained step by step, there’s a second view: error over time. One &lt;strong&gt;epoch&lt;/strong&gt; is one full pass of the training data through the model.&lt;/p&gt;
&lt;p&gt;Plot training error and validation error after each epoch and you get two curves. If neural networks are new to you, my &lt;a href=&quot;/blog/tensorflow-create-tensors-python/&quot;&gt;TensorFlow tensors guide&lt;/a&gt; starts from the basics.&lt;/p&gt;
&lt;p&gt;A healthy model shows both falling and levelling off. An overfitting model shows the training curve still falling while the validation curve turns upward.&lt;/p&gt;
&lt;h2 id=&quot;when-should-you-stop-training&quot;&gt;When should you stop training?&lt;/h2&gt;
&lt;p&gt;At the point where validation error turns up. Everything after that is memorization.&lt;/p&gt;
&lt;p&gt;To see it, I trained the same kind of model, a degree-14 polynomial, with gradient descent instead of solving for the best fit in one step.&lt;/p&gt;
&lt;p&gt;The code writes it in a Chebyshev basis, which spans the same polynomials but keeps the numbers well behaved. Gradient descent nudges the weights a little after every epoch, which is how neural networks learn.&lt;/p&gt;
&lt;p&gt;This time I used three sets. The training set fits the weights.&lt;/p&gt;
&lt;p&gt;The validation set picks the stopping epoch. The test set only grades the final result, so it never influences a choice.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy.polynomial &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chebyshev &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rng &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.default_rng(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; true_curve&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.exp(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x_train &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.linspace(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_train &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true_curve(x_train) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x_val &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.uniform(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_val &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true_curve(x_val) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.uniform(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true_curve(x_test) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rng.normal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;DEGREE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 14&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; features&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; C.chebvander(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;DEGREE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A_train, A_val, A_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; features(x_train), features(x_val), features(x_test)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; mse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(y, y_hat):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.mean((y &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; y_hat) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;w &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.zeros(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;DEGREE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;learning_rate &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;best_val, best_epoch, best_w &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.inf, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, w.copy()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200_001&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    gradient &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; A_train.T &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (A_train &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; w &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; y_train) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(y_train)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    w &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; learning_rate &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; gradient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    val_error &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mse(y_val, A_val &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; w)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val_error &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; best_val:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        best_val, best_epoch, best_w &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val_error, epoch, w.copy()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1_000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10_000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50_000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200_000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;epoch &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;epoch&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&amp;gt;7&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  train &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mse(y_train, A_train &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; w)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  validation &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;val_error&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;best validation error &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;best_val&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; at epoch &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;best_epoch&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;test error at that epoch &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mse(y_test, A_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; best_w)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;test error if we kept going &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mse(y_test, A_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; w)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;epoch      10  train 0.088  validation 0.442&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;epoch    1000  train 0.001  validation 0.317&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;epoch   10000  train 0.001  validation 0.138&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;epoch   50000  train 0.000  validation 0.368&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;epoch  200000  train 0.000  validation 2.646&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;best validation error 0.138 at epoch 10919&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;test error at that epoch 0.127&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;test error if we kept going 1.183&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three things stand out.&lt;/p&gt;
&lt;p&gt;First, training error was already tiny by epoch 1,000 and stayed near zero. If I had only watched that column, I’d have said everything looked great.&lt;/p&gt;
&lt;p&gt;Second, validation error kept improving long after training error stopped moving.&lt;/p&gt;
&lt;p&gt;It bottomed out at epoch 10,919, then climbed to 2.646 by epoch 200,000 while training error sat at 0.000. The stopping point is where validation turns upward, not where training flattens.&lt;/p&gt;
&lt;p&gt;Third, look at the test error. Stopped at the best epoch, the same model scores 0.127 on fresh data. Left running, it scores 1.183.&lt;/p&gt;
&lt;p&gt;This trick is called early stopping. It’s a small fix with a big effect.&lt;/p&gt;
&lt;p&gt;It also beat the one-step fit from earlier, which scored 7.005 on test. Same degree-14 family, very different result. Both scores come from 200 fresh points on the same curve.&lt;/p&gt;
&lt;h2 id=&quot;how-do-you-fix-overfitting-and-underfitting&quot;&gt;How do you fix overfitting and underfitting?&lt;/h2&gt;
&lt;p&gt;The two need opposite fixes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To fix underfitting&lt;/strong&gt;, give the model more room:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use a more flexible model, such as a higher polynomial degree or a deeper network.&lt;/li&gt;
&lt;li&gt;Add features that carry real signal.&lt;/li&gt;
&lt;li&gt;Train for longer, since an underfit model may simply not have converged yet.&lt;/li&gt;
&lt;li&gt;Reduce any regularization you added.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;To fix overfitting&lt;/strong&gt;, take room away, or add evidence:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get more training data. It’s the most reliable fix, and often the hardest.&lt;/li&gt;
&lt;li&gt;Use a simpler model. Degree 4 beat degree 14 here.&lt;/li&gt;
&lt;li&gt;Stop early, as above.&lt;/li&gt;
&lt;li&gt;Add regularization, which penalizes large weights. For neural networks, dropout does something similar.&lt;/li&gt;
&lt;li&gt;Use cross-validation, so one lucky split can’t fool you.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Most models have a dial for this. The number of neighbors &lt;code&gt;k&lt;/code&gt; in KNN is one such dial.&lt;/p&gt;
&lt;p&gt;With &lt;code&gt;k = 1&lt;/code&gt; each point is its own neighbor, so the model memorizes the training set. A large &lt;code&gt;k&lt;/code&gt; smooths so much that it starts to underfit.&lt;/p&gt;
&lt;p&gt;The simplest model of all is a straight line, and it’s a good place to start. I covered how it’s fitted in &lt;a href=&quot;/blog/linear-regression-python/&quot;&gt;linear regression in Python&lt;/a&gt;. The scikit-learn docs also have a &lt;a href=&quot;https://scikit-learn.org/stable/auto_examples/model_selection/plot_underfitting_overfitting.html&quot;&gt;worked underfitting vs overfitting example&lt;/a&gt; if you want a second take.&lt;/p&gt;
&lt;h2 id=&quot;where-does-overfitting-matter-outside-a-toy-problem&quot;&gt;Where does overfitting matter outside a toy problem?&lt;/h2&gt;
&lt;p&gt;Two of my SDSU projects sit right on this problem. Both are described in &lt;a href=&quot;https://github.com/Ishraq21/BigData-ML-Final-Grad-Projects&quot;&gt;my grad projects repo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The first was my individual final project for LING 583, a statistical text analysis class.&lt;/p&gt;
&lt;p&gt;I used the &lt;a href=&quot;https://www.kaggle.com/datasets/manchunhui/us-election-2020-tweets&quot;&gt;2020 US Election Tweets dataset&lt;/a&gt; from Kaggle to predict which candidate a tweet was about, from the text alone. I trained several different classifiers, then brought in the hashtags to see how they related to each candidate.&lt;/p&gt;
&lt;p&gt;Comparing classifiers is a model-selection problem, and it’s the reason this whole post exists.&lt;/p&gt;
&lt;p&gt;The only fair way to pick between them is how each one does on tweets it never trained on. A classifier with the best training score has told you nothing yet.&lt;/p&gt;
&lt;p&gt;My notebook ended with a summary of the preprocessing, the models, the predictions, the limitations, and the results.&lt;/p&gt;
&lt;p&gt;The limitations part is a habit worth keeping. A model that only reports its wins hasn’t really been tested.&lt;/p&gt;
&lt;p&gt;The second was my part of the BDA 600 capstone. I built an LSTM for time series prediction on crypto data, and used the VADER sentiment analyzer on Twitter posts.&lt;/p&gt;
&lt;p&gt;The project’s stated goal was a tool for cryptocurrency investors, and the team published the result as a &lt;a href=&quot;https://storymaps.arcgis.com/stories/6a5bd841643c4313b020a51e99dfb7dc&quot;&gt;StoryMap called Forecast and Predict Crypto Trend&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Time series overfit in an expensive way. A model can fit the past almost perfectly, and the past is exactly the data it has already seen.&lt;/p&gt;
&lt;p&gt;The test that counts is forward in time. That’s why you split by date, not at random: train on the earlier dates and test on the later ones. A random split lets the model peek at the future.&lt;/p&gt;
&lt;h2 id=&quot;what-should-you-remember-about-overfitting-vs-underfitting&quot;&gt;What should you remember about overfitting vs underfitting?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Training error alone is deceptive. Always measure on data the model hasn’t seen.&lt;/li&gt;
&lt;li&gt;Both errors high means underfit. Training low and test high means overfit.&lt;/li&gt;
&lt;li&gt;More complexity never raises training error. It can raise test error.&lt;/li&gt;
&lt;li&gt;Pick your stopping point, or your model, using a validation set. Grade once on the test set.&lt;/li&gt;
&lt;li&gt;The noise floor is real. Chasing zero error means chasing noise.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Have you ever trained a model that looked perfect until it met new data, and what gave it away?&lt;/p&gt;
</content:encoded><category>python</category><category>machine-learning</category><category>model-evaluation</category><category>numpy</category></item><item><title>Claude Code Auto Mode: Lessons From the Source Leak</title><link>https://my-space.io/blog/claude-code-auto-mode/</link><guid isPermaLink="true">https://my-space.io/blog/claude-code-auto-mode/</guid><description>How Claude Code auto mode keeps an agent from doing something stupid: a three-layer funnel with a fast judge and a slow judge, and what to copy.</description><pubDate>Tue, 31 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Claude Code auto mode does not beg the model to behave. It puts a small, fast judge between the agent deciding to act and the agent acting, and it only hands control back to you after repeated denials. I found that out the day the source leaked.&lt;/p&gt;
&lt;p&gt;I woke up this morning to chaos on my timeline. Anthropic messed up. They accidentally shipped a 59 megabyte source map file to npm.&lt;/p&gt;
&lt;p&gt;Just like that, 512,000 lines of unobfuscated TypeScript for Claude Code hit the open internet. Everyone immediately rushed to find the juicy stuff. Hidden features. Unreleased code names.&lt;/p&gt;
&lt;p&gt;I did not care about that.&lt;/p&gt;
&lt;p&gt;I went looking for one specific thing:&lt;/p&gt;
&lt;p&gt;How do they stop the AI from doing something spectacularly stupid?&lt;/p&gt;
&lt;p&gt;If you build AI agents, you know the fear. You write an auto loop script. You give an LLM your API keys. You go grab a coffee. You come back, and it has either deleted your database or hallucinated a massive cloud bill.&lt;/p&gt;
&lt;p&gt;I ran into this exact nightmare while building my terminal platform, Tradion. I needed my trading agents to run autonomously, but I could not trust them with the big red button. It’s the same fear I didn’t solve well enough the first time, &lt;a href=&quot;/blog/open-source-ai-project/&quot;&gt;on the open source AI project I built and never launched&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Most developers try to fix this with begging. We write prompts like, “You are a careful assistant. Please do not wipe my hard drive.”&lt;/p&gt;
&lt;p&gt;Begging an LLM does not scale.&lt;/p&gt;
&lt;p&gt;When I cracked open the leaked source code, I found Anthropic knows this too. They do not beg. They built a three layer security funnel. And yes, they literally named part of it the “YOLO Classifier.”&lt;/p&gt;
&lt;p&gt;Here is how it actually works, and what I learned.&lt;/p&gt;
&lt;h2 id=&quot;the-problem-with-claude-code-auto-mode&quot;&gt;The Problem With Claude Code Auto Mode&lt;/h2&gt;
&lt;p&gt;We all want the dream. We want to type “build a SaaS app,” hit enter, and go to sleep.&lt;/p&gt;
&lt;p&gt;But you cannot just set a flag to &lt;code&gt;dangerouslySkipPermissions: true&lt;/code&gt; and hope for the best.&lt;/p&gt;
&lt;p&gt;Anthropic built a system inside &lt;code&gt;yoloClassifier.ts&lt;/code&gt;. It sits between the AI deciding to do something and the AI actually doing it.&lt;/p&gt;
&lt;p&gt;Here is the three step funnel they use.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Runs when&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1: Fast path&lt;/td&gt;
&lt;td&gt;Every action&lt;/td&gt;
&lt;td&gt;Lets reads, in-project edits and known-safe actions straight through.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2: Fast judge&lt;/td&gt;
&lt;td&gt;The action is risky, like a shell command&lt;/td&gt;
&lt;td&gt;Sends a tiny payload to a constrained model that answers block or allow.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3: Slow judge&lt;/td&gt;
&lt;td&gt;The fast judge flags it&lt;/td&gt;
&lt;td&gt;A slower reasoning pass makes the final call. After a block, the agent can try a safer path.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;layer-1-the-fast-path&quot;&gt;Layer 1: The Fast Path&lt;/h2&gt;
&lt;p&gt;First, the system does a cheap check.&lt;/p&gt;
&lt;p&gt;Is the action just reading data? Is it editing a normal file inside the project? Is it something already considered safe?&lt;/p&gt;
&lt;p&gt;If yes, it goes through. No drama. No extra tokens. No slowdown.&lt;/p&gt;
&lt;h2 id=&quot;layer-2-the-fast-judge&quot;&gt;Layer 2: The Fast Judge&lt;/h2&gt;
&lt;p&gt;If the action is risky, like running a shell command, it hits the YOLO classifier.&lt;/p&gt;
&lt;p&gt;Anthropic does not send the entire chat history. That would burn money for no reason. Instead, they serialize the action into tiny JSONL style strings.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&quot;user&quot;:&quot;fix the login bug&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&quot;Bash&quot;:&quot;rm -rf node_modules&quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That tiny payload gets sent to a second, heavily constrained model. This model has a strict token budget. It is not there to brainstorm. It is not there to be creative. It is there to look at the action and make a binary judgment.&lt;/p&gt;
&lt;p&gt;Block or allow.&lt;/p&gt;
&lt;p&gt;It acts like a mechanical circuit breaker.&lt;/p&gt;
&lt;h2 id=&quot;layer-3-the-slow-judge&quot;&gt;Layer 3: The Slow Judge&lt;/h2&gt;
&lt;p&gt;If the fast model flags the action, the system escalates.&lt;/p&gt;
&lt;p&gt;Now the same payload goes to a slower reasoning pass that takes a more careful look at why the command might be dangerous before making the final call.&lt;/p&gt;
&lt;p&gt;And this is the important part: if the action gets blocked, the system does not always instantly stop and hand control back to the user. It can deny the action and let the agent try a safer path first. Only after repeated denials does it escalate back to the human.&lt;/p&gt;
&lt;p&gt;That part is smart.&lt;/p&gt;
&lt;p&gt;Anthropic describes the same two-stage design in &lt;a href=&quot;https://www.anthropic.com/engineering/claude-code-auto-mode&quot;&gt;its engineering write-up on auto mode&lt;/a&gt;. A fast single-token filter runs first, and the careful reasoning runs only if that filter flags the action. The same write-up puts the escalation point at three denials in a row or 20 in total. &lt;a href=&quot;https://code.claude.com/docs/en/permission-modes&quot;&gt;The permission modes docs&lt;/a&gt; say the same about auto mode pausing and prompting you again.&lt;/p&gt;
&lt;h2 id=&quot;my-hot-take&quot;&gt;My Hot Take&lt;/h2&gt;
&lt;p&gt;You cannot rely only on prompts for safety.&lt;/p&gt;
&lt;p&gt;If you want an AI to act autonomously, you need actual walls.&lt;/p&gt;
&lt;p&gt;You need a primary agent that does the creative work. But right next to it, you need a tiny, fast, brutally simple judge.&lt;/p&gt;
&lt;p&gt;The judge does not write code. It does not talk to the user. It does not explain itself for six paragraphs.&lt;/p&gt;
&lt;p&gt;It looks at an intended action and says yes or no.&lt;/p&gt;
&lt;p&gt;That is it.&lt;/p&gt;
&lt;h2 id=&quot;what-you-should-do-next&quot;&gt;What You Should Do Next&lt;/h2&gt;
&lt;p&gt;Stop trusting your primary agent.&lt;/p&gt;
&lt;p&gt;If you are building an AI app, a stock bot, or a coding assistant, build your own version of this. Route dangerous actions through a fast, cheap model. Make it judge the action, not the vibes.&lt;/p&gt;
&lt;p&gt;Safety is not about nerfing the AI. It is about building gates, escape hatches, and escalation paths.&lt;/p&gt;
&lt;p&gt;That is the real lesson here.&lt;/p&gt;
&lt;p&gt;Running Claude Code all day also has a price tag, which I break down in &lt;a href=&quot;/blog/claude-code-cost/&quot;&gt;Claude Code cost: 4 rules that work, 4 that don’t&lt;/a&gt;. The same “don’t beg the model, build a wall” reasoning is what my &lt;a href=&quot;/blog/automated-code-review-claude-code/&quot;&gt;automated code review pipeline&lt;/a&gt; is built on, just aimed at merges instead of shell commands.&lt;/p&gt;
&lt;p&gt;How are you keeping your own agents from doing something spectacularly stupid?&lt;/p&gt;
</content:encoded><category>claude-code</category><category>ai-agents</category><category>ai-tools</category></item><item><title>TensorFlow in Python: How to Create Tensors</title><link>https://my-space.io/blog/tensorflow-create-tensors-python/</link><guid isPermaLink="true">https://my-space.io/blog/tensorflow-create-tensors-python/</guid><description>What deep learning and neural networks are, why tensors matter, and how to create tensors in TensorFlow with Python, using Google Colab.</description><pubDate>Wed, 27 Nov 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;TensorFlow is hands down one of my all-time favorite deep learning tools! I was going to call it an AI tool, but let’s be real, ’AI tools’ these days make people think of image generators, essay writers, or tools to generate one of those weird videos of Will Smith eating spaghetti. Before the ‘AI Boom’ days of 2018–2019, calling something an AI tool meant it was a coding framework to build AI applications. Funny how the meaning has shifted over the years, huh? But that is not what I am writing about today. Today we will learn how to create tensors using TensorFlow in Python.&lt;/p&gt;
&lt;p&gt;This TensorFlow in Python guide is primarily for people who are new to deep learning or machine learning frameworks, but I still expect you to have a decent knowledge of Python programming.&lt;/p&gt;
&lt;p&gt;And if you are a complete beginner to the world of Machine Learning, then here are some of the articles that you may like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/blog/knn-algorithm-scikit-learn-iris/&quot;&gt;KNN Algorithm Using Scikit-Learn – Classifying Iris Species (Tutorial)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We have a lot of material to cover today, so let’s get started.&lt;/p&gt;
&lt;h2 id=&quot;what-is-deep-learning&quot;&gt;What is Deep Learning?&lt;/h2&gt;
&lt;p&gt;Before we understand deep learning. Let’s understand what is machine learning.&lt;/p&gt;
&lt;p&gt;Machine learning is the process of writing code that allows machines or computers to learn from observational data and make predictions based on that.&lt;/p&gt;
&lt;p&gt;The best part is we don’t have to hand-feed machines every single instruction to make predictions. Past data or observational data is the main ingredient we need. The computer figures out the patterns on its own and gets smarter with experience.&lt;/p&gt;
&lt;p&gt;Now, here’s where things get interesting. Deep learning is a subset of machine learning, but it cranks things up a notch.&lt;/p&gt;
&lt;p&gt;While traditional machine learning works with structured data and requires you to select the important features manually, deep learning takes a more hands-off approach.&lt;/p&gt;
&lt;p&gt;It uses artificial neural networks, a fancy term for algorithms inspired by how our brains work to automatically find patterns and features through data. ANN or artificial neural networks are capable of learning from data that is unstructured or unlabeled.&lt;/p&gt;
&lt;h2 id=&quot;why-use-deep-learning&quot;&gt;Why use Deep Learning?&lt;/h2&gt;
&lt;p&gt;Deep learning isn’t just a buzzword. It’s the engine behind the current AI boom that we are going through. It’s driving innovations from large language models that feel eerily human to self-driving cars that actually stay in their lane.&lt;/p&gt;
&lt;p&gt;But why is deep learning stealing the show?&lt;/p&gt;
&lt;h3 id=&quot;the-traditional-approach-long-lists-of-rules&quot;&gt;The Traditional Approach: Long Lists of Rules&lt;/h3&gt;
&lt;p&gt;Once upon a time, solving problems with computers meant creating endless lists of rules. Want to identify whether an image contains a cat? You’d have to manually code rules like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;“If it has pointy ears and whiskers, it’s probably a cat.”&lt;/li&gt;
&lt;li&gt;“If it meows, chances are high.”&lt;/li&gt;
&lt;li&gt;“If it scratches your sofa, definitely a cat.”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sounds exhausting, right? And that’s just one problem. What happens when you want to expand your rules to dogs, birds, or, heaven forbid, giraffes? The traditional approach collapses under its own complexity.&lt;/p&gt;
&lt;p&gt;Deep learning eliminates this hassle. Instead of you crafting every rule, the model learns the rules by itself from data.&lt;/p&gt;
&lt;h3 id=&quot;deep-learning-adapts-to-new-challenges&quot;&gt;Deep Learning Adapts to New Challenges&lt;/h3&gt;
&lt;p&gt;The world isn’t static, and neither are the problems we need to solve. That’s where deep learning truly shines. Unlike traditional methods, which struggle with changing environments, deep learning models can be retrained or updated as new data rolls in.&lt;/p&gt;
&lt;p&gt;Let’s say you’ve trained a model to recognize fruit. Now, someone asks you to identify not just apples and oranges, but 101 different kinds of food. Creating rules manually for all that? No thanks. With deep learning, you feed the model new data, and it does the job for us.&lt;/p&gt;
&lt;h3 id=&quot;discovering-insights-in-massive-data-collections&quot;&gt;Discovering Insights in Massive Data Collections&lt;/h3&gt;
&lt;p&gt;Here’s the thing, we’re drowning in data. Text, images, videos, you name it. Hidden in those terabytes are valuable insights waiting to be uncovered. But imagine trying to manually sift through all that information to find patterns. You’d go gray before you get anywhere.&lt;/p&gt;
&lt;p&gt;Deep learning, on the other hand, thrives on this kind of challenge. Whether it’s detecting diseases in medical images or identifying customer trends, deep learning can handle the heavy lifting. It’s like having a detective who never sleeps and gets smarter with every case.&lt;/p&gt;
&lt;h3 id=&quot;real-world-applications-that-prove-the-point&quot;&gt;Real-World Applications That Prove the Point&lt;/h3&gt;
&lt;p&gt;If you’re still wondering why deep learning is worth the hype, just look around:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Voice Assistants: Siri and Alexa don’t magically understand your accent, they rely on deep learning.&lt;/li&gt;
&lt;li&gt;Self-Driving Cars: Deep learning helps them recognize stop signs, avoid pedestrians, and navigate changing traffic conditions.&lt;/li&gt;
&lt;li&gt;Personalized Recommendations: Ever wondered how Netflix knows what you want to watch next? Yep, deep learning.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These systems wouldn’t work nearly as well, or maybe at all with traditional methods.&lt;/p&gt;
&lt;p&gt;Whether you’re dealing with complex problems, mountains of data, or rapidly changing environments, deep learning offers solutions that traditional approaches simply can’t match.&lt;/p&gt;
&lt;h2 id=&quot;when-not-to-use-deep-learning&quot;&gt;When not to Use Deep learning?&lt;/h2&gt;
&lt;p&gt;Deep learning is the superstar of the AI world, but it’s not always the right choice. Believe it or not, there are times when you should put the deep learning hype aside and go for a simpler solution.&lt;/p&gt;
&lt;h3 id=&quot;when-you-need-explainability&quot;&gt;When You Need Explainability&lt;/h3&gt;
&lt;p&gt;Deep learning models learn patterns that are often uninterpretable by humans. For instance, if you’re in a field like healthcare or finance, where decisions must be transparent and explainable, deep learning can be a dealbreaker.&lt;/p&gt;
&lt;p&gt;Like seriously can you imagine telling a patient, “The AI says you need this treatment, but I can’t tell you why.”. Not something I would want to hear as a patient.&lt;/p&gt;
&lt;h3 id=&quot;when-a-traditional-approach-works-just-fine&quot;&gt;When a Traditional Approach Works Just Fine&lt;/h3&gt;
&lt;p&gt;If you are a fan of a show known as The Office, you may heard Michael Scott saying to Dwight, keep it simple stupid or aka K.I.S.S. And this mantra goes well for deep learning. You don’t have to overcomplicate things. If you can solve your problems with a basic rule-based system, why bother with deep learning?&lt;/p&gt;
&lt;p&gt;For example, if you’re sorting emails based on whether they contain the word “urgent,” a few lines of rule-based code will do the trick. Seriously, no need to unleash a neural network on such a straightforward task.&lt;/p&gt;
&lt;h3 id=&quot;when-errors-are-not-an-option&quot;&gt;When Errors Are Not an Option&lt;/h3&gt;
&lt;p&gt;Although deep learning models are powerful, they aren’t perfect.&lt;/p&gt;
&lt;p&gt;Their outputs can be unpredictable, and mistakes are bound to happen. That’s why its important to use a more predictable approach for tasks that are mission-critical.&lt;/p&gt;
&lt;h3 id=&quot;when-you-dont-have-much-data&quot;&gt;When You Don’t Have Much Data&lt;/h3&gt;
&lt;p&gt;This is probably my favorite one. No data? then no AI.&lt;/p&gt;
&lt;p&gt;AI, machine learning, or deep learning heavily depends on data. Especially deep learning. It is hungry for data, like a lot of data. So, without enough data or training examples, these models tend to underperform, &lt;a href=&quot;/blog/overfitting-vs-underfitting/&quot;&gt;overfit&lt;/a&gt;, or just simply fail.&lt;/p&gt;
&lt;p&gt;If you’re working with a small dataset, you’re better off sticking to traditional machine learning or even statistical methods. Deep learning’s real strength comes into play with large datasets where it can learn complex patterns.&lt;/p&gt;
&lt;h3 id=&quot;when-to-ask-is-deep-learning-worth-it&quot;&gt;When to Ask, “Is Deep Learning Worth It?”&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Before diving headfirst into deep learning, ask yourself these questions:&lt;/li&gt;
&lt;li&gt;Do I need explainable results? If yes, look elsewhere.&lt;/li&gt;
&lt;li&gt;Is my problem complex enough to justify it? If not, save yourself the trouble.&lt;/li&gt;
&lt;li&gt;Can I afford some errors? If no, steer clear.&lt;/li&gt;
&lt;li&gt;Do I have enough data? If not, don’t even think about it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Knowing when to use fancy tools like deep learning is great, but knowing when not to use them is even better. Sometimes, keeping things simple isn’t only just smart, but it can save you from disastrous outcomes.&lt;/p&gt;
&lt;h2 id=&quot;what-are-neural-networks&quot;&gt;What are Neural Networks?&lt;/h2&gt;
&lt;p&gt;We can’t really start coding neural networks unless we know what they are. I won’t be diving into the specifics of neural networks in this guide.&lt;/p&gt;
&lt;p&gt;But for this tutorial, let’s at least understand the basic concepts and workflow that you need to know.&lt;/p&gt;
&lt;p&gt;By definition, a neural network is a fancy term for algorithms mimicking the biological neurons of a human brain. These algorithms are the heart of any deep learning model and their purpose is to figure out complex patterns.&lt;/p&gt;
&lt;p&gt;But how exactly do they work?&lt;/p&gt;
&lt;h3 id=&quot;turning-data-into-numbers&quot;&gt;Turning Data Into Numbers&lt;/h3&gt;
&lt;p&gt;Before you can feed the data into a neural network, we need to translate the data into numerical values that the model understands.&lt;/p&gt;
&lt;p&gt;For example, converting a picture of a flower into numerical pixel values. Or transforming a sentence into embeddings or tokenized numbers. And even sound waves can become a series of numerical amplitudes.&lt;/p&gt;
&lt;p&gt;These numbers, representing your data, are fed into the neural network, which learns patterns, features, and relationships to solve the problem at hand. Whether it’s recognizing objects in images, understanding text, or converting audio to text, the process starts with data becoming numbers.&lt;/p&gt;
&lt;h3 id=&quot;how-does-neural-network-work&quot;&gt;How Does Neural Network Work?&lt;/h3&gt;
&lt;p&gt;To keep things simple, I want you to think of it as a three-step process. We can talk about the specifics of these steps later.&lt;/p&gt;
&lt;p&gt;But here is the breakdown:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Feed the Data&lt;/strong&gt;: Once the data is turned into numbers, it’s fed into the neural network.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learn Representations&lt;/strong&gt;: The neural network identifies patterns or features in these numbers, like colors, edges, or shapes in an image.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Produce Outputs&lt;/strong&gt;: It transforms the patterns into representation outputs. These might not make sense to us yet. But they’re also just numbers representing what the network has learned.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;turning-outputs-into-human-readable-information&quot;&gt;Turning Outputs Into Human-Readable Information&lt;/h3&gt;
&lt;p&gt;The neural network’s output is a numerical representation of the patterns it found. It’s up to us to convert this into something meaningful.&lt;/p&gt;
&lt;p&gt;For example, you feed in a picture of a flower. The neural network processes it, discovers patterns like petal shapes and colors, and produces a representation output (e.g., “It’s 85% likely to be a rose”). Then you can interpret that output and label it as rose.&lt;/p&gt;
&lt;h3 id=&quot;types-of-learning-in-neural-networks&quot;&gt;Types of Learning in Neural Networks&lt;/h3&gt;
&lt;p&gt;Neural networks can learn in different ways depending on how much information you give them about the data.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Supervised Learning&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;What it is: The neural network learns from labeled data or data already paired with the correct answer or label.&lt;/p&gt;
&lt;p&gt;Example: You have images of flowers labeled with their names, like “rose” or “tulip.” The network learns to predict labels based on the patterns in the images.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Semi-Supervised Learning&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;What it is: The network has a mix of labeled and unlabeled data.&lt;/p&gt;
&lt;p&gt;Example: You have 10,000 flower images, but only 1,000 are labeled. The network learns from the labeled images and uses that knowledge to make predictions on the unlabeled ones.&lt;/p&gt;
&lt;p&gt;3. &lt;strong&gt;Unsupervised Learning&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What it is: The network only has data and no labels. It finds patterns or structures on its own.&lt;/p&gt;
&lt;p&gt;Example: You give the network a dataset of flower images. It might group them into clusters based on similarities (e.g., grouping roses together) without knowing their names.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Transfer Learning&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What it is: The network takes what it learned from one task and applies it to a new, similar task.&lt;/p&gt;
&lt;p&gt;Example: Training a neural network to recognize animals in photos can be adapted to identify flowers with much less training data.&lt;/p&gt;
&lt;h3 id=&quot;why-are-neural-networks-so-powerful&quot;&gt;Why Are Neural Networks So Powerful?&lt;/h3&gt;
&lt;p&gt;Neural networks shine because they can automatically learn patterns and relationships in data. Especially data that are complex, unstructured, or huge in size. They’re adaptable to new problems, can generalize across tasks, and make life easier by handling data that would be technically impossible for humans to analyze manually.&lt;/p&gt;
&lt;p&gt;Anyway, let’s talk about Tensorflow. A powerful deep learning library that I just can’t get enough of.&lt;/p&gt;
&lt;h2 id=&quot;tensorflow-in-python-the-toolkit-for-deep-learning-greatness&quot;&gt;TensorFlow in Python: The Toolkit for Deep Learning Greatness&lt;/h2&gt;
&lt;p&gt;Now that we’ve talked about neural networks and how they work their magic, let’s dive into the real MVP of deep learning, which is TensorFlow.&lt;/p&gt;
&lt;p&gt;I have been using TensorFlow in Python since 2018 and it has been a regular tool in my arsenal for deep learning because it combines simplicity with power. This library is a game changer for building, training, and deploying deep learning models. If you are not using TensorFlow yet, you are missing out on one of the best software libraries in the AI world.&lt;/p&gt;
&lt;h3 id=&quot;what-is-tensorflow&quot;&gt;What Is TensorFlow?&lt;/h3&gt;
&lt;p&gt;TensorFlow is a complete machine learning platform that takes raw data and transforms it into a fully deployed deep learning model. It supports coding in Python and other languages while enabling you to run your models on specialized hardware like GPUs and TPUs.&lt;/p&gt;
&lt;p&gt;The library also includes TensorFlow Hub, a collection of pre-built models that you can easily integrate into your projects.&lt;/p&gt;
&lt;p&gt;Whether you are solving tasks in computer vision or natural language processing, TensorFlow provides a head start by offering models we can utilize for specific use cases.&lt;/p&gt;
&lt;h3 id=&quot;why-tensorflow&quot;&gt;Why TensorFlow?&lt;/h3&gt;
&lt;p&gt;TensorFlow comes with tons of features that make it stand out from the crowd. Here’s why it’s a favorite for both beginners and experts:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Easy Model Building&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;TensorFlow simplifies the process of creating complex neural network architectures. Its Keras API allows you to design and train models with minimal effort while maintaining flexibility for customization.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Scalable Production&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It supports deploying models across multiple platforms, including mobile devices, web applications, and even edge devices. For example, training a computer vision model to recognize objects in a smartphone app to identify items in real-time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Powerful Experimentation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Researchers use TensorFlow in Python for its flexibility and capability to test experimental architectures. Moreover, TensorFlow provides robust tools for iterating on models and fine-tuning them for specific tasks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Cost Efficiency&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;TensorFlow optimizes training by leveraging GPUs and TPUs, significantly reducing the time and computational cost required for deep learning tasks. Additionally, being open source makes TensorFlow in Python an accessible tool for anyone, regardless of budget.&lt;/p&gt;
&lt;h3 id=&quot;hardware-support-gpus-and-tpus&quot;&gt;Hardware Support: GPUs and TPUs&lt;/h3&gt;
&lt;p&gt;TensorFlow utilizes special hardware that accelerates deep learning workflows. Here is a quick overview of GPUs and TPUs:&lt;/p&gt;
&lt;p&gt;The primary use of GPUs were originally for rendering graphics, but they’re insanely good at crunching numbers. When it comes to deep learning, GPUs can perform thousands of numerical calculations simultaneously, making them perfect for training neural networks. And, TPUs are even faster than GPUs when handling TensorFlow tasks and are often used for large-scale machine learning projects. Google created TPUs specifically for TensorFlow. These are advanced chips optimized for AI workloads.&lt;/p&gt;
&lt;h2 id=&quot;tensors-in-neural-networks&quot;&gt;Tensors in Neural Networks&lt;/h2&gt;
&lt;p&gt;In the neural network section we discussed about inputs such as images or text and how we turned those inputs into numerical encodings, fed them into the neural network, and then got outputs that we could interpret.&lt;/p&gt;
&lt;p&gt;Well, here’s the thing, both those numerical encodings (inputs) and the representational outputs (outputs) are actually tensors. Sounds easy right?&lt;/p&gt;
&lt;p&gt;A Tensor is simply a numerical representation of data. It’s that simple. It is how we encode information into a form that a neural network can process. Whether that information is an image, text, audio or any other type of data. I like to think of tensors as the universal language of deep learning.&lt;/p&gt;
&lt;h3 id=&quot;how-tensors-work-in-neural-networks&quot;&gt;How Tensors Work in Neural Networks&lt;/h3&gt;
&lt;p&gt;Here’s a little break down on how the whole process works:&lt;/p&gt;
&lt;p&gt;1. &lt;strong&gt;Input as a Tensor&lt;/strong&gt;: When we encode raw input data (like an image of a flower) into numbers, we are essentially turning it into a tensor.&lt;/p&gt;
&lt;p&gt;2. &lt;strong&gt;Processing the Tenso&lt;/strong&gt;r: The tensor passes through the neural network. The network analyzes the numbers, learns patterns, and figures out what matters most in those numbers.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Output as a Tensor:&lt;/strong&gt; The network outputs another tensor, which represents the patterns it has learned from the original input tensor.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;4. &lt;strong&gt;Interpreting the Output&lt;/strong&gt;: Finally, we take the output tensor and convert it into something that we humans can understand. For instance, like a label (“this is a rose”).&lt;/p&gt;
&lt;p&gt;Tensors are how deep learning models see the world. They are the numerical form of data, allowing neural networks to analyze and learn from inputs. Whether it is finding patterns in an image or interpreting the meaning of a sentence, tensors make it possible.&lt;/p&gt;
&lt;h2 id=&quot;creating-tensors-with-tensorflow-in-python&quot;&gt;Creating Tensors with TensorFlow in Python&lt;/h2&gt;
&lt;p&gt;Now that we understand what tensors are, let’s jump into creating them using TensorFlow. For this tutorial I will be using Google Colab.&lt;/p&gt;
&lt;p&gt;So open your browser and head over to &lt;a href=&quot;https://colab.research.google.com/&quot;&gt;https://colab.research.google.com/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Colab is a free cloud-based Jupyter Notebook that lets you run Python code and store your projects on Google Drive.&lt;/p&gt;
&lt;p&gt;The best part? TensorFlow already comes with it, and you can even use free GPUs. It is perfect if you want to skip the hassle of setting up TensorFlow on your local machine.&lt;/p&gt;
&lt;p&gt;If a window pops up, go ahead and click on &lt;strong&gt;New Notebook&lt;/strong&gt; on the bottom left corner.&lt;/p&gt;
&lt;p&gt;After that name your notebook and click the &lt;strong&gt;Connect&lt;/strong&gt; button at the top right to start coding.&lt;/p&gt;
&lt;h3 id=&quot;creating-tensors-with-tfconstant&quot;&gt;Creating Tensors with tf.constant()&lt;/h3&gt;
&lt;p&gt;First, we need to import TensorFlow in Python. Run the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tensorflow &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are using the alias &lt;strong&gt;tf&lt;/strong&gt;, which is the standard convention for TensorFlow in Python, but you can choose any alias you like.&lt;/p&gt;
&lt;p&gt;Now that TensorFlow is ready to go, let’s create our first tensor.&lt;/p&gt;
&lt;h4 id=&quot;creating-a-scalar-tensor&quot;&gt;Creating a Scalar Tensor&lt;/h4&gt;
&lt;p&gt;Let’s start with the simplest tensor, scalar:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scalar &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf.constant(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scalar&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you run the code, you get:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Tensor: shape=(), dtype=int32, numpy=4&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s what the output tells us:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;tf.Tensor&lt;/strong&gt;: It’s a tensor object.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;shape=()&lt;/strong&gt;: It’s a scalar, so there’s no shape (empty).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;dtype=int32&lt;/strong&gt;: The data type is 32-bit integers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;numpy=4&lt;/strong&gt;: The actual value of the tensor.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now I want to check the number of dimensions (or &lt;strong&gt;ndim&lt;/strong&gt;) of our scaler:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scalar.ndim&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/p&gt;
&lt;p&gt;A scalar has no dimensions, which is why the &lt;strong&gt;ndim&lt;/strong&gt; is 0.&lt;/p&gt;
&lt;h4 id=&quot;creating-a-vector-tensor&quot;&gt;Creating a Vector Tensor&lt;/h4&gt;
&lt;p&gt;Now let’s move to a vector, which has one dimension.&lt;/p&gt;
&lt;p&gt;Here we will pass a Python list into &lt;strong&gt;tf.constant()&lt;/strong&gt; to create it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;vector &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf.constant([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;vector&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Tensor: shape=(2,), dtype=int32, numpy=array([8, 8], dtype=int32)&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s what’s different from the scalar we saw earlier:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;shape=(2,)&lt;/strong&gt;: The vector has two elements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;numpy=array([8, 8])&lt;/strong&gt;: A list of two numbers.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now let’ts check the dimensions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;vector.ndim&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A vector has one dimension, the shape &lt;strong&gt;(2,)&lt;/strong&gt; indicates.&lt;/p&gt;
&lt;h4 id=&quot;creating-a-matrix-tensor&quot;&gt;Creating a Matrix Tensor&lt;/h4&gt;
&lt;p&gt;A matrix has two dimensions. Let’s create one:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;matrix &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf.constant([[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;matrix&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Tensor: shape=(2, 2), dtype=int32, numpy=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;array([[4, 5],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       [5, 4]], dtype=int32)&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The shape &lt;strong&gt;(2, 2)&lt;/strong&gt; means we have two rows and two columns.&lt;/p&gt;
&lt;p&gt;To check the dimensions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;matrix.ndim&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our matrix here has two dimensions.&lt;/p&gt;
&lt;h4 id=&quot;specifying-data-types&quot;&gt;Specifying Data Types&lt;/h4&gt;
&lt;p&gt;By default, tensors created with &lt;strong&gt;tf.constant()&lt;/strong&gt; have the &lt;strong&gt;dtype=int32&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Let’s specify a different data type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newer_matrix &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf.constant([[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;., &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.], [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;., &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.], [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;., &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.]], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tf.float16)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newer_matrix&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Tensor: shape=(3, 2), dtype=float16, numpy=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;array([[4., 5.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       [5., 4.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       [6., 7.]], dtype=float16)&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;dtype=float16&lt;/strong&gt; means the tensor uses 16-bit precision, which takes up less memory than &lt;strong&gt;int32&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If you ever encounter a data type error in the future, you can adjust it using the dtype parameter.&lt;/p&gt;
&lt;h4 id=&quot;creating-a-3d-tensor&quot;&gt;Creating a 3D Tensor&lt;/h4&gt;
&lt;p&gt;Let’s level up and create a 3-dimensional tensor:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;three_dimension_arr = tf.constant([[[1, 2, 3],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    [2, 3, 4],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    [4, 5, 6]]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;three_dimension_arr&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Tensor: shape=(1, 3, 3), dtype=int32, numpy=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;array([[[1, 2, 3],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        [2, 3, 4],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        [4, 5, 6]]], dtype=int32)&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The shape &lt;strong&gt;(1, 3, 3)&lt;/strong&gt; indicates one array containing three rows and three columns.&lt;/p&gt;
&lt;p&gt;Checking the dimensions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A three-dimensional tensor has three dimensions.&lt;/p&gt;
&lt;h3 id=&quot;relationship-between-shapes-and-dimensions&quot;&gt;Relationship between Shapes and Dimensions&lt;/h3&gt;
&lt;p&gt;Let’s summarize the relationship between shapes and dimensions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scalar&lt;/strong&gt;: A single number with no dimensions (&lt;strong&gt;shape=()&lt;/strong&gt; and &lt;strong&gt;ndim=0&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vector&lt;/strong&gt;: A one-dimensional array (&lt;strong&gt;shape=(n,)&lt;/strong&gt; and &lt;strong&gt;ndim=1&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Matrix&lt;/strong&gt;: A two-dimensional array (&lt;strong&gt;shape=(n, m)&lt;/strong&gt; and &lt;strong&gt;ndim=2&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tensor&lt;/strong&gt;: An n-dimensional array, where n can be 0, 1, 2, or higher.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When I first started learning about tensors, thinking of tensors as levels also helped me. This is an approach that I followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;0D Tensor&lt;/strong&gt;: Scalar (a single number).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1D Tensor&lt;/strong&gt;: Vector (a list of numbers, like car speed and direction).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2D Tensor&lt;/strong&gt;: Matrix (rows and columns).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;3D Tensor&lt;/strong&gt;: 3D Array (layers of matrices).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now that you know how to to create tensors with &lt;strong&gt;tf.constant()&lt;/strong&gt;, we will explore another way of creating tensors, &lt;strong&gt;tf.Variable()&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;creating-tensors-with-tfvariable&quot;&gt;Creating Tensors with tf.Variable()&lt;/h3&gt;
&lt;p&gt;So far we’ve covered &lt;strong&gt;tf.constant()&lt;/strong&gt;. These are unchangeable tensors, let’s look at its sibling, &lt;strong&gt;tf.Variable()&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This one’s a bit different because it allows you to create changeable tensors. These types of tensors are perfect for when you need flexibility in your deep learning models.&lt;/p&gt;
&lt;h4 id=&quot;what-exactly-is-tfvariable&quot;&gt;What Exactly Is tf.Variable()?&lt;/h4&gt;
&lt;p&gt;A &lt;strong&gt;tf.Variable()&lt;/strong&gt; tensor is mutable, meaning you can change its values after it’s creation. This sets it apart from &lt;strong&gt;tf.constant()&lt;/strong&gt;, which creates unchangeable tensors.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tensor1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf.Variable([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tensor2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tf.constant([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Tensor 1: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, tensor1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Tensor 2: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, tensor2)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Tensor 1:  &amp;lt;tf.Variable &apos;Variable:0&apos; shape=(2,) dtype=int32, numpy=array([4, 5], dtype=int32)&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Tensor 2:  tf.Tensor([4 5], shape=(2,), dtype=int32)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At first glance, both look pretty similar. But notice how &lt;strong&gt;Tensor 1&lt;/strong&gt; uses &lt;strong&gt;tf.Variable&lt;/strong&gt;, while &lt;strong&gt;Tensor 2&lt;/strong&gt; uses &lt;strong&gt;tf.constant&lt;/strong&gt;. The difference becomes clear when we try to modify their values.&lt;/p&gt;
&lt;h4 id=&quot;modifying-a-tfvariable-tensor&quot;&gt;Modifying a tf.Variable() Tensor&lt;/h4&gt;
&lt;p&gt;Let’s start by indexing &lt;strong&gt;Tensor 1&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tensor1[0]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Tensor: shape=(), dtype=int32, numpy=4&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives us the value &lt;strong&gt;4&lt;/strong&gt; at index position &lt;strong&gt;0&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now, let’s try to change the value at index &lt;strong&gt;0&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tensor1[0] = 8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tensor1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;---------------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;TypeError: &apos;ResourceVariable&apos; object does not support item assignment&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Oops! That didn’t work. Directly assigning a value doesn’t work for &lt;strong&gt;tf.Variable&lt;/strong&gt; objects. But don’t worry, there’s another way.&lt;/p&gt;
&lt;h4 id=&quot;using-assign-to-modify-tensors&quot;&gt;Using .assign() to Modify Tensors&lt;/h4&gt;
&lt;p&gt;To change the value of a &lt;strong&gt;tf.Variable&lt;/strong&gt; tensor, you need to use the &lt;strong&gt;.assign()&lt;/strong&gt; method. Here’s how:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tensor1[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].assign(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tensor1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;tf.Variable &apos;Variable:0&apos; shape=(2,) dtype=int32, numpy=array([8, 5], dtype=int32)&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Success! We replaced the value &lt;strong&gt;4&lt;/strong&gt; with &lt;strong&gt;8&lt;/strong&gt; using &lt;strong&gt;.assign()&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&quot;using-assign-on-tfconstant&quot;&gt;Using .assign() on tf.constant()&lt;/h4&gt;
&lt;p&gt;Let’s try modifying &lt;strong&gt;Tensor&lt;/strong&gt; &lt;strong&gt;2&lt;/strong&gt;, created with &lt;strong&gt;tf.constant()&lt;/strong&gt;, using &lt;strong&gt;.assign()&lt;/strong&gt; to see what happens:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tensor2[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].assign(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tensor2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;---------------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;AttributeError: &apos;tensorflow.python.framework.ops.EagerTensor&apos; object has no attribute &apos;assign&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thus, we get an attribute error. That’s because &lt;strong&gt;tf.constant()&lt;/strong&gt; creates immutable tensors, so their values can’t be changed after creation.&lt;/p&gt;
&lt;h3 id=&quot;when-to-use-tfvariable-vs-tfconstant&quot;&gt;When to Use tf.Variable() vs tf.constant()&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;tf.Variable()&lt;/strong&gt; when your tensor needs to change during computation, such as in training models where weights and biases are updated.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;tf.constant()&lt;/strong&gt; for fixed data, like configuration values or inputs that don’t need to change.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;If you are interested in working with deep learning, then TensorFlow in Python isn’t just another library you’ll occasionally use. It is your trusted resource for building, experimenting, and deploying AI models. And the best part is, Google made it absolutely free and open source for us to use.&lt;/p&gt;
&lt;p&gt;In short, tensors are the backbone of deep learning, and understanding them is the first step to unlocking TensorFlow’s full potential. So go ahead, try out the examples, play with the dimensions, and experiment with what you’ve learned. The more you practice, the closer you’ll get to TensorFlow wizardry (okay, maybe not wizardry, but close enough).&lt;/p&gt;
&lt;p&gt;TensorFlow is everchanging and evergrowing. Despite using it for the last 6 years, I am constantly learning new things about TensorFlow in Python. What we just learned here is probably the tip of an iceberg. There is so much to learn about TensorFlow and I plan to write about them on my blog as much as i can. So stay tuned and make sure to check out my other guides.&lt;/p&gt;
</content:encoded><category>python</category><category>deep-learning</category><category>tensorflow</category><category>numpy</category></item><item><title>150 Python Interview Questions You Should Know</title><link>https://my-space.io/blog/python-interview-questions/</link><guid isPermaLink="true">https://my-space.io/blog/python-interview-questions/</guid><description>A study guide of 150 Python interview questions with plain-English answers and code, from syntax basics to functions and core language features.</description><pubDate>Sun, 24 Nov 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;So you have a Python interview coming up or you are interested in positions that require you to be a hotshot at Python? Maybe you are just a curious bird like me and looking for python interview questions to sharpen your coding skills. Whatever it is, you are at the right place. Because in this post I will list not just 10, 20 or 30 but 150 Python interview questions and their answers that are packed to help you ace your next Python interview.&lt;/p&gt;
&lt;h2 id=&quot;python-interview-questions-and-answers&quot;&gt;Python Interview Questions and Answers&lt;/h2&gt;
&lt;h3 id=&quot;1-what-is-python&quot;&gt;1. What is Python?&lt;/h3&gt;
&lt;p&gt;Python is a high-level, interpreted, general-purpose programming language known for its simplicity and readability.&lt;/p&gt;
&lt;h3 id=&quot;2-who-created-python-and-what-language-did-it-succeed&quot;&gt;2. Who created Python, and what language did it succeed?&lt;/h3&gt;
&lt;p&gt;Python was created by Guido van Rossum in 1991. It is the successor of the ABC programming language, which inspired its design.&lt;/p&gt;
&lt;h3 id=&quot;3-is-python-compiled-or-interpreted&quot;&gt;3. Is Python compiled or interpreted?&lt;/h3&gt;
&lt;p&gt;Python is an &lt;strong&gt;interpreted language&lt;/strong&gt;, meaning it executes code line by line, making it easy to debug.&lt;/p&gt;
&lt;h3 id=&quot;4-what-are-some-key-features-of-python&quot;&gt;&lt;strong&gt;4. What are some key features of Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;User-friendly syntax.&lt;/li&gt;
&lt;li&gt;Object-oriented.&lt;/li&gt;
&lt;li&gt;Dynamically typed.&lt;/li&gt;
&lt;li&gt;Portable and platform independent.&lt;/li&gt;
&lt;li&gt;Extensive standard library.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-how-is-pythons-syntax-different-from-javas&quot;&gt;&lt;strong&gt;5. How is Python’s syntax different from Java’s?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Python uses &lt;strong&gt;indentation&lt;/strong&gt; to define code blocks, whereas Java relies on curly braces &lt;strong&gt;{}&lt;/strong&gt;. This makes Python more readable and concise.&lt;/p&gt;
&lt;h3 id=&quot;6-whats-the-difference-between-a-list-and-a-tuple-in-python&quot;&gt;&lt;strong&gt;6. What’s the difference between a list and a tuple in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Lists&lt;/strong&gt; are mutable (modifiable), while &lt;strong&gt;tuples&lt;/strong&gt; are immutable (non-modifiable). Lists use square brackets &lt;strong&gt;[]&lt;/strong&gt; and tuples use parentheses &lt;strong&gt;()&lt;/strong&gt;. Tuples are generally faster and more memory-efficient than lists.&lt;/p&gt;
&lt;h3 id=&quot;7-explain-local-and-global-variables-in-python&quot;&gt;&lt;strong&gt;7. Explain local and global variables in Python.&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Local variables&lt;/strong&gt;: Declared inside a function and accessible only within that function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Global variables&lt;/strong&gt;: Declared outside any function and accessible throughout the program.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;8-what-is-type-conversion-name-some-methods-used-in-python&quot;&gt;8. What is type conversion? Name some methods used in Python.&lt;/h3&gt;
&lt;p&gt;Type conversion refers to converting one data type to another. Common methods include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;int()&lt;/li&gt;
&lt;li&gt;float()&lt;/li&gt;
&lt;li&gt;str()&lt;/li&gt;
&lt;li&gt;tuple()&lt;/li&gt;
&lt;li&gt;list()&lt;/li&gt;
&lt;li&gt;set()&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;9-how-do-you-create-a-function-in-python&quot;&gt;&lt;strong&gt;9. How do you create a function in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Functions in Python are created using the &lt;strong&gt;def&lt;/strong&gt; keyword:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; greet&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;10-what-is-a-default-argument&quot;&gt;10. What is a default argument?&lt;/h3&gt;
&lt;p&gt;A default argument is a predefined value assigned to a function parameter. If no value is passed during the function call, the default value is used.&lt;/p&gt;
&lt;h3 id=&quot;11-what-are-anonymous-functions-in-python&quot;&gt;&lt;strong&gt;11. What are anonymous functions in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Anonymous functions, also known as &lt;strong&gt;lambda functions&lt;/strong&gt;, are single-expression functions defined using the lambda keyword.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; lambda&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x, y: x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(add(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; 8&lt;/p&gt;
&lt;h3 id=&quot;12-name-two-other-scopes-in-python-apart-from-local-and-global&quot;&gt;&lt;strong&gt;12. Name two other scopes in Python apart from local and global.&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Module-level scope&lt;/strong&gt;: Variables accessible throughout the module.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built-in scope&lt;/strong&gt;: Contains Python’s built-in functions like &lt;strong&gt;print()&lt;/strong&gt; and &lt;strong&gt;len()&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;13-what-are-pythons-built-in-collections&quot;&gt;13. What are Python’s built-in collections?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tuple&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Set&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;14-how-do-you-comment-on-a-single-line-in-python&quot;&gt;&lt;strong&gt;14. How do you comment on a single line in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;#&lt;/strong&gt; symbol:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# This is a single-line comment&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;15-how-do-you-comment-on-multiple-lines-in-python&quot;&gt;&lt;strong&gt;15. How do you comment on multiple lines in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use triple quotes &lt;strong&gt;‘’’ ‘’’:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&apos;&apos;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a multi-line comment.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;It spans multiple lines.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&apos;&apos;&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;16. Write a loop to iterate over a list in Python.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;my_list &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; my_list:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(item)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;17. What’s the difference between &lt;strong&gt;break&lt;/strong&gt; and &lt;strong&gt;continue&lt;/strong&gt;?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;break&lt;/strong&gt;: Terminates the loop entirely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;continue&lt;/strong&gt;: Skips the rest of the current iteration and moves to the next.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;18. What is the &lt;strong&gt;pass&lt;/strong&gt; statement in Python?&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;pass&lt;/strong&gt; statement acts as a placeholder and does nothing when executed. Unlike comments, it’s required for code structure in some places.&lt;/p&gt;
&lt;p&gt;19. What is a dictionary in Python?&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;my_dict &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;20-which-python-collection-does-not-allow-duplicate-values&quot;&gt;&lt;strong&gt;20. Which Python collection does not allow duplicate values?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Sets&lt;/strong&gt; do not allow duplicate values.&lt;/p&gt;
&lt;h3 id=&quot;21-how-do-you-check-if-a-string-starts-with-a-capital-letter-in-python&quot;&gt;21. How do you check if a string starts with a capital letter in Python?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;istitle()&lt;/strong&gt; method:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.istitle())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; True&lt;/p&gt;
&lt;h3 id=&quot;22-how-do-you-check-if-a-substring-exists-in-a-string&quot;&gt;22. How do you check if a substring exists in a string?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;in&lt;/strong&gt; operator:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Welcome to Python programming.&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Python&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; text)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; True&lt;/p&gt;
&lt;h3 id=&quot;23-what-built-in-function-is-used-to-check-the-length-of-a-string&quot;&gt;23. What built-in function is used to check the length of a string?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;len()&lt;/strong&gt; function is used to determine the length of a string.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Python&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(text))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 6&lt;/p&gt;
&lt;h3 id=&quot;24-how-do-you-convert-a-string-into-a-list&quot;&gt;&lt;strong&gt;24. How do you convert a string into a list?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;split()&lt;/strong&gt; method to break a string into a list of substrings. For more on working with strings, see &lt;a href=&quot;/blog/python-text-editing-processing/&quot;&gt;Python text editing and processing&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Python is amazing&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;words &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; text.split()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(words)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: [‘Python’, ‘is’, ‘amazing’]&lt;/p&gt;
&lt;h3 id=&quot;25-write-a-python-code-snippet-to-reverse-a-string&quot;&gt;25. Write a Python code snippet to reverse a string.&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Python&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;reversed_text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; text[::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(reversed_text)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: nohtyP&lt;/p&gt;
&lt;h3 id=&quot;26-what-happens-when-you-add-a-string-and-an-integer-in-python&quot;&gt;26. What happens when you add a string and an integer in Python?&lt;/h3&gt;
&lt;p&gt;It raises a &lt;strong&gt;TypeError&lt;/strong&gt; because Python does not allow implicit type conversion between strings and integers.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;result = &quot;Python&quot; + 123&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: TypeError&lt;/p&gt;
&lt;h3 id=&quot;27-how-do-you-check-if-all-characters-in-a-string-are-uppercase&quot;&gt;27. How do you check if all characters in a string are uppercase?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;isupper()&lt;/strong&gt; method.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PYTHON&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(text.isupper())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;28-what-is-the-difference-between-range-and-xrange-in-a-string-are-uppercase&quot;&gt;28. What is the difference between range() and xrange()? in a string are uppercase?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;range():&lt;/strong&gt; Returns a list in Python 2 and a range object in Python 3.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;xrange():&lt;/strong&gt; Only available in Python 2, it returns an xrange object that generates numbers on the fly (memory-efficient).&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;29-name-some-immutable-types-in-python&quot;&gt;29. Name some immutable types in Python.&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Tuple&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Number (int, float)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;30-name-some-mutable-types-in-python&quot;&gt;&lt;strong&gt;30. Name some mutable types in Python.&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Set&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;31-what-is-a-lambda-function-in-python&quot;&gt;31. What is a lambda function in Python?&lt;/h3&gt;
&lt;p&gt;A lambda function is a small, anonymous function that can take multiple arguments but only contains a single expression.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;square &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; lambda&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x: x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(square(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; 16&lt;/p&gt;
&lt;h3 id=&quot;32-how-do-you-write-a-lambda-function-in-python&quot;&gt;32. How do you write a lambda function in Python?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; lambda&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x, y: x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(add(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/p&gt;
&lt;h3 id=&quot;33-how-many-types-of-loops-are-available-in-python&quot;&gt;33. How many types of loops are available in Python?&lt;/h3&gt;
&lt;p&gt;Python supports two types of loops:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;for&lt;/strong&gt; loop&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;while&lt;/strong&gt; loop&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;34-what-will-the-following-code-output&quot;&gt;34. What will the following code output?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;numbers &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(numbers[:&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; [1, 2]&lt;/p&gt;
&lt;h3 id=&quot;35-what-will-the-following-code-output&quot;&gt;35. What will the following code output?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;numbers &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(numbers[:])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: [1, 2, 3, 4, 5]&lt;/p&gt;
&lt;h3 id=&quot;36-what-are-python-modules&quot;&gt;36. What are Python modules?&lt;/h3&gt;
&lt;p&gt;Python modules are files containing Python code (functions, classes, or variables) that can be reused in other Python programs.&lt;/p&gt;
&lt;p&gt;37 Name some common built-in Python modules.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;os&lt;/li&gt;
&lt;li&gt;sys&lt;/li&gt;
&lt;li&gt;math&lt;/li&gt;
&lt;li&gt;random&lt;/li&gt;
&lt;li&gt;json&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;38-how-do-you-import-a-specific-attribute-from-a-module&quot;&gt;&lt;strong&gt;38. How do you import a specific attribute from a module?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;from module import attribute&lt;/strong&gt; syntax.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; math &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sqrt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sqrt(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 4.0&lt;/p&gt;
&lt;h3 id=&quot;39-can-you-write-python-code-without-indentation&quot;&gt;&lt;strong&gt;39. Can you write Python code without indentation?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;No, Python strictly enforces indentation to define code blocks. Without it, the code will throw an &lt;strong&gt;IndentationError&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;40-whats-the-difference-between-python-arrays-and-lists&quot;&gt;40. What’s the difference between Python arrays and lists?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Arrays&lt;/strong&gt;: Can only store elements of the same data type.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lists&lt;/strong&gt;: Can store elements of different data types.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; array &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; arr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;numbers &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; arr.array(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;i&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;41-how-do-you-create-an-array-in-python&quot;&gt;&lt;strong&gt;41. How do you create an array in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;array&lt;/strong&gt; module to create an array.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; array &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; arr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;my_array &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; arr.array(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;i&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(my_array)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; array(‘i’, [1, 2, 3])&lt;/p&gt;
&lt;h3 id=&quot;42-what-will-be-the-output-of-the-following-code&quot;&gt;&lt;strong&gt;42. What will be the output of the following code?&lt;/strong&gt;&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;s &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(s)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: {1}&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: &lt;strong&gt;Sets&lt;/strong&gt; in Python do not allow duplicate values.&lt;/p&gt;
&lt;h3 id=&quot;43-can-you-create-an-empty-set-using-&quot;&gt;&lt;strong&gt;43. Can you create an empty set using {}?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;No, using &lt;strong&gt;{}&lt;/strong&gt; creates an empty dictionary. To create an empty set, use the &lt;strong&gt;set()&lt;/strong&gt; function:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;empty_set &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(empty_set))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: &lt;code&gt;&amp;lt;class &apos;set&apos;&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;44-what-is-the-purpose-of-the-set-function&quot;&gt;44. What is the purpose of the set() function?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;set()&lt;/strong&gt; function creates a set, which is an unordered collection of unique elements.&lt;/p&gt;
&lt;h3 id=&quot;45-which-python-collection-is-used-for-operations-like-union-intersection-and-difference&quot;&gt;&lt;strong&gt;45. Which Python collection is used for operations like union, intersection, and difference?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Sets are used for these operations.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;b &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(a.union(b))  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Output: {1, 2, 3, 4, 5}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: {1, 2, 3, 4, 5}&lt;/p&gt;
&lt;h3 id=&quot;46-does-python-support-object-oriented-programming&quot;&gt;46. Does Python support object-oriented programming?&lt;/h3&gt;
&lt;p&gt;Yes, Python supports object-oriented programming with features like classes, inheritance, and polymorphism.&lt;/p&gt;
&lt;h3 id=&quot;47-what-is-__init__-in-python&quot;&gt;47. What is __&lt;strong&gt;init__&lt;/strong&gt; in Python?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Person&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, name):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; name&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;48-what-is-the-purpose-of-the-self-keyword-in-python&quot;&gt;48. What is the purpose of the self keyword in Python?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;self&lt;/strong&gt; keyword represents the instance of a class. It is used to access attributes and methods within the class.&lt;/p&gt;
&lt;h3 id=&quot;49-is-it-necessary-to-use-self-in-a-class&quot;&gt;49. Is it necessary to use self in a class?&lt;/h3&gt;
&lt;p&gt;Yes, it is necessary to use &lt;strong&gt;self&lt;/strong&gt; in instance methods to refer to the current object.&lt;/p&gt;
&lt;h3 id=&quot;50-what-is-a-class-attribute&quot;&gt;50. What is a class attribute?&lt;/h3&gt;
&lt;p&gt;A class attribute is shared across all instances of a class. It is defined outside the methods and belongs to the class rather than an instance.&lt;/p&gt;
&lt;h3 id=&quot;51-does-python-support-inheritance&quot;&gt;&lt;strong&gt;51. Does Python support inheritance?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Yes, Python supports inheritance, including single, multiple, and multilevel inheritance.&lt;/p&gt;
&lt;h3 id=&quot;52-what-is-the-purpose-of-the-super-method&quot;&gt;52. What is the purpose of the &lt;strong&gt;super()&lt;/strong&gt; method?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;super()&lt;/strong&gt; method allows access to methods in a parent class.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Parent&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; greet&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello from Parent&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Child&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Parent&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; greet&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        super&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().greet()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello from Child&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;53-what-does-the-is-operator-do-in-python&quot;&gt;53. What does the &lt;strong&gt;is&lt;/strong&gt; operator do in Python?&lt;/h3&gt;
&lt;p&gt;The is operator checks if two variables point to the same object in memory.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;b &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; b)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: False&lt;/p&gt;
&lt;h3 id=&quot;54-how-do-you-work-with-dates-and-times-in-python&quot;&gt;54 How do you work with dates and times in Python?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;datetime&lt;/strong&gt; module.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;now &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime.datetime.now()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(now)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: It should print the current date and time.&lt;/p&gt;
&lt;h3 id=&quot;55-what-does-it-mean-that-python-is-dynamically-typed&quot;&gt;55. What does it mean that Python is dynamically typed?&lt;/h3&gt;
&lt;p&gt;It means variables in Python do not need explicit declaration of their type. The type is determined during runtime.&lt;/p&gt;
&lt;h3 id=&quot;56-are-python-functions-first-class-objects&quot;&gt;56. Are Python functions first-class objects?&lt;/h3&gt;
&lt;p&gt;Yes, Python functions are first-class objects, meaning they can be assigned to variables, passed as arguments, and returned from other functions.&lt;/p&gt;
&lt;h3 id=&quot;57-how-do-you-reverse-an-array-in-python&quot;&gt;57. How do you reverse an array in Python?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; array &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; arr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;my_array &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; arr.array(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;i&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(my_array[::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: array(‘i’, [4, 3, 2, 1])&lt;/p&gt;
&lt;h3 id=&quot;58-what-are-args-used-for-in-python&quot;&gt;58. What are &lt;strong&gt;*args&lt;/strong&gt; used for in Python?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;*args&lt;/strong&gt; syntax allows a function to accept a variable number of positional arguments.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; greet&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;names):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; names:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;greet(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;59-how-do-you-split-a-string-into-a-list-in-python&quot;&gt;59. How do you split a string into a list in Python?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;split()&lt;/strong&gt; method.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Python is great&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;words &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; text.split()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(words)  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Output: [&apos;Python&apos;, &apos;is&apos;, &apos;great&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: [‘Python’, ‘is’, ‘great’]&lt;/p&gt;
&lt;h3 id=&quot;60-how-do-you-check-if-a-given-string-is-a-palindrome&quot;&gt;60. How do you check if a given string is a palindrome?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; is_palindrome&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(s):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s[::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(is_palindrome(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;madam&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(is_palindrome(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: It should print &lt;strong&gt;True&lt;/strong&gt; for the first &lt;strong&gt;print()&lt;/strong&gt; statement and &lt;strong&gt;False&lt;/strong&gt; for the second.&lt;/p&gt;
&lt;h3 id=&quot;61-how-do-you-remove-vowels-from-a-string-in-python&quot;&gt;61. How do you remove vowels from a string in Python?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; remove_vowels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(s):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    vowels &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;aeiouAEIOU&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.join([char &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; char &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;not&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; vowels])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(remove_vowels(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Python&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: Pythn&lt;/p&gt;
&lt;h3 id=&quot;62-can-you-pass-one-function-as-an-argument-to-another-function-in-python&quot;&gt;62. Can you pass one function as an argument to another function in Python?&lt;/h3&gt;
&lt;p&gt;Yes, since Python functions are first-class objects, you can pass one function as an argument to another.&lt;/p&gt;
&lt;h3 id=&quot;63-what-are-docstrings-in-python&quot;&gt;63. What are &lt;strong&gt;docstrings&lt;/strong&gt; in Python?&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Docstrings&lt;/strong&gt; are string literals used to document a function, class, or module. They are written using triple quotes.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; greet&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;This function greets the user.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;64-how-can-you-access-a-functions-docstring&quot;&gt;64. How can you access a function’s &lt;strong&gt;docstring&lt;/strong&gt;?&lt;/h3&gt;
&lt;p&gt;Use the __&lt;strong&gt;doc__&lt;/strong&gt; attribute.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; greet&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;This function greets the user.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(greet.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;__doc__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: This function greets the user.&lt;/p&gt;
&lt;h3 id=&quot;65-whats-the-difference-between-a-comment-and-a-docstring&quot;&gt;65. What’s the difference between a comment and a &lt;strong&gt;docstring&lt;/strong&gt;?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Comments are ignored by Python and cannot be accessed during runtime.&lt;/li&gt;
&lt;li&gt;Docstrings are used to document code and can be accessed using the __&lt;strong&gt;doc&lt;/strong&gt;__ attribute.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;66-does-python-support-multiple-inheritance&quot;&gt;66. Does Python support multiple inheritance?&lt;/h3&gt;
&lt;p&gt;Yes, Python supports multiple inheritance.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; A&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; B&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; C&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;67-how-do-you-create-an-empty-class-in-python&quot;&gt;67. How do you create an empty class in Python?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;pass&lt;/strong&gt; statement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; EmptyClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;68-what-are-the-different-types-of-functions-in-python&quot;&gt;68. What are the different types of functions in Python?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Built-in functions (e.g., &lt;strong&gt;len()&lt;/strong&gt;, &lt;strong&gt;print()&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;User-defined functions&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;69-what-types-of-operators-are-available-in-python&quot;&gt;&lt;strong&gt;69. What types of operators are available in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Arithmetic operators&lt;/li&gt;
&lt;li&gt;Logical operators&lt;/li&gt;
&lt;li&gt;Comparison operators&lt;/li&gt;
&lt;li&gt;Bitwise operators&lt;/li&gt;
&lt;li&gt;Assignment operators&lt;/li&gt;
&lt;li&gt;Identity operators&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;70-where-can-python-be-used&quot;&gt;&lt;strong&gt;70. Where can Python be used?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Python is used in various domains, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Web development&lt;/li&gt;
&lt;li&gt;Data science&lt;/li&gt;
&lt;li&gt;Machine learning&lt;/li&gt;
&lt;li&gt;Artificial intelligence&lt;/li&gt;
&lt;li&gt;Game development&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;71-what-is-django&quot;&gt;&lt;strong&gt;71. What is Django?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Not sure whether Django or Flask fits your project? Read &lt;a href=&quot;/blog/django-vs-flask/&quot;&gt;Django vs. Flask&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;72-what-are-some-key-features-of-django&quot;&gt;72. What are some key features of Django?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Built-in authentication system&lt;/li&gt;
&lt;li&gt;URL routing&lt;/li&gt;
&lt;li&gt;ORM (Object-Relational Mapper)&lt;/li&gt;
&lt;li&gt;Template engine&lt;/li&gt;
&lt;li&gt;Built-in admin interface&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;73-can-django-be-used-for-backend-development&quot;&gt;&lt;strong&gt;73. Can Django be used for backend development?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Yes, Django is primarily used for backend development, offering powerful tools for handling server-side logic and database operations.&lt;/p&gt;
&lt;h3 id=&quot;74-name-some-databases-supported-by-django&quot;&gt;74. Name some databases supported by Django.&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;SQLite&lt;/li&gt;
&lt;li&gt;MySQL&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;Oracle&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;75-what-architectural-pattern-does-django-follow&quot;&gt;75. What architectural pattern does Django follow?&lt;/h3&gt;
&lt;p&gt;Django follows the Model-View-Template (MVT) architectural pattern.&lt;/p&gt;
&lt;h3 id=&quot;76-what-are-the-advantages-of-using-django&quot;&gt;76. What are the advantages of using Django?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Easy to learn and use.&lt;/li&gt;
&lt;li&gt;Secure by default.&lt;/li&gt;
&lt;li&gt;Scalable for large applications.&lt;/li&gt;
&lt;li&gt;Extensive community support.&lt;/li&gt;
&lt;li&gt;Reduces development time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;77-what-are-the-disadvantages-of-using-django&quot;&gt;&lt;strong&gt;77. What are the disadvantages of using Django?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Overhead for small applications.&lt;/li&gt;
&lt;li&gt;Limited flexibility in ORM.&lt;/li&gt;
&lt;li&gt;Can be bulky for simple projects.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;78-what-are-django-signals&quot;&gt;78. What are Django signals?&lt;/h3&gt;
&lt;p&gt;Django signals allow certain senders to notify listeners when specific events occur.&lt;/p&gt;
&lt;h3 id=&quot;79-name-two-key-parameters-used-in-django-signals&quot;&gt;&lt;strong&gt;79. Name two key parameters used in Django signals.&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sender&lt;/strong&gt;: The model or function that sends the signal.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Receiver&lt;/strong&gt;: The function or method that listens to the signal.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;80-what-is-a-django-session&quot;&gt;80. What is a Django session?&lt;/h3&gt;
&lt;p&gt;A Django session allows storing user-specific data, such as login information, on the server side.&lt;/p&gt;
&lt;h3 id=&quot;81-what-is-the-role-of-django-views&quot;&gt;81. What is the role of Django views?&lt;/h3&gt;
&lt;p&gt;Django views handle user requests, process data from models, and return responses to templates for rendering.&lt;/p&gt;
&lt;h3 id=&quot;82-how-do-django-views-work-with-models-and-templates&quot;&gt;&lt;strong&gt;82. How do Django views work with models and templates?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Models&lt;/strong&gt;: Fetch data from the database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Views&lt;/strong&gt;: Process the data and pass it to templates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Templates&lt;/strong&gt;: Display the processed data in a user-friendly format.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;83-what-is-flask&quot;&gt;&lt;strong&gt;83. What is Flask?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Flask is a &lt;strong&gt;microframework written in Python&lt;/strong&gt; that is lightweight and ideal for small applications or prototyping.&lt;/p&gt;
&lt;h3 id=&quot;84-how-can-flask-send-emails&quot;&gt;&lt;strong&gt;84. How can Flask send emails?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Flask can send emails using the &lt;strong&gt;flask-mail&lt;/strong&gt; extension, which integrates email-sending functionality.&lt;/p&gt;
&lt;h3 id=&quot;85-why-is-flask-called-a-microframework&quot;&gt;85. Why is Flask called a microframework?&lt;/h3&gt;
&lt;p&gt;Flask is termed a &lt;strong&gt;microframework&lt;/strong&gt; because it provides only core functionalities, such as routing and request handling, while additional features are implemented via extensions&lt;/p&gt;
&lt;h3 id=&quot;86-how-do-you-add-orm-functionality-to-flask&quot;&gt;&lt;strong&gt;86. How do you add ORM functionality to Flask?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;Flask-SQLAlchemy&lt;/strong&gt; extension to integrate an ORM with Flask applications.&lt;/p&gt;
&lt;h3 id=&quot;87-what-are-the-advantages-of-using-flask&quot;&gt;&lt;strong&gt;87. What are the advantages of using Flask?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Lightweight and flexible&lt;/li&gt;
&lt;li&gt;Easy to learn for beginners&lt;/li&gt;
&lt;li&gt;Supports extensions for added functionality&lt;/li&gt;
&lt;li&gt;Comes with a built-in development server&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;88-how-do-you-differentiate-between-django-and-flask&quot;&gt;&lt;strong&gt;88. How do you differentiate between Django and Flask?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Django: A full-stack framework suited for large applications.&lt;/li&gt;
&lt;li&gt;Flask: A microframework ideal for smaller applications and prototyping.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;89-what-are-iterators-in-python&quot;&gt;&lt;strong&gt;89. What are iterators in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Iterators are objects in Python that allow traversing through elements, one at a time, using the __&lt;strong&gt;next__&lt;/strong&gt;() method.&lt;/p&gt;
&lt;h3 id=&quot;90-which-method-is-used-to-create-custom-iterators-in-python&quot;&gt;90. Which method is used to create custom iterators in Python?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;__iter__()&lt;/strong&gt; method is used to initialize the iterator, and the &lt;strong&gt;__next__()&lt;/strong&gt; method is used to iterate through elements.&lt;/p&gt;
&lt;h3 id=&quot;91-what-happens-at-the-end-of-an-iterator&quot;&gt;&lt;strong&gt;91. What happens at the end of an iterator?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;StopIteration&lt;/strong&gt; exception is raised when there are no more elements to traverse.&lt;/p&gt;
&lt;h3 id=&quot;92-what-are-python-generators&quot;&gt;92. What are Python generators?&lt;/h3&gt;
&lt;p&gt;Generators are a special type of iterator that allows generating items lazily using the &lt;strong&gt;yield&lt;/strong&gt; keyword instead of storing them in memory.&lt;/p&gt;
&lt;p&gt;93. Write a simple Python generator example.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; generate_numbers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        yield&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; number &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; generate_numbers():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(number)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;94-what-is-the-difference-between-yield-and-return&quot;&gt;94. What is the difference between &lt;strong&gt;yield&lt;/strong&gt; and &lt;strong&gt;return&lt;/strong&gt;?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;return&lt;/strong&gt;: Terminates the function and returns a value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;yield&lt;/strong&gt;: Pauses the function, saving its state, and resumes on the next call.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;95-do-generators-automatically-implement-__iter__-and-__next__&quot;&gt;95. Do generators automatically implement __&lt;strong&gt;iter__&lt;/strong&gt;() and __&lt;strong&gt;next__&lt;/strong&gt;()?&lt;/h3&gt;
&lt;p&gt;Yes, Python generators automatically implement these methods, making them easier to use than custom iterators.&lt;/p&gt;
&lt;h3 id=&quot;96-can-you-create-a-generator-without-using-yield&quot;&gt;96. Can you create a generator without using &lt;strong&gt;yield&lt;/strong&gt;?&lt;/h3&gt;
&lt;p&gt;No, the &lt;strong&gt;yield&lt;/strong&gt; keyword is mandatory for defining a generator.&lt;/p&gt;
&lt;h3 id=&quot;97-what-are-python-decorators&quot;&gt;&lt;strong&gt;97. What are Python decorators?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Decorators are functions in Python that modify the behavior of another function or method. They are often used for logging, validation, or access control.&lt;/p&gt;
&lt;h3 id=&quot;98-write-an-example-of-a-python-decorator&quot;&gt;&lt;strong&gt;98. Write an example of a Python decorator.&lt;/strong&gt;&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; decorator&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(func):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; wrapper&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Before function call&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        func()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;After function call&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wrapper&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@decorator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; say_hello&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;say_hello()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;99-how-do-you-use-regular-expressions-in-python&quot;&gt;&lt;strong&gt;99. How do you use regular expressions in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Import the &lt;strong&gt;re&lt;/strong&gt; module to work with regular expressions.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; re&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pattern &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;match &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; re.search(pattern, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;There are 123 apples&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(match.group())  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Output: 123&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 123&lt;/p&gt;
&lt;h3 id=&quot;100-what-does-the--signify-in-regular-expressions&quot;&gt;100. What does the &lt;strong&gt;*&lt;/strong&gt; signify in regular expressions?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;*&lt;/strong&gt; matches zero or more occurrences of the preceding character or group.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Congrats if you made it this far in the guide! We are almost there. ;)&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;101-is-python-2-still-supported&quot;&gt;&lt;strong&gt;101. Is Python 2 still supported?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;No, Python 2 reached its end of life on January 1, 2020, and is no longer officially supported.&lt;/p&gt;
&lt;h3 id=&quot;102-what-is-pep-8&quot;&gt;&lt;strong&gt;102. What is PEP 8?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;PEP 8 is the official Python style guide that provides conventions for writing clean, readable code.&lt;/p&gt;
&lt;h3 id=&quot;103-how-does-python-manage-memory&quot;&gt;&lt;strong&gt;103. How does Python manage memory?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Python manages memory using:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A private heap space for all objects and data structures.&lt;/li&gt;
&lt;li&gt;An in-built garbage collector to reclaim unused memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;104-what-is-slicing-in-python&quot;&gt;&lt;strong&gt;104. What is slicing in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Slicing allows you to extract a portion of a sequence (e.g., list, string) using a specified range of indices.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;my_list &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(my_list[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Output: [1, 2, 3]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; [1, 2, 3]&lt;/p&gt;
&lt;h3 id=&quot;105-how-do-you-copy-an-object-in-python&quot;&gt;105. How do you copy an object in Python?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;copy()&lt;/strong&gt; or &lt;strong&gt;deepcopy()&lt;/strong&gt; method from the copy module.&lt;/p&gt;
&lt;h3 id=&quot;106-what-is-the-pyramid-framework-used-for&quot;&gt;&lt;strong&gt;106. What is the Pyramid framework used for?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Pyramid is a Python web framework designed for building large, complex web applications.&lt;/p&gt;
&lt;h3 id=&quot;107-what-are-the-limitations-of-python&quot;&gt;107. What are the limitations of Python?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Slower compared to compiled languages like C++.&lt;/li&gt;
&lt;li&gt;Limited for mobile development.&lt;/li&gt;
&lt;li&gt;GIL (Global Interpreter Lock) can be a bottleneck for multithreaded applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;108-how-do-you-remove-duplicate-values-from-a-list-in-python&quot;&gt;&lt;strong&gt;108. How do you remove duplicate values from a list in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;my_list &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;unique_list &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(my_list))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(unique_list)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: [1, 2, 3, 4]&lt;/p&gt;
&lt;h3 id=&quot;110-what-is-the-purpose-of-the--operator&quot;&gt;110. What is the purpose of the ** operator?&lt;/h3&gt;
&lt;p&gt;The ** operator is used for exponentiation.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 8&lt;/p&gt;
&lt;h3 id=&quot;110-what-are-membership-operators-in-python&quot;&gt;110. What are membership operators in Python?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;in&lt;/strong&gt;: Checks if a value exists in a sequence. -&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;not in:&lt;/strong&gt; Checks if a value does not exist in a sequence.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;111-what-is-raw_input-in-python&quot;&gt;111. What is &lt;strong&gt;raw_input()&lt;/strong&gt; in Python?&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;raw_input()&lt;/strong&gt; is used in Python 2 to take user input as a string. In Python 3, it has been replaced with &lt;strong&gt;input()&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;112-which-python-collection-is-most-similar-to-javascript-objects&quot;&gt;112. Which Python collection is most similar to JavaScript objects?&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;dictionary&lt;/strong&gt; in Python is similar to JavaScript objects, as both store key-value pairs.&lt;/p&gt;
&lt;h3 id=&quot;113-why-is-python-often-recommended-for-beginners&quot;&gt;113. Why is Python often recommended for beginners?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Simple and readable syntax.&lt;/li&gt;
&lt;li&gt;Extensive standard library.&lt;/li&gt;
&lt;li&gt;Versatile for various use cases.&lt;/li&gt;
&lt;li&gt;Dynamically typed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;114-whats-the-difference-between-python-and-java&quot;&gt;&lt;strong&gt;114. What’s the difference between Python and Java?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Typing&lt;/strong&gt;: Python is dynamically typed, while Java is statically typed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: Python uses indentation for blocks, while Java uses curly braces &lt;strong&gt;{}&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Speed&lt;/strong&gt;: Java is faster due to its compiled nature, whereas Python is interpreted.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;115-explain-the-following-code-snippet&quot;&gt;115. Explain the following code snippet.&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;a, b, c = 1, 2, 3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;: The code initializes three variables &lt;strong&gt;a&lt;/strong&gt;, &lt;strong&gt;b&lt;/strong&gt;, and &lt;strong&gt;c&lt;/strong&gt; with the values 1, 2, and 3, respectively.&lt;/p&gt;
&lt;h3 id=&quot;116-what-does-the-following-code-do&quot;&gt;116. What does the following code do?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;a = b = c = 1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Answer: All three variables &lt;strong&gt;a&lt;/strong&gt;, &lt;strong&gt;b&lt;/strong&gt;, and &lt;strong&gt;c&lt;/strong&gt; are assigned the same value of 1.&lt;/p&gt;
&lt;h3 id=&quot;117-what-is-the-with-statement-in-python-used-for&quot;&gt;117. What is the with statement in Python used for?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;with&lt;/strong&gt; statement is used for resource management, such as opening files. It ensures that resources are properly released, even if an error occurs.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;file.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    content &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.read()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;118-what-does-the-global-keyword-do&quot;&gt;118. What does the &lt;strong&gt;global&lt;/strong&gt; keyword do?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;global&lt;/strong&gt; keyword allows modification of a variable declared in the global scope from within a function.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; modify_global&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    global&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;modify_global()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 10&lt;/p&gt;
&lt;h3 id=&quot;119-what-will-the-following-code-output&quot;&gt;119. What will the following code output?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Python&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;: [‘Python’, ‘Python’]&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: The multiplication operator repeats the elements of the list.&lt;/p&gt;
&lt;h3 id=&quot;120-what-is-pickling-in-python&quot;&gt;120. What is pickling in Python?&lt;/h3&gt;
&lt;p&gt;Pickling is the process of serializing a Python object into a byte stream using the &lt;strong&gt;pickle&lt;/strong&gt; module.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pickle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;data.pkl&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pickle.dump(data, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;121-what-is-unpickling&quot;&gt;121. What is unpickling?&lt;/h3&gt;
&lt;p&gt;Unpickling is the reverse process of converting a byte stream back into a Python object using the &lt;strong&gt;pickle&lt;/strong&gt; module.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;data.pkl&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;rb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pickle.load(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(data)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: {‘key’: ‘value’}&lt;/p&gt;
&lt;h3 id=&quot;122-does-python-pass-arguments-by-value-or-reference&quot;&gt;122. Does Python pass arguments by value or reference?&lt;/h3&gt;
&lt;p&gt;Python uses &lt;strong&gt;pass-by-object-reference&lt;/strong&gt;, meaning that mutable objects can be modified inside a function, while immutable objects cannot.&lt;/p&gt;
&lt;h3 id=&quot;123-is-python-case-sensitive&quot;&gt;&lt;strong&gt;123. Is Python case-sensitive?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Yes, Python is case-sensitive, meaning variable names like &lt;strong&gt;Var&lt;/strong&gt; and &lt;strong&gt;var&lt;/strong&gt; are treated as distinct.&lt;/p&gt;
&lt;h3 id=&quot;125-what-will-be-the-output-of-this-code&quot;&gt;125. What will be the output of this code?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;l &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(l[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;: An empty list &lt;strong&gt;[]&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: Slicing a list beyond its range does not raise an error but returns an empty list.&lt;/p&gt;
&lt;h3 id=&quot;126-what-types-of-databases-are-supported-by-python&quot;&gt;&lt;strong&gt;126. What types of databases are supported by Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Python supports both SQL and NoSQL databases, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SQL: MySQL, PostgreSQL, SQLite&lt;/li&gt;
&lt;li&gt;NoSQL: MongoDB, Cassandra&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;127-can-python-code-written-on-windows-run-on-linux&quot;&gt;&lt;strong&gt;127. Can Python code written on Windows run on Linux?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Yes, Python code is cross-platform and can run on different operating systems without modification, provided the required dependencies are available.&lt;/p&gt;
&lt;h3 id=&quot;128-what-happens-when-you-execute-the-following-code&quot;&gt;128. What happens when you execute the following code?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;f &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;new.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;: It opens the file &lt;strong&gt;new.txt&lt;/strong&gt; for writing. If the file does not exist, it creates it. If it exists, its content is overwritten. To learn more about file modes, see &lt;a href=&quot;/blog/reading-writing-files-python/&quot;&gt;reading and writing files in Python&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;129-when-is-the-zerodivisionerror-exception-raised&quot;&gt;129. When is the &lt;strong&gt;ZeroDivisionError&lt;/strong&gt; exception raised?&lt;/h3&gt;
&lt;p&gt;This exception is raised when a division or modulo operation has a denominator of zero.&lt;/p&gt;
&lt;h3 id=&quot;130-whats-the-difference-between-abs-and-mathfabs&quot;&gt;130. What’s the difference between abs() and math.fabs()?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;abs():&lt;/strong&gt; A built-in function that works with integers, floats, and complex numbers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;math.fabs():&lt;/strong&gt; Part of the math module and works only with floats.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;131-what-does-the--1-index-signify-in-a-sequence&quot;&gt;131. What does the &lt;strong&gt;-1&lt;/strong&gt; index signify in a sequence?&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;l &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(l[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 30&lt;/p&gt;
&lt;h3 id=&quot;132-what-is-a-python-module&quot;&gt;132. What is a Python module?&lt;/h3&gt;
&lt;p&gt;A Python module is a file containing Python code (functions, classes, or variables) that can be reused in other programs.&lt;/p&gt;
&lt;h3 id=&quot;133-how-do-you-import-a-module-in-python&quot;&gt;&lt;strong&gt;133. How do you import a module in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;import&lt;/strong&gt; statement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(math.sqrt(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 4.0&lt;/p&gt;
&lt;h3 id=&quot;134-can-you-create-custom-modules-in-python&quot;&gt;134. Can you create custom modules in Python?&lt;/h3&gt;
&lt;p&gt;Yes, you can create a custom module by saving Python code in a &lt;strong&gt;.py&lt;/strong&gt; file and importing it into other scripts.&lt;/p&gt;
&lt;h3 id=&quot;135-what-is-the-dir-function-used-for&quot;&gt;135. What is the &lt;strong&gt;dir()&lt;/strong&gt; function used for?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;dir()&lt;/strong&gt; function is used to display the attributes and methods of an object.&lt;/p&gt;
&lt;h3 id=&quot;136-how-do-you-check-the-type-of-a-variable-in-python&quot;&gt;&lt;strong&gt;136. How do you check the type of a variable in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;type()&lt;/strong&gt; function:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: &lt;code&gt;&amp;lt;class &apos;int&apos;&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;137-how-do-you-handle-exceptions-in-python&quot;&gt;&lt;strong&gt;137. How do you handle exceptions in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use &lt;strong&gt;try-except&lt;/strong&gt; blocks:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    result &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;except&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ZeroDivisionError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Cannot divide by zero&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;138-what-is-the-difference-between-is-and--in-python&quot;&gt;138. What is the difference between &lt;strong&gt;is&lt;/strong&gt; and &lt;strong&gt;==&lt;/strong&gt; in Python?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;is&lt;/strong&gt;: Checks if two variables point to the same object in memory. -&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;==&lt;/strong&gt;: Checks if two variables have the same value.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;139-what-is-pythons-gil&quot;&gt;139. What is Python’s GIL?&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;Global Interpreter Lock (GIL)&lt;/strong&gt; is a mutex in CPython that allows only one thread to execute Python bytecode at a time, limiting multi-threading capabilities.&lt;/p&gt;
&lt;h3 id=&quot;140-how-do-you-create-a-virtual-environment-in-python&quot;&gt;140. How do you create a virtual environment in Python?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;venv&lt;/strong&gt; module:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; venv&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; myenv&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;activate-it-using&quot;&gt;Activate it using:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Windows: &lt;strong&gt;myenv\Scripts\activate&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Linux/Mac: &lt;strong&gt;source myenv/bin/activate&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;141-how-do-you-list-all-installed-python-packages&quot;&gt;141. How do you list all installed Python packages?&lt;/h3&gt;
&lt;p&gt;Use the &lt;strong&gt;pip list&lt;/strong&gt; command in the terminal to display all installed Python packages and their versions.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;pip list&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;142-how-do-you-install-a-specific-version-of-a-python-package&quot;&gt;&lt;strong&gt;142. How do you install a specific version of a Python package?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the pip install command with the package name and version:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; package_name==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1.2.3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;143-what-is-a-namespace-in-python&quot;&gt;&lt;strong&gt;143. What is a namespace in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;A namespace is a container that holds identifiers (variable names, function names, etc.) and maps them to their corresponding objects.&lt;/p&gt;
&lt;h3 id=&quot;144-what-are-pythons-built-in-data-types&quot;&gt;&lt;strong&gt;144. What are Python’s built-in data types?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Numeric types: &lt;strong&gt;int&lt;/strong&gt;, &lt;strong&gt;float&lt;/strong&gt;, &lt;strong&gt;complex&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Sequence types: &lt;strong&gt;list&lt;/strong&gt;, &lt;strong&gt;tuple&lt;/strong&gt;, &lt;strong&gt;range&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Text type: &lt;strong&gt;str&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Mapping type: &lt;strong&gt;dict&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Set types: &lt;strong&gt;set&lt;/strong&gt;, &lt;strong&gt;frozenset&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Boolean type: &lt;strong&gt;bool&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;145-what-is-the-purpose-of-the-enumerate-function-in-python&quot;&gt;&lt;strong&gt;145. What is the purpose of the enumerate() function in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;enumerate()&lt;/strong&gt; function adds a counter to an iterable and returns it as an enumerate object.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;colors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;red&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;blue&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;green&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index, color &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; enumerate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(colors):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(index, color)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: 0 red 1 blue 2 green&lt;/p&gt;
&lt;h3 id=&quot;146-what-is-the-difference-between-deepcopy-and-copy&quot;&gt;146. What is the difference between &lt;strong&gt;deepcopy()&lt;/strong&gt; and &lt;strong&gt;copy()&lt;/strong&gt;?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;copy():&lt;/strong&gt; Creates a shallow copy of an object, which copies the object but not nested objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;deepcopy():&lt;/strong&gt; Creates a deep copy of an object, including all nested objects.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;147-what-are-pythons-file-handling-modes&quot;&gt;147. What are Python’s file handling modes?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;“r”: Read mode&lt;/li&gt;
&lt;li&gt;“w”: Write mode (overwrites file)&lt;/li&gt;
&lt;li&gt;“x”: Create mode (fails if the file exists)&lt;/li&gt;
&lt;li&gt;“a”: Append mode&lt;/li&gt;
&lt;li&gt;“b”: Binary mode&lt;/li&gt;
&lt;li&gt;“t”: Text mode&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;148-what-is-a-python-metaclass&quot;&gt;&lt;strong&gt;148. What is a Python metaclass?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;A metaclass is a class of a class. It defines how classes behave and can be used to control class creation.&lt;/p&gt;
&lt;h3 id=&quot;149-how-do-you-create-a-thread-in-python&quot;&gt;&lt;strong&gt;149. How do you create a thread in Python?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use the threading module:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; threading&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; task&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Thread is running&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; threading.Thread(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;t.start()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;150-what-are-f-strings-in-python&quot;&gt;150. What are f-strings in Python?&lt;/h3&gt;
&lt;p&gt;F-strings, introduced in Python 3.6, provide a concise and efficient way to format strings.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Alice&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;age &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 25&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;My name is &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; and I am &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; years old.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Phew! That was a lot to digest I assume. But you made it to the end.&lt;/p&gt;
&lt;p&gt;Preparing for an interview can feel overwhelming. And with these 150 Python interview questions. you have a solid resource to help you with the most commonly asked Python interview questions.&lt;/p&gt;
&lt;p&gt;Remember, to practice each question thoroughly. I also have other guides and tutorials on my website, so feel free to go over them to help you better understand some of the concepts.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;What other Python interview questions can you think of?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>interview</category></item><item><title>Django vs. Flask: Choosing the Right Web Framework</title><link>https://my-space.io/blog/django-vs-flask/</link><guid isPermaLink="true">https://my-space.io/blog/django-vs-flask/</guid><description>Django or Flask? Compare the two Python web frameworks on databases, ease of learning, scalability, security, and community, and when to use each.</description><pubDate>Thu, 31 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Python programming language has gained immense popularity in the last two decades.&lt;/p&gt;
&lt;p&gt;Today, Python is not only one of the most widely used programming languages but also one of the most loved programming languages.&lt;/p&gt;
&lt;p&gt;There are many reasons why Python wins over other programming languages. And one of the reasons is that it is beginner-friendly.&lt;/p&gt;
&lt;p&gt;That’s why its use is increasing in web development, machine learning, game development, and data science.&lt;/p&gt;
&lt;p&gt;Web development is one of the essential domains where you can use Python.&lt;/p&gt;
&lt;p&gt;Python provides several web frameworks that are very useful in developing web applications. For example, some popular Python web frameworks are Django, Pyramid, Flask, Grok, TurboGears, and CherryPy.&lt;/p&gt;
&lt;p&gt;However, we will not discuss any framework other than Django vs. Flask.&lt;/p&gt;
&lt;p&gt;Django and Flask are the two most popular Python web frameworks.&lt;/p&gt;
&lt;p&gt;Both frameworks are helpful to web developers worldwide in developing modern web applications.&lt;/p&gt;
&lt;p&gt;But people are often confused about choosing between these two frameworks. Also not know how they differ. There are several differences between them, and one should choose carefully.&lt;/p&gt;
&lt;p&gt;This article will compare these two frameworks and discuss their pros and cons. Ultimately, we will see which of them is better and when to choose Django or Flask.&lt;/p&gt;
&lt;h2 id=&quot;origin&quot;&gt;Origin&lt;/h2&gt;
&lt;p&gt;Django is the most famous name in the Python web development community. It is an open-source, free Python web framework based on MTV architectural patterns.&lt;/p&gt;
&lt;p&gt;Returning to its origin, Django was created in 2003 by Adrian Holovaty and Simon Willison. And then it officially came out in 2005.&lt;/p&gt;
&lt;p&gt;The main aim of creating Django was to simplify web development because, at that time, JavaScript had its limitations.&lt;/p&gt;
&lt;p&gt;There was still time for popular JavaScript frameworks such as Angular and React to emerge. So Python, being a popular language, was used to create Django. Its purpose was to simplify the whole web application development process. So the creators decided to focus more on reusing components, rapid growth, and less code.&lt;/p&gt;
&lt;p&gt;Earlier frameworks such as Plone and Zope influenced Django. And Django’s success inspired other popular Python frameworks such as CherryPy, Pyramid, and Bottle.&lt;/p&gt;
&lt;p&gt;Unlike Django, Flask did not have a long-term purpose. Instead, it started as an April fool joke and had a surprise beginning. Armin Ronacher created two different solutions.&lt;/p&gt;
&lt;p&gt;One was Werkzeug, a server framework, and Jinja2, a template library. He tried an experiment just for fun.&lt;/p&gt;
&lt;p&gt;He added these two solutions in a zip file and made it part of a framework he named denied framework. Armin worked on this framework when his earlier answers became part of it. When another developer installed the restricted framework, the solutions unzipped automatically.&lt;/p&gt;
&lt;p&gt;When these two solutions ran together with the denied framework, to surprise, it received a positive framework. Thus, Armin created this new framework which is now known as Flask.&lt;/p&gt;
&lt;h2 id=&quot;django-vs-flask&quot;&gt;Django vs. Flask&lt;/h2&gt;
&lt;p&gt;Both Django and Flask are Python frameworks. But Django is a full-fledged framework while Flask is a microframework. Simply put, Flask provides the minimum features required to run the server and does not have additional features.&lt;/p&gt;
&lt;p&gt;On the other hand, Django is a high-level full-stack framework that provides several other features, including database layers. In simple words, with Django, almost everything present that is required. On the other hand, you can integrate additional features in Flask.&lt;/p&gt;
&lt;p&gt;Django follows the Model-View-Template architectural pattern.&lt;/p&gt;
&lt;p&gt;As a result, the design pattern helps build web applications more efficiently in less time and with less code. But Flask only comes with essential tools that help build basic web applications, which is one of the main differences between Django and Flask.&lt;/p&gt;
&lt;h2 id=&quot;working-with-databases&quot;&gt;Working with databases&lt;/h2&gt;
&lt;p&gt;Perhaps, one of the essential differences between Django and Flask is the way both of them work with databases. A database is an integral part of a web application.&lt;/p&gt;
&lt;p&gt;Databases are one of the main focuses of backend development. Thus, a framework needs to be efficient.&lt;/p&gt;
&lt;p&gt;Django has a Relational Object Mapping known as the Django ORM.&lt;/p&gt;
&lt;p&gt;Some of its features allow the Django ORM to work with databases such as MySQL, SQLite, and Prophet.&lt;/p&gt;
&lt;p&gt;It does not require long, confusing SQL queries. Moreover, its ORM uses data models that are useful in linking databases with classes.&lt;/p&gt;
&lt;p&gt;Flask neither has ORM nor data models.&lt;/p&gt;
&lt;p&gt;On the hand, developers need third-party toolkits to perform everyday database operations with Flask.&lt;/p&gt;
&lt;h2 id=&quot;ease-of-learning&quot;&gt;Ease of learning&lt;/h2&gt;
&lt;p&gt;Both Django and Flask are Python web frameworks. Hence, it is critical to understand the Python programming language well.&lt;/p&gt;
&lt;p&gt;Both can be integral to web development, so the concepts are more or less similar. But, as mentioned earlier, Django is a full-stack framework, and it offers many ideas, and it takes time to grab them.&lt;/p&gt;
&lt;p&gt;Flask, on the other hand, is a microframework that has fewer concepts as compared to Django. If you have proper Python and basic HTML knowledge, learning Flask will not take much time.&lt;/p&gt;
&lt;p&gt;But Flask requires additional extensions. So to work correctly with it, you need to understand them.&lt;/p&gt;
&lt;h2 id=&quot;packages&quot;&gt;Packages&lt;/h2&gt;
&lt;p&gt;Django comes with a wide range of built-in packages. There are over four thousand packages in Django, and thus it makes it easier to implement complex functionalities in an application.&lt;/p&gt;
&lt;p&gt;In comparison to Django, Flask does not offer such packages. Instead, it is a minimalistic framework that allows us to choose third-party packages. However, Flask is more flexible and extensible than Django.&lt;/p&gt;
&lt;h2 id=&quot;scalability&quot;&gt;Scalability&lt;/h2&gt;
&lt;p&gt;Django MVT architectural pattern makes it very powerful and scalable. As mentioned earlier, Django supports a variety of databases because of its powerful ORM. Moreover, its data models make it more efficient to work with the backend. Web applications built using Django are much faster and more scalable than Flask.&lt;/p&gt;
&lt;p&gt;However, Flask does not have Django-like architecture and ORM.&lt;/p&gt;
&lt;h2 id=&quot;security&quot;&gt;Security&lt;/h2&gt;
&lt;p&gt;Being secure is an essential feature of a web framework. Today, security is the top priority of every web application.&lt;/p&gt;
&lt;p&gt;Django has different measures for different attacks. For example, templates in Django are capable of protecting from XSS attacks. In addition, it has built-in protection from CSRF attacks and measures for SQL injections.&lt;/p&gt;
&lt;p&gt;Flask also has a security library called Flask-security library that provides security benefits over such attacks. But it is not as efficient as Django.&lt;/p&gt;
&lt;h2 id=&quot;popularity&quot;&gt;Popularity&lt;/h2&gt;
&lt;p&gt;Both of these frameworks are most popular in the Python web development community.&lt;/p&gt;
&lt;p&gt;According to StackOverflow’s developer survey 2020, both Django and Flask had received the same stats (i.e., 14.2%) in the list of most popular web frameworks by all respondents.&lt;/p&gt;
&lt;p&gt;But in the same list of professional developers, Flask received 13.6% while Django received 13.2%.&lt;/p&gt;
&lt;p&gt;And again, in the same survey, Django got 4th position in the list of “most wanted web frameworks,” while Flask got 7th.&lt;/p&gt;
&lt;p&gt;So no doubt, both of them are popular. But Django is more in demand than Flask.&lt;/p&gt;
&lt;h2 id=&quot;community-support&quot;&gt;Community support&lt;/h2&gt;
&lt;p&gt;Django is older than Flask. As it is an open-source product, its community support has increased tremendously. As a result, it has enormous community support, while Flask’s community is not significant.&lt;/p&gt;
&lt;h2 id=&quot;when-to-use-django-and-flask&quot;&gt;When to use Django and Flask?&lt;/h2&gt;
&lt;p&gt;So, where should we use Django and Flask? Both are Python frameworks used to develop web applications and share some common concepts, but they are pretty different in some aspects.&lt;/p&gt;
&lt;p&gt;Django is a full-fledged high-level full-stack web framework.&lt;/p&gt;
&lt;p&gt;As discussed, it provides almost everything required to build web applications. It comes with an ORM and data models that are very helpful with backend development.&lt;/p&gt;
&lt;p&gt;Moreover, it has thousands of built-in packages, and because of these packages, there is no need to look elsewhere.&lt;/p&gt;
&lt;p&gt;Because of such features and its MVT architectural pattern, Django is powerful enough to develop large-scale web applications.&lt;/p&gt;
&lt;p&gt;Some of the most popular applications that use Django are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Instagram&lt;/li&gt;
&lt;li&gt;Spotify&lt;/li&gt;
&lt;li&gt;DropBox&lt;/li&gt;
&lt;li&gt;Mozilla&lt;/li&gt;
&lt;li&gt;Washington Post&lt;/li&gt;
&lt;li&gt;National Geographic&lt;/li&gt;
&lt;li&gt;Pinterest&lt;/li&gt;
&lt;li&gt;NASA&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, these applications are complex. And their everyday traffic is enormous. Not only that but also these applications has to integrate advanced security features. And as Django is robust, secure, high-performing, and scalable, it meets many complex requirements.&lt;/p&gt;
&lt;p&gt;On the other hand, Flask does not have some heavy-duty features such as Django. But that does not mean the giants do not use Flask.&lt;/p&gt;
&lt;p&gt;Because of its simplicity, extensibility, and, most importantly, flexibility, Flask is helpful to developers who want more room for development.&lt;/p&gt;
&lt;p&gt;Some of the popular applications that use Flask are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Netflix&lt;/li&gt;
&lt;li&gt;Airbnb&lt;/li&gt;
&lt;li&gt;Red Hat&lt;/li&gt;
&lt;li&gt;Reddit&lt;/li&gt;
&lt;li&gt;Lyft&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, if you want to create a medium to large-sized application and you want everything in a single place, then Django is a good choice. I&lt;/p&gt;
&lt;p&gt;Whereas, if you want to make a simple, small-sized application with flexibility, room for developers, and custom components, then Flask is a good choice.&lt;/p&gt;
&lt;h2 id=&quot;django-or-flask&quot;&gt;Django or Flask?&lt;/h2&gt;
&lt;p&gt;So, both frameworks help build web applications. That’s why choosing one of them for your project depends on your requirements.&lt;/p&gt;
&lt;p&gt;If the project is large and requires powerful features, then Django is a perfect option.&lt;/p&gt;
&lt;p&gt;But if the project is small and needs only core features, Flask is a good option.&lt;/p&gt;
&lt;p&gt;Also, a developer’s perspective is essential. If you want flexibility, then Flask is better than Django. Moreover, choose Django instead of Flask if you want everything in one place.&lt;/p&gt;
&lt;p&gt;But the size of the project also comes into play. There is a good chance Flask can get the job done for smaller projects. So, no need to choose a bulky framework like Django.&lt;/p&gt;
&lt;p&gt;However, if the project is medium-sized to large, go for Django.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This article focuses on the two most popular Python web frameworks - Django vs. Flask.&lt;/p&gt;
&lt;p&gt;We discussed how the processes behind their development and their basis.&lt;/p&gt;
&lt;p&gt;In addition, we discussed their main differences and features. We also discussed where Django and Flask are useful and how to make decisions reflecting on the purpose of your projects.&lt;/p&gt;
&lt;p&gt;Let’s discuss their differences in brief:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Django came out in 2005, while Flask came out in 2010.&lt;/li&gt;
&lt;li&gt;Django is a full-fledged framework with several features, such as ORM, data models, and packages. Flask is a lightweight micro-framework.&lt;/li&gt;
&lt;li&gt;Flask is simple and beginner-friendly. In comparison, Django is huge and requires experience.&lt;/li&gt;
&lt;li&gt;Django’s community is vast. And Flask’s community is still growing.&lt;/li&gt;
&lt;li&gt;Flask is a minimalistic framework that only provides core features. At the same time, Django has numerous amount of packages available to use.&lt;/li&gt;
&lt;li&gt;In short, Django is a good option for medium and large-sized projects. And in comparison, Flask is efficient for smaller-sized projects.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both Django and Flask are very popular.&lt;/p&gt;
&lt;p&gt;Because of that, they are well known among developers for building web applications. Furthermore, their popularity is increasing exponentially. Not only that, but they also have a perfect place in the job market.&lt;/p&gt;
&lt;p&gt;Want to test what you know about Django? The &lt;a href=&quot;/blog/python-interview-questions/&quot;&gt;Python interview questions&lt;/a&gt; guide includes a few Django basics.&lt;/p&gt;
</content:encoded><category>python</category><category>web-development</category><category>django</category><category>flask</category></item><item><title>An Essential Guide to Python Text Editing &amp; Processing</title><link>https://my-space.io/blog/python-text-editing-processing/</link><guid isPermaLink="true">https://my-space.io/blog/python-text-editing-processing/</guid><description>A practical guide to Python text editing and processing: searching, manipulating, formatting, replacing, and removing characters in strings.</description><pubDate>Mon, 17 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As a programmer, a large part of my work may involve working with texts &amp;amp; Strings. That’s why it is crucial to know some of the fun aspects of Python text editing &amp;amp; processing using the standard library.&lt;/p&gt;
&lt;p&gt;As a result, I thought about writing this tutorial for those who want to get their hands dirty with Python text editing &amp;amp; processing.&lt;/p&gt;
&lt;p&gt;Here’s what we will cover:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Work on three basic String operations through the standard library.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learn how to search Strings for specific information.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Understand how to parse and manipulate strings.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learn how to format Strings.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before I move further, you already need to be familiar with Python. Moreover, I highly recommend that you have the basics down first before jumping into Python text editing &amp;amp; processing.&lt;/p&gt;
&lt;p&gt;So without a further due, let’s get started.&lt;/p&gt;
&lt;h2 id=&quot;basic-string-operations&quot;&gt;Basic String Operations&lt;/h2&gt;
&lt;p&gt;In the first part of the Python text editing &amp;amp; processing article, we will learn how to perform basic String operations.&lt;/p&gt;
&lt;p&gt;One of the powerful features of the Python standard library is that it provides essential tools for testing and filtering String content.&lt;/p&gt;
&lt;p&gt;Go ahead and open your IDE to import the &lt;code&gt;string&lt;/code&gt; module.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; string&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s see some of the pre-defined String constants the &lt;code&gt;string&lt;/code&gt; module provides.  And these are:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(string.ascii_letters)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(string.digits)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(string.hexdigits)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(string.punctuation)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0123456789&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0123456789abcdefABCDEF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;!&quot;#$%&amp;amp;&apos;()*+,-./:;&amp;lt;=&amp;gt;?@[\]^_`{|}~&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we used the string module to print out some of the constants that the standard library provides. And as a result, you can see we have some values such as letters, digits, and even hex values.&lt;/p&gt;
&lt;p&gt;Moving on, let’s create three variables and assign some values or Strings to them.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;stringOne = &apos;It is raining cats and dogs.&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;stringTwo = &apos;Labratory&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;stringThree = &apos;01234567&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Moreover, I also want to check if a String is alphanumeric or not. For that, we can use the &lt;code&gt;alnum()&lt;/code&gt; function. Here’s how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stringOne.isalnum())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stringTwo.isalnum())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stringThree.isalnum())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The outputs are all boolean values. The first result shows that our variable &lt;code&gt;stringOne&lt;/code&gt; is not alphanumeric since there are spaces within the Strings. On the other hand, &lt;code&gt;stringTwo&lt;/code&gt; &amp;amp; &lt;code&gt;stringThree&lt;/code&gt; came out to be alphanumeric since they meet the condition of having alphabets or numbers.&lt;/p&gt;
&lt;p&gt;Similarly, we can also check if a String only contains alphabets. To do that, we can use the &lt;code&gt;isalpha()&lt;/code&gt; function. Let’s check it on the &lt;code&gt;stringOne.&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stringOne.isalpha())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output is &lt;code&gt;False&lt;/code&gt; because &lt;code&gt;stringOne&lt;/code&gt; has spaces between the words.&lt;/p&gt;
&lt;p&gt;Spaces are not considered alphabets by Python. As a result, when you run the function &lt;code&gt;isalpha()&lt;/code&gt; on &lt;code&gt;stringOne&lt;/code&gt;, we get &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, we can also verify if a String is numeric or not using the &lt;code&gt;isnumeric()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;And this is how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stringThree.isnumeric())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s run this function on &lt;code&gt;stringTwo&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stringTwo.isnumeric())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, that the outputs are &lt;code&gt;True&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;True&lt;/code&gt; because &lt;code&gt;stringThree&lt;/code&gt; has numeric characters, while the other one is &lt;code&gt;False&lt;/code&gt; since that has no numeric values.&lt;/p&gt;
&lt;h2 id=&quot;searching-strings&quot;&gt;Searching Strings&lt;/h2&gt;
&lt;p&gt;We can also search Strings for specific data. Fortunately, the Python String object comes with built-in methods to do this.&lt;/p&gt;
&lt;p&gt;First, let’s see how we can use the &lt;code&gt;startswith()&lt;/code&gt; &amp;amp; &lt;code&gt;endswith()&lt;/code&gt; functions to check if the String starts or ends with a specific sequence of characters.&lt;/p&gt;
&lt;p&gt;Go ahead and type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;sampleString = &apos;The sky is blue&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I want to check whether our &lt;code&gt;sampleString&lt;/code&gt; has ‘the’ in it. Thus, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sampleString.startswith(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;The&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We get &lt;code&gt;True&lt;/code&gt; since the word is present.&lt;/p&gt;
&lt;p&gt;Okay, now what if we passed the lowercase version of that word.&lt;/p&gt;
&lt;p&gt;Type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sampleString.startswith(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;the&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, these functions are case-sensitive. Since lowercase ‘the’ is not present, we are getting &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Lastly, let’s try the &lt;code&gt;endswith()&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sampleString.endswith(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;blue&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We get &lt;code&gt;True&lt;/code&gt; because we can see that the word ‘blue’ is at the end of our phrase.&lt;/p&gt;
&lt;p&gt;Apart from the &lt;code&gt;startswith()&lt;/code&gt; and &lt;code&gt;endswith()&lt;/code&gt; functions, there are also other general functions such as &lt;code&gt;find()&lt;/code&gt; and &lt;code&gt;rfind()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can use these two functions for finding data anywhere in the String.&lt;/p&gt;
&lt;p&gt;First, let’s see the &lt;code&gt;find()&lt;/code&gt; function in action. We can try it on the &lt;code&gt;sampleString&lt;/code&gt; that we created earlier.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sampleString.find(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;sky&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, we passed the &lt;code&gt;sky&lt;/code&gt;. This String exists at index position &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Additionally, you can also check for sub-String using the &lt;strong&gt;in&lt;/strong&gt; operator:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;blue&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sampleString)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a result, we get &lt;code&gt;True&lt;/code&gt; since the word blue is present within the &lt;code&gt;sampleString.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Moving on, let’s talk about the &lt;code&gt;replace()&lt;/code&gt; function to replace contents within a String.&lt;/p&gt;
&lt;p&gt;Let’s try it on our &lt;code&gt;sampleSring.&lt;/code&gt; For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sampleString.replace(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;blue&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;black&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The sky is black&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, I used the function to replace the word &lt;code&gt;blue&lt;/code&gt; with &lt;code&gt;black&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before I end this section, I also want to show you the &lt;code&gt;count()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;This function allows us to count the instances of a sub-String and returns the number of times it occurs.&lt;/p&gt;
&lt;p&gt;I am going to create a new String and use the &lt;code&gt;count()&lt;/code&gt; function on that.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;newString = &apos;I can see a car parked outside our house. It is a red car.&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let’s see the &lt;code&gt;count()&lt;/code&gt; function in action:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(newString.count(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;car&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And as you can see that we get two since the word &lt;strong&gt;car&lt;/strong&gt; occurs twice. Also, remember that these functions are all case-sensitive. If I pass the word &lt;strong&gt;car&lt;/strong&gt; with an upper case &lt;strong&gt;C&lt;/strong&gt;, then the result would be 0. Since no words are within our String, starting with an upper case &lt;strong&gt;C&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;string-manipulation&quot;&gt;String Manipulation&lt;/h2&gt;
&lt;p&gt;An essential concept that involves Python text editing &amp;amp; processing is the ability to manipulate String.&lt;/p&gt;
&lt;p&gt;First, let’s start by defining a new sample String. So, I will type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;newString = &apos;It is a sunny day in Southern California.&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To convert all the letters into upper case, we can use the &lt;code&gt;upper()&lt;/code&gt; method.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(newString.upper())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;IT IS A SUNNY DAY IN SOUTHERN CALIFORNIA.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same goes with converting all the letters to lower case. We will use the &lt;code&gt;lower()&lt;/code&gt; method.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(newString.lower())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;it is a sunny day in southern california.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let’s talk about the &lt;code&gt;split()&lt;/code&gt; function, which you can use to break up a single text String into multiple Strings. For instance, suppose we want to split each of the words within our &lt;code&gt;newString&lt;/code&gt; into an array based on spaces between them.&lt;/p&gt;
&lt;p&gt;Here’s what I mean. Type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;splitString &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; newString.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(splitString)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[&apos;It&apos;, &apos;is&apos;, &apos;a&apos;, &apos;sunny&apos;, &apos;day&apos;, &apos;in&apos;, &apos;Southern&apos;, &apos;California.&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As I ran that, you can see that we have split the String into an array.&lt;/p&gt;
&lt;p&gt;What if you want to join all the words inside your array? For that, we can use the &lt;code&gt;join()&lt;/code&gt; function. It does the exact opposite.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;join()&lt;/code&gt; function will take the individual Strings and convert them into one single String.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;joinString &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos; &apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.join(splitString)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(joinString)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;It is a sunny day in Southern California.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we have successfully converted the &lt;code&gt;splitString&lt;/code&gt; array and joined them together again.&lt;/p&gt;
&lt;p&gt;We joined each String into one single String, with the spaces between them being the separators.&lt;/p&gt;
&lt;p&gt;On the other hand, if you don’t want any space between the words, then type this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;joinString &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.join(splitString)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(joinString)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ItisasunnydayinSouthernCalifornia.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a result, the function went with attaching all the words since we passed no separators.&lt;/p&gt;
&lt;p&gt;Python has a feature that allows us to perform batch replacement of characters in a String through a translation table.&lt;/p&gt;
&lt;p&gt;Here’s what I mean.&lt;/p&gt;
&lt;p&gt;First, let’s create a translation table with the &lt;code&gt;maketrans()&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;transTable &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.maketrans(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;abcdef&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;123456&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So now we have our translation table defined as &lt;code&gt;transTable&lt;/code&gt; with some characters that we want to replace with some numbers. For instance, the letter &lt;code&gt;a&lt;/code&gt; will become 1, and &lt;code&gt;b&lt;/code&gt; will become 2, and so on&lt;/p&gt;
&lt;p&gt;Then go ahead and define a new variable with the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myString &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;This is a big deal for California.&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myString)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a big deal for California.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let’s go ahead and use the translation table &lt;strong&gt;transTable&lt;/strong&gt; to perform the translate operation. To do that, we’ll use the &lt;strong&gt;translate()&lt;/strong&gt; function.&lt;/p&gt;
&lt;p&gt;So type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myString.translate(transTable))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is 1 2ig 451l 6or C1li6orni1.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see that some of the characters have now become numbers we defined in our translation table &lt;strong&gt;transTable&lt;/strong&gt; earlier.&lt;/p&gt;
&lt;p&gt;To sum up, batch replacement in Python allows us to define translation tables that we can use to replace characters with some other characters that we specify.&lt;/p&gt;
&lt;p&gt;In the next part of the Python text editing &amp;amp; processing tutorial, we learn how to work with String formatting.&lt;/p&gt;
&lt;h2 id=&quot;string-formatting&quot;&gt;String Formatting&lt;/h2&gt;
&lt;p&gt;In this part of the Python text editing &amp;amp; processing tutorial, we will take a good look into String formatting.&lt;/p&gt;
&lt;p&gt;Above all, we will solely focus on formatting Strings using the template Strings and the &lt;code&gt;format()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Now, let me create and define a new variable. We can call it &lt;code&gt;newString2&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;newString2 = &apos;It is a $weather day in the $direction Coast.&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I will explain in a bit why I am using the dollar signs.&lt;/p&gt;
&lt;p&gt;Okay, the first concept in this part is how to use template Strings to perform String formatting.&lt;/p&gt;
&lt;p&gt;I have to import the class &lt;code&gt;Template&lt;/code&gt; from the &lt;code&gt;String&lt;/code&gt; module. Here’s how you do it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; string &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Template&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now you can see that  &lt;code&gt;newString2&lt;/code&gt; has two dollar signs which shows that these are placeholders or variables.&lt;/p&gt;
&lt;p&gt;We can substitute these placeholders with Strings or values. There are usually two steps involved to do this.&lt;/p&gt;
&lt;p&gt;First, we create an instance of the Template class:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;theInstance &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Template(newString2)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you have to call the &lt;code&gt;substitute()&lt;/code&gt; function on the template and pass the placeholders with values.&lt;/p&gt;
&lt;p&gt;Here’s how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;outputString &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; theInstance.substitute(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;weather&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Rainy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;direction&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;East&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(outputString)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;It is a Rainy day in the East Coast.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output shows that we have actual values &lt;code&gt;rainy&lt;/code&gt; &amp;amp; &lt;code&gt;East&lt;/code&gt; replacing the placeholders we defined before using the dollar sign.&lt;/p&gt;
&lt;p&gt;Let’s move on to learning how to use the &lt;code&gt;format()&lt;/code&gt; function. First, I will start by creating two variables as follows:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;fruit = &apos;apples&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;color = &apos;red&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So what we are going to do is substitute these two values into a new String using the &lt;code&gt;format()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;And this is how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Those &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; are &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.format(fruit,color)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(newStr)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Those apples are red.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see that the curly braces acted as placeholders. After that, we used the &lt;code&gt;format()&lt;/code&gt; function and passed the names of the variables &lt;code&gt;fruit&lt;/code&gt; and &lt;code&gt;color&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In short, it took the two variables in order and put their values into the String where we have the curly braces.&lt;/p&gt;
&lt;p&gt;In addition, you can also substitute named variables. Here’s how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Those &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{var1}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; are &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{var2}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.format(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;var1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;fruit,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;var2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;color))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Those apples are red.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The variables &lt;code&gt;var1&lt;/code&gt; and &lt;code&gt;var2&lt;/code&gt; are substituted.&lt;/p&gt;
&lt;h2 id=&quot;replacing--removing-characters-from-a-string&quot;&gt;Replacing &amp;amp; Removing Characters from a String&lt;/h2&gt;
&lt;p&gt;One of the fastest ways to replace a character with a new character in Python is by using the &lt;code&gt;replace()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;For example, we have the following String:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;myStr = &apos;abcdefghijk12345&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I want to replace the first character a with the letter z, and here’s how I can do it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myStr.replace(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;z&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;zbcdefghijk12345&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;replace()&lt;/code&gt; function returns a new String, while the original String remains unchanged. If you want the change to be permanent, then you can do this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; myStr.replace(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;z&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myStr)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;zbcdefghijk12345&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the change is permanent for &lt;strong&gt;myStr.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;replace()&lt;/code&gt; function can also remove characters if we don’t pass any values as the second parameter.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myStr.replace(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;z&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;bcdefghijk12345&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a result, we can now see that the letter z is gone from our String.&lt;/p&gt;
&lt;p&gt;Lastly, the &lt;code&gt;replace()&lt;/code&gt; can also remove spaces from a String.&lt;/p&gt;
&lt;p&gt;Let’s say I have the following String:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;newStr = &apos;a b c d e f g h&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see that we have extra spaces between the characters, so I want to get rid of them.&lt;/p&gt;
&lt;p&gt;For that I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(newStr.replace(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;abcdefgh&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Moreover, to make the change permanent:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; newStr.replace(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(newStr)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;abcdefgh&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a result, the change is now permanent for &lt;code&gt;newStr&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This section brings us to the end of Python text editing and processing. We went over some of the crucial concepts that you will need to know if you want to edit, process, and manipulate text with Python.&lt;/p&gt;
&lt;p&gt;Depending on your situation, you may have to search Strings, format Strings, remove characters from a String or even replace them with new values. When it comes to Python text editing &amp;amp; processing, the functionality of Python can be endless.&lt;/p&gt;
&lt;p&gt;Most importantly, if you work with big data or need to parse specific characters or sub-String, Python text editing and processing is an efficient way to handle those data.&lt;/p&gt;
&lt;p&gt;Once you can handle text in memory, &lt;a href=&quot;/blog/reading-writing-files-python/&quot;&gt;reading and writing files in Python&lt;/a&gt; shows you how to work with it on disk.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;The text editing &amp;amp; processing functionality of Python is pretty cool. What other programming languages do you think are also suited for working with text?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>strings</category><category>files</category></item><item><title>KNN Algorithm Using Scikit-Learn: Classifying Iris Species</title><link>https://my-space.io/blog/knn-algorithm-scikit-learn-iris/</link><guid isPermaLink="true">https://my-space.io/blog/knn-algorithm-scikit-learn-iris/</guid><description>Learn how the KNN algorithm works, then build a scikit-learn classifier for the iris dataset step by step, from exploring the data to 97% accuracy.</description><pubDate>Wed, 22 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;KNN Algorithm (K-Nearest Neighbor) is a supervised learning classification algorithm to perform predictive modeling and classify data.&lt;/p&gt;
&lt;p&gt;The KNN algorithm learns from example data and able to classify new data based on feature similarity.&lt;/p&gt;
&lt;p&gt;The primary function of supervised learning algorithms is to learn from labeled data points with some features to identify unlabeled data.&lt;/p&gt;
&lt;p&gt;Think of labeled data as training data or examples that already come with the correct answer or classification. Therefore, this is the data that you feed into the model for training.&lt;/p&gt;
&lt;p&gt;And, you feed the test data to test and evaluate your model’s prediction.&lt;/p&gt;
&lt;p&gt;The purpose of this article is to get your hands dirty with the KNN algorithm by building an actual machine learning model. The model that we will create is going to classify different species of iris.&lt;/p&gt;
&lt;h2 id=&quot;how-does-knn-algorithm-work&quot;&gt;How Does KNN Algorithm Work?&lt;/h2&gt;
&lt;p&gt;The KNN algorithm uses the concept of similarity. It assumes that similar objects or data points exist near to each other.&lt;/p&gt;
&lt;p&gt;Imagine plotting your data as points on a graph. Similar data points end up close to each other. Let’s say the &lt;strong&gt;blue&lt;/strong&gt; points are cats. And the &lt;strong&gt;red&lt;/strong&gt; points are the dogs. Let’s assume that this data is inputted based on &lt;strong&gt;features&lt;/strong&gt; such as &lt;strong&gt;weights&lt;/strong&gt; and &lt;strong&gt;heights&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;As a result, points with similar features are clustered close to each other.&lt;/p&gt;
&lt;p&gt;Now imagine a few &lt;strong&gt;green&lt;/strong&gt; data points. They represent animals that we need to classify and predict, whether we have cats or dogs. In this case, you only have the information on their weights and heights.&lt;/p&gt;
&lt;p&gt;Based on their features we are able to associate them with either the neighboring blue points or the red points.&lt;/p&gt;
&lt;p&gt;For example, imagine a green point that sits close to the red points. This data point is most likely to be a dog.&lt;/p&gt;
&lt;p&gt;Now to make the prediction, that’s where the KNN algorithm will come into play.&lt;/p&gt;
&lt;p&gt;KNN will calculate the distance between a new data point with every single data point from your training set.&lt;/p&gt;
&lt;p&gt;It converts the data points into mathematical values or vectors, which it uses to find the distances.&lt;/p&gt;
&lt;p&gt;One of the most popular distance measuring metrics is the &lt;strong&gt;Euclidean distance&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Euclidean distance works by calculating the distance between two data points in a plane.&lt;/p&gt;
&lt;p&gt;For instance, if we want to calculate the Euclidean distance between two data points &lt;strong&gt;p&lt;/strong&gt; &amp;amp; &lt;strong&gt;q&lt;/strong&gt;, we can use the following formula:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;d(p, q) = √( (q₁ − p₁)² + (q₂ − p₂)² + … + (qₙ − pₙ)² )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once the algorithm calculates every single distance, it selects the &lt;strong&gt;K&lt;/strong&gt; nearest data points.&lt;/p&gt;
&lt;p&gt;What do I mean by &lt;strong&gt;K&lt;/strong&gt; nearest?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;K&lt;/strong&gt; is the number of nearest neighbors or labeled data points in proximity to your new data point that we are trying to classify. Most importantly, the value of K is a positive integer.&lt;/p&gt;
&lt;p&gt;In other words, if we have &lt;strong&gt;K=4&lt;/strong&gt;, then the algorithm looks for the four nearest data points around the unlabeled point.&lt;/p&gt;
&lt;p&gt;Let’s assume that the value of K for the unknown green data point is &lt;strong&gt;3&lt;/strong&gt;. Thus, we will draw a circle around the green data point with three neighbors inside the proximity.&lt;/p&gt;
&lt;p&gt;Out of 3 of the neighboring data points, 2 of the data points are red, and the other is blue. In other words, we have two nearest points that belong to the red &lt;strong&gt;class&lt;/strong&gt; and one nearest point belonging to the blue.&lt;/p&gt;
&lt;p&gt;For example, the majority of the neighbors inside the circle are red. And as a result, our algorithm will most likely predict that our new data point belongs to the red class.&lt;/p&gt;
&lt;p&gt;However, it probably is not going to be this simple. This is an example using imaginary data points. But hopefully, you get the idea of how our algorithm uses the distance and the K value to make predictions.&lt;/p&gt;
&lt;p&gt;It is crucial to pick the optimal value for K to minimize the number of biases and variances. Thus, a K value should neither be too small nor too large.&lt;/p&gt;
&lt;p&gt;And the best way to find out the optimal K value is by testing your model with different K values.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To sum up, here’s how we implement the KNN algorithm:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Load and store the data.&lt;/li&gt;
&lt;li&gt;Calculate the distance from &lt;strong&gt;x&lt;/strong&gt; (new data point) to all other data points.&lt;/li&gt;
&lt;li&gt;Sort all the distances from your data in ascending order.&lt;/li&gt;
&lt;li&gt;Initialize the &lt;strong&gt;K&lt;/strong&gt; value for the nearest data points.&lt;/li&gt;
&lt;li&gt;Make a prediction based on the majority of data points with the same label within the &lt;strong&gt;K&lt;/strong&gt; value.&lt;/li&gt;
&lt;li&gt;Evaluate your machine learning model.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Before we jump into the new section, I am assuming you are already familiar with the following concepts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;NumPy&lt;/li&gt;
&lt;li&gt;Pandas&lt;/li&gt;
&lt;li&gt;Matplotlib&lt;/li&gt;
&lt;li&gt;Supervised Machine Learning&lt;/li&gt;
&lt;li&gt;Scikit-Learn (Optional)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;classifying-iris-species&quot;&gt;Classifying Iris Species&lt;/h2&gt;
&lt;p&gt;Moving onto the fun part where we will now build an actual machine learning model.&lt;/p&gt;
&lt;p&gt;For this tutorial, we want to create a machine model that will allow botanists to classify different species of iris flowers.&lt;/p&gt;
&lt;p&gt;The way our model will predict the types of flowers is by learning from their features. These features are the length and width of the petals and the sepals.&lt;/p&gt;
&lt;p&gt;By learning from these measurements, our model will predict whether the flower is a &lt;strong&gt;setosa, versicolor, or virginica.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since we are training the model using labeled data for which we already know the answer, this is a supervised machine learning problem.&lt;/p&gt;
&lt;p&gt;For instance, when we feed our model with unclassified iris species, it will give us predictions based on its learning from training data.&lt;/p&gt;
&lt;p&gt;In other words, this problem is also a classification problem as it involves grouping data into different classes.&lt;/p&gt;
&lt;p&gt;For problems such as this, we address possible outputs (different iris species) as &lt;strong&gt;classes&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now open your &lt;strong&gt;Jupyter notebook&lt;/strong&gt; and type in the following imports:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; plt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;matplotlib&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;part-1---data-preparation&quot;&gt;Part 1 - Data Preparation&lt;/h3&gt;
&lt;p&gt;In this part, we will prepare and analyze the data. Our data is the iris dataset. An advantage of using Scikit-Learn is that it already includes the iris dataset for us.&lt;/p&gt;
&lt;p&gt;We just have to load it by calling the &lt;code&gt;load_iris&lt;/code&gt;. So type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sklearn.datasets &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; load_iris&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;iris_dataset &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; load_iris()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Keys of iris_dataset:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, iris_dataset.keys())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Keys of iris_dataset:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; dict_keys([&apos;data&apos;, &apos;target&apos;, &apos;target_names&apos;, &apos;DESCR&apos;, &apos;feature_names&apos;, &apos;filename&apos;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output that we get is a &lt;code&gt;Bunch&lt;/code&gt; object. This type is similar to a dictionary as it contains keys and values. Therefore, we can see &lt;strong&gt;key-value&lt;/strong&gt; pairs.&lt;/p&gt;
&lt;p&gt;First, the &lt;code&gt;data&lt;/code&gt; key is a &lt;a href=&quot;/blog/numpy-tutorial-arrays/&quot;&gt;NumPy&lt;/a&gt; array that contains measurements of sepal length, sepal width, petal length, and petal width for 150 different flowers.&lt;/p&gt;
&lt;p&gt;Type the following to learn more:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Data Type:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;data&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Data Type: &amp;lt;class &apos;numpy.ndarray&apos;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each row in this NumPy array corresponds to a flower. On the other hand, the columns correspond to their measurements.&lt;/p&gt;
&lt;p&gt;Let’s take a look at the shape of the array to understand our data better.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Shape of Data:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;data&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].shape)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Shape of Data: (150, 4)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The shape confirms that our data has &lt;strong&gt;150&lt;/strong&gt; &lt;strong&gt;rows.&lt;/strong&gt; And each row has &lt;strong&gt;4 columns&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;So, when it comes to machine learning, the items or rows are known as &lt;strong&gt;samples.&lt;/strong&gt; And the columns are &lt;strong&gt;features.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We can take a look into the features of the first ten samples from our data set. Type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;First 10 Samples and Their Features:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;data&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;First 10 Samples and Their Features:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [[5.1 3.5 1.4 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4.9 3.  1.4 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4.7 3.2 1.3 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4.6 3.1 1.5 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [5.  3.6 1.4 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [5.4 3.9 1.7 0.4]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4.6 3.4 1.4 0.3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [5.  3.4 1.5 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4.4 2.9 1.4 0.2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4.9 3.1 1.5 0.1]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now moving onto the next key, which is the &lt;code&gt;target.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;target&lt;/code&gt; key is a NumPy array that holds the iris species encoded as integers from &lt;strong&gt;0&lt;/strong&gt; to &lt;strong&gt;2&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And most importantly, this key is a one-dimensional array. We can find out more about the target through the following lines of code:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Type of Target:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Shape of Target:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].shape)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Type of Target: &amp;lt;class &apos;numpy.ndarray&apos;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Shape of Target: (150,)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 2 2]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a result, the outputs tell us that this is a one-dimensional NumPy array. Moreover, it has 150 species encoded as integers from 0 to 2.&lt;/p&gt;
&lt;p&gt;0 means &lt;strong&gt;setosa&lt;/strong&gt;, 1 means &lt;strong&gt;versicolor&lt;/strong&gt;, and 2 means &lt;strong&gt;virginica&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Next, we have the &lt;code&gt;target_names.&lt;/code&gt; This key holds an array of strings. These strings are the name of the species of the flowers.&lt;/p&gt;
&lt;p&gt;Let’s take a look:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Target names:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target_names&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Target names: [&apos;setosa&apos; &apos;versicolor&apos; &apos;virginica&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we have the key &lt;code&gt;DESCR&lt;/code&gt;. This key provides us with a description of the iris dataset.&lt;/p&gt;
&lt;p&gt;Let’s take a look:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;DESCR&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;.. _iris_dataset:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Iris plants dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;--------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;**Data Set Characteristics:**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Number of Instances: 150 (50 in each of three classes)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Number of Attributes: 4 numeric, predictive attributes and the class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Attribute Information:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - sepal length in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - sepal width in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - petal length in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - petal width in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - class:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                - Iris-Setosa&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                - Iris-Versicolour&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                - Iris-Virginica&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Summary Statistics:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ============== ==== ==== ======= ===== ====================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                    Min  Max   Mean    SD   Class Correlation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ============== ==== ==== ======= ===== ====================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    sepal length:   4.3  7.9   5.84   0.83    0.7826&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    sepal width:    2.0  4.4   3.05   0.43   -0.4194&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ============== ==== ==== ======= ===== ====================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Missing Attribute Values: None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Class Distribution: 33.3% for each of 3 classes.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Creator: R.A. Fisher&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Date: July, 1988&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;from Fisher&apos;s paper. Note that it&apos;s the same as in R, but not as in the UCI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Machine Learning Repository, which has two wrong data points.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is perhaps the best known database to be found in the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;pattern recognition literature.  Fisher&apos;s paper is a classic in the field and&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;is referenced frequently to this day.  (See Duda &amp;amp; Hart, for example.)  The&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;data set contains 3 classes of 50 instances each, where each class refers to a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;type of iris plant.  One class is linearly separable from the other 2; the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;latter are NOT linearly separable from each other.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;.. topic:: References&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Fisher, R.A. &quot;The use of multiple measurements in taxonomic problems&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     Annual Eugenics, 7, Part II, 179-188 (1936); also in &quot;Contributions to&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     Mathematical Statistics&quot; (John Wiley, NY, 1950).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Duda, R.O., &amp;amp; Hart, P.E. (1973) Pattern Classification and Scene Analysis.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     (Q327.D83) John Wiley &amp;amp; Sons.  ISBN 0-471-22361-1.  See page 218.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Dasarathy, B.V. (1980) &quot;Nosing Around the Neighborhood: A New System&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     Structure and Classification Rule for Recognition in Partially Exposed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     Environments&quot;.  IEEE Transactions on Pattern Analysis and Machine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     Intelligence, Vol. PAMI-2, No. 1, 67-71.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Gates, G.W. (1972) &quot;The Reduced Nearest Neighbor Rule&quot;.  IEEE Transactions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     on Information Theory, May 1972, 431-433.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al&quot;s AUTOCLASS II&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     conceptual clustering system finds 3 classes in the data.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Many, many more ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Moreover, you can also print a specific part of the &lt;code&gt;DESCR&lt;/code&gt; instead of the complete description.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;DESCR&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;...&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;.. _iris_dataset:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Iris plants dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;--------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;**Data Set Characteristics:**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Number of Instances: 150 (50 in each of three classes)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Number of Attributes: 4 numeric, predictive attributes and the class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    :Attribute Information:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - sepal length in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - sepal width in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - petal length in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - petal width in cm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        - class:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                - Iris-Setosa&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                - Iris-Versicolour&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                - Iris-Virginica&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key &lt;code&gt;feature_names&lt;/code&gt; is a list of strings that provides us with the names of available features.&lt;/p&gt;
&lt;p&gt;Go ahead and type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Feature Names:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;feature_names&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Feature Names: [&apos;sepal length (cm)&apos;, &apos;sepal width (cm)&apos;, &apos;petal length (cm)&apos;, &apos;petal width (cm)&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see we get a list of all the features and their unit.&lt;/p&gt;
&lt;p&gt;For now, these are the crucial keys that should allow us to get familiar with the data. You can go ahead and learn more about them by performing various operations with the dataset. But we are good to move onto the training and testing phase.&lt;/p&gt;
&lt;h3 id=&quot;part-2---training-and-testing-data&quot;&gt;Part 2 - Training and Testing Data&lt;/h3&gt;
&lt;p&gt;In this part, we will split our data into two sets, the training set and the testing set.&lt;/p&gt;
&lt;p&gt;Training set is the dataset which we use to train our model. The KNN algorithm will learn from the training first how to distinguish the different iris species.&lt;/p&gt;
&lt;p&gt;During the training phase, we don’t expose our testing dataset to the model. As a result, when the training is over, we use the testing dataset to measure its success.&lt;/p&gt;
&lt;p&gt;Testing dataset is what we use to assess how well our model works.&lt;/p&gt;
&lt;p&gt;One of the benefits of using Scikit-Learn is that it comes prebuilt &lt;code&gt;train_test_split()&lt;/code&gt; function. This function will split &lt;strong&gt;75%&lt;/strong&gt; of the rows in the dataset into a training set. And the rest &lt;strong&gt;25%&lt;/strong&gt; into the testing set.&lt;/p&gt;
&lt;p&gt;So type the following code and then I will explain what’s going on.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sklearn.model_selection &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; train_test_split&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;X_train, X_test, y_train, y_test &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; train_test_split(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;data&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;random_state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first line of code imports the &lt;code&gt;train_test_split()&lt;/code&gt; function from the module &lt;code&gt;sklearn.model_selection&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, we are using the &lt;code&gt;train_test_split()&lt;/code&gt; function to randomize and split our dataset into the following variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;X -&lt;/code&gt; These are the inputs or features that you feed into your model. This value corresponds to a two-dimensional array (&lt;strong&gt;matrix&lt;/strong&gt;). As a result, we capitalize the &lt;code&gt;X&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;y -&lt;/code&gt; These are the expected outcomes or the labels. It corresponds to a one-dimensional array (&lt;strong&gt;vector&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;X_train&lt;/code&gt; - This is the training dataset.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;y_train&lt;/code&gt; - The labels that to the training dataset (&lt;code&gt;X_train&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;X_test&lt;/code&gt; - This is the testing dataset.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;y-test -&lt;/code&gt; The labels that to the training dataset (&lt;code&gt;X_test&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And the parameters that we passed are our &lt;code&gt;data&lt;/code&gt; itself and the &lt;code&gt;target&lt;/code&gt;. I like to think of this function as &lt;code&gt;train_test_split(X, y),&lt;/code&gt; where &lt;strong&gt;X&lt;/strong&gt; corresponds to features and &lt;strong&gt;y&lt;/strong&gt; to the labels.&lt;/p&gt;
&lt;p&gt;If you can remember, our &lt;code&gt;data&lt;/code&gt; holds a two-dimensional array of features to each species. And the &lt;code&gt;target&lt;/code&gt; contains a one-dimensional array of labels encoded as integers from 0 to 2.&lt;/p&gt;
&lt;p&gt;One more parameter that we are using is the &lt;code&gt;random_state&lt;/code&gt;. And we set it to &lt;code&gt;0&lt;/code&gt;. Therefore, it ensures that our function always splits the same data every time we run the &lt;code&gt;train_test_split()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;So if you don’t assign a fixed value like &lt;strong&gt;0, 1, or 42&lt;/strong&gt;, then every time you run the code, our training and testing datasets will have different sets of values.&lt;/p&gt;
&lt;p&gt;Let’s see what we have so far by printing out their shapes. First, the training dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;X_train Shape:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, X_train.shape)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;y_train Shape:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, y_train.shape)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;X_train Shape: (112, 4)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;y_train Shape: (112,)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the test dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;X_test Shape:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, X_test.shape)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;y_test Shape:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, y_test.shape)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;X_test Shape: (38, 4)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;y_test Shape: (38,)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see that &lt;code&gt;X_train&lt;/code&gt; contains &lt;strong&gt;75%&lt;/strong&gt; of all the rows. On the other hand, &lt;code&gt;X_test&lt;/code&gt; holds &lt;strong&gt;25%&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;part-3---data-visualiztion&quot;&gt;Part 3 - Data Visualiztion&lt;/h3&gt;
&lt;p&gt;It is always a good idea to visualize your data for further inspection before creating a model.&lt;/p&gt;
&lt;p&gt;At this point, we will visualize our data to see what we have. And one of the best ways to visualize our data is through &lt;strong&gt;scatter plots&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Scatter plots are diagrams where we represent our data using dots. We put one feature on the &lt;strong&gt;x-axis&lt;/strong&gt; and one on the &lt;strong&gt;y-axis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;One of the purposes of a scatter plot is to observe the relationship between variables.&lt;/p&gt;
&lt;p&gt;To create a scatter plot, first, we will create a dataframe from the data  &lt;code&gt;X_train&lt;/code&gt; using &lt;a href=&quot;/blog/pandas-tutorial-for-beginners/&quot;&gt;Pandas&lt;/a&gt;. After that, label the columns with the strings we saw in &lt;code&gt;feature_names&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;df &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.DataFrame(X_train, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;columns&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;iris_dataset.feature_names)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;df.head()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now it is time to convert the dataframe into a scatter plot. We will use the function &lt;code&gt;plotting.scatter_matrix()&lt;/code&gt; to pass our dataframe and color our data points based on the labels from &lt;code&gt;y_train.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pd.plotting.scatter_matrix(df,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_train,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;figsize&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;marker&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;o&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;alpha&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;.8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.show()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the code, you can see that we have passed several other parameters to modify our chart as we like. For example, &lt;code&gt;figsize&lt;/code&gt; lets us determine the size of the diagrams or figures.&lt;/p&gt;
&lt;p&gt;You can learn more about the parameters that are available to the &lt;code&gt;plotting.scatter_matrix()&lt;/code&gt; function through the official Pandas documentation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Here’s a challenge for you. Go ahead and try to visualize the X_test data using the same procedure.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;See if you can figure it out. You can use the code snippet above as an example.&lt;/p&gt;
&lt;p&gt;However, if not, then don’t worry about it. Here’s how you do it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;df &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.DataFrame(X_test, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;columns&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;iris_dataset.feature_names)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pd.plotting.scatter_matrix(df,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;y_test,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;figsize&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;marker&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;o&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;alpha&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;.8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.show()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;part-4---creating-the-model&quot;&gt;Part 4 - Creating the Model&lt;/h3&gt;
&lt;p&gt;Building the model using Scikit-Learn is not as complicated as it sounds. As you may already know that it comes with many classification algorithms. So the one that we will import is the KNN algorithm or &lt;strong&gt;K-nearest neighbors&lt;/strong&gt; classifier.&lt;/p&gt;
&lt;p&gt;First, we have to import the &lt;code&gt;KNeighborsClassifier&lt;/code&gt; class from the &lt;code&gt;neighbors&lt;/code&gt; module. After that, instantiate an object of that class.&lt;/p&gt;
&lt;p&gt;Then we will set the value of &lt;strong&gt;K&lt;/strong&gt; to &lt;strong&gt;1&lt;/strong&gt; using the parameter &lt;strong&gt;n_neighbors=1&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And here’s the code for all of that:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sklearn.neighbors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; KNeighborsClassifier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;knnObject &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; KNeighborsClassifier(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n_neighbors&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next step is to fit our training data (&lt;code&gt;X_train, y_train)&lt;/code&gt; with the &lt;code&gt;knnObject&lt;/code&gt; using the method &lt;code&gt;fit()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Think of fitting the data as training. The &lt;code&gt;fit()&lt;/code&gt; method allows us to take in our training dataset and train them to make predictions.&lt;/p&gt;
&lt;p&gt;So, type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;knnObject.fit(X_train, y_train)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;KNeighborsClassifier(algorithm=&apos;auto&apos;, leaf_size=30, metric=&apos;minkowski&apos;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                     metric_params=None, n_jobs=None, n_neighbors=1, p=2,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                     weights=&apos;uniform&apos;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For now, you don’t have to worry about the parameters we got as an output. All we did was fit the model using our training dataset so that we can make predictions.&lt;/p&gt;
&lt;h3 id=&quot;part-4---making-predictions&quot;&gt;Part 4 - Making Predictions&lt;/h3&gt;
&lt;p&gt;Imagine you are a machine learning engineer for a company. A client of yours reached out to you to verify an iris species they found in the wild.&lt;/p&gt;
&lt;p&gt;They only gave us the following information:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sepal length: 40 cm&lt;/li&gt;
&lt;li&gt;Sepal width: 10 cm&lt;/li&gt;
&lt;li&gt;Petal length: 5 cm&lt;/li&gt;
&lt;li&gt;Petal width: 2 cm&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Based on these features, we have to make a prediction.&lt;/p&gt;
&lt;p&gt;So, our first step is to create a two-dimensional NumPy array and calculate the shape:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newIris &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.array([[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;40&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;newIris Shape:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, newIris.shape)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;newIris Shape: (1, 4)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The reason we have created a two-dimensional array is that Scikit-Learn only expects two-dimensional arrays.&lt;/p&gt;
&lt;p&gt;Now we are going to make the prediction by calling the &lt;code&gt;predict()&lt;/code&gt;method on &lt;code&gt;knnObject&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;prediction &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; knnObject.predict(newIris)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Prediction Value:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, prediction)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Predicted Target Name:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       iris_dataset[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target_names&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][prediction])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Prediction Value: [2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Predicted Target Name: [&apos;virginica&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The iris our client found belongs to &lt;strong&gt;class 2,&lt;/strong&gt; which corresponds to the species &lt;strong&gt;virginica&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;But before we confirm our result to the client, we have to make sure that our model predicted the correct species. And that’s where model evaluation comes into play.&lt;/p&gt;
&lt;h3 id=&quot;part-5---model-evaluation&quot;&gt;Part 5 - Model Evaluation&lt;/h3&gt;
&lt;p&gt;For model evaluation, we have to use the test set. Even though you don’t use that data to build the model, but only to test it. (If you wonder why the test set stays untouched, &lt;a href=&quot;/blog/overfitting-vs-underfitting/&quot;&gt;overfitting vs underfitting&lt;/a&gt; explains it.)&lt;/p&gt;
&lt;p&gt;To clarify, this process involves predicting each iris on the test dataset using &lt;code&gt;X_test&lt;/code&gt;. And then compare that prediction against the test data’s labels or the &lt;code&gt;y_test&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First, we will make the prediction using &lt;code&gt;knnObect&lt;/code&gt;.&lt;code&gt;predict()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;testSetPredictions &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; knnObject.predict(X_test)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Test Set Predictions:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, testSetPredictions)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Test Set Predictions: [2 1 0 2 0 2 0 1 1 1 2 1 1 1 1 0 1 1 0 0 2 1 0 0 2 0 0 1 1 0 2 1 0 2 2 1 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 2]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then call the &lt;strong&gt;score&lt;/strong&gt; method to measure the model’s accuracy:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;accuaracy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(knnObject.score(X_test, y_test),&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;The Test Set Accuracy is:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,accuaracy)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The Test Set Accuracy is: 0.97&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In short, our model made the correct prediction of the iris species in the test dataset 97% of the time.&lt;/p&gt;
&lt;p&gt;There are ways to tune our model to improve its accuracy and performance. But we will not go into that right now.&lt;/p&gt;
&lt;p&gt;To sum up, 97% accuracy is a trustworthy model for such scenarios. However, depending on requirements, that may not be enough. And that’s where model tuning comes into play.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In conclusion, we have created a simple iris classification model using the KNN algorithm. Not only that, but you have also learned how the KNN algorithm works and how to implement it using Scikit-Learn.&lt;/p&gt;
&lt;p&gt;Additionally, some of the aspects that we discussed require prior knowledge of specific tools and concepts. So you may need to brush up on those skills.&lt;/p&gt;
&lt;p&gt;Besides, focus on learning how to read the official Scikit-Learn documentation. That is a great help.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Found this tutorial useful? Still, having problems understanding the KNN algorithm? What other algorithm do you think we can use to classify iris species?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>machine-learning</category><category>scikit-learn</category><category>data-analysis</category></item><item><title>Pandas Tutorial for Beginners: The Ultimate Guide</title><link>https://my-space.io/blog/pandas-tutorial-for-beginners/</link><guid isPermaLink="true">https://my-space.io/blog/pandas-tutorial-for-beginners/</guid><description>A beginner&apos;s guide to Pandas: what it is, how to install it, and how to work with Series and DataFrames, with hands-on Python examples.</description><pubDate>Sat, 06 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you are an aspiring data scientist or interested in data analysis, you must know working with Pandas. So in this Python Pandas tutorial, I will break down the basics and show you how to work with Series and DataFrame.&lt;/p&gt;
&lt;p&gt;Moreover, I will show you how to use Pandas to read, clean, transform and store data.&lt;/p&gt;
&lt;h2 id=&quot;what-is-pandas&quot;&gt;What is Pandas?&lt;/h2&gt;
&lt;p&gt;Pandas is a scientific computing library for data analysis in Python. It was developed in 2008 by Wes McKinney. Moreover, it’s completely open-source and built on top of &lt;a href=&quot;/blog/numpy-tutorial-arrays/&quot;&gt;Numpy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Some of the key features of Pandas includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Provides fast performance to process large data sets.&lt;/li&gt;
&lt;li&gt;Able to load data from a wide variety of sources.&lt;/li&gt;
&lt;li&gt;Offers data structures and operations to manipulate large data sets.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We will learn about other important features as we go along.&lt;/p&gt;
&lt;h2 id=&quot;installing-pandas&quot;&gt;Installing Pandas&lt;/h2&gt;
&lt;p&gt;Before we continue with our Pandas tutorial, I am assuming that you have basic Python knowledge.&lt;/p&gt;
&lt;p&gt;To install Pandas, I recommend that you download the &lt;a href=&quot;https://www.anaconda.com/distribution/&quot;&gt;Anaconda&lt;/a&gt; distribution of Python.&lt;/p&gt;
&lt;p&gt;This distribution is an open-source data science platform that comes with Pandas &amp;amp; other scientific libraries.&lt;/p&gt;
&lt;p&gt;If you do not want to use the Anaconda, then you can also install Pandas from your terminal using the commands below in the following order:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; numpy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pandas&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other than that, Google has a free Jupyter notebook platform known as Google Colab that already provides Pandas and other Python data science libraries. You can use that as well.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Visit here to learn more about Google Colab:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://colab.research.google.com/&quot;&gt;https://colab.research.google.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;series&quot;&gt;Series&lt;/h2&gt;
&lt;p&gt;In this part of our Pandas tutorial, I will talk about Series.&lt;/p&gt;
&lt;p&gt;Pandas Series is a one-dimensional labeled array to hold any data type such as integer, float, string, and Python objects.&lt;/p&gt;
&lt;p&gt;In short, you can think of Series as columns as in excel sheet. A series represents a single column in the computer memory.&lt;/p&gt;
&lt;p&gt;Let’s see how we can create a Series.&lt;/p&gt;
&lt;p&gt;First, we have to import Pandas using the &lt;code&gt;import&lt;/code&gt; keyword.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, type following to create a Series:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mySeries &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mySeries)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0     0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1     1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2     2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;3     3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;4     4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;5     5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;6    10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also, you can give your Series a name.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mySeries &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Num&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mySeries)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0     0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1     1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2     2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;3     3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;4     4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;5     5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;6    10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Name: Num, dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since Pandas works on top of Numpy, the Series contains a Numpy array within itself.&lt;/p&gt;
&lt;p&gt;And we can extract the Numpy array using the method &lt;code&gt;values&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mySeries.values&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;array([ &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,  &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, that the output is a Numpy array.&lt;/p&gt;
&lt;p&gt;A Series also uses index positions.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mySeries.index&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RangeIndex(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;stop&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;step&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To extract values at certain index positions, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mySeries[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mySeries[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The values at index position &lt;code&gt;6&lt;/code&gt; &amp;amp; &lt;code&gt;4&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similar to other data structures, Python also allows us to slice a Series. For instance, I want to extract the values between the index positions 5 &amp;amp; 7. For that, we can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mySeries[5:7]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;5     5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;6    10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Name: Num, dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember that the column on the left holds the index values or positions.&lt;/p&gt;
&lt;p&gt;We can also assign names to the indexes when we create a Series.&lt;/p&gt;
&lt;p&gt;So let’s create a new Series with student scores. Besides, I will also pass the index parameter.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;70&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;80&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;95&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;85&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Sam&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Andrea&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Marcos&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Peng&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Karen&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Chen&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(scores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Sam        70&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Andrea     90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Marcos     80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Peng      100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Karen      95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Chen       85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I passed an index parameter, and this parameter is a list object. Furthermore, this list holds all of our index names or labels.&lt;/p&gt;
&lt;p&gt;The left column is the index column or, you can refer to it as the &lt;strong&gt;label&lt;/strong&gt; column. On the right, we have all our values.&lt;/p&gt;
&lt;p&gt;If you wish to extract all the index labels, then type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scores.index&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Index([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Sam&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Andrea&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Marcos&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Peng&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Karen&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Chen&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;object&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;According to the &lt;code&gt;dtype&lt;/code&gt;, we have a collection of Python objects.&lt;/p&gt;
&lt;h3 id=&quot;indexing--slicing-series&quot;&gt;Indexing &amp;amp; Slicing Series&lt;/h3&gt;
&lt;p&gt;I did touch a little bit on indexing &amp;amp; slicing Series objects previously. However, let’s try out some more examples to see how they really work.&lt;/p&gt;
&lt;p&gt;Let’s perform some indexing and slicing operations using numbers on the Series &lt;code&gt;scores.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;For example, I want to check the score of Karen at the index position &lt;code&gt;4&lt;/code&gt;. So, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scores[4]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;95&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Furthermore, we can slice the Series using numbers as well. Let’s say I want the scores of Andrea, Marcos, Peng &amp;amp; Karen.&lt;/p&gt;
&lt;p&gt;Therefore, I have to slice from the index position &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;5&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scores[1:5]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Andrea     90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Marcos     80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Peng      100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Karen      95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we perform slicing, the ending number is the number up to which Python slices. But it does not include the value.&lt;/p&gt;
&lt;p&gt;For example, here, we are slicing up to the index position &lt;code&gt;5.&lt;/code&gt; But not including the actual value.&lt;/p&gt;
&lt;p&gt;You can also slice a Series using the index labels.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scores[&apos;Sam&apos;:&apos;Peng&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Sam        70&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Andrea     90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Marcos     80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Peng      100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how slicing with index labels is different compared to the way we slice using numbers.&lt;/p&gt;
&lt;p&gt;In contrast to using numerical indexes, you can see that the value of the ending index label (&lt;code&gt;Peng&lt;/code&gt;) is in our result.&lt;/p&gt;
&lt;h4 id=&quot;iloc--loc&quot;&gt;iloc &amp;amp; loc&lt;/h4&gt;
&lt;p&gt;Pandas have a built-in object known as the &lt;code&gt;iloc&lt;/code&gt; to extract data using the integer-based index.&lt;/p&gt;
&lt;p&gt;So, let’s see how we can use the &lt;code&gt;iloc&lt;/code&gt; to extract information from &lt;code&gt;scores:&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scores.iloc[0:2]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Sam       70&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Andrea    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, the &lt;code&gt;iloc&lt;/code&gt; tells Python to extract data using the integer-based index.&lt;/p&gt;
&lt;p&gt;Similarly, we also have the &lt;code&gt;loc&lt;/code&gt; object at our disposal to extract data using a label-based index.&lt;/p&gt;
&lt;p&gt;This is how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scores.loc[&apos;Sam&apos;:&apos;Peng&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Sam        70&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Andrea     90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Marcos     80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Peng      100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You may learn more about &lt;code&gt;iloc&lt;/code&gt; &amp;amp; &lt;code&gt;loc&lt;/code&gt; when we get to the DataFrame section. But for now, let’s keep it simple.&lt;/p&gt;
&lt;p&gt;Before I go to the DataFrame section of this Pandas tutorial, I like to show you two more ways to create Series.&lt;/p&gt;
&lt;p&gt;You can also create Series from a Numpy array. First, import Numpy and Pandas into your IDE:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then create a Numpy array:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.array([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;X&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Y&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Z&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To convert &lt;code&gt;data&lt;/code&gt; into a Pandas Series, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mySeries &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(data)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mySeries)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0    X&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1    Y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2    Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: object&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;series-from-dictionaries&quot;&gt;Series from Dictionaries&lt;/h3&gt;
&lt;p&gt;We can also create Pandas Series from Python dictionaries.&lt;/p&gt;
&lt;p&gt;So let’s start by creating a dictionary:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dictScores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jacob&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;98&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Mae&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;70&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Kayla&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;95&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now pass the &lt;code&gt;dictScores&lt;/code&gt; into &lt;code&gt;pd.Series()&lt;/code&gt; method:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mySeries &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(dictScores)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mySeries)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jacob    98&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Mae      70&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When creating a Series with a dictionary, you should remember that the dictionary keys are index labels.&lt;/p&gt;
&lt;p&gt;Well, that’s it for Series. In the next part of the Pandas tutorial, we will talk about DataFrames.&lt;/p&gt;
&lt;h2 id=&quot;dataframes&quot;&gt;DataFrames&lt;/h2&gt;
&lt;p&gt;Great job if you have made it through Series. In this part of the Pandas tutorial, we learn about DataFrames and the multiple ways we can create them. Not only that, but you will also learn how to set indexes and select, combine and create columns on a DataFrame.&lt;/p&gt;
&lt;p&gt;Now, what is a DataFrame in Pandas?&lt;/p&gt;
&lt;p&gt;A DataFrame is a two-dimensional data structure that has rows and columns. These columns and rows are known as &lt;strong&gt;labeled axes&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And columns of a Dataframe are made up of separate or multiple Series objects. In short, one of the ways you can create a DataFrame is by having two or more Series.&lt;/p&gt;
&lt;p&gt;Coming back to the axes, a DataFrame has two axes. They are &lt;strong&gt;axis 0&lt;/strong&gt; &amp;amp; &lt;strong&gt;axis 1&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In simple terms, &lt;strong&gt;axis 0&lt;/strong&gt; represents rows and, &lt;strong&gt;axis 1&lt;/strong&gt; represents columns. Sometimes I like to think of Pandas DataFrame as Excel sheets.&lt;/p&gt;
&lt;p&gt;Anyway, let’s get our hands dirty with some coding.&lt;/p&gt;
&lt;p&gt;First, I will show you how to create DataFrames using a dictionary.&lt;/p&gt;
&lt;p&gt;Go ahead and import the following libraries:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, create a dictionary objects called &lt;code&gt;scores:&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Chan&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Alex&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Age&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;17&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Grade&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;C&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;B&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The keys of our dictionary will be the column names of our DataFrame. And the values of the dictionary will be the list of items under those columns.&lt;/p&gt;
&lt;p&gt;Now convert &lt;code&gt;score&lt;/code&gt; into a DataFrame:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;df &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.DataFrame(scores)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(df)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   Name  Age Grade&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0  Jake   15     C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1  Chan   16     B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2  Alex   17     A&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you see, we got ourselves a nice-looking DataFrame.&lt;/p&gt;
&lt;p&gt;And this DataFrame looks similar to an excel sheet.&lt;/p&gt;
&lt;p&gt;Also, notice that the keys of our dictionary became the column names. Then the values are the list of items under the columns.&lt;/p&gt;
&lt;p&gt;On the far left side of the DataFrame, we have the index column. Each index value represents a row.&lt;/p&gt;
&lt;p&gt;If you want to check the column names, then you can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;df.columns&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Index([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Age&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Grade&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;object&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember that each column is a Python object it self.&lt;/p&gt;
&lt;p&gt;By the way, each row is an index on our DataFrame. So, to see all the index or rows, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;df.index&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RangeIndex(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;stop&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;step&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output shows that our index starts at &lt;code&gt;0&lt;/code&gt; and stops at &lt;code&gt;3&lt;/code&gt; with a default step of &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;dataframes-from-series&quot;&gt;DataFrames from Series&lt;/h3&gt;
&lt;p&gt;So far, we have learned how to create Pandas DataFrame using Python dictionary.&lt;/p&gt;
&lt;p&gt;Now, I will show you how to create DataFrames using Series.&lt;/p&gt;
&lt;h4 id=&quot;step-1---create-dictionaries&quot;&gt;Step 1 - Create Dictionaries&lt;/h4&gt;
&lt;p&gt;We will start by creating two dictionaries, with each having yearly scores of students.&lt;/p&gt;
&lt;p&gt;So the first one holds student score information from the year 2020. Then the second tells us their score from 2021.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2020 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Kayla&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;85&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Muhammad&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;95&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Alexis&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;98&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2021 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;85&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Kayla&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;95&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Muhammad&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Alexis&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;97&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;step-2---convert-the-dictionaries-into-series&quot;&gt;Step 2 - Convert the Dictionaries into Series&lt;/h4&gt;
&lt;p&gt;The keys in our dictionaries are the index labels. So, we have created two dictionaries. Now the next step is to convert them into Series objects using &lt;code&gt;pd.Series().&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;seriesOne &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(scores2020)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;seriesTwo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(scores2021)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;step-3---create-a-new-dictionary-using-the-series-objects&quot;&gt;Step 3 - Create a New Dictionary Using the Series Objects&lt;/h4&gt;
&lt;p&gt;In this step, we will create a new dictionary that we will pass into the method &lt;code&gt;pd.DataFrame()&lt;/code&gt; later.&lt;/p&gt;
&lt;p&gt;Let’s create a &lt;code&gt;newDict&lt;/code&gt; first:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newDict &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2020&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: seriesOne,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2021&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: seriesTwo}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The keys of the &lt;code&gt;newDict&lt;/code&gt; are the column names. And the values (Series objects) are the data each column holds.&lt;/p&gt;
&lt;h4 id=&quot;step-4---convert-the-new-dictionary-into-a-dataframe&quot;&gt;Step 4 - Convert the New Dictionary into a DataFrame&lt;/h4&gt;
&lt;p&gt;Lastly, we will create the DataFrame by converting the &lt;code&gt;newDict&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For this to work, I have to use &lt;code&gt;pd.DataFrame()&lt;/code&gt; and pass our &lt;code&gt;newDict&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Here’s what I mean:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;yearlyScores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.DataFrame(newDict)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        90    85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Take a look at what’s going on here. I used the &lt;code&gt;pd.DataFrame()&lt;/code&gt; method and passed in our &lt;code&gt;newDict&lt;/code&gt; where the keys were the columns. And the rows were the values.&lt;/p&gt;
&lt;h3 id=&quot;working-with-columns&quot;&gt;Working with Columns&lt;/h3&gt;
&lt;p&gt;As a data scientist, you will work with Pandas a lot. As a result, you also need to work with columns to make changes or retrieve specific information.&lt;/p&gt;
&lt;p&gt;So, let me show you the ways you can work with the columns of a DataFrame.&lt;/p&gt;
&lt;p&gt;We will use the &lt;code&gt;yearlyScores&lt;/code&gt; DataFrame that we just created.&lt;/p&gt;
&lt;p&gt;To select a column, type this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;yearlyScores[&apos;2020&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Name: 2020, dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, we can also select the year &lt;code&gt;2021&lt;/code&gt; using the same process:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;yearlyScores[&apos;2021&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      97&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Name: 2021, dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also see that we have the index column on the left. So, no matter what column you select, the output will always have an index column as a default.&lt;/p&gt;
&lt;p&gt;It’s time to see how we can create a new column and add it to an existing DataFrame.&lt;/p&gt;
&lt;p&gt;I will first create a new Pandas Series, which is technically a column, and then add it to the DataFrame &lt;code&gt;yearlyScores.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Creating a Series from a dictionary:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2019 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; scores2019 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;78&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Kayla&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Muhammad&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;96&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Alexis&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;seriesThree &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(scores2019)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;seriesThree&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake         78&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla        90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad     96&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that, I can add this Series to our DataFrame as a new column:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;yearlyScores[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2019&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; seriesThree&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So we have our usual DataFrame and, then I have used the square brackets to name our new column &lt;code&gt;&apos;2019&apos;&lt;/code&gt;. Then I have assigned the &lt;code&gt;yearlyScores[&apos;2019&apos;]&lt;/code&gt; to the seriesThree object. As a result, it adds a new column to the DataFrame.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021  2019&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        90    85    78&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90    96&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97   100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But before we move further, we have a problem with our DataFrame. And that is, we want column &lt;code&gt;2019&lt;/code&gt; to be at the first instead of last.&lt;/p&gt;
&lt;p&gt;We have to reorder it. And to perform reordering operation, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;yearlyScores = yearlyScores[[&apos;2019&apos;,&apos;2020&apos;,&apos;2021&apos;]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s print it out:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2019  2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        78    90    85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       90    85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    96    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis     100    98    97&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The order of the columns is now changed.&lt;/p&gt;
&lt;p&gt;One of the aspects of Pandas is that when we create DataFrames from Series, it will automatically match the index and add the column.&lt;/p&gt;
&lt;p&gt;You can also delete columns. For instance, I want to delete the column &lt;code&gt;2019&lt;/code&gt; from our DataFrame. And to do that, I can use the &lt;code&gt;del&lt;/code&gt; keyword:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;del yearlyScores[&apos;2019&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Print out &lt;code&gt;yearlyScores&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        90    85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see that column &lt;code&gt;2019&lt;/code&gt; is now gone.&lt;/p&gt;
&lt;p&gt;Often you may also need to delete multiple columns. Therefore, we can do that using the &lt;code&gt;drop()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;Here’s how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;yearlyScores.drop([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2020&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2021&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;axis&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;inplace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, I want to delete the columns &lt;code&gt;2020&lt;/code&gt; &amp;amp; &lt;code&gt;2021&lt;/code&gt;. That’s why I passed them as a list. Since we are dealing with columns, I also set &lt;code&gt;axis=1&lt;/code&gt;. Lastly, I have &lt;code&gt;inplace=True&lt;/code&gt;, which means that we want to make the change permanent. If it is &lt;code&gt;False&lt;/code&gt;, then the &lt;code&gt;drop()&lt;/code&gt; method would return a copy instead of making the change permanent.&lt;/p&gt;
&lt;p&gt;Also, do know that, by default, &lt;code&gt;inplace&lt;/code&gt; is always &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let’s print it out:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Empty DataFrame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Columns: []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Index: [Jake, Kayla, Muhammad, Alexis]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All of our columns are now gone. Python tells us that it is an empty DataFrame.&lt;/p&gt;
&lt;p&gt;Similar to working with columns, you can also select, add and delete rows.&lt;/p&gt;
&lt;p&gt;Since we dropped all of our columns from the DataFrame, we have to recreate &lt;code&gt;yearlyScores&lt;/code&gt; again.&lt;/p&gt;
&lt;p&gt;Use the following code to do that:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2020 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Kayla&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;85&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Muhammad&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;95&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Alexis&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;98&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2021 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;85&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Kayla&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;95&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Muhammad&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Alexis&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;97&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;seriesOne &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(scores2020)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;seriesTwo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series(scores2021)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newDict &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2020&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: seriesOne,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2021&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: seriesTwo}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;yearlyScores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.DataFrame(newDict)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        90    85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, we have our &lt;code&gt;yearlyScores&lt;/code&gt; DataFrame back. I kept all the data the same as before for the sake of this tutorial.&lt;/p&gt;
&lt;h3 id=&quot;working-with-rows&quot;&gt;Working with Rows&lt;/h3&gt;
&lt;p&gt;Okay, let’s see how to work with rows.&lt;/p&gt;
&lt;p&gt;There are two ways you can select a row. It’s either using the index labels or the index values.&lt;/p&gt;
&lt;p&gt;To select rows using labels, I can use the &lt;code&gt;loc&lt;/code&gt; function and pass the row name.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;yearlyScores.loc[&apos;Muhammad&apos;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2020    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2021    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Name: Muhammad, dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result that we got is a Series. Moreover, notice that the column names of our DataFrame became the index labels.&lt;/p&gt;
&lt;p&gt;Then we have the &lt;code&gt;iloc&lt;/code&gt; function to select rows. bypassing the integer-based index value.&lt;/p&gt;
&lt;p&gt;For instance, I want to extract Kayla’s scores. The index value at which her score exists is &lt;code&gt;1&lt;/code&gt;. As a result, I have to pass the index value 1 to the &lt;code&gt;iloc&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Here’s how it works:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;yearlyScores.iloc[1]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2020    85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2021    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Name: Kayla, dtype: int64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Besides, we can also slice multiple rows using the colon operator.
For example, I want to extract all the scores starting from Kayla to Alexis.&lt;/p&gt;
&lt;p&gt;And the procedure is the same as how you would slice a Python list.&lt;/p&gt;
&lt;p&gt;So, I will type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;yearlyScores[1:4]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output is a DataFrame containing the rows that we sliced starting from the index position one and till but not including the index position four.&lt;/p&gt;
&lt;p&gt;Moreover, we can also add new rows to a DataFrame using the &lt;code&gt;append()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;However, I will create a new DataFrame first. You’ll see why in a bit.&lt;/p&gt;
&lt;p&gt;You can create a DataFrame in any way you like. But, I will use Series.&lt;/p&gt;
&lt;p&gt;So, this DataFrame will have two new students with their scores.&lt;/p&gt;
&lt;p&gt;To create the DataFrame, I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Create the Series objects&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2020 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Lori&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;80&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Vince&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;82&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores2021 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.Series({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Lori&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Vince&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;92&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Pass and convert our Series objects to DataFrame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;newStudentsDf &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pd.DataFrame({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2020&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:scores2020,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2021&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:scores2021})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Lori     80    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Vince    82    92&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the end, we have our new DataFrame. So, let’s add rows using &lt;code&gt;newStudentDf&lt;/code&gt; to our previous DataFrame &lt;code&gt;yearlyScores.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Type the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;yearlyScores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; yearlyScores.append(newStudentsDf)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Jake        90    85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Lori        80    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Vince       82    92&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To sum up, first, I created a new DataFrame, &lt;code&gt;newStudentDf.&lt;/code&gt;  This DataFrame holds two new students and their scores. Then, I passed that DataFrame using the &lt;code&gt;append()&lt;/code&gt; function to merge its rows with &lt;code&gt;yearlyScores&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You may need to go over the whole procedure few times to wrap your head around it. In contrast to columns, rows may be complex.&lt;/p&gt;
&lt;p&gt;We are almost at the end of this Pandas tutorial. If you have been able to follow along till this point, then well done! We are almost there.&lt;/p&gt;
&lt;p&gt;The last concept that I want to talk about is deleting rows.&lt;/p&gt;
&lt;p&gt;Deleting rows is similar to deleting columns. Like before, we can use the &lt;code&gt;drop()&lt;/code&gt; function to delete rows in Pandas.&lt;/p&gt;
&lt;p&gt;For instance, I want to delete Jake and Lori’s information from our DataFrame. As simple as it sounds, I will type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;yearlyScores.drop([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Jake&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Lori&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;inplace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(yearlyScores)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have to use the &lt;strong&gt;inplace&lt;/strong&gt; parameter to make our change permanent.&lt;/p&gt;
&lt;p&gt;So, here is what we have:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          2020  2021&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Kayla       85    95&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Muhammad    95    90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Alexis      98    97&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Vince       82    92&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Finally, we are at the end of our Pandas tutorial. I hope you learned some of the valuable concepts on how to work with Pandas.&lt;/p&gt;
&lt;p&gt;I recommend going over the tutorial a few times to understand the concepts vividly. Although, it may take a while. To this day, I still have to practice Pandas regularly to keep up my skills sharp.&lt;/p&gt;
&lt;p&gt;Working with DataFrame can be complex. That’s why follow the order that I have mentioned when it comes to learning Pandas. Besides, the best way to get good at a skill is by practicing.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;How do you plan to use Pandas? Is there any part of this Pandas tutorial that was confusing? What other Python libraries can you think of that are great for data analysis?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>data-analysis</category><category>pandas</category></item><item><title>Reading and Writing Files in Python</title><link>https://my-space.io/blog/reading-writing-files-python/</link><guid isPermaLink="true">https://my-space.io/blog/reading-writing-files-python/</guid><description>Learn how to read and write files in Python: opening files, the access modes, and the file operations you need, with a worked example of each mode.</description><pubDate>Wed, 29 Jul 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A program can be short or long. It can have several functionalities in it, such as asking input from a user and then giving an output. Eventually, when the program terminates, you will lose your results if you don’t save them. That’s why reading and writing files in Python comes in handy.&lt;/p&gt;
&lt;p&gt;Similarly, we may also need to read data or information in our code from an external source. Let’s say you want your program to read data from a text file. That’s why the data needs to be in a format that you can use your application to read later.&lt;/p&gt;
&lt;p&gt;In programming, we use files to store data. And there are two types of files:&lt;/p&gt;
&lt;p&gt;1. Text files: The text files are simple files used for storing standard text. A text file has a .&lt;strong&gt;txt&lt;/strong&gt; extension, and which you can create easily using a notepad.&lt;/p&gt;
&lt;p&gt;2. Bin files: The bin files store data in binary format. A bin file has a .&lt;strong&gt;bin&lt;/strong&gt; extension.&lt;/p&gt;
&lt;p&gt;We can use such files our programs using file operations that Python provides. So in this article, we will discuss how to perform file operations and learn the nuts and bolts of reading and writing files in Python.&lt;/p&gt;
&lt;h2 id=&quot;file-operations-in-python&quot;&gt;File Operations in Python&lt;/h2&gt;
&lt;p&gt;There are two types of file operations when it comes to reading and writing files in Python - &lt;strong&gt;Read&lt;/strong&gt; and &lt;strong&gt;Write&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;1. Read operation: The &lt;strong&gt;read&lt;/strong&gt; operation reads a file. Suppose we have a text file, and we want to use the text in our program. Then, we have to perform the &lt;strong&gt;read&lt;/strong&gt; operation.&lt;/p&gt;
&lt;p&gt;2. Write operation: The &lt;strong&gt;write&lt;/strong&gt; operation writes on a file. When a program terminates, every data associated with it is lost. If we want to store the data, we can use the &lt;strong&gt;write&lt;/strong&gt; operation to save it in a new file, or to an existing one.&lt;/p&gt;
&lt;h2 id=&quot;access-modes&quot;&gt;Access modes&lt;/h2&gt;
&lt;p&gt;The access modes determine why a file is open and what kind of operation we can perform. These modes also define their location.&lt;/p&gt;
&lt;p&gt;One of the key concepts that you have to understand about reading and writing files in Python are the access modes.&lt;/p&gt;
&lt;p&gt;Python has six access modes for accessing files. Let’s discuss each of them with the help of examples.&lt;/p&gt;
&lt;h3 id=&quot;read-only&quot;&gt;Read-only&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;read-only&lt;/strong&gt; mode reads a file, and it is denoted by “r.”&lt;/p&gt;
&lt;p&gt;In simple words, this mode opens an existing file. Python has an in-built function called &lt;strong&gt;open&lt;/strong&gt;(), which we can use to open our file. Python then places a stream at the beginning of the text to start reading from the very first character.&lt;/p&gt;
&lt;p&gt;Think of a stream as a general pointer that points to a particular location on our file.&lt;/p&gt;
&lt;p&gt;Let’s see how to use the &lt;strong&gt;open&lt;/strong&gt;() function to read a file in Python.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;open&lt;/strong&gt;() function takes two parameters - &lt;strong&gt;location&lt;/strong&gt; of the file and the &lt;strong&gt;access mode&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In the end, we have a &lt;strong&gt;close&lt;/strong&gt;() function. It will deallocate the memory assigned to open the file. Also, reset the access mode.&lt;/p&gt;
&lt;p&gt;If the file is present in the same directory in which the program exists, then we can also write the file, else we have to specify the full path.&lt;/p&gt;
&lt;p&gt;Remember, &lt;strong&gt;r&lt;/strong&gt; denotes the &lt;strong&gt;read-only&lt;/strong&gt; mode.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;file1&lt;/strong&gt; is the object for &lt;strong&gt;sample1&lt;/strong&gt;.&lt;strong&gt;txt&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To read the data, Python provides three functions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. read()&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;read&lt;/strong&gt;() function reads a file and returns a string.&lt;/p&gt;
&lt;p&gt;If there is no argument, then it will read the entire file and give you an output.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Moreover, if the argument is a number, the function will read only that many characters.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;read&lt;/strong&gt;() function by itself will read the entire file.&lt;/p&gt;
&lt;p&gt;Let’s read the first ten characters.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;2. readline()&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;readline&lt;/strong&gt;() function is similar to the &lt;strong&gt;read&lt;/strong&gt;() function, but it only reads one line.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;readline: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,file1.readline(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;40&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;read: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,file1.read(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;40&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;readline: This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;read: This is a sample file.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This is a sample&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both &lt;strong&gt;read&lt;/strong&gt;() and &lt;strong&gt;readline&lt;/strong&gt;() functions should read first 40 characters. Let’s see what happens.&lt;/p&gt;
&lt;p&gt;While the &lt;strong&gt;read&lt;/strong&gt;() function successfully reads the first 40 characters, the &lt;strong&gt;readline&lt;/strong&gt;() function only reads the first line.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. readlines()&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;readlines&lt;/strong&gt;() function returns a list of lines in the file.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.readlines())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[&apos;This is a sample file.\n&apos;, &apos;This is a sample file.\n&apos;, &apos;This is a sample file.\n&apos;, &apos;This is a sample file.\n&apos;, &apos;This is a sample file.\n&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Additionally, the &lt;strong&gt;read-only&lt;/strong&gt; mode is the default mode in Python. As you use the &lt;strong&gt;open&lt;/strong&gt;() function, it will still work without passing the &lt;strong&gt;r.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;read--write&quot;&gt;Read &amp;amp; Write&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;read and write&lt;/strong&gt; mode reads and then writes on a file. It is denoted by &lt;strong&gt;r+&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The content that we are trying to add or append will be at the end of a file.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample2.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r+&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Before write: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;After write: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Before write: This is a sample.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;After write: This is a sample. Hello World!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s understand the above code step by step.&lt;/p&gt;
&lt;p&gt;First, the code is opening the file using the &lt;strong&gt;open&lt;/strong&gt;() function. We here have the &lt;strong&gt;read and write&lt;/strong&gt; mode, which is &lt;strong&gt;r+&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Then, there is the &lt;strong&gt;write&lt;/strong&gt;() function to write on the already opened file.  We then used the &lt;strong&gt;seek&lt;/strong&gt;() function and provided &lt;strong&gt;0&lt;/strong&gt; as the argument.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;0&lt;/strong&gt; means the stream will move to the first character.&lt;/p&gt;
&lt;p&gt;At last, there is the &lt;strong&gt;read&lt;/strong&gt;() function to read the file.&lt;/p&gt;
&lt;h3 id=&quot;write-only&quot;&gt;Write-only&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;write-only&lt;/strong&gt; mode allows us to write on a file. This mode has the denotation &lt;strong&gt;w&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Before: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;After: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Before: This is a sample&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;After: Hello world!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We cannot use any other function to read the file when the access mode is &lt;strong&gt;write-only.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After the write operation, the &lt;strong&gt;close&lt;/strong&gt;() function allows us to open the file again using the &lt;strong&gt;read-only&lt;/strong&gt; mode.&lt;/p&gt;
&lt;p&gt;Now, let’s try to read with the &lt;strong&gt;write-only&lt;/strong&gt; mode.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;It will throw an error:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Traceback (most recent call last):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  File &quot;D:\example.py&quot;, line 5, in &amp;lt;module&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    print(file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;io.UnsupportedOperation: not readable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, when the mode is &lt;strong&gt;write-only&lt;/strong&gt;, we cannot read the file.&lt;/p&gt;
&lt;h3 id=&quot;write--read&quot;&gt;Write &amp;amp; Read&lt;/h3&gt;
&lt;p&gt;Like &lt;strong&gt;read and write&lt;/strong&gt; mode, the &lt;strong&gt;write and read&lt;/strong&gt; mode works to read and write on a file. It has a denotation of &lt;strong&gt;w.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;But there are a couple of differences. And try to understand them carefully.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The read and write mode throws an error if the file does not exist while write and read will create a new file if it does not exists.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;demo.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r+&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above code uses the &lt;strong&gt;read and write&lt;/strong&gt; mode to open a file named &lt;strong&gt;demo.txt.&lt;/strong&gt; You can see that the file does not exist.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Traceback (most recent call last):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  File &quot;D:\example.py&quot;, line 5, in &amp;lt;module&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    file1 = open(&quot;demo.txt&quot;, &quot;r+&quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;FileNotFoundError: [Errno 2] No such file or directory: &apos;demo.txt&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It throws an error.&lt;/p&gt;
&lt;p&gt;Now let’s try the same with &lt;strong&gt;write and read&lt;/strong&gt; mode.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;demo.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;w+&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hello World!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It will create a new file and writes on it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The write and read mode overwrites the content of the existing file.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After the above example, &lt;strong&gt;demo&lt;/strong&gt;.&lt;strong&gt;txt&lt;/strong&gt; has “&lt;strong&gt;Hello&lt;/strong&gt; &lt;strong&gt;World&lt;/strong&gt;!”.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;demo.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;w+&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Say Hi&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read())&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The write operation will overwrite “&lt;strong&gt;Hello World!&lt;/strong&gt;” with “&lt;strong&gt;Say Hi&lt;/strong&gt;.”&lt;/p&gt;
&lt;h3 id=&quot;append-only&quot;&gt;Append-only&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;append-only&lt;/strong&gt; mode appends characters at the end of the content. It is denoted by &lt;strong&gt;a.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Before: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;After: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Before: This is a sample.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;After: This is a sample.Hello World!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While the &lt;strong&gt;write-only&lt;/strong&gt; overwrites the content, the &lt;strong&gt;append-only&lt;/strong&gt; mode appends at the end of a line or a character.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;append&lt;/strong&gt; mode will also throw an error if we try to read the file.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Traceback (most recent call last):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  File &quot;D:\example.py&quot;, line 5, in &amp;lt;module&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    print(file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;io.UnsupportedOperation: not readable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;append--read&quot;&gt;Append &amp;amp; Read&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;append and read&lt;/strong&gt; mode is similar to the append-only but with one exception. It can also read the file.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sample1.txt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;a+&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Before:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.seek(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;After: &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, file1.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;file1.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;It will not throw an error&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Before: This is a sample.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;After: This is a sample.Hello World!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;points-to-remember&quot;&gt;Points to Remember&lt;/h2&gt;
&lt;p&gt;There are few functions and six access modes in total for reading and writing files in Python.&lt;/p&gt;
&lt;p&gt;Let’s discuss some of the essential points you should keep in mind while performing read and write operations in Python:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Always be careful while specifying the access mode.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Always close the file using the close() function. It will deallocate the memory.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Use the read() and readline() function carefully. Remember, the readline() function will only read the first line.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. The read and write mode do not create a new file while the write and read mode does.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. The write mode will overwrite the existing content of a file. It is similar in the case of write and read mode.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6. The append mode will append at the last of the content. It is similar in the case of append and read mode.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;7. Always use the seek() function with 0 as the argument if it is required to move the stream to the beginning.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I recommend that you go over this tutorial again and again until you are clear with the modes. This may take a while.&lt;/p&gt;
&lt;p&gt;For structured data, &lt;a href=&quot;/blog/json-with-python/&quot;&gt;JSON with Python&lt;/a&gt; is a natural next step.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Is there any part of this tutorial that is confusing you? Do you like reading and writing files in Python? Are there any mistakes in this article?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>files</category></item><item><title>JSON with Python: Reading &amp; Writing (With Examples)</title><link>https://my-space.io/blog/json-with-python/</link><guid isPermaLink="true">https://my-space.io/blog/json-with-python/</guid><description>How to work with JSON in Python: serialize and de-serialize data with dumps, dump, loads, and load, including custom objects, with examples.</description><pubDate>Sun, 26 Jul 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Modern software development is very much dependent on storing and exchanging data. The last two decades have seen many ways of storing and transferring data. Today, One of the efficient and accessible ways is using JSON with Python.&lt;/p&gt;
&lt;p&gt;JSON stands for Javascript Object Notation. One another popular way is using XML. It isn’t easy to parse, read, and write in XML when compared to JSON. The syntax of JSON comes from Javascript object literals. But JSON is in the text format. You can generate JSON through any programming language and quickly parse it.&lt;/p&gt;
&lt;p&gt;As mentioned above, JSON is javascript objects in text format.&lt;/p&gt;
&lt;p&gt;Observe the following code:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    name : &quot;John&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    age : 25,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    location : &quot;New York&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following is the JSON format of the above data:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;name&quot; : &quot;John&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;age&quot; : 25,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;location&quot; : &quot;New York&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see both are very similar. In the JSON, we write keys inside quotes. JSON files uses the &lt;strong&gt;.json&lt;/strong&gt; extension. You have to store the JSON file with the .json extension.&lt;/p&gt;
&lt;p&gt;The most common use of JSON is exchanging data between a REST API client and a web server.&lt;/p&gt;
&lt;h2 id=&quot;json-with-python&quot;&gt;JSON with Python&lt;/h2&gt;
&lt;p&gt;JSON data is a string. Let’s say you want to convert a Python dictionary. To do that, you have to encode the dictionary as a series of bytes.&lt;/p&gt;
&lt;p&gt;This process is called serialization. Similarly, the reverse of serialization is deserialization. It means, decoding JSON into a python object. Let’s discuss serialization and deserialization in Python using the in-built methods.&lt;/p&gt;
&lt;p&gt;Python also comes with the JSON module.&lt;/p&gt;
&lt;p&gt;To work with JSON in Python, you need to import the JSON module in your python file. You can import the JSON module using the following statement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;serialization&quot;&gt;Serialization&lt;/h2&gt;
&lt;p&gt;Serialization means converting a Python data type into JSON. The JSON encoder only supports data types that are native to Python. These are int, str, bool, float, list, dict, and tuple. You can convert the native data types into JSON.&lt;/p&gt;
&lt;p&gt;The JSON module has two methods for serialization - &lt;code&gt;dumps()&lt;/code&gt; and &lt;code&gt;dump()&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;dumps&quot;&gt;dumps()&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;dumps()&lt;/code&gt; method takes a Python object as a parameter and returns a JSON string. Here’s an example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;location&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;New York&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;js &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.dumps(dc)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(js)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above code, I created the dictionary &lt;code&gt;dc&lt;/code&gt;, and serialized it using &lt;code&gt;dumps()&lt;/code&gt; method using JSON module. Let’s see what is the output:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25, &quot;location&quot;: &quot;New York&quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It returns a JSON object. You may say that a Python dictionary and the returned JSON looks similar.&lt;/p&gt;
&lt;p&gt;Let’s try to convert another data type into JSON.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;js &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.dumps(tp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(js)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This time, instead of dictionary, I am using a tuple.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[1, 2, 3, 4, 5]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our code converts the tuple into a JSON array.&lt;/p&gt;
&lt;p&gt;There are two additional parameters that you can use with the dumps() method - &lt;code&gt;sort_keys&lt;/code&gt; and &lt;code&gt;indent&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Setting the &lt;code&gt;sort_keys&lt;/code&gt; to &lt;code&gt;True&lt;/code&gt; will sort key-value pairs in the result. We get our results in ascending order.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;indent&lt;/code&gt; parameter will customize the indentation of our output according to the value we specify.&lt;/p&gt;
&lt;p&gt;Both of these parameters make the result easily readable.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;location&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;New York&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;js &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.dumps(dc, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sort_keys&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;indent&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(js)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;sort_keys&lt;/code&gt; is set &lt;code&gt;True&lt;/code&gt; and the &lt;code&gt;indent&lt;/code&gt; is 3. Let’s check out the output:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &quot;age&quot;: 25,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &quot;location&quot;: &quot;New York&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &quot;name&quot;: &quot;John&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;dump&quot;&gt;dump()&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;dump()&lt;/code&gt; method also converts a python object into JSON, but it stores the resultant data in a file. Observe the following code:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;location&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; : &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;New York&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;newfile.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;json.dump(dc, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sort_keys&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;indent&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The name of the file is &lt;strong&gt;newfile.json&lt;/strong&gt;, in which we will store the data.&lt;/p&gt;
&lt;p&gt;You can see that we are passing the name of the file as the second parameter.&lt;/p&gt;
&lt;p&gt;Please have a look at the &lt;strong&gt;newfile.json&lt;/strong&gt; file after we ran the code:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &quot;age&quot;: 25,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &quot;location&quot;: &quot;New York&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &quot;name&quot;: &quot;John&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The converted dictionary is present in our JSON file.&lt;/p&gt;
&lt;h2 id=&quot;de-serialization&quot;&gt;De-serialization&lt;/h2&gt;
&lt;p&gt;De-serialization is the reverse process of serialization. It converts encoded JSON data into a Python native data type.&lt;/p&gt;
&lt;p&gt;The JSON module has two methods for de-serialization - &lt;code&gt;loads()&lt;/code&gt; and &lt;code&gt;load().&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;loads&quot;&gt;&lt;strong&gt;loads()&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;loads()&lt;/code&gt; method takes JSON data as the first parameter and returns a python native object. It is the reverse of the method &lt;code&gt;dumps()&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;js &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{ &quot;name&quot; : &quot;John&quot;, &quot;age&quot; : 25, &quot;location&quot; : &quot;New York&quot;}&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.loads(js)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(dc)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I created a JSON object &lt;code&gt;js&lt;/code&gt;. Then passed it to the &lt;code&gt;loads()&lt;/code&gt; method of the JSON module. Note that JSON data is a string.&lt;/p&gt;
&lt;p&gt;And here is our output:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&apos;name&apos;: &apos;John&apos;, &apos;age&apos;: 25, &apos;location&apos;: &apos;New York&apos;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;load&quot;&gt;&lt;strong&gt;load()&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Now let’s come to the method &lt;code&gt;load()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;load()&lt;/code&gt; method is to read JSON from a file and convert it into a native python data type. First, read the file and then pass it to the &lt;code&gt;load()&lt;/code&gt; method.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;newfile.json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.load(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(data)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.close()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After reading the &lt;strong&gt;newfile.json&lt;/strong&gt;, it is passed to the load() method. The variable &lt;code&gt;data&lt;/code&gt; stores the converted value. Printing it gives:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&apos;age&apos;: 25, &apos;location&apos;: &apos;New York&apos;, &apos;name&apos;: &apos;John&apos;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;custom-objects&quot;&gt;Custom Objects&lt;/h2&gt;
&lt;p&gt;Until now, we discussed JSON with Python using in-built Python methods.&lt;/p&gt;
&lt;p&gt;No doubt, these methods are useful, but in a real-time application, you may need to deal with manually created class-based objects. You can also perform serialization and de-serialization on custom objects.&lt;/p&gt;
&lt;p&gt;Observe the following python code:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DemoClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, val1, val2):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.val1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.val2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;obj1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DemoClass(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Value1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Value2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have a class, &lt;code&gt;DemoClass&lt;/code&gt;. &lt;code&gt;The __init__()&lt;/code&gt; method is initializing two attributes &lt;code&gt;val1&lt;/code&gt; and &lt;code&gt;val2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Then, we create an instance of &lt;code&gt;DemoClass&lt;/code&gt;, &lt;code&gt;obj1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let’s serialize this object using the &lt;code&gt;dumps()&lt;/code&gt; method and see what we get.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DemoClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, val1, val2):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.val1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.val2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;obj1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DemoClass(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Value1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Value2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;js &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.dumps(obj1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(js)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It throws an error:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;TypeError: Object of type DemoClass is not JSON serializable&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;According to this error, you can not serialize an object of a manually created class.&lt;/p&gt;
&lt;p&gt;To solve this problem, Python has a &lt;code&gt;__dict__&lt;/code&gt; attribute. Let’s pass &lt;code&gt;obj1.__dict__&lt;/code&gt; to the &lt;code&gt;dumps()&lt;/code&gt; method instead.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DemoClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, val1, val2):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.val1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.val2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; val2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;obj1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DemoClass(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Value1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Value2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;js &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.dumps(obj1.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;__dict__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(js)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s see if it works now:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&quot;val1&quot;: &quot;Value1&quot;, &quot;val2&quot;: &quot;Value2&quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works perfectly.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In modern software development, JSON plays a crucial part.&lt;/p&gt;
&lt;p&gt;Throughout this article, you may have noticed how I stored data in JSON and made conversions.&lt;/p&gt;
&lt;p&gt;Python has handy in-built methods for such serializations and de-serializations. That’s one of the reasons why I love to work JSON with Python so much.&lt;/p&gt;
&lt;p&gt;Your JSON usually ends up in a file, so &lt;a href=&quot;/blog/reading-writing-files-python/&quot;&gt;reading and writing files in Python&lt;/a&gt; is a good next read.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Do you love to handle JSON with Python? What are your thoughts on this article? Do you have any questions?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>files</category><category>json</category></item><item><title>NumPy Tutorial for Beginners: Arrays</title><link>https://my-space.io/blog/numpy-tutorial-arrays/</link><guid isPermaLink="true">https://my-space.io/blog/numpy-tutorial-arrays/</guid><description>Learn NumPy arrays in Python: install NumPy, create arrays from lists, built-in functions and random numbers, and reshape vectors into matrices.</description><pubDate>Mon, 11 Nov 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In this tutorial, we will learn about one of the most powerful scientific computing libraries Numpy. We will also learn what are Numpy’s n-dimensional arrays (ndarrays) and how to create, analyze, and manipulate them using Python.&lt;/p&gt;
&lt;p&gt;To follow this article, you will need basic knowledge of the Python programming language.&lt;/p&gt;
&lt;h2 id=&quot;what-is-numpy&quot;&gt;What is Numpy?&lt;/h2&gt;
&lt;p&gt;Numpy is a scientific computing package used for performing mathematical and logical operations on arrays. And arrays are data structures that store the value of the same data type.&lt;/p&gt;
&lt;p&gt;Numpy itself is written in C. However, Numpy extends Python by integrating powerful data structures by allowing us to use multi-dimensional arrays and matrices.&lt;/p&gt;
&lt;p&gt;One of the reasons why Numpy is so useful for data analysis, machine learning &amp;amp; AI is that it already comes with powerful multidimensional array objects to work with large data sets.&lt;/p&gt;
&lt;h2 id=&quot;installing-numpy&quot;&gt;Installing Numpy&lt;/h2&gt;
&lt;p&gt;To install Numpy I suggest that you download the &lt;a href=&quot;https://www.anaconda.com/distribution/&quot;&gt;Anaconda&lt;/a&gt; distribution of Python, which is an open-source data science platform that comes with Numpy &amp;amp; other Python scientific libraries.&lt;/p&gt;
&lt;p&gt;If you don’t want to use the Anaconda, then you can also install Numpy from your terminal using the following command:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; numpy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;numpy-arrays-from-list&quot;&gt;Numpy Arrays from List&lt;/h2&gt;
&lt;p&gt;Before we jump into Numpy arrays, let’s define again what actually is an array. Well, an array is simply a data structure that contains a group of elements that are all of the same data types.&lt;/p&gt;
&lt;p&gt;A Numpy array is also a data structure similar to an array but they are the core data structure of the whole Numpy library and comes in two forms. They are vectors and matrices.&lt;/p&gt;
&lt;p&gt;Vectors are strictly 1-dimensional arrays. Where matrix or matrices are 2-dimensional arrays that can hold rows and columns.&lt;/p&gt;
&lt;p&gt;One of the most common ways to create a Numpy array from a Python list is by using the &lt;em&gt;np.array()&lt;/em&gt; function. The example below shows how you can create a 1-dimensional Numpy array.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Creating an 1-d array from Python list.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myList &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.array(myList)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Over here we are creating a 1-dimensional Numpy array using the function &lt;em&gt;np.array().&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It may look like a regular Python list, but it’s not. And we can verify that using the &lt;em&gt;type()&lt;/em&gt; function*.*&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;class &apos;numpy.ndarray&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After running the previous line, we see that &lt;em&gt;myArray&lt;/em&gt; is of the type &lt;em&gt;numpy.ndarray.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now, let’s create a 2-d Numpy array.&lt;/p&gt;
&lt;p&gt;In order to create a 2-d array, we have to create a list of lists. And then similar to before, we will pass that list onto &lt;em&gt;&lt;strong&gt;np.array()&lt;/strong&gt;&lt;/em&gt; function to generate our 2-d Numpy array.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Creating an 2-d array from Python list.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myList &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.array(myList)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, what we get is a 2-dimensional array with 3 rows and 3 columns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[[1, 2, 3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [4, 5, 6]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [7, 8, 9]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also check the type of the array to verify that it is of type &lt;em&gt;numpy.ndarray.&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;class &apos;numpy.ndarray&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we see that our 2-d array or matrix is also an object of type &lt;em&gt;numpy.ndarray&lt;/em&gt;. And &lt;em&gt;nd&lt;/em&gt; refers to an n-dimensional or multi-dimensional array.&lt;/p&gt;
&lt;h2 id=&quot;numpy-arrays-using-built-in-functions&quot;&gt;Numpy Arrays Using Built-In Functions&lt;/h2&gt;
&lt;p&gt;Next up on this Numpy tutorial is creating Numpy arrays using built-functions.&lt;/p&gt;
&lt;p&gt;Now that we know, how to create Numpy arrays from regular Python lists, I will go ahead and show you how to create them using built-in functions that Numpy provides.&lt;/p&gt;
&lt;h3 id=&quot;arange&quot;&gt;arange()&lt;/h3&gt;
&lt;p&gt;Numpy has a built-in function called the &lt;em&gt;arange()&lt;/em&gt; function. It is similar to the Python built-in &lt;em&gt;range()&lt;/em&gt; function.&lt;/p&gt;
&lt;p&gt;Let’s say we want to create an array that contains 5 digits from index 0 to 5. We can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myList &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.arange(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myList)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0, 1, 2, 3, 4]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Numpy created an array of 5 digits starting from index 0 to 5.&lt;/p&gt;
&lt;p&gt;Let’s take another example. Say that we want to generate an array starting from 2 to 10.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myList &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.arange(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myList)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[2, 3, 4, 5, 6, 7, 8, 9]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how that our output on the previous line gave us an array starting from 2 till 10. But the 10 is actually not included. If you want to grab 10 as well then you can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myList &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.arange(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myList)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[ 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;arange()&lt;/em&gt; function can also take a third argument which specifies the number of steps or spacing between values.&lt;/p&gt;
&lt;p&gt;For example, if you want to generate all the even numbers between 0 to 10, pass 2 as the third argument. This will tell Python how many steps to take between the values inside your array.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.arange(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[ 0,  2,  4,  6,  8, 10]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All you have to add is to add a step size of 2 to get an array of even numbers. By default, the step size in &lt;em&gt;arange()&lt;/em&gt; function is set to 1.&lt;/p&gt;
&lt;h3 id=&quot;zeros&quot;&gt;zeros()&lt;/h3&gt;
&lt;p&gt;There is also a function known as &lt;em&gt;zeros()&lt;/em&gt; to generate an array of all zeroes. And it looks like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Generating 1-d array of zeros.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.zeros(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What we got here is a 1-dimensional array of 10 zeros. On the other hand, we can also create a 2-d array by passing a tuple.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Generating 2-d array of zeros.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.zeros((&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[[0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0.]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first number that we passed represents the number of rows and the 2nd number represents the number of columns. In this case, we passed (3,3) to generate an array of 3 rows and 3 columns.&lt;/p&gt;
&lt;h3 id=&quot;ones&quot;&gt;ones()&lt;/h3&gt;
&lt;p&gt;The same way we can also generate an array of ones.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Generating 1-d array of ones.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.ones(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[1., 1., 1., 1., 1., 1., 1., 1.]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And for generating a 2-d array of ones:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Generating 2-d array of ones.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.ones((&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[[1., 1., 1.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [1., 1., 1.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [1., 1., 1.]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;linspace&quot;&gt;linspace()&lt;/h3&gt;
&lt;p&gt;There is another useful built-in function that comes with Numpy. We call it the &lt;em&gt;linspace().&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And what it does is, it will return evenly spaced numbers over a specific interval. Let’s say we want 5 evenly spaced points between 1 to 2. For that I will type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;linArr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.linspace(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(linArr)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[1.  , 1.25, 1.5 , 1.75, 2.  ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So our output tells us that, our array starts from 1 and stops at 2. I specified the third argument to be 5 to get 5 evenly spaced points between my start and stop.&lt;/p&gt;
&lt;p&gt;I can also generate an array of 50 evenly spaced points between 1 to 2.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;linArr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.linspace(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(linArr)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[1.        , 1.02040816, 1.04081633, 1.06122449, 1.08163265,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.10204082, 1.12244898, 1.14285714, 1.16326531, 1.18367347,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.20408163, 1.2244898 , 1.24489796, 1.26530612, 1.28571429,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.30612245, 1.32653061, 1.34693878, 1.36734694, 1.3877551 ,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.40816327, 1.42857143, 1.44897959, 1.46938776, 1.48979592,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.51020408, 1.53061224, 1.55102041, 1.57142857, 1.59183673,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.6122449 , 1.63265306, 1.65306122, 1.67346939, 1.69387755,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.71428571, 1.73469388, 1.75510204, 1.7755102 , 1.79591837,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.81632653, 1.83673469, 1.85714286, 1.87755102, 1.89795918,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 1.91836735, 1.93877551, 1.95918367, 1.97959184, 2.]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do remember that our output here is a 1-d vector or 1-d array as we have only one set of square brackets.&lt;/p&gt;
&lt;p&gt;Now here is an important thing, don’t confuse &lt;em&gt;linspace()&lt;/em&gt; with &lt;em&gt;arange()&lt;/em&gt;. The &lt;em&gt;linspace()&lt;/em&gt; gives you evenly spaced precise data points between two values. On the other hand, &lt;em&gt;arange()&lt;/em&gt; takes the third argument that specifies the increments after each value.&lt;/p&gt;
&lt;p&gt;The way you can see this is that you can use &lt;em&gt;linspace()&lt;/em&gt; to get more precise evenly spaced data points between two numbers where &lt;em&gt;arange()&lt;/em&gt; gives you more larger intervals between two numbers.&lt;/p&gt;
&lt;h3 id=&quot;eye&quot;&gt;eye()&lt;/h3&gt;
&lt;p&gt;We can also create an identity matrix using Numpy. An identity matrix is a 2-dimensional square matrix where the number of rows is equal to the number of columns.&lt;/p&gt;
&lt;p&gt;To create an identity matrix, we have to use the function &lt;em&gt;np.eye()&lt;/em&gt; and pass just one argument. It looks something like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;identityMat &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.eye(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(identityMat)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[[1., 0., 0., 0., 0., 0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 1., 0., 0., 0., 0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 1., 0., 0., 0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0., 1., 0., 0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0., 0., 1., 0., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0., 0., 0., 1., 0., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0., 0., 0., 0., 1., 0.],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0., 0., 0., 0., 0., 0., 0., 1.]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output shows that we have a 2-dimensional matrix with 8 tows and 8 columns. And it only consists of 1s and 0s.&lt;/p&gt;
&lt;p&gt;Also, notice how the diagonals are the 1s and the rest are just 0s.&lt;/p&gt;
&lt;h2 id=&quot;numpy-arrays-of-random-numbers&quot;&gt;Numpy Arrays of Random Numbers&lt;/h2&gt;
&lt;p&gt;You can also generate random numbers using Numpy.&lt;/p&gt;
&lt;h3 id=&quot;randomnormal&quot;&gt;random.normal()&lt;/h3&gt;
&lt;p&gt;To generate a random number from the normal distribution you can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;randomNum &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.normal()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(randomNum)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0.4788677086535386&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Obviously the result is most likely to be different for you since Numpy generates this number randomly.&lt;/p&gt;
&lt;p&gt;We can also specify how many random numbers we want from the normal distribution.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;randomNums &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.normal(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(randomNums)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[ 1.80517058, -1.00428261,  0.55339115,  1.28505388, -0.76103492]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have an array of 5 random values from the normal distribution.&lt;/p&gt;
&lt;h3 id=&quot;randomuniform&quot;&gt;random.uniform()&lt;/h3&gt;
&lt;p&gt;Now, let’s say we want values from the uniform distribution. For that you can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;randomNum &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.uniform()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(randomNum)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0.7361103066273562&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the same way we can generate an array of random numbers from the uniform distribution.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;randomNums &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.uniform(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(randomNums)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0.72582994, 0.59795509, 0.47816143, 0.49102028, 0.60275951]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;randomrand&quot;&gt;random.rand()&lt;/h3&gt;
&lt;p&gt;Okay, so what if we want to generate a 2-d matrix with two rows and two columns? I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;randomMat &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.rand(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(randomMat)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0.72914557 0.28649648]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0.57465855 0.55108253]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;randomrandint&quot;&gt;random.randint()&lt;/h3&gt;
&lt;p&gt;One last thing that I want to talk about this post is how to generate a certain amount of random integers between certain values.&lt;/p&gt;
&lt;p&gt;Let’s say I want to generate a Numpy array of four random integers between 1 and 50. I can type:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;randomNums &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.randint (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;low&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;high&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(randomNums)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[68, 23, 23, 81]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I passed three arguments. The first two arguments specify that I want the array to be between 1 &amp;amp; 100. And the last one specifies that I only want four values.&lt;/p&gt;
&lt;h2 id=&quot;vectors-to-matrices&quot;&gt;Vectors to Matrices&lt;/h2&gt;
&lt;p&gt;So far in this Numpy tutorial, we saw how to create 1-dimensional vectors or arrays and 2-d matrices. But there’s one more thing that I want to talk about is converting a vector into a matrix in Numpy.&lt;/p&gt;
&lt;h3 id=&quot;reshape&quot;&gt;reshape()&lt;/h3&gt;
&lt;p&gt;Let’s start by generating a random 1-d array of 16 integers.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.rand(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0.98603364, 0.47461217, 0.12793579, 0.75083417, 0.08093329,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 0.68316509, 0.95807723, 0.89187899, 0.06593721, 0.72183955,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 0.71391102, 0.35244838, 0.4700743 , 0.71870386, 0.78268923,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; 0.73916623]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We only have one set of brackets and thus our output is a 1-d array.&lt;/p&gt;
&lt;p&gt;In order to convert this into a 2-d array or a matrix, we have to use the &lt;em&gt;reshape()&lt;/em&gt; function. And this is how it goes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;myArray &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; myArray.reshape(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(myArray)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[[0.98603364, 0.47461217, 0.12793579, 0.75083417],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0.08093329, 0.68316509, 0.95807723, 0.89187899],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0.06593721, 0.72183955, 0.71391102, 0.35244838],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; [0.4700743 , 0.71870386, 0.78268923, 0.73916623]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So our matrix has 4 rows and 4 columns. Since we have converted a vector with 16 integers, I passed 4 and 4 as arguments. If you have an array of 25 values then you have to pass 5 and 5 as arguments.&lt;/p&gt;
&lt;p&gt;The reason for this is, the &lt;em&gt;reshape()&lt;/em&gt; function can only convert to equal numbers of rows and columns. Since the vector &lt;em&gt;myArray&lt;/em&gt; consists of 16 values, we can only use the &lt;em&gt;reshape ()&lt;/em&gt; function to create a 4x4 matrix.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Well, that’s it for this post. Make sure to go over this post again if any of the concepts confuse you.&lt;/p&gt;
&lt;p&gt;Pandas is built on top of NumPy, so the &lt;a href=&quot;/blog/pandas-tutorial-for-beginners/&quot;&gt;Pandas tutorial for beginners&lt;/a&gt; is a good next step.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Did you find any part of this Numpy tutorial confusing? If so, which part?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
</content:encoded><category>python</category><category>data-analysis</category><category>numpy</category></item></channel></rss>