<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python for Absolute Beginners]]></title><description><![CDATA[Python for Absolute Beginners]]></description><link>https://python-where-my-journey-began.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 09:23:08 GMT</lastBuildDate><atom:link href="https://python-where-my-journey-began.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Abstraction in Python]]></title><description><![CDATA[When we build software, not everything needs to be visible to the user.
Think about using a mobile phone.
You tap an app icon → the app opens.
You don’t see the thousands of lines of code running behind the scenes.
You only see what you need.
This id...]]></description><link>https://python-where-my-journey-began.hashnode.dev/abstraction-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/abstraction-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[oop]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[software development]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Thu, 19 Feb 2026 08:36:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1771490092046/58a4d652-def7-4b02-b1f8-847270da3082.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we build software, not everything needs to be visible to the user.</p>
<p>Think about using a mobile phone.</p>
<p>You tap an app icon → the app opens.</p>
<p>You don’t see the thousands of lines of code running behind the scenes.</p>
<p>You only see what you need.</p>
<p>This idea — <strong>showing only essential details and hiding internal complexity</strong> — is called <strong>Abstraction</strong>.</p>
<p>It is one of the four major pillars of Object-Oriented Programming (OOP).</p>
<hr />
<h1 id="heading-what-is-abstraction">🧠 What is Abstraction?</h1>
<p>Abstraction means:</p>
<blockquote>
<p>Hiding internal implementation details and exposing only the necessary functionality.</p>
</blockquote>
<p>In simple words:</p>
<p>You focus on <strong>what an object does</strong>, not <strong>how it does it</strong>.</p>
<hr />
<h1 id="heading-real-life-example-driving-a-car">🌍 Real-Life Example — Driving a Car</h1>
<p>When you drive a car:</p>
<ul>
<li><p>You press the accelerator → car moves</p>
</li>
<li><p>You press the brake → car stops</p>
</li>
</ul>
<p>You don’t need to understand:</p>
<ul>
<li><p>Engine combustion</p>
</li>
<li><p>Fuel injection system</p>
</li>
<li><p>Mechanical components</p>
</li>
</ul>
<p>The car hides the complexity.</p>
<p>That is abstraction.</p>
<hr />
<h1 id="heading-abstract-classes-the-blueprint-with-rules">🧩 Abstract Classes — The Blueprint with Rules</h1>
<p>An <strong>abstract class</strong> is a special type of class that:</p>
<p>✔ Defines structure<br />✔ Contains abstract methods<br />✔ Cannot be instantiated (no objects allowed)</p>
<p>It acts like a <strong>rulebook</strong> for other classes.</p>
<p>It tells child classes:</p>
<blockquote>
<p>“You must implement these methods.”</p>
</blockquote>
<hr />
<h1 id="heading-abstract-methods-methods-without-implementation">🔧 Abstract Methods — Methods Without Implementation</h1>
<p>An <strong>abstract method</strong> is a method that:</p>
<ul>
<li><p>Is declared</p>
</li>
<li><p>Has no body</p>
</li>
<li><p>Must be implemented in child classes</p>
</li>
</ul>
<p>Example idea:</p>
<p>Every animal must make a sound — but each animal’s sound is different.</p>
<p>So we define the rule, not the behavior.</p>
<hr />
<h1 id="heading-how-python-implements-abstraction">🧑‍💻 How Python Implements Abstraction</h1>
<p>Python provides abstraction using the <strong>abc module</strong>.</p>
<p>We import:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> abc <span class="hljs-keyword">import</span> ABC, abstractmethod
</code></pre>
<p>ABC → Abstract Base Class<br />@abstractmethod → decorator to define abstract methods</p>
<hr />
<h1 id="heading-creating-an-abstract-class-step-by-step">🏗️ Creating an Abstract Class — Step by Step</h1>
<h2 id="heading-step-1-import-required-module">Step 1: Import Required Module</h2>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> abc <span class="hljs-keyword">import</span> ABC, abstractmethod
</code></pre>
<h2 id="heading-step-2-create-abstract-class">Step 2: Create Abstract Class</h2>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>(<span class="hljs-params">ABC</span>):</span>

<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">pass</span>
</code></pre>
<p>Here:</p>
<ul>
<li><p>Animal → abstract class</p>
</li>
<li><p>sound() → abstract method</p>
</li>
<li><p>pass → no implementation</p>
</li>
</ul>
<p>It simply says:</p>
<blockquote>
<p>Every animal must have a sound method.</p>
</blockquote>
<hr />
<h1 id="heading-important-rule-cannot-create-object">⚠️ Important Rule — Cannot Create Object</h1>
<p>You cannot do this:</p>
<pre><code class="lang-python">a = Animal()   <span class="hljs-comment"># ❌ Error</span>
</code></pre>
<p>Why?</p>
<p>Because the class is incomplete.</p>
<p>It only defines rules.</p>
<hr />
<h1 id="heading-creating-child-classes">🐶 Creating Child Classes</h1>
<p>Now we create real implementations.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Dog barks"</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Cat meows"</span>)
</code></pre>
<p>Each child class provides its own implementation.</p>
<hr />
<h1 id="heading-using-the-classes">▶️ Using the Classes</h1>
<pre><code class="lang-python">d = Dog()
c = Cat()

d.sound()
c.sound()
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">Dog barks
Cat meows
</code></pre>
<p>Now everything works perfectly.</p>
<hr />
<h1 id="heading-what-happens-if-we-dont-implement">🚨 What Happens If We Don’t Implement?</h1>
<p>If a child class does not implement the abstract method:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-keyword">pass</span>

d = Dog()
</code></pre>
<p>Python will raise an error.</p>
<p>Because:</p>
<blockquote>
<p>You broke the contract defined by the abstract class.</p>
</blockquote>
<hr />
<h1 id="heading-why-do-we-use-abstraction">🎯 Why Do We Use Abstraction?</h1>
<p>Abstraction helps us:</p>
<p>✔ Reduce complexity<br />✔ Enforce structure<br />✔ Improve maintainability<br />✔ Create scalable architecture<br />✔ Separate interface from implementation</p>
<p>It is heavily used in:</p>
<ul>
<li><p>Frameworks</p>
</li>
<li><p>APIs</p>
</li>
<li><p>Large applications</p>
</li>
<li><p>System design</p>
</li>
</ul>
<hr />
<h1 id="heading-real-life-example-remote-control">🌍 Real-Life Example — Remote Control</h1>
<p>A remote control has buttons:</p>
<ul>
<li><p>power()</p>
</li>
<li><p>volume_up()</p>
</li>
<li><p>volume_down()</p>
</li>
</ul>
<p>But TV, AC, and Projector implement them differently.</p>
<p>Remote → Abstract class<br />Devices → Child classes</p>
<p>The user presses buttons without knowing internal logic.</p>
<p>That is abstraction.</p>
<hr />
<h1 id="heading-abstract-class-vs-normal-class">🔑 Abstract Class vs Normal Class</h1>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Abstract Class</td><td>Normal Class</td></tr>
</thead>
<tbody>
<tr>
<td>Object Creation</td><td>❌ Not allowed</td><td>✅ Allowed</td></tr>
<tr>
<td>Abstract Methods</td><td>✅ Yes</td><td>❌ No</td></tr>
<tr>
<td>Purpose</td><td>Blueprint with rules</td><td>Full implementation</td></tr>
<tr>
<td>Forces Child Implementation</td><td>✅ Yes</td><td>❌ No</td></tr>
</tbody>
</table>
</div><hr />
<h1 id="heading-abstract-method-vs-normal-method">🧠 Abstract Method vs Normal Method</h1>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Abstract Method</strong></td><td><strong>Normal Method</strong></td></tr>
</thead>
<tbody>
<tr>
<td>No body</td><td>Has body</td></tr>
<tr>
<td>Only declaration</td><td>Full logic</td></tr>
<tr>
<td>Must be overridden</td><td>Optional override</td></tr>
<tr>
<td>Used in abstract class</td><td>Used anywhere</td></tr>
</tbody>
</table>
</div><hr />
<h1 id="heading-memory-trick">🚀 Memory Trick</h1>
<p>Abstract = Incomplete Blueprint</p>
<p>Concrete Class = Complete Object</p>
<hr />
<h1 id="heading-when-should-you-use-abstraction">✨ When Should You Use Abstraction?</h1>
<p>Use abstraction when:</p>
<p>✔ Multiple classes share a common structure<br />✔ Implementation differs across classes<br />✔ You want strict rules for developers<br />✔ You are designing large systems</p>
<hr />
<h1 id="heading-practice-questions">🧪 Practice Questions</h1>
<h3 id="heading-beginner-level">Beginner Level</h3>
<ol>
<li><p>Create an abstract class <code>Vehicle</code> with abstract method <code>start()</code></p>
</li>
<li><p>Create child classes <code>Car</code> and <code>Bike</code> implementing <code>start()</code></p>
</li>
<li><p>Print different messages for both</p>
</li>
</ol>
<hr />
<h3 id="heading-intermediate-level">Intermediate Level</h3>
<p>Create an abstract class <code>Payment</code> with:</p>
<ul>
<li><p>pay(amount)</p>
</li>
<li><p>refund(amount)</p>
</li>
</ul>
<p>Implement:</p>
<ul>
<li><p>CreditCardPayment</p>
</li>
<li><p>UPIBasedPayment</p>
</li>
</ul>
<hr />
<h3 id="heading-thinking-question">Thinking Question</h3>
<p>Why is abstraction useful in large software projects?</p>
<hr />
<h1 id="heading-final-thoughts">🏁 Final Thoughts</h1>
<p>If:</p>
<p>Encapsulation protects data 🔒<br />Inheritance shares code 🔁<br />Polymorphism changes behavior 🎭</p>
<p>Then:</p>
<blockquote>
<p><strong>Abstraction hides complexity and enforces structure 🎯</strong></p>
</blockquote>
<p>It allows developers to build powerful systems without exposing unnecessary details.</p>
<hr />
<h1 id="heading-whats-next">🔜 What’s Next?</h1>
<p>👉 Composition vs Inheritance — Which One Should You Use?</p>
<p>That’s where OOP design becomes even more interesting 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Polymorphism in Python]]></title><description><![CDATA[If you’ve been following this series, you already learned about:
✔ Classes and Objects — how we create real-world entities in code✔ Encapsulation — how we protect and control data✔ Inheritance — how child classes reuse parent features
Now comes one o...]]></description><link>https://python-where-my-journey-began.hashnode.dev/polymorphism-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/polymorphism-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[oop]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[programming]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[software development]]></category><category><![CDATA[learning python]]></category><category><![CDATA[coding]]></category><category><![CDATA[Object Oriented Programming]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Thu, 12 Feb 2026 06:21:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1770877200092/ab06db1c-53c8-4733-b5c3-4b6bbb6f017a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you’ve been following this series, you already learned about:</p>
<p>✔ Classes and Objects — how we create real-world entities in code<br />✔ Encapsulation — how we protect and control data<br />✔ Inheritance — how child classes reuse parent features</p>
<p>Now comes one of the most powerful and flexible concepts in Object-Oriented Programming:</p>
<p>👉 <strong>Polymorphism</strong></p>
<p>Don’t worry if the word sounds complicated. The idea behind it is actually very simple — and once you understand it, your code starts to feel much smarter and more natural.</p>
<hr />
<h2 id="heading-what-is-polymorphism">🌍 What is Polymorphism?</h2>
<p>Let’s break the word first:</p>
<p><strong>Poly</strong> = Many<br /><strong>Morphism</strong> = Forms</p>
<p>👉 <strong>Polymorphism means “one thing that can take many forms.”</strong></p>
<p>In programming, this means:</p>
<blockquote>
<p>The same method name can behave differently depending on the object using it.</p>
</blockquote>
<p>Instead of writing completely different functions, we reuse the same method name — but each object responds in its own way.</p>
<hr />
<h2 id="heading-real-life-example">🧠 Real-Life Example</h2>
<p>Think about the action <strong>“speak.”</strong></p>
<ul>
<li><p>A Dog speaks → Bark 🐶</p>
</li>
<li><p>A Cat speaks → Meow 🐱</p>
</li>
<li><p>A Human speaks → Talk 🧑</p>
</li>
</ul>
<p>Same action name — <strong>different behavior</strong>.</p>
<p>That’s exactly what polymorphism does in Python.</p>
<hr />
<h2 id="heading-basic-example-method-overriding">🧑‍💻 Basic Example — Method Overriding</h2>
<p>Polymorphism most commonly appears through <strong>method overriding</strong> (which you saw a bit in inheritance).</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Animal makes a sound"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Dog barks"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Cat meows"</span>)
</code></pre>
<p>Now let’s create objects:</p>
<pre><code class="lang-python">animals = [Dog(), Cat()]

<span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> animals:
    a.speak()
</code></pre>
<h3 id="heading-output">Output:</h3>
<pre><code class="lang-python">Dog barks
Cat meows
</code></pre>
<p>👉 Notice something powerful:</p>
<p>We called the <strong>same method</strong> <code>speak()</code><br />But each object behaved differently.</p>
<p>That’s polymorphism.</p>
<hr />
<h2 id="heading-why-is-polymorphism-useful">🔍 Why is Polymorphism Useful?</h2>
<p>Imagine building a large system like a game or a school application.</p>
<p>Instead of writing:</p>
<pre><code class="lang-python">dog_bark()
cat_meow()
human_talk()
</code></pre>
<p>You simply write:</p>
<pre><code class="lang-python">object.speak()
</code></pre>
<p>Python automatically decides what to do.</p>
<h3 id="heading-benefits">Benefits:</h3>
<p>✔ Cleaner code<br />✔ Less repetition<br />✔ Easier expansion<br />✔ Real-world behavior modeling</p>
<hr />
<h2 id="heading-polymorphism-without-inheritance-duck-typing">⚙️ Polymorphism Without Inheritance (Duck Typing)</h2>
<p>Here’s something interesting about Python:</p>
<p>👉 Polymorphism doesn’t always need inheritance.</p>
<p>If different classes have the same method name, Python still treats them similarly.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">move</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Car drives"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Boat</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">move</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Boat sails"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Plane</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">move</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Plane flies"</span>)
</code></pre>
<p>Now:</p>
<pre><code class="lang-python">vehicles = [Car(), Boat(), Plane()]

<span class="hljs-keyword">for</span> v <span class="hljs-keyword">in</span> vehicles:
    v.move()
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">Car drives
Boat sails
Plane flies
</code></pre>
<p>Python doesn’t care about the class type —<br />it only cares whether the object has a <code>move()</code> method.</p>
<p>This concept is called <strong>Duck Typing</strong>:</p>
<blockquote>
<p>“If it walks like a duck and quacks like a duck, it’s a duck.”</p>
</blockquote>
<hr />
<h2 id="heading-method-overloading-vs-method-overriding">🔄 Method Overloading vs Method Overriding</h2>
<p>Many beginners confuse these two, so let’s clarify clearly.</p>
<h3 id="heading-method-overriding">✔ Method Overriding</h3>
<p>Happens in inheritance.</p>
<p>Child class replaces parent method behavior.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Animal sound"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Dog bark"</span>)
</code></pre>
<p>Same method name — new behavior.</p>
<hr />
<h3 id="heading-method-overloading-in-python">⚠ Method Overloading in Python</h3>
<p>Traditional overloading (same function name with different parameters like Java) is <strong>not directly supported</strong> in Python.</p>
<p>But Python achieves similar flexibility using:</p>
<ul>
<li><p>Default arguments</p>
</li>
<li><p><code>*args</code></p>
</li>
<li><p><code>**kwargs</code></p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Math</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">self, a, b, c=<span class="hljs-number">0</span></span>):</span>
        print(a + b + c)

m = Math()
m.add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
m.add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
</code></pre>
<p>Same method — different usage.</p>
<hr />
<h2 id="heading-real-world-analogy">🎯 Real-World Analogy</h2>
<p>Think about a remote control:</p>
<ul>
<li><p>Press <strong>Play</strong> on TV → Video starts</p>
</li>
<li><p>Press <strong>Play</strong> on Music Player → Song plays</p>
</li>
<li><p>Press <strong>Play</strong> on Game Console → Game resumes</p>
</li>
</ul>
<p>Same button.<br />Different behavior.</p>
<p>That’s polymorphism in everyday life.</p>
<hr />
<h2 id="heading-beginner-practice-questions">🧪 Beginner Practice Questions</h2>
<p>Try these after reading:</p>
<h3 id="heading-1-animal-sounds-practice">1️⃣ Animal Sounds Practice</h3>
<p>Create:</p>
<ul>
<li><p>Parent class <code>Animal</code></p>
</li>
<li><p>Child classes <code>Dog</code>, <code>Cat</code>, <code>Cow</code></p>
</li>
</ul>
<p>Each should override a <code>sound()</code> method.</p>
<hr />
<h3 id="heading-2-shape-area-program">2️⃣ Shape Area Program</h3>
<p>Create classes:</p>
<ul>
<li><p><code>Circle</code></p>
</li>
<li><p><code>Rectangle</code></p>
</li>
<li><p><code>Triangle</code></p>
</li>
</ul>
<p>Each should have a method <code>area()</code> that prints its area.</p>
<p>Call all objects inside a list and loop through them.</p>
<hr />
<h3 id="heading-3-transport-example-duck-typing">3️⃣ Transport Example (Duck Typing)</h3>
<p>Create classes:</p>
<ul>
<li><p>Bike</p>
</li>
<li><p>Car</p>
</li>
<li><p>Train</p>
</li>
</ul>
<p>Each should have a <code>move()</code> method.</p>
<p>Call them using a loop without inheritance.</p>
<hr />
<h2 id="heading-final-thoughts">✨ Final Thoughts</h2>
<p>Polymorphism makes your programs flexible and elegant.</p>
<p>Instead of writing different logic for every object, you define a common action — and let each class decide how to perform it.</p>
<p>If:</p>
<p>✔ Classes are blueprints<br />✔ Inheritance connects them</p>
<p>👉 Then <strong>polymorphism brings them to life</strong> by allowing dynamic behavior.</p>
<hr />
<h2 id="heading-whats-next">🔜 What’s Next?</h2>
<p>Now that you understand:</p>
<ul>
<li><p>Classes &amp; Objects</p>
</li>
<li><p>Encapsulation</p>
</li>
<li><p>Inheritance</p>
</li>
<li><p>Polymorphism</p>
</li>
</ul>
<p>You’re ready for the next pillar of OOP:</p>
<p>👉 <strong>Abstraction in Python — Hiding Complexity, Showing Only What Matters</strong></p>
<p>Stay tuned for Article 6 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Inheritance in Python – Building New Classes from Existing Ones]]></title><description><![CDATA[When we write programs, we often notice something interesting:many objects share common features.
For example:

A Student and a Teacher both have a name and an email.

A Car and a Bike are both vehicles.

A Dog and a Cat are both animals.


Instead o...]]></description><link>https://python-where-my-journey-began.hashnode.dev/inheritance-in-python-building-new-classes-from-existing-ones</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/inheritance-in-python-building-new-classes-from-existing-ones</guid><category><![CDATA[Object Oriented Programming]]></category><category><![CDATA[oop]]></category><category><![CDATA[Inheritance  in python]]></category><category><![CDATA[Python]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Mon, 02 Feb 2026 09:54:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1770025944009/2dfd2936-41da-4a3c-8b98-a1d7d7161235.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write programs, we often notice something interesting:<br />many objects share <strong>common features</strong>.</p>
<p>For example:</p>
<ul>
<li><p>A <strong>Student</strong> and a <strong>Teacher</strong> both have a name and an email.</p>
</li>
<li><p>A <strong>Car</strong> and a <strong>Bike</strong> are both vehicles.</p>
</li>
<li><p>A <strong>Dog</strong> and a <strong>Cat</strong> are both animals.</p>
</li>
</ul>
<p>Instead of rewriting the same code again and again, Python gives us a powerful concept called <strong>Inheritance</strong>.</p>
<hr />
<h2 id="heading-what-is-inheritance">🔍 What is Inheritance?</h2>
<p>Inheritance allows one class to <strong>reuse the properties and methods of another class</strong>.</p>
<ul>
<li><p>The existing class is called the <strong>Parent (Base) class</strong></p>
</li>
<li><p>The new class is called the <strong>Child (Derived) class</strong></p>
</li>
</ul>
<p>👉 In simple words:<br /><strong>A child class “inherits” everything from its parent and can also add its own features.</strong></p>
<hr />
<h2 id="heading-real-life-example">🌍 Real-Life Example</h2>
<p>Think of a <strong>family</strong>:</p>
<ul>
<li><p>Parents pass down traits like surname, habits, or values</p>
</li>
<li><p>Children automatically get them</p>
</li>
<li><p>But children also have their own unique qualities</p>
</li>
</ul>
<p>Programming inheritance works the same way.</p>
<hr />
<h2 id="heading-basic-example-in-python">🧑‍💻 Basic Example in Python</h2>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Animal makes a sound"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bark</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Dog barks"</span>)
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>Animal</code> is the <strong>parent class</strong></p>
</li>
<li><p><code>Dog</code> is the <strong>child class</strong></p>
</li>
<li><p><code>Dog</code> automatically gets access to <code>speak()</code> from <code>Animal</code></p>
</li>
</ul>
<pre><code class="lang-python">dog = Dog()
dog.speak()
dog.bark()
</code></pre>
<p>✅ No code duplication<br />✅ Clean and reusable design</p>
<hr />
<h2 id="heading-why-do-we-use-inheritance">🚀 Why Do We Use Inheritance?</h2>
<p>Inheritance helps us:</p>
<p>✔ Avoid code repetition<br />✔ Improve readability<br />✔ Make programs easier to maintain<br />✔ Follow real-world relationships</p>
<hr />
<h2 id="heading-types-of-inheritance-in-python">🔁 Types of Inheritance in Python</h2>
<h3 id="heading-1-single-inheritance">1️⃣ Single Inheritance</h3>
<p>One child, one parent.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent</span>:</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span>(<span class="hljs-params">Parent</span>):</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<hr />
<h3 id="heading-2-multiple-inheritance">2️⃣ Multiple Inheritance</h3>
<p>One child, multiple parents.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span>:</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Mother</span>:</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span>(<span class="hljs-params">Father, Mother</span>):</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<p>⚠ Use carefully — can become confusing if overused.</p>
<hr />
<h3 id="heading-3-multilevel-inheritance">3️⃣ Multilevel Inheritance</h3>
<p>A chain of inheritance.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Grandparent</span>:</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent</span>(<span class="hljs-params">Grandparent</span>):</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span>(<span class="hljs-params">Parent</span>):</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<hr />
<h3 id="heading-4-hierarchical-inheritance">4️⃣ Hierarchical Inheritance</h3>
<p>Multiple children, one parent.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<hr />
<h2 id="heading-method-overriding-inheritance-polymorphism">🔄 Method Overriding (Inheritance + Polymorphism)</h2>
<p>This is where many learners get confused, so let’s be very clear.</p>
<p>👉 <strong>Overriding happens in inheritance</strong>.</p>
<p>A child class can <strong>change the behavior</strong> of a method inherited from the parent.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Animal sound"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Dog barks"</span>)
</code></pre>
<p>Same method name.<br />Different behavior.</p>
<p>This is called <strong>method overriding</strong>, and it is a form of <strong>polymorphism</strong>.</p>
<hr />
<h2 id="heading-common-misconception">❌ Common Misconception</h2>
<blockquote>
<p>“Method overloading comes under inheritance.”</p>
</blockquote>
<p>❌ Incorrect.</p>
<p>✔ <strong>Overloading</strong> → Polymorphism<br />✔ <strong>Overriding</strong> → Inheritance + Polymorphism</p>
<p>Python doesn’t support traditional method overloading like Java or C++.</p>
<hr />
<h2 id="heading-when-should-you-use-inheritance">🧠 When Should You Use Inheritance?</h2>
<p>Use inheritance when:</p>
<ul>
<li><p>There is a <strong>clear “is-a” relationship</strong></p>
</li>
<li><p>Code can genuinely be reused</p>
</li>
<li><p>You want to extend functionality, not just copy code</p>
</li>
</ul>
<p>Example:</p>
<ul>
<li><p>Dog <strong>is an</strong> Animal ✅</p>
</li>
<li><p>Car <strong>is an</strong> Vehicle ✅</p>
</li>
</ul>
<hr />
<h2 id="heading-final-thoughts">✨ Final Thoughts</h2>
<p>Inheritance is one of the <strong>core pillars of Object-Oriented Programming</strong>.<br />Once you understand it clearly, concepts like <strong>polymorphism</strong> and <strong>abstraction</strong> become much easier.</p>
<p>If classes are the <strong>blueprints</strong>, inheritance is what lets us <strong>build smarter, connected designs instead of isolated ones</strong>.</p>
<hr />
<p>This article is part of my <strong>Python OOP Series</strong>. If you missed the earlier articles, start from Classes &amp; Objects and Encapsulation to build a strong foundation.</p>
<h3 id="heading-whats-next">🔜 What’s Next?</h3>
<p>👉 <strong>Polymorphism in Python – Same Method, Different Behavior</strong></p>
<p>That’s where your OOP journey gets really exciting 🚀</p>
]]></content:encoded></item><item><title><![CDATA[🛡️Encapsulation in Python — Protecting Your Data]]></title><description><![CDATA[In the previous articles, we learned how classes and objects help us structure our programs in a better way.Now, let’s move one step forward and understand another important concept in Object-Oriented Programming — Encapsulation.
Encapsulation is one...]]></description><link>https://python-where-my-journey-began.hashnode.dev/encapsulation-in-python-protecting-your-data</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/encapsulation-in-python-protecting-your-data</guid><category><![CDATA[Python]]></category><category><![CDATA[oop]]></category><category><![CDATA[encapsulation]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Object Oriented Programming]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Fri, 02 Jan 2026 05:06:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1767330311274/1caecb31-47fb-46b7-b745-daba5a8958b1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the previous articles, we learned how <strong>classes and objects</strong> help us structure our programs in a better way.<br />Now, let’s move one step forward and understand another important concept in Object-Oriented Programming — <strong>Encapsulation</strong>.</p>
<p>Encapsulation is one of those topics that sounds complex at first, but once you relate it to real life, it becomes very easy to understand.</p>
<hr />
<h2 id="heading-what-is-encapsulation">🤔 What Is Encapsulation?</h2>
<p>Encapsulation means <strong>binding data and the methods that operate on that data together</strong>, and <strong>restricting direct access to some parts of the data</strong>.</p>
<p>In simple words:</p>
<blockquote>
<p>Encapsulation helps us <strong>protect data</strong> and <strong>control how it is accessed or modified</strong>.</p>
</blockquote>
<p>We don’t allow anyone to change important data directly.<br />Instead, we provide <strong>safe ways</strong> to access or update it.</p>
<hr />
<h2 id="heading-real-life-example-bank-account">🏦 Real-Life Example: Bank Account</h2>
<p>Think about your bank account.</p>
<p>Can you directly go to the bank’s database and change your balance?<br />No.</p>
<p>What can you do instead?</p>
<ul>
<li><p>Deposit money</p>
</li>
<li><p>Withdraw money</p>
</li>
<li><p>Check balance</p>
</li>
</ul>
<p>Your <strong>balance is hidden</strong>, and you can access it only through proper actions.</p>
<p>This is exactly how encapsulation works.</p>
<hr />
<h2 id="heading-bank-account-example-in-python">🧪 Bank Account Example in Python</h2>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BankAccount</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, balance</span>):</span>
        self.__balance = balance   <span class="hljs-comment"># private variable</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_balance</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.__balance

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deposit</span>(<span class="hljs-params">self, amount</span>):</span>
        <span class="hljs-keyword">if</span> amount &gt; <span class="hljs-number">0</span>:
            self.__balance += amount

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">withdraw</span>(<span class="hljs-params">self, amount</span>):</span>
        <span class="hljs-keyword">if</span> amount &lt;= self.__balance:
            self.__balance -= amount
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>__balance</code> is <strong>not accessible directly</strong></p>
</li>
<li><p>The user must use <code>deposit()</code> or <code>withdraw()</code></p>
</li>
<li><p>Rules are applied automatically</p>
</li>
</ul>
<p>This makes the program <strong>safe and realistic</strong>.</p>
<hr />
<h2 id="heading-real-life-example-mobile-phone">📱 Real-Life Example: Mobile Phone</h2>
<p>Think about your mobile phone.</p>
<p>You can:</p>
<ul>
<li><p>Increase or decrease volume</p>
</li>
<li><p>Open apps</p>
</li>
<li><p>Lock the phone</p>
</li>
</ul>
<p>But you cannot:</p>
<ul>
<li><p>Directly control battery internals</p>
</li>
<li><p>Modify hardware settings</p>
</li>
</ul>
<p>The internal system is hidden, and you interact only through buttons and options.</p>
<p>This is encapsulation in real life.</p>
<hr />
<h2 id="heading-real-life-example-student-marks">🏫 Real-Life Example: Student Marks</h2>
<p>In a school system:</p>
<ul>
<li><p>Students should not directly change their marks</p>
</li>
<li><p>Only teachers can update marks</p>
</li>
<li><p>Students can only view them</p>
</li>
</ul>
<hr />
<h3 id="heading-python-example">Python Example</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, marks</span>):</span>
        self.name = name
        self.__marks = marks

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_marks</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.__marks

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_marks</span>(<span class="hljs-params">self, new_marks</span>):</span>
        <span class="hljs-keyword">if</span> <span class="hljs-number">0</span> &lt;= new_marks &lt;= <span class="hljs-number">100</span>:
            self.__marks = new_marks
</code></pre>
<p>Here:</p>
<ul>
<li><p>Marks are protected</p>
</li>
<li><p>Invalid values are prevented</p>
</li>
<li><p>Data remains consistent</p>
</li>
</ul>
<hr />
<h2 id="heading-public-protected-and-private-variables">🔐 Public, Protected, and Private Variables</h2>
<p>Python controls access using naming conventions.</p>
<h3 id="heading-public-variable">Public Variable</h3>
<pre><code class="lang-python">self.name
</code></pre>
<p>Accessible anywhere.</p>
<hr />
<h3 id="heading-protected-variable">Protected Variable</h3>
<pre><code class="lang-python">self._email
</code></pre>
<p>Should not be accessed directly, but still possible.</p>
<hr />
<h3 id="heading-private-variable">Private Variable</h3>
<pre><code class="lang-python">self.__password
</code></pre>
<p>Strongly protected and used for sensitive data.</p>
<hr />
<h2 id="heading-difference-between-public-protected-and-private-variables">🔍 Difference Between Public, Protected, and Private Variables</h2>
<p>Now that we’ve seen all three types, let’s clearly understand <strong>how they differ</strong>, especially in <strong>initialization and access</strong>.</p>
<h3 id="heading-public-variables">🟢 Public Variables</h3>
<p>Public variables are created normally and can be accessed <strong>from anywhere</strong>.</p>
<h4 id="heading-example">Example:</h4>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        self.name = name   <span class="hljs-comment"># public variable</span>
</code></pre>
<p>Usage:</p>
<pre><code class="lang-python">u = User(<span class="hljs-string">"Alice"</span>)
print(u.name)   <span class="hljs-comment"># Accessible</span>
</code></pre>
<p>👉 Public variables are best used when the data is <strong>safe to expose</strong>, like names or IDs.</p>
<hr />
<h3 id="heading-protected-variables">🟡 Protected Variables</h3>
<p>Protected variables are defined using a <strong>single underscore</strong> <code>_</code>.</p>
<h4 id="heading-example-1">Example:</h4>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, email</span>):</span>
        self._email = email   <span class="hljs-comment"># protected variable</span>
</code></pre>
<p>Usage:</p>
<pre><code class="lang-python">u = User(<span class="hljs-string">"abc@gmail.com"</span>)
print(u._email)   <span class="hljs-comment"># Accessible, but not recommended</span>
</code></pre>
<p>👉 Protected variables <strong>can be accessed</strong>, but by convention:</p>
<ul>
<li><p>They are meant to be used <strong>inside the class or its child classes</strong></p>
</li>
<li><p>Developers understand: <em>“Use this carefully”</em></p>
</li>
</ul>
<hr />
<h3 id="heading-private-variables">🔴 Private Variables</h3>
<p>Private variables use <strong>double underscore</strong> <code>__</code>.</p>
<h4 id="heading-example-2">Example:</h4>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, password</span>):</span>
        self.__password = password   <span class="hljs-comment"># private variable</span>
</code></pre>
<p>Usage:</p>
<pre><code class="lang-python">u = User(<span class="hljs-string">"secret123"</span>)
<span class="hljs-comment"># print(u.__password)   ❌ Error</span>
</code></pre>
<p>Private variables:</p>
<ul>
<li><p>Cannot be accessed directly</p>
</li>
<li><p>Are internally renamed by Python (name mangling)</p>
</li>
<li><p>Must be accessed using methods</p>
</li>
</ul>
<p>Correct way:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, password</span>):</span>
        self.__password = password

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_password</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.__password
</code></pre>
<hr />
<h2 id="heading-whats-the-real-difference">🧠 What’s the Real Difference?</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Type</td><td>Syntax</td><td>Direct Access</td><td>Intended Use</td></tr>
</thead>
<tbody>
<tr>
<td>Public</td><td><a target="_blank" href="http://self.name"><code>self.name</code></a></td><td>✅ Yes</td><td>Open data</td></tr>
<tr>
<td>Protected</td><td><code>self._email</code></td><td>⚠️ Yes (not recommended)</td><td>Internal use</td></tr>
<tr>
<td>Private</td><td><code>self.__password</code></td><td>❌ No</td><td>Sensitive data</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-important-note-about-private-variables">⚠️ Important Note About Private Variables</h2>
<p>To make a variable <strong>truly private in Python</strong>, you must use <strong>double underscore (</strong><code>__</code>), not a single underscore.</p>
<h3 id="heading-this-is-not-private">❌ This is NOT private:</h3>
<pre><code class="lang-python">self._password
</code></pre>
<p>This is only <strong>protected</strong>, not private.<br />It can still be accessed directly.</p>
<hr />
<h3 id="heading-this-is-private">✅ This IS private:</h3>
<pre><code class="lang-python">self.__password
</code></pre>
<p>This tells Python to:</p>
<ul>
<li><p>Apply name mangling</p>
</li>
<li><p>Prevent direct access</p>
</li>
<li><p>Treat the variable as internal to the class</p>
</li>
</ul>
<hr />
<h3 id="heading-why-double-underscore-matters">🔍 Why Double Underscore Matters</h3>
<p>Python uses the <strong>double underscore</strong> to change the variable name internally.</p>
<p>Example:</p>
<pre><code class="lang-python">self.__password
</code></pre>
<p>Internally becomes:</p>
<pre><code class="lang-python">_User__password
</code></pre>
<p>This is why:</p>
<pre><code class="lang-python">user.__password   <span class="hljs-comment"># ❌ Error</span>
</code></pre>
<p>But:</p>
<pre><code class="lang-python">user._User__password   <span class="hljs-comment"># ⚠️ Technically possible, but not recommended</span>
</code></pre>
<p>👉 This mechanism exists to <strong>discourage misuse</strong>, not to break your code.</p>
<hr />
<h2 id="heading-simple-rule-to-remember">🧠 Simple Rule to Remember</h2>
<ul>
<li><p><code>_variable</code> → Protected (use carefully)</p>
</li>
<li><p><code>__variable</code> → Private (use double underscore only)</p>
</li>
<li><p>Never assume <code>_</code> means private — <strong>it does not</strong></p>
</li>
</ul>
<h2 id="heading-key-takeaway">💡 Key Takeaway</h2>
<ul>
<li><p><strong>Public</strong> → Anyone can access</p>
</li>
<li><p><strong>Protected</strong> → Access allowed, but use carefully</p>
</li>
<li><p><strong>Private</strong> → Access only through methods</p>
</li>
</ul>
<p>This system helps us <strong>control data</strong>, <strong>prevent misuse</strong>, and <strong>write professional code</strong>.</p>
<h2 id="heading-why-encapsulation-is-important">⚠️ Why Encapsulation Is Important</h2>
<p>Without encapsulation, this could happen:</p>
<pre><code class="lang-python">account.balance = <span class="hljs-number">-5000</span>
</code></pre>
<p>Which makes no sense.</p>
<p>Encapsulation prevents:</p>
<ul>
<li><p>Invalid data</p>
</li>
<li><p>Accidental changes</p>
</li>
<li><p>Logical errors</p>
</li>
</ul>
<hr />
<h2 id="heading-important-point-to-remember">🧠 Important Point to Remember</h2>
<p>Encapsulation does not mean hiding everything.</p>
<p>It means:</p>
<ul>
<li><p>Hide important data</p>
</li>
<li><p>Allow controlled access using methods</p>
</li>
</ul>
<hr />
<h2 id="heading-why-encapsulation-matters-in-real-projects">💡 Why Encapsulation Matters in Real Projects</h2>
<p>Encapsulation helps in:</p>
<ul>
<li><p>Writing clean code</p>
</li>
<li><p>Protecting data</p>
</li>
<li><p>Avoiding bugs</p>
</li>
<li><p>Managing large applications</p>
</li>
<li><p>Working in teams</p>
</li>
</ul>
<p>That’s why encapsulation is used in <strong>real-world software systems</strong>.</p>
<hr />
<h2 id="heading-practice-questions">📝 Practice Questions</h2>
<p>Try answering these:</p>
<ol>
<li><p>What is encapsulation in simple words?</p>
</li>
<li><p>Why should data not be accessed directly?</p>
</li>
<li><p>Create a <code>User</code> class with a private password.</p>
</li>
<li><p>What is the difference between public and private variables?</p>
</li>
</ol>
<hr />
<h2 id="heading-conclusion">🏁 Conclusion</h2>
<p>In this article, we learned:</p>
<ul>
<li><p>What encapsulation is</p>
</li>
<li><p>Why data protection is important</p>
</li>
<li><p>How Python implements encapsulation</p>
</li>
<li><p>How real-life systems use encapsulation</p>
</li>
</ul>
<p>Encapsulation helps us write <strong>secure, logical, and maintainable code</strong>.</p>
<hr />
<h3 id="heading-whats-next">🔜 What’s Next?</h3>
<p>In the next article, we’ll learn about <strong>Inheritance in Python</strong> — how one class can reuse and extend another class.</p>
<p>👉 <strong>Article 4: Inheritance in Python — Reusing Code Efficiently</strong></p>
<p>Keep learning 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Classes and Objects in Python — Bringing Code to Life]]></title><description><![CDATA[If you’ve read the first article of this series, you already know what Object-Oriented Programming (OOP) is and how it differs from the traditional procedural approach.Now, let’s take the next step — understanding the heart of OOP: Classes and Object...]]></description><link>https://python-where-my-journey-began.hashnode.dev/classes-and-objects-in-python-bringing-code-to-life</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/classes-and-objects-in-python-bringing-code-to-life</guid><category><![CDATA[Python]]></category><category><![CDATA[oop]]></category><category><![CDATA[##OOP in Python]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Programming Tips]]></category><category><![CDATA[Object Oriented Programming]]></category><category><![CDATA[python beginner]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Sun, 23 Nov 2025 17:36:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1763917739284/27b541e7-9078-4129-9185-c0ebe28b3eb1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you’ve read the first article of this series, you already know what <em>Object-Oriented Programming (OOP)</em> is and how it differs from the traditional procedural approach.<br />Now, let’s take the next step — understanding the heart of OOP: <strong>Classes</strong> and <strong>Objects</strong>.</p>
<p>Think of this as the point where your code stops being just lines of instructions and starts acting like real-world entities.</p>
<hr />
<h2 id="heading-what-are-classes-and-objects">🧩 What Are Classes and Objects?</h2>
<p>Let’s start with a simple question:</p>
<blockquote>
<p>How do you describe a “car” in real life?</p>
</blockquote>
<p>You might say it has a color, brand, speed, and it can drive or stop.<br />In OOP, we represent such things as <strong>objects</strong> — something that has <strong>data (attributes)</strong> and <strong>actions (methods)</strong>.</p>
<p>But before we can make an object, we need a <strong>class</strong> — a blueprint that tells Python what an object should look like.</p>
<hr />
<h2 id="heading-class-the-blueprint">🏗️ Class — The Blueprint</h2>
<p>A <strong>class</strong> is like a recipe or a template.<br />It defines what data (attributes) and actions (methods) every object of that type will have.</p>
<p>For example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, color, speed</span>):</span>
        self.color = color
        self.speed = speed

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">drive</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"Driving the <span class="hljs-subst">{self.color}</span> car at <span class="hljs-subst">{self.speed}</span> km/h"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">brake</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"The <span class="hljs-subst">{self.color}</span> car has stopped."</span>)
</code></pre>
<p>Here’s what’s happening:</p>
<ul>
<li><p><strong>class Car: —</strong> defines a blueprint called Car.</p>
</li>
<li><p><strong>__init__() —</strong> a special method called the constructor, automatically runs when you create an object.</p>
</li>
<li><p><strong>self —</strong> refers to the current object being created.</p>
</li>
<li><p><strong>color and speed —</strong> attributes (data).</p>
</li>
<li><p><strong>drive() and brake() —</strong> methods (behaviors).</p>
</li>
</ul>
<hr />
<h2 id="heading-object-the-real-instance">🚗 Object — The Real Instance</h2>
<p>Now that we have a blueprint, we can create actual cars!</p>
<pre><code class="lang-python">my_car = Car(<span class="hljs-string">"Red"</span>, <span class="hljs-number">120</span>)
friend_car = Car(<span class="hljs-string">"Blue"</span>, <span class="hljs-number">100</span>)

my_car.drive()
friend_car.brake()
</code></pre>
<p>Each object (my_car, friend_car) has its own copy of attributes.<br />They both come from the same blueprint (Car), but they can have different colors, speeds, and behaviors.</p>
<p>So, in simple words:</p>
<blockquote>
<p><strong>Class → Design,</strong></p>
<p><strong>Object → Real thing created using that design.</strong></p>
</blockquote>
<hr />
<h2 id="heading-a-simpler-example-the-student-class">🎓 A Simpler Example — The Student Class</h2>
<p>Let’s represent something more relatable: a student.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, grade</span>):</span>
        self.name = name
        self.grade = grade

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display_info</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"Student: <span class="hljs-subst">{self.name}</span>, Grade: <span class="hljs-subst">{self.grade}</span>"</span>)

s1 = Student(<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"A"</span>)
s2 = Student(<span class="hljs-string">"Bob"</span>, <span class="hljs-string">"B"</span>)

s1.display_info()
s2.display_info()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Student: Alice, Grade: A  
Student: Bob, Grade: B
</code></pre>
<p>Here:</p>
<ul>
<li><p>Each student has their own data (name, grade).</p>
</li>
<li><p>The method display_info() defines how that data is displayed.</p>
</li>
<li><p>Both s1 and s2 are independent objects created from the same Student class.</p>
</li>
</ul>
<hr />
<h2 id="heading-understanding-the-self-keyword-improved-amp-beginner-friendly">⚙️ Understanding the <code>self</code> Keyword (Improved &amp; Beginner-Friendly)</h2>
<p>If there's one part of classes that confuses beginners the most, it’s this little word: <strong>self</strong>.<br />But don’t worry — once you understand it, classes instantly make sense.</p>
<h3 id="heading-what-is-self">👉 What is self?</h3>
<p><strong><em>self</em></strong> simply refers to the <strong>current object</strong> that is being created or used.</p>
<p>Think of it like this:</p>
<ul>
<li><p>When you make <strong>my_car = Car("Red", 120)</strong><br />  → <em>self becomes my_car inside the class</em></p>
</li>
<li><p>When you make <strong>friend_car = Car("Blue", 100)</strong><br />  → <em>self becomes friend_car</em></p>
</li>
</ul>
<p>So each object gets its own separate data, and <code>self</code> helps Python know <strong>which object we are talking about</strong>.</p>
<hr />
<h2 id="heading-a-simple-analogy">🧠 A Simple Analogy</h2>
<p>Imagine a class as a form with blank fields:</p>
<p><strong>Student Form</strong></p>
<ul>
<li><p>Name: ___</p>
</li>
<li><p>Grade: ___</p>
</li>
</ul>
<p>Each student fills in their own details.<br />Inside the system:</p>
<ul>
<li><p>When Alice fills the form → <strong>self refers to Alice</strong></p>
</li>
<li><p>When Bob fills the form → <strong>self refers to Bob</strong></p>
</li>
</ul>
<p>So <a target="_blank" href="http://self.name"><code>self.name</code></a> is Alice’s name when Alice is the object,<br />and <a target="_blank" href="http://self.name"><code>self.name</code></a> is Bob’s name when Bob is the object.</p>
<hr />
<h2 id="heading-why-is-self-needed">🔍 Why is <code>self</code> needed?</h2>
<p>Because inside a class, Python must know:</p>
<ul>
<li><p>Which object's <code>name</code>?</p>
</li>
<li><p>Which object's <code>grade</code>?</p>
</li>
<li><p>Which object's <code>color</code>?</p>
</li>
<li><p>Which object's <code>speed</code>?</p>
</li>
</ul>
<p>If we didn’t have <code>self</code>, every object would get confused with others.</p>
<hr />
<h2 id="heading-lets-look-at-a-clear-example">🧪 Let's Look at a Clear Example</h2>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Hello from"</span>, self)
</code></pre>
<p>When we do:</p>
<pre><code class="lang-python">obj = Example()
obj.show()
</code></pre>
<p>Python actually converts it internally to:</p>
<pre><code class="lang-plaintext">Example.show(obj)
</code></pre>
<p>So <code>self</code> is automatically assigned the object <strong>obj</strong>.</p>
<hr />
<h2 id="heading-in-short">📝 In Short</h2>
<ul>
<li><p><code>self</code> represents the <strong>object calling the method</strong>.</p>
</li>
<li><p>It connects the <strong>class blueprint</strong> to the <strong>actual object in memory</strong>.</p>
</li>
<li><p>Without <code>self</code>, your class wouldn’t know which object’s data to access.</p>
</li>
</ul>
<hr />
<h2 id="heading-why-do-we-need-classes-and-objects">🧠 Why Do We Need Classes and Objects?</h2>
<p>Without classes, our code becomes messy as we try to manage data and behavior separately.<br />Using classes gives us several advantages:</p>
<ol>
<li><p><strong>Modularity</strong> – Each class focuses on a single purpose (like <code>Car</code>, <code>Student</code>, etc.).</p>
</li>
<li><p><strong>Reusability</strong> – Once you write a class, you can reuse it in multiple programs.</p>
</li>
<li><p><strong>Scalability</strong> – You can easily expand your code by adding more classes or features.</p>
</li>
<li><p><strong>Real-World Mapping</strong> – Classes make your code feel natural, as they represent real-world entities.</p>
</li>
</ol>
<hr />
<h2 id="heading-analogy-why-this-matters">💡 Analogy: Why This Matters</h2>
<p>Imagine a school management system.<br />You’ll have:</p>
<ul>
<li><p>A <code>Student</code> class (name, roll number, grade)</p>
</li>
<li><p>A <code>Teacher</code> class (subject, experience)</p>
</li>
<li><p>A <code>Course</code> class (title, duration)</p>
</li>
</ul>
<p>Each one has unique data and behaviors.<br />When you create an object, you’re basically saying, <em>“Here’s a real example of that class.”</em><br />This modular design makes complex systems much easier to manage.</p>
<hr />
<h2 id="heading-conclusion">🏁 Conclusion</h2>
<p>In this article, you learned:</p>
<ul>
<li><p>What classes and objects are.</p>
</li>
<li><p>How to define a class using the <code>class</code> keyword.</p>
</li>
<li><p>How to create and use objects.</p>
</li>
<li><p>Why <code>self</code> is important.</p>
</li>
<li><p>And how classes make your programs more structured and realistic.</p>
</li>
</ul>
<p>Classes and objects are the foundation of OOP.<br />Now that you know how to create them, you’re ready to move on to the next exciting topic — <strong>Encapsulation</strong> — where we’ll learn how to protect and control access to data inside classes.</p>
<p>Stay tuned for <strong>Article 3: “Encapsulation in Python — Protecting Your Data”</strong> 🔒</p>
<hr />
<h2 id="heading-practice-questions-test-your-understanding">📝 Practice Questions — Test Your Understanding</h2>
<p>Here are some simple yet meaningful exercises to strengthen what you learned in this article.</p>
<h3 id="heading-1-create-a-class-called-book"><strong>1️⃣ Create a class called</strong> <code>Book</code></h3>
<p>Write a Python class that represents a book with:</p>
<ul>
<li><p>title</p>
</li>
<li><p>author</p>
</li>
<li><p>price</p>
</li>
</ul>
<p>Add a method <strong>display_info()</strong> that prints the book's details.</p>
<p>Then create two book objects and display their information.</p>
<hr />
<h3 id="heading-2-build-a-phone-class"><strong>2️⃣ Build a</strong> <code>Phone</code> class</h3>
<p>Your class should have:</p>
<ul>
<li><p>brand</p>
</li>
<li><p>model</p>
</li>
<li><p>battery_percentage</p>
</li>
</ul>
<p>Add two methods:</p>
<ul>
<li><p><strong>charge()</strong> → prints “Charging…”</p>
</li>
<li><p><strong>status()</strong> → prints battery percentage</p>
</li>
</ul>
<p>Create an object and call these methods.</p>
<hr />
<h3 id="heading-3-create-a-pet-class"><strong>3️⃣ Create a</strong> <code>Pet</code> class</h3>
<p>Attributes:</p>
<ul>
<li><p>name</p>
</li>
<li><p>animal_type (ex: dog, cat)</p>
</li>
</ul>
<p>Methods:</p>
<ul>
<li><p><strong>sound()</strong> → print a generic sound:</p>
<ul>
<li><p>“Bark” for dog</p>
</li>
<li><p>“Meow” for cat</p>
</li>
<li><p>Something else for others</p>
</li>
</ul>
</li>
</ul>
<p>Hint: Use <strong>if–elif</strong> inside the method.</p>
<hr />
<h3 id="heading-4-trace-the-value-of-self"><strong>4️⃣ Trace the value of</strong> <code>self</code></h3>
<p>Given:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Demo</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show</span>(<span class="hljs-params">self</span>):</span>
        print(self)

d1 = Demo()
d2 = Demo()

d1.show()
d2.show()
</code></pre>
<p>❓ What do you think the output represents?<br />(Explain how Python internally treats <code>self</code>.)</p>
<hr />
<h3 id="heading-5-write-a-class-movie"><strong>5️⃣ Write a class</strong> <code>Movie</code></h3>
<p>Attributes:</p>
<ul>
<li><p>name</p>
</li>
<li><p>year</p>
</li>
<li><p>rating</p>
</li>
</ul>
<p>Method:</p>
<ul>
<li><p><strong>is_hit()</strong> → prints</p>
<ul>
<li><p>“Hit movie!” if rating &gt; 8</p>
</li>
<li><p>“Average movie” if rating between 5 and 8</p>
</li>
<li><p>“Flop movie” otherwise</p>
</li>
</ul>
</li>
</ul>
<p>Create three objects and test your method.</p>
<hr />
<h3 id="heading-6-predict-the-output"><strong>6️⃣ Predict the Output</strong></h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, x</span>):</span>
        self.x = x

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update</span>(<span class="hljs-params">self, value</span>):</span>
        self.x = value

t1 = Test(<span class="hljs-number">5</span>)
t2 = Test(<span class="hljs-number">10</span>)

t1.update(<span class="hljs-number">20</span>)
print(t1.x)
print(t2.x)
</code></pre>
<p>❓ What will be printed?<br />(Explain why the two objects behave independently.)</p>
<hr />
<h3 id="heading-7-create-your-own-real-world-class"><strong>7️⃣ Create your own real-world class</strong></h3>
<p>Pick anything from your daily life:</p>
<ul>
<li><p>Laptop</p>
</li>
<li><p>Bag</p>
</li>
<li><p>Coffee Machine</p>
</li>
<li><p>Bicycle</p>
</li>
<li><p>Teacher</p>
</li>
<li><p>Game Character</p>
</li>
</ul>
<p>Define at least:</p>
<ul>
<li><p>2 attributes</p>
</li>
<li><p>2 methods</p>
</li>
</ul>
<p>and then create two objects of that class.</p>
<p>This exercise helps you think in an object-oriented way.</p>
]]></content:encoded></item><item><title><![CDATA[Introduction to Object-Oriented Programming (OOP) in Python — A Beginner’s Guide with Real-Life Examples)]]></title><description><![CDATA[🌍 Once Upon a Time in the World of Programming...
Imagine you’re creating a game.You have players, enemies, weapons, and magical potions.Each of these has attributes (like health, power, name) and behaviors (like attack, heal, move).
Now, how do we ...]]></description><link>https://python-where-my-journey-began.hashnode.dev/introduction-to-oop-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/introduction-to-oop-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Object Oriented Programming]]></category><category><![CDATA[CodingTutorial]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Learning Journey]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Sat, 11 Oct 2025 04:36:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1760156647525/e78aa89c-2b66-4125-a772-4cad96031c20.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-once-upon-a-time-in-the-world-of-programming">🌍 <strong>Once Upon a Time in the World of Programming...</strong></h3>
<p>Imagine you’re creating a game.<br />You have players, enemies, weapons, and magical potions.<br />Each of these has <strong>attributes</strong> (like health, power, name) and <strong>behaviors</strong> (like attack, heal, move).</p>
<p>Now, how do we manage all this in code?</p>
<p>If we just used variables and functions everywhere, our code would become messy — a <em>jungle</em> of logic and repetition.</p>
<p>That’s when the idea of <strong>Object-Oriented Programming (OOP)</strong> was born — to make code organized, reusable, and real-world-like.</p>
<hr />
<h2 id="heading-what-is-object-oriented-programming-oop">💡 <strong>What is Object-Oriented Programming (OOP)?</strong></h2>
<p><strong>Object-Oriented Programming</strong> is a programming approach where we model the real world using <strong>classes</strong> and <strong>objects</strong>.</p>
<p>Instead of writing loose code, we group related data and actions together.</p>
<p>You can think of it as:</p>
<blockquote>
<p>Classes are blueprints.<br />Objects are actual things built from those blueprints.</p>
</blockquote>
<hr />
<h3 id="heading-analogy-class-and-object">🏠 <strong>Analogy: Class and Object</strong></h3>
<p>Think of a <strong>Class</strong> like an architect’s blueprint for a house.<br />It defines the design — how the house will look, how many doors, windows, etc.</p>
<p>An <strong>Object</strong> is the <em>actual house</em> built using that blueprint.</p>
<p>Similarly, in Python:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">House</span>:</span>
    <span class="hljs-keyword">pass</span>

my_house = House()
</code></pre>
<p>Here:</p>
<ul>
<li><p>House is a <strong>class</strong></p>
</li>
<li><p>my_house is an <strong>object (or instance)</strong> of that class</p>
</li>
</ul>
<hr />
<h2 id="heading-why-do-we-need-oop">🧱 <strong>Why Do We Need OOP?</strong></h2>
<p>Before OOP, code was mostly <em>procedural</em> — we just wrote functions and called them in sequence.</p>
<p>But as programs got bigger, we needed:</p>
<ul>
<li><p>Reusability ♻️</p>
</li>
<li><p>Organization 🗂️</p>
</li>
<li><p>Readability 👀</p>
</li>
<li><p>Real-world representation 🧍‍♀️💻</p>
</li>
</ul>
<p>OOP solved all of these by letting us <strong>group data (variables)</strong> and <strong>behavior (functions)</strong> together in one unit — the <strong>object</strong>.</p>
<hr />
<h2 id="heading-oop-concepts-at-a-glance">🧩 <strong>OOP Concepts at a Glance</strong></h2>
<p>There are <strong>4 pillars</strong> (core concepts) that form the foundation of OOP:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Concept</td><td>Meaning</td><td>Example</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Encapsulation</strong></td><td>Bundling data &amp; methods together</td><td>A class containing variables and functions</td></tr>
<tr>
<td><strong>Abstraction</strong></td><td>Hiding unnecessary details</td><td>Using .sort() without knowing its inner code</td></tr>
<tr>
<td><strong>Inheritance</strong></td><td>Reusing code from another class</td><td>Child inherits properties from Parent</td></tr>
<tr>
<td><strong>Polymorphism</strong></td><td>One thing behaving differently in different contexts</td><td>Same function name working for multiple objects</td></tr>
</tbody>
</table>
</div><p>We’ll explore each one deeply in the upcoming articles — but for now, let’s learn the basics.</p>
<hr />
<h2 id="heading-creating-a-class-in-python">🧱 <strong>Creating a Class in Python</strong></h2>
<p>A <strong>class</strong> defines how an object will look and behave.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, age</span>):</span>
        self.name = name
        self.age = age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">introduce</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"Hi, I'm <span class="hljs-subst">{self.name}</span> and I'm <span class="hljs-subst">{self.age}</span> years old."</span>)
</code></pre>
<p>Here:</p>
<ul>
<li><p>__init()__ is a <strong>constructor</strong> — it automatically runs when you create a new object.</p>
</li>
<li><p>self refers to the <em>current object</em>.</p>
</li>
<li><p>introduce() is a method (a function that belongs to a class).</p>
</li>
</ul>
<hr />
<h2 id="heading-creating-an-object">🧍‍♀️ <strong>Creating an Object</strong></h2>
<p>Now, let’s create objects (instances) of the class:</p>
<pre><code class="lang-python">student1 = Student(<span class="hljs-string">"Prajitha"</span>, <span class="hljs-number">20</span>)
student2 = Student(<span class="hljs-string">"John"</span>, <span class="hljs-number">21</span>)

student1.introduce()
student2.introduce()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Hi, I'm Prajitha and I'm 20 years old.
Hi, I'm John and I'm 21 years old.
</code></pre>
<p>🎯 Each object has its own copy of the data (name, age) but shares the same method structure.</p>
<hr />
<h2 id="heading-understanding-init-and-self">⚙️ <strong>Understanding</strong> __init()__ and self</h2>
<p>When you write:</p>
<pre><code class="lang-python">student1 = Student(<span class="hljs-string">"Prajitha"</span>, <span class="hljs-number">20</span>)
</code></pre>
<p>Python internally calls:</p>
<pre><code class="lang-python">Student.__init__(student1, <span class="hljs-string">"Prajitha"</span>, <span class="hljs-number">20</span>)
</code></pre>
<p>That’s why the first parameter is always self — it refers to the <em>current instance</em> being created.</p>
<p>Think of self like saying <em>“this particular object.”</em></p>
<hr />
<h2 id="heading-adding-methods-behaviors">💬 <strong>Adding Methods (Behaviors)</strong></h2>
<p>You can add multiple methods to define what an object can <em>do</em>:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand, color</span>):</span>
        self.brand = brand
        self.color = color

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_engine</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"<span class="hljs-subst">{self.brand}</span> is starting..."</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show_details</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"Car Brand: <span class="hljs-subst">{self.brand}</span>, Color: <span class="hljs-subst">{self.color}</span>"</span>)
</code></pre>
<p>Now create an object:</p>
<pre><code class="lang-python">car1 = Car(<span class="hljs-string">"Tesla"</span>, <span class="hljs-string">"Red"</span>)
car1.start_engine()
car1.show_details()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Tesla is starting...
Car Brand: Tesla, Color: Red
</code></pre>
<hr />
<h2 id="heading-modifying-attributes">🧰 <strong>Modifying Attributes</strong></h2>
<p>Objects can have their data updated later:</p>
<pre><code class="lang-python">car1.color = <span class="hljs-string">"Blue"</span>
car1.show_details()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Car Brand: Tesla, Color: Blue
</code></pre>
<hr />
<h2 id="heading-class-vs-instance-variables">🪄 <strong>Class vs Instance Variables</strong></h2>
<p>Let’s go a bit deeper 🔍</p>
<h3 id="heading-instance-variable">Instance Variable</h3>
<p>Belongs to a specific object.</p>
<pre><code class="lang-python">self.name = <span class="hljs-string">"Prajitha"</span>
</code></pre>
<h3 id="heading-class-variable">Class Variable</h3>
<p>Shared by <em>all</em> objects of that class.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    college = <span class="hljs-string">"Aditya Degree College"</span>  <span class="hljs-comment"># class variable</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        self.name = name  <span class="hljs-comment"># instance variable</span>
</code></pre>
<pre><code class="lang-python">s1 = Student(<span class="hljs-string">"Prajitha"</span>)
s2 = Student(<span class="hljs-string">"John"</span>)

print(s1.college)  <span class="hljs-comment"># Aditya Degree College</span>
print(s2.college)  <span class="hljs-comment"># Aditya Degree College</span>
</code></pre>
<hr />
<h2 id="heading-recap-what-you-learned-so-far">🧠 <strong>Recap — What You Learned So Far</strong></h2>
<p>You’ve now learned:<br />✅ What OOP is and why we use it<br />✅ What classes and objects are<br />✅ How constructors (__init__) work<br />✅ The purpose of self<br />✅ How to define methods and variables inside classes</p>
<p>You’ve officially taken your first step into the <strong>OOP universe</strong> 🌌</p>
<hr />
<h2 id="heading-next-in-the-series">🚀 <strong>Next in the Series</strong></h2>
<p>In <strong>Article 2</strong>, we’ll explore more about<br />👉 <strong>Classes, Objects and constructors.</strong></p>
<hr />
<h2 id="heading-final-thought">✨ <strong>Final Thought</strong></h2>
<blockquote>
<p>“Procedural code makes you think in steps,<br />OOP makes you think in systems — just like the real world.”</p>
</blockquote>
<p>The world around you is made of objects — from your phone to your laptop to your car.<br />Now you’ve learned how to represent them in code. 🌍💻</p>
]]></content:encoded></item><item><title><![CDATA[Exception Handling in Python]]></title><description><![CDATA[1. INTRODUCTION
Even after we develop a project and test it thoroughly, there might be cases we miss. If such errors occur when the program is running, we don’t want our clients or users to see a scary error message. Instead, we want to handle it gra...]]></description><link>https://python-where-my-journey-began.hashnode.dev/exception-handling-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/exception-handling-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[exceptionhandling]]></category><category><![CDATA[error handling]]></category><category><![CDATA[PythonForBeginners]]></category><category><![CDATA[Programming Tips]]></category><category><![CDATA[learn python]]></category><category><![CDATA[Developer]]></category><category><![CDATA[coding journey]]></category><category><![CDATA[100DaysOfCode]]></category><category><![CDATA[bug fixing]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Mon, 29 Sep 2025 06:43:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1759128160425/bb206278-003a-4c5d-936f-8cbf368e6790.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-1-introduction">1. INTRODUCTION</h2>
<p>Even after we develop a project and test it thoroughly, there might be cases we miss. If such errors occur when the program is running, we don’t want our clients or users to see a scary error message. Instead, we want to handle it gracefully and display a message of our choice.</p>
<p>This is where Exception Handling comes into play.</p>
<p>👉 It is a safe way to deal with errors without crashing the program.</p>
<hr />
<h2 id="heading-2-what-is-an-exception">2. WHAT IS AN EXCEPTION?</h2>
<p>An exception is an error that happens during the execution of a program. Unlike syntax errors (which stop the program before running), exceptions occur while the program is running.</p>
<p>Some common examples are:</p>
<p><strong>ZeroDivisionError</strong> → when we try to divide a number by zero.</p>
<p><strong>FileNotFoundError</strong> → when the program tries to open a file that doesn’t exist.</p>
<p><strong>TypeError</strong> → when an operation is applied to an object of an inappropriate type.</p>
<hr />
<h2 id="heading-3-basic-try-and-except">3. BASIC TRY AND EXCEPT</h2>
<p>The try-except block is the foundation of exception handling.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    <span class="hljs-comment"># risky code</span>

<span class="hljs-keyword">except</span> SomeError:
    <span class="hljs-comment"># handling code</span>
</code></pre>
<p><strong>Example: Dividing by zero</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    a = int(input(<span class="hljs-string">"Enter A value: "</span>))
    b = int(input(<span class="hljs-string">"Enter B value: "</span>))
    result = a / b
    print(result)

<span class="hljs-keyword">except</span> ZeroDivisionError:
    print(<span class="hljs-string">"We can't divide a number by zero"</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Enter A value: 5
Enter B value: 0
We can't divide a number by zero
</code></pre>
<hr />
<h2 id="heading-4-multiple-except-blocks">4. MULTIPLE EXCEPT BLOCKS</h2>
<p>We can handle different types of errors separately.</p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    a = int(input(<span class="hljs-string">"Enter A value: "</span>))
    b = int(input(<span class="hljs-string">"Enter B value: "</span>))
    result = a / b
    print(result)

<span class="hljs-keyword">except</span> ZeroDivisionError:
    print(<span class="hljs-string">"Error: Division by zero is not allowed."</span>)

<span class="hljs-keyword">except</span> ValueError:
    print(<span class="hljs-string">"Error: Please enter only numbers."</span>)
</code></pre>
<hr />
<h2 id="heading-5-using-else-block">5. USING ELSE BLOCK</h2>
<p>The else block runs only if no error occurs in the try block.</p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    num = int(input(<span class="hljs-string">"Enter a number: "</span>))
    print(<span class="hljs-string">"Square:"</span>, num ** <span class="hljs-number">2</span>)

<span class="hljs-keyword">except</span> ValueError:
    print(<span class="hljs-string">"Please enter a valid number."</span>)

<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"No errors occurred!"</span>)
</code></pre>
<hr />
<h2 id="heading-6-finally-block">6. FINALLY BLOCK</h2>
<p>The finally block runs no matter what — whether there is an exception or not. It is useful for cleanup tasks like closing a file or database connection.</p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    file = open(<span class="hljs-string">"sample.txt"</span>, <span class="hljs-string">"r"</span>)
    content = file.read()

<span class="hljs-keyword">except</span> FileNotFoundError:
    print(<span class="hljs-string">"File not found!"</span>)

<span class="hljs-keyword">finally</span>:
    print(<span class="hljs-string">"Execution completed."</span>)
</code></pre>
<hr />
<h2 id="heading-7-raising-exceptions">7. RAISING EXCEPTIONS</h2>
<p>We can raise our own exceptions using the raise keyword.</p>
<pre><code class="lang-python">age = int(input(<span class="hljs-string">"Enter your age: "</span>))
<span class="hljs-keyword">if</span> age &lt; <span class="hljs-number">0</span>:
    <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">"Age cannot be negative"</span>)

<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"Valid age entered"</span>)
</code></pre>
<hr />
<h2 id="heading-8-custom-exceptions">8. CUSTOM EXCEPTIONS</h2>
<p>Sometimes the built-in exceptions like ValueError, TypeError, or ZeroDivisionError may not be enough for our program’s needs. In those cases, we can define our <strong>own exceptions</strong>.</p>
<p>A <strong>custom exception</strong> is nothing but a user-defined error type. We create it by writing a class that inherits from the built-in Exception class. This makes our code more readable and helps us provide <strong>specific error messages</strong> for special situations.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidAgeError</span>(<span class="hljs-params">Exception</span>):</span>
    <span class="hljs-keyword">pass</span>

age = int(input(<span class="hljs-string">"Enter your age: "</span>))
<span class="hljs-keyword">if</span> age &lt; <span class="hljs-number">18</span>:
    <span class="hljs-keyword">raise</span> InvalidAgeError(<span class="hljs-string">"You must be 18 or older to register"</span>)
</code></pre>
<hr />
<h2 id="heading-9-why-is-it-important">9. WHY IS IT IMPORTANT?</h2>
<p>✔ Prevents sudden crashes</p>
<p>✔ Makes debugging easier</p>
<p>✔ Provides user-friendly error messages</p>
<p>✔ Essential in file handling, user input validation, and working with APIs</p>
<p>✔ Helps in building reliable applications</p>
<hr />
<h2 id="heading-10-practice-questions">10. PRACTICE QUESTIONS</h2>
<p>Handle division by zero error with a custom message.</p>
<p>Write a program to read a file and handle the case if the file doesn’t exist.</p>
<p>Create a custom exception for invalid password length.</p>
<hr />
<h2 id="heading-11-conclusion">11. CONCLUSION</h2>
<p>Exception handling makes Python programs strong, reliable, and crash-proof. It ensures that even if errors occur, our application handles them gracefully.</p>
]]></content:encoded></item><item><title><![CDATA[File Handling in Python Explained with Examples | Beginner’s Guide]]></title><description><![CDATA[1. INTRODUCTION
File handling is a very crucial and important concept when we want to store user data. For example, if we develop a website and want to store the information of each and every user and their details, then we can use this concept of fi...]]></description><link>https://python-where-my-journey-began.hashnode.dev/file-handling-in-python-explained-with-examples-beginners-guide</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/file-handling-in-python-explained-with-examples-beginners-guide</guid><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[File handling]]></category><category><![CDATA[python programming]]></category><category><![CDATA[coding tips]]></category><category><![CDATA[Developer]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[python tutorial]]></category><category><![CDATA[Programming basics]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Mon, 15 Sep 2025 12:07:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1757938004657/66a117b7-7112-47f8-9361-91a02996331f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-1-introduction">1. INTRODUCTION</h2>
<p>File handling is a very crucial and important concept when we want to store user data. For example, if we develop a website and want to store the information of each and every user and their details, then we can use this concept of file handling.</p>
<p>We can create files in different formats such as text files, CSV files, Excel files, or JSON files. File handling helps us to store the user data permanently. This means that even if the user exits our program, the data will not be lost unless we explicitly delete the file. Once we write code for file handling, the program can automatically store and manage information.</p>
<p>We have different types of file handling modes such as reading, writing/overwriting, appending, and even deleting a file.</p>
<hr />
<h2 id="heading-2-opening-files">2. OPENING FILES</h2>
<p>To open a file, we use the open() function.</p>
<p><strong>Syntax</strong></p>
<pre><code class="lang-python">file = open(<span class="hljs-string">"file_name"</span>, file_mode)
</code></pre>
<p>Here, file mode refers to the action we want to perform. The common modes are:</p>
<p>"r" → read (default mode)</p>
<p>"w" → write (overwrites existing content)</p>
<p>"a" → append (adds content at the end of file)</p>
<p>"x" → create a new file (gives error if file already exists)</p>
<hr />
<h2 id="heading-3-file-properties">3. FILE PROPERTIES</h2>
<p>After opening a file, we can check its properties using different methods:</p>
<p>f.name → returns the name of the file</p>
<p>f.mode → returns the current file mode</p>
<p>f.closed → returns True if the file is closed, else False</p>
<p>f.close() → closes the file</p>
<hr />
<h2 id="heading-4-reading-files">4. READING FILES</h2>
<p>When reading a file, there are three main methods:</p>
<p>read() → Reads the entire file at once.</p>
<p>readline() → Reads one line at a time.</p>
<p>readlines() → Reads all lines into a list.</p>
<p>Example file: practice.txt</p>
<pre><code class="lang-plaintext">Hello
This is a text file
It is an example for read() function
It reads entire file
</code></pre>
<p><strong>Example 1 – read()</strong></p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"practice.txt"</span>, <span class="hljs-string">"r"</span>)
content = f.read()
print(content)
f.close()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Hello
This is a text file
It is an example for read() function
It reads entire file
</code></pre>
<p><strong>Example 2 – readline()</strong></p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"practice.txt"</span>, <span class="hljs-string">"r"</span>)
print(f.readline())
f.close()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Hello
</code></pre>
<p><strong>Example 3 – multiple readline()</strong></p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"practice.txt"</span>, <span class="hljs-string">"r"</span>)
print(f.readline())
print(f.readline())
f.close()
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Hello
This is a text file
</code></pre>
<hr />
<h2 id="heading-5-writing-files"><strong>5. WRITING FILES</strong></h2>
<p>To write into a file, we use:</p>
<p>write() → Writes a single string into the file.</p>
<p>writelines() → Writes multiple lines into the file.</p>
<p>⚠️ Note: If we open a file in "w" mode, it will erase all existing content and start fresh.</p>
<p>Example:</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"practice.txt"</span>, <span class="hljs-string">"w"</span>)
f.write(<span class="hljs-string">"This text will overwrite the file content."</span>)
f.close()
</code></pre>
<hr />
<h2 id="heading-6-appending-to-files">6. APPENDING TO FILES</h2>
<p>Appending allows us to add new content to the end of the file without deleting the existing data.</p>
<p>Example:</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"practice.txt"</span>, <span class="hljs-string">"a"</span>)
f.write(<span class="hljs-string">"\nThis line is added at the end."</span>)
f.close()
</code></pre>
<hr />
<h2 id="heading-7-closing-files">7. CLOSING FILES</h2>
<p>It’s important to close a file after operations to free up system resources.</p>
<pre><code class="lang-python">f.close()
</code></pre>
<p>If we forget to close, Python may still keep it open in memory, which can cause errors in bigger applications.</p>
<hr />
<h2 id="heading-8-using-with-statement">8. USING with STATEMENT</h2>
<p>Instead of manually closing a file, we can use a with statement which automatically closes it once the block ends.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> open(<span class="hljs-string">"practice.txt"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> f:
content = f.read()
print(content)
<span class="hljs-comment"># file is automatically closed here</span>
</code></pre>
<p>This is considered the best practice.</p>
<hr />
<h2 id="heading-9-working-with-different-file-types">9. WORKING WITH DIFFERENT FILE TYPES</h2>
<p>Python makes it easy to work with different file types:</p>
<p>Text Files → read/write using normal methods.</p>
<p>CSV Files → use the csv module.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> csv
<span class="hljs-keyword">with</span> open(<span class="hljs-string">"data.csv"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> f:
    reader = csv.reader(f)
    <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> reader:
        print(row)
</code></pre>
<p>JSON Files → use the json module.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
data = {<span class="hljs-string">"name"</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-string">"age"</span>: <span class="hljs-number">22</span>}
<span class="hljs-keyword">with</span> open(<span class="hljs-string">"data.json"</span>, <span class="hljs-string">"w"</span>) <span class="hljs-keyword">as</span> f:
    json.dump(data, f)
</code></pre>
<hr />
<h2 id="heading-10-practice-questions">10. PRACTICE QUESTIONS</h2>
<p>Try solving these on your own 👇</p>
<ol>
<li><p>Read a text file and count how many words are in it.</p>
</li>
<li><p>Write student names and marks into a file.</p>
</li>
<li><p>Append daily logs with date and time.</p>
</li>
<li><p>Convert a Python dictionary into JSON and save it.</p>
</li>
<li><p>Read a CSV file and print each row.</p>
</li>
</ol>
<hr />
<h2 id="heading-11-conclusion">11. CONCLUSION</h2>
<p>File handling allows Python programs to interact with the outside world.</p>
<p>It is useful for:</p>
<p>—&gt; Storing user data permanently</p>
<p>—&gt; Automating tasks</p>
<p>—&gt; Handling logs</p>
<p>—&gt; Building real-world applications</p>
<p>By learning how to open, read, write, and manage files, we make our programs more powerful and practical.</p>
]]></content:encoded></item><item><title><![CDATA[Modules and Packages in Python Explained for Beginners]]></title><description><![CDATA[1. INTRODUCTION
When we write small programs, everything fits into one file. But as our project grows, writing all the code in a single place becomes confusing and hard to manage.
That’s where modules and packages in Python come to the rescue.
Module...]]></description><link>https://python-where-my-journey-began.hashnode.dev/modules-and-packages-in-python-explained-for-beginners</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/modules-and-packages-in-python-explained-for-beginners</guid><category><![CDATA[Python]]></category><category><![CDATA[modules]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[package manager]]></category><category><![CDATA[learn coding]]></category><category><![CDATA[#codenewbies]]></category><category><![CDATA[software development]]></category><category><![CDATA[Developer]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Tue, 09 Sep 2025 06:41:14 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1757357293758/1e0eb1a4-55ed-495a-bc32-b29c69174756.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-1-introduction">1. INTRODUCTION</h2>
<p>When we write small programs, everything fits into one file. But as our project grows, writing all the code in a single place becomes confusing and hard to manage.</p>
<p>That’s where modules and packages in Python come to the rescue.</p>
<p>Module → A Python file (.py) that contains functions, variables, or classes.</p>
<p>Package → A collection of modules grouped together inside a folder with an __init__.py file.</p>
<p>📘 Think of it like this:</p>
<p>A module is like a single chapter in a book.</p>
<p>A package is like the whole book with multiple chapters.</p>
<p>Both are meant to keep our code organized, reusable, and easier to share.</p>
<hr />
<h2 id="heading-2-why-do-we-need-modules">2. WHY DO WE NEED MODULES?</h2>
<p>Imagine you are building a calculator. First, you write an add function. Later, you need subtraction, multiplication, division, etc. If you put everything inside one file, it becomes messy.</p>
<p>Instead, you can create a module called calculator.py that contains all the math-related functions. Later, whenever you want to use it, just import it into another program.</p>
<p><strong>Benefits of modules &amp; packages:</strong></p>
<p>✅ Avoids code duplication.</p>
<p>✅ Makes projects structured and readable.</p>
<p>✅ Allows teamwork (different people can work on different modules).</p>
<p>✅ Easy to debug and maintain.</p>
<p>✅ Shareable across projects.</p>
<hr />
<h2 id="heading-3-importing-built-in-modules">3. IMPORTING BUILT-IN MODULES</h2>
<p>Python already comes with many built-in modules that we can use directly.</p>
<p><strong>Example using the math module:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> math
print(math.sqrt(<span class="hljs-number">16</span>)) <span class="hljs-comment"># Square root</span>
print(math.factorial(<span class="hljs-number">5</span>)) <span class="hljs-comment"># Factorial of 5</span>
print(math.pi) <span class="hljs-comment"># Value of Pi</span>
</code></pre>
<p><strong>Example using the random module:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> random
print(random.randint(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)) <span class="hljs-comment"># Random number between 1 and 10</span>
print(random.choice([<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>])) <span class="hljs-comment"># Randomly picks one</span>
</code></pre>
<p><strong>Example using the datetime module:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> datetime
today = datetime.date.today()
print(<span class="hljs-string">"Today's date:"</span>, today)
</code></pre>
<p>💡 These built-in modules save a lot of time since we don’t need to write everything from scratch.</p>
<hr />
<h2 id="heading-4-different-ways-to-import">4. DIFFERENT WAYS TO IMPORT</h2>
<p>There are multiple ways to bring a module into our code:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> math
print(math.sqrt(<span class="hljs-number">25</span>)) <span class="hljs-comment"># Using full module name</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> sqrt
print(sqrt(<span class="hljs-number">25</span>)) <span class="hljs-comment"># Directly using the function</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> math <span class="hljs-keyword">as</span> m
print(m.sqrt(<span class="hljs-number">36</span>)) <span class="hljs-comment"># Using alias</span>
</code></pre>
<p>👉 Choosing between these depends on readability. If we use only 1–2 functions, from ... import ... makes sense. If we use many, import module is better.</p>
<hr />
<h2 id="heading-5-creating-your-own-module">5. CREATING YOUR OWN MODULE</h2>
<p>We can create our own custom module too.</p>
<p><strong>Example:</strong> Create a file mymodule.py with:</p>
<pre><code class="lang-python"><span class="hljs-comment"># mymodule.py</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>):</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">f"Hello, <span class="hljs-subst">{name}</span>!"</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>):</span>
<span class="hljs-keyword">return</span> a + b
</code></pre>
<p>Now in another Python file:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> mymodule
print(mymodule.greet(<span class="hljs-string">"Prajitha"</span>))
print(mymodule.add(<span class="hljs-number">5</span>, <span class="hljs-number">3</span>))
</code></pre>
<p>💡 This makes your functions reusable across multiple projects.</p>
<hr />
<h2 id="heading-6-packages">6. PACKAGES</h2>
<p>Modules are good for small programs. But what if your project has dozens of modules?</p>
<p>That’s where packages help.</p>
<p>A package is simply a folder with multiple modules and a file called __init__.py.</p>
<p><strong>Example project structure:</strong></p>
<pre><code class="lang-python">mypackage/

__init__.py

calculator.py

greetings.py
</code></pre>
<p><strong>Inside calculator.py:</strong></p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>):</span>
    <span class="hljs-keyword">return</span> a + b
</code></pre>
<p><strong>Inside greetings.py:</strong></p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say_hello</span>():</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello from greetings!"</span>
</code></pre>
<p>Now, you can use them:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> mypackage <span class="hljs-keyword">import</span> calculator, greetings
    print(calculator.add(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>))
    print(greetings.say_hello())
</code></pre>
<p>📦 Packages are very useful in large applications where code needs to be modular.</p>
<hr />
<h2 id="heading-7-installing-external-packages">7. INSTALLING EXTERNAL PACKAGES</h2>
<p>Apart from built-in modules and our own modules, Python has a huge library of third-party packages created by developers worldwide. These packages are stored in PyPI (Python Package Index). We install them using pip.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">pip install requests
</code></pre>
<p><strong>Usage in code:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
response = requests.get(<span class="hljs-string">"https://api.github.com"</span>)
print(response.status_code) <span class="hljs-comment"># Output: 200</span>
</code></pre>
<p>💡 Famous Python packages:</p>
<p>numpy → numerical operations</p>
<p>pandas → data analysis</p>
<p>matplotlib → data visualization</p>
<p>flask → web applications</p>
<p>requests → APIs and web requests</p>
<hr />
<h2 id="heading-8-practice-questions">8. PRACTICE QUESTIONS</h2>
<p>Try these on your own 👇</p>
<ol>
<li><p>Import the datetime module and print today’s date.</p>
</li>
<li><p>Create your own module with a function that checks whether a number is prime.</p>
</li>
<li><p>Write a program using the random module to simulate rolling a dice.</p>
</li>
<li><p>Create a package shapes with circle.py and square.py that calculate areas.</p>
</li>
<li><p>Install the emoji package (pip install emoji) and print a smiling emoji.</p>
</li>
</ol>
<hr />
<h2 id="heading-9-conclusion">9. CONCLUSION</h2>
<p>🔑 Key takeaways:</p>
<p>Modules = Single file with reusable code.</p>
<p>Packages = Collection of modules inside a folder.</p>
<p>Built-in modules like math, random, datetime make life easier.</p>
<p>External packages from PyPI make Python powerful and versatile.</p>
<p>👉 Mastering modules and packages is essential if you want to move from small scripts to real-world scalable projects.</p>
]]></content:encoded></item><item><title><![CDATA[Functions in Python: A Beginner’s Guide to Writing Reusable Code]]></title><description><![CDATA[1. INTRODUCTION
Functions are used when we want to group a block of code and reuse it multiple times. Instead of writing the same code again and again, we can just create a function and call it whenever needed.
👉 Example from real life: If you use a...]]></description><link>https://python-where-my-journey-began.hashnode.dev/functions-in-python-a-beginners-guide-to-writing-reusable-code</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/functions-in-python-a-beginners-guide-to-writing-reusable-code</guid><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[#Python-functions]]></category><category><![CDATA[functions]]></category><category><![CDATA[beginnersguide]]></category><category><![CDATA[#codingNewbies]]></category><category><![CDATA[developer-journey]]></category><category><![CDATA[software development]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Thu, 04 Sep 2025 16:39:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756830566849/c3494565-8bd7-471d-8532-48306db91361.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-1-introduction">1. INTRODUCTION</h2>
<p>Functions are used when we want to group a block of code and reuse it multiple times. Instead of writing the same code again and again, we can just create a function and call it whenever needed.</p>
<p>👉 Example from real life: If you use a calculator, you don’t need to build “add” or “subtract” again and again. The calculator already has those functions. Similarly, in Python, we can define functions once and use them whenever we need.</p>
<hr />
<h2 id="heading-2-creating-a-function">2. CREATING A FUNCTION</h2>
<p><strong>Syntax –</strong></p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">function_name</span>():</span>
    <span class="hljs-comment"># code block</span>
</code></pre>
<h3 id="heading-example">Example:</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">greet</span>():</span>
    print(<span class="hljs-string">"Hello, welcome to Python!"</span>)

greet()
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python">Hello, welcome to Python!
</code></pre>
<p>So here:</p>
<ul>
<li><p><code>def</code> → keyword used to define a function.</p>
</li>
<li><p><code>greet()</code> → function name.</p>
</li>
<li><p><code>()</code> → parentheses where arguments can be passed (if required).</p>
</li>
<li><p>Function is called by using its name followed by <code>()</code>.</p>
</li>
</ul>
<hr />
<h2 id="heading-3-function-arguments">3. FUNCTION ARGUMENTS</h2>
<p>Arguments are values that we pass to a function so that it can work with them.</p>
<p>Let’s see the different types:</p>
<h3 id="heading-a-positional-arguments">(a) Positional Arguments</h3>
<p>These are the most common. The order matters.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>):</span>
    print(a + b)

add(<span class="hljs-number">5</span>, <span class="hljs-number">3</span>)
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python"><span class="hljs-number">8</span>
</code></pre>
<p>Here <code>a=5</code> and <code>b=3</code>. The function adds them.</p>
<hr />
<h3 id="heading-b-default-arguments">(b) Default Arguments</h3>
<p>We can give a default value. If we don’t pass anything, the default is used.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name=<span class="hljs-string">"Guest"</span></span>):</span>
    print(<span class="hljs-string">"Hello,"</span>, name)

greet()
greet(<span class="hljs-string">"Prajitha"</span>)
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python">Hello, Guest
Hello, Prajitha
</code></pre>
<hr />
<h3 id="heading-c-keyword-arguments">(c) Keyword Arguments</h3>
<p>We can pass arguments in <strong>any order</strong> by using keywords.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">student</span>(<span class="hljs-params">name, age</span>):</span>
    print(<span class="hljs-string">"Name:"</span>, name, <span class="hljs-string">"Age:"</span>, age)

student(age=<span class="hljs-number">20</span>, name=<span class="hljs-string">"John"</span>)
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python">Name: John Age: <span class="hljs-number">20</span>
</code></pre>
<hr />
<h3 id="heading-d-arbitrary-arguments">(d) Arbitrary Arguments</h3>
<p>Sometimes we don’t know how many arguments will be passed.</p>
<ol>
<li><code>*args</code> → for multiple positional arguments</li>
</ol>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fruits</span>(<span class="hljs-params">*args</span>):</span>
    <span class="hljs-keyword">for</span> fruit <span class="hljs-keyword">in</span> args:
        print(fruit)

fruits(<span class="hljs-string">"Apple"</span>, <span class="hljs-string">"Mango"</span>, <span class="hljs-string">"Banana"</span>)
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python">Apple
Mango
Banana
</code></pre>
<p>Here, <code>args</code> behaves like a tuple.</p>
<ol start="2">
<li><code>**kwargs</code> → for multiple keyword arguments</li>
</ol>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">student_details</span>(<span class="hljs-params">**kwargs</span>):</span>
    <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> kwargs.items():
        print(key, <span class="hljs-string">":"</span>, value)

student_details(name=<span class="hljs-string">"Ananya"</span>, age=<span class="hljs-number">22</span>, course=<span class="hljs-string">"AI"</span>)
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python">name : Ananya
age : <span class="hljs-number">22</span>
course : AI
</code></pre>
<p>Here, <code>kwargs</code> behaves like a dictionary.</p>
<hr />
<h2 id="heading-4-return-statement">4. RETURN STATEMENT</h2>
<p>Functions can also <strong>return values</strong> back.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">multiply</span>(<span class="hljs-params">a, b</span>):</span>
    <span class="hljs-keyword">return</span> a * b

result = multiply(<span class="hljs-number">4</span>, <span class="hljs-number">5</span>)
print(result)
</code></pre>
<p><strong>Outpu –</strong></p>
<pre><code class="lang-python"><span class="hljs-number">20</span>
</code></pre>
<p>👉 Difference:</p>
<ul>
<li><p><code>print()</code> → only displays.</p>
</li>
<li><p><code>return</code> → sends the result back, which we can use later.</p>
</li>
</ul>
<hr />
<h2 id="heading-5-variable-scope">5. VARIABLE SCOPE</h2>
<ul>
<li><p><strong>Local Variable</strong> → created inside a function (used only inside).</p>
</li>
<li><p><strong>Global Variable</strong> → created outside any function (used everywhere).</p>
</li>
</ul>
<pre><code class="lang-python">x = <span class="hljs-number">10</span>  <span class="hljs-comment"># global</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show</span>():</span>
    y = <span class="hljs-number">5</span>  <span class="hljs-comment"># local</span>
    print(<span class="hljs-string">"Inside:"</span>, x + y)

show()
print(<span class="hljs-string">"Outside:"</span>, x)
</code></pre>
<hr />
<h2 id="heading-6-nested-functions">6. NESTED FUNCTIONS</h2>
<p>A function inside another function.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">outer</span>():</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inner</span>():</span>
        print(<span class="hljs-string">"This is inner function"</span>)
    inner()

outer()
</code></pre>
<hr />
<h2 id="heading-7-recursion">7. RECURSION</h2>
<p>When a function calls itself.</p>
<p>Example – Factorial</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">factorial</span>(<span class="hljs-params">n</span>):</span>
    <span class="hljs-keyword">if</span> n == <span class="hljs-number">0</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> n * factorial(n<span class="hljs-number">-1</span>)

print(factorial(<span class="hljs-number">5</span>))
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python"><span class="hljs-number">120</span>
</code></pre>
<hr />
<h2 id="heading-8-lambda-functions">8. LAMBDA FUNCTIONS</h2>
<p>Anonymous (nameless) functions. Usually one-liners.</p>
<pre><code class="lang-python">square = <span class="hljs-keyword">lambda</span> x: x * x
print(square(<span class="hljs-number">6</span>))
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python"><span class="hljs-number">36</span>
</code></pre>
<p>Also useful with <code>map()</code> and <code>filter()</code>.</p>
<pre><code class="lang-python">nums = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
squared = list(map(<span class="hljs-keyword">lambda</span> x: x**<span class="hljs-number">2</span>, nums))
print(squared)
</code></pre>
<p><strong>Output –</strong></p>
<pre><code class="lang-python">[<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>, <span class="hljs-number">16</span>]
</code></pre>
<hr />
<h2 id="heading-9-practice-questions">9. PRACTICE QUESTIONS</h2>
<p>Try solving these 👇</p>
<ol>
<li><p>Write a function to calculate the area of a circle (take radius as input).</p>
</li>
<li><p>Create a function that returns the maximum number in a list.</p>
</li>
<li><p>Write a recursive function for Fibonacci numbers.</p>
</li>
<li><p>Write a function that checks if a number is prime.</p>
</li>
<li><p>Write a function that checks if a string is a palindrome.</p>
</li>
<li><p>Create a lambda function to find the cube of a number.</p>
</li>
<li><p>Write a function that counts vowels in a string.</p>
</li>
<li><p>Use <code>*args</code> to find the sum of any number of numbers.</p>
</li>
</ol>
<hr />
<h2 id="heading-10-conclusion">10. CONCLUSION</h2>
<p>Functions are used to write <strong>clean, reusable and modular code</strong>.</p>
<ul>
<li><p>Positional, default, keyword, <em>args and</em> *kwargs make functions flexible.</p>
</li>
<li><p>Return statement is used to send results back.</p>
</li>
<li><p>Scope decides where variables can be used.</p>
</li>
<li><p>Advanced concepts like recursion and lambda make functions even more powerful.</p>
</li>
</ul>
<p>👉 Mastering functions is very important because every Python program is built on top of them.</p>
<hr />
<h2 id="heading-11-bonus-practice">11. BONUS PRACTICE</h2>
<p>🔹 <strong>Calculator Function</strong> → Create a function that performs add, subtract, multiply, divide.<br />🔹 <strong>ATM Function</strong> → A function with deposit, withdraw, and balance check.<br />🔹 <strong>Student Grade Function</strong> → Function to calculate grade from marks.</p>
]]></content:encoded></item><item><title><![CDATA[Loops in Python Explained : (Beginner’s Guide)]]></title><description><![CDATA[1. INTRODUCTION
Loops which are also called as iterative statements are useful when we want to repeat some of the tasks until a condition is satisfied. For example, if there are 10 students then we want to check the grade of every student, then we ca...]]></description><link>https://python-where-my-journey-began.hashnode.dev/loops-in-python-explained-beginners-guide</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/loops-in-python-explained-beginners-guide</guid><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Loops]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[learn coding]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Mon, 01 Sep 2025 16:00:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756742259454/02ea0dc3-786b-440c-af7d-e7b1f7b4c3f7.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-1-introduction">1. INTRODUCTION</h2>
<p>Loops which are also called as iterative statements are useful when we want to repeat some of the tasks until a condition is satisfied. For example, if there are 10 students then we want to check the grade of every student, then we can use the concept of loops. The number of times the loop has to be executed are called as iterations.</p>
<hr />
<h2 id="heading-2-types-of-loops">2. TYPES OF LOOPS</h2>
<p>There are three different types of loops that are available<br />1. for loop<br />2. while loop<br />3. nested loop</p>
<hr />
<h2 id="heading-3-for-loop">3. FOR LOOP</h2>
<p><strong>Syntax -</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> variable_name <span class="hljs-keyword">in</span> range (how many times to iterate):
    <span class="hljs-comment"># code to be executed for each iteration</span>
</code></pre>
<p><strong>EXAMPLE - 1 :</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range (<span class="hljs-number">6</span>):
    print(i)

<span class="hljs-string">'''
  output - 0
           1
           2
           3
           4
           5
'''</span>

<span class="hljs-comment"># The reason that the loop stopped printing at 5 is that when we mention a number n then </span>
<span class="hljs-comment"># range will iterate from 0 to n-1</span>
</code></pre>
<p><strong>EXAMPLE -2 :</strong></p>
<pre><code class="lang-python">i = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range (i,<span class="hljs-number">10</span>,<span class="hljs-number">3</span>):
    print(num)

<span class="hljs-string">'''
Output - 1
         4
         7
'''</span>

<span class="hljs-comment"># In the loop we have mentioned as i, 10, 3 so for each iteration it will add the i value to 3 and</span>
<span class="hljs-comment"># checks the condition.</span>
</code></pre>
<p>EXAMPLE - 3 : We can use loops to iterate over the elements in a list, tuple, dictionary or even set.</p>
<pre><code class="lang-python">colours = [<span class="hljs-string">"Blue"</span>, <span class="hljs-string">"Green"</span>, <span class="hljs-string">"Red"</span>, <span class="hljs-string">"Yellow"</span>]
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> colours:
    print(element)

<span class="hljs-string">'''
Output - Blue
         Green
         Red
         Yellow
'''</span>
</code></pre>
<hr />
<h2 id="heading-4-while-loop">4. WHILE LOOP</h2>
<p>It is very similar to for loop but the main difference is based on if we know how many times the loop has to iterate. In for loop we have to mention how many times the loop has to iterate but not many of the times we will know answer to this question so in that case we can use while loop.</p>
<p>A real life example for while loop is that if we open a website or an application we may use it just for a few seconds or for minutes or for hours so we can use while loop stating that the app has to work until the user is using the app.</p>
<p><strong>Syntax -</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">while</span> condition:
    <span class="hljs-comment"># statements to be executed</span>
</code></pre>
<p><strong>EXAMPLE - 1</strong></p>
<pre><code class="lang-python">num = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> num &lt;= <span class="hljs-number">5</span>:
    print(num)
    num += <span class="hljs-number">1</span> <span class="hljs-comment"># num = num + 1</span>

<span class="hljs-string">'''
Output - 0
         1
         2
         3
         4
         5
'''</span>
</code></pre>
<hr />
<h2 id="heading-5-loop-control-statements">5. LOOP CONTROL STATEMENTS</h2>
<p>These loop control statements are used while the loop is executing and will control a loop if a condition is satisfied. There are three types of loop control statements</p>
<ol>
<li><p>break</p>
</li>
<li><p>continue</p>
</li>
<li><p>pass</p>
</li>
</ol>
<p><strong>1. BREAK -</strong> It will stop the loop if a condition is satisfied.</p>
<p>Example - In an e-commerce website we may want to cancel the whole order if an UPI payment is failed</p>
<pre><code class="lang-python">i = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range (i,<span class="hljs-number">10</span>):
    <span class="hljs-keyword">if</span> num == <span class="hljs-number">5</span>:
        <span class="hljs-keyword">break</span>
    print(num)

<span class="hljs-string">'''
Output - 1
         2
         3
         4
'''</span>
</code></pre>
<p><strong>2. CONTINUE -</strong> continue is used when we want to skip an iteration if a condition is satisfied.</p>
<p>Example - Just like in the above example of e-commerce the the user selects COD then we may want to skip the UPI or any other online payment option.</p>
<pre><code class="lang-python">i = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range (i,<span class="hljs-number">6</span>):
    <span class="hljs-keyword">if</span> num == <span class="hljs-number">3</span>:
        <span class="hljs-keyword">continue</span>
    print(num)

<span class="hljs-string">'''
Output - 1
         2
         4
         5
'''</span>
</code></pre>
<p><strong>3. PASS -</strong> Pass is used for checking a condition and it does not do anything and acts like a placeholder.</p>
<p>Example -</p>
<pre><code class="lang-python">i = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range (i,<span class="hljs-number">6</span>):
    <span class="hljs-keyword">if</span> num == <span class="hljs-number">3</span>:
        <span class="hljs-keyword">pass</span>
    print(num)

<span class="hljs-string">'''
Output - 1
         2
         3
         4
         5
'''</span>
</code></pre>
<hr />
<h2 id="heading-6-nested-loops">6. NESTED LOOPS</h2>
<p>A nested loop is a loop that is present inside a loop.</p>
<p>Example -</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range (<span class="hljs-number">1</span>,<span class="hljs-number">6</span>):
    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range (<span class="hljs-number">1</span>,<span class="hljs-number">6</span>):
        print(<span class="hljs-string">"*"</span>,end=<span class="hljs-string">""</span>) 
<span class="hljs-comment"># By default, print() moves the cursor to a new line after each call. </span>
<span class="hljs-comment"># Using end="" prevents that, so the stars are printed on the same line.</span>

<span class="hljs-string">'''Output -
*****
*****
*****
*****
*****
'''</span>
</code></pre>
<hr />
<h2 id="heading-7-difference-between-pass-and-continue">7. DIFFERENCE BETWEEN PASS AND CONTINUE</h2>
<ul>
<li><p><code>continue</code> →Skips the current iteration and moves to the next one.</p>
</li>
<li><p><code>pass</code> → Does nothing, just acts as a placeholder, and execution continues normally.</p>
</li>
</ul>
<hr />
<h2 id="heading-8-practice-questions">8. PRACTICE QUESTIONS</h2>
<p>Try solving these on your own 👇</p>
<ol>
<li><p>Write a <code>for</code> loop to print the first 10 even numbers.</p>
</li>
<li><p>Print all characters of the string <code>"Python"</code> using a <code>for</code> loop.</p>
</li>
<li><p>Use a <code>while</code> loop to print numbers from 10 down to 1.</p>
</li>
<li><p>Print the multiplication table of 7 using a loop.</p>
</li>
<li><p>Write a loop that calculates the sum of numbers from 1 to 100.</p>
</li>
<li><p>Print only odd numbers from 1 to 20 using a loop.</p>
</li>
<li><p>Write a nested loop to print a 3×3 matrix of numbers.</p>
</li>
<li><p>Use <code>continue</code> to skip printing the number <code>5</code> from a range of 1–10.</p>
</li>
<li><p>Write a program that uses <code>pass</code> inside a loop.</p>
</li>
<li><p>Create a <code>while</code> loop that keeps asking the user to input a number until they type <code>"stop"</code>.</p>
</li>
</ol>
<hr />
<h2 id="heading-9-conclusion">9. CONCLUSION</h2>
<ul>
<li><p>Loops help in executing repetitive tasks efficiently.</p>
</li>
<li><p><code>for</code> loop → Best when the number of iterations is known.</p>
</li>
<li><p><code>while</code> loop → Best when the number of iterations is unknown.</p>
</li>
<li><p>Loop control statements (<code>break</code>, <code>continue</code>, <code>pass</code>) give more flexibility while controlling iterations.</p>
</li>
<li><p>Mastering loops is essential, as they are the backbone of problem-solving in Python.</p>
</li>
</ul>
<p><strong>QUICK REVISION</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Statement</td><td>Use Case</td></tr>
</thead>
<tbody>
<tr>
<td><code>for</code></td><td>When the number of iterations is <strong>known</strong></td></tr>
<tr>
<td><code>while</code></td><td>When the number of iterations is <strong>unknown</strong></td></tr>
<tr>
<td><code>break</code></td><td>Exit the loop completely</td></tr>
<tr>
<td><code>continue</code></td><td>Skip the current iteration</td></tr>
<tr>
<td><code>pass</code></td><td>Placeholder – does nothing</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-10-bonus-practice">10. BONUS PRACTICE</h2>
<ol>
<li><p><strong>Number Guessing Game</strong></p>
<ul>
<li><p>Generate a random number.</p>
</li>
<li><p>Keep asking the user to guess until they get it right.</p>
</li>
<li><p>Provide hints like "Too High" / "Too Low".</p>
</li>
</ul>
</li>
<li><p><strong>To-Do List Program</strong></p>
<ul>
<li><p>Keep asking for tasks until the user types <code>"done"</code>.</p>
</li>
<li><p>Store and print the final to-do list.</p>
</li>
</ul>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Conditional Statements in Python]]></title><description><![CDATA[🔹 Introduction
Conditional statements are very helpful in any programming language. They allow us to execute certain parts of code only when specific conditions are met.
Think of them as decision-making tools in programming:

If it rains, take an um...]]></description><link>https://python-where-my-journey-began.hashnode.dev/conditional-statements-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/conditional-statements-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[learn coding]]></category><category><![CDATA[Conditional statement]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[control flow]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Sat, 30 Aug 2025 06:23:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756534892663/3240c53b-a07c-4ec0-af31-9a500d12eb04.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">🔹 Introduction</h2>
<p>Conditional statements are very helpful in any programming language. They allow us to execute certain parts of code only when specific conditions are met.</p>
<p>Think of them as decision-making tools in programming:</p>
<ul>
<li><p><em>If it rains, take an umbrella.</em></p>
</li>
<li><p><em>If it’s summer, wear light-colored clothes to stay cool.</em></p>
</li>
</ul>
<p>In Python, the main conditional statements are:</p>
<ul>
<li><p><code>if</code></p>
</li>
<li><p><code>if-else</code></p>
</li>
<li><p><code>if-elif-else</code></p>
</li>
<li><p>Nested <code>if</code></p>
</li>
</ul>
<hr />
<h2 id="heading-precautions">🔹 Precautions</h2>
<p>While writing conditional statements in Python, remember:</p>
<ul>
<li><p><strong>Indentation (spaces)</strong> is mandatory.</p>
</li>
<li><p>Don’t forget the colon <code>:</code> at the end of each condition.</p>
</li>
</ul>
<hr />
<h2 id="heading-taking-input-from-user">🔹 Taking Input from User</h2>
<p>In real-world programs, we cannot predict what value a user will enter. So, we take input using the <code>input()</code> function.</p>
<pre><code class="lang-python">number = int(input(<span class="hljs-string">"Enter a value: "</span>))
print(number)

<span class="hljs-comment"># Example Input: 20</span>
<span class="hljs-comment"># Output: 20</span>

text = input(<span class="hljs-string">"Enter some text: "</span>)
print(text)

<span class="hljs-comment"># Example Input: Hello</span>
<span class="hljs-comment"># Output: Hello</span>
</code></pre>
<hr />
<h2 id="heading-1-simple-if-statement">🔹 1. Simple <code>if</code> Statement</h2>
<p>Used when we want to check a single condition.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> (condition):
    <span class="hljs-comment"># statements to execute if condition is True</span>
</code></pre>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">num1 = <span class="hljs-number">10</span>
<span class="hljs-keyword">if</span> num1 &gt; <span class="hljs-number">0</span>:
    print(<span class="hljs-string">"Positive"</span>)
<span class="hljs-comment"># Output: Positive</span>
</code></pre>
<hr />
<h2 id="heading-2-if-else-statement">🔹 2. <code>if-else</code> Statement</h2>
<p>Used when we want one block to run if the condition is True, and another if it’s False.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> (condition):
    <span class="hljs-comment"># statements if condition is True</span>
<span class="hljs-keyword">else</span>:
    <span class="hljs-comment"># statements if condition is False</span>
</code></pre>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">age = int(input(<span class="hljs-string">"Enter your age to check voting eligibility: "</span>))

<span class="hljs-keyword">if</span> age &gt;= <span class="hljs-number">18</span>:
    print(<span class="hljs-string">"You are eligible to vote."</span>)
<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"You are not eligible to vote."</span>)
</code></pre>
<hr />
<h2 id="heading-3-if-elif-else-statement">🔹 3. <code>if-elif-else</code> Statement</h2>
<p>Used when we need to check multiple conditions.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> (condition1):
    <span class="hljs-comment"># statements</span>
<span class="hljs-keyword">elif</span> (condition2):
    <span class="hljs-comment"># statements</span>
<span class="hljs-keyword">else</span>:
    <span class="hljs-comment"># statements if all conditions fail</span>
</code></pre>
<p><strong>Examples:</strong></p>
<p>👉 Checking positive, negative, or zero:</p>
<pre><code class="lang-python">num = int(input(<span class="hljs-string">"Enter a value: "</span>))

<span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">0</span>:
    print(<span class="hljs-string">"Positive"</span>)
<span class="hljs-keyword">elif</span> num &lt; <span class="hljs-number">0</span>:
    print(<span class="hljs-string">"Negative"</span>)
<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"Zero"</span>)
</code></pre>
<p>👉 Grading System:</p>
<pre><code class="lang-python">marks = int(input(<span class="hljs-string">"Enter your marks: "</span>))

<span class="hljs-keyword">if</span> marks &gt;= <span class="hljs-number">85</span>:
    print(<span class="hljs-string">"A grade"</span>)
<span class="hljs-keyword">elif</span> marks &gt;= <span class="hljs-number">70</span>:
    print(<span class="hljs-string">"B grade"</span>)
<span class="hljs-keyword">elif</span> marks &gt;= <span class="hljs-number">50</span>:
    print(<span class="hljs-string">"C grade"</span>)
<span class="hljs-keyword">elif</span> marks &gt;= <span class="hljs-number">35</span>:
    print(<span class="hljs-string">"D grade"</span>)
<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"F grade"</span>)
</code></pre>
<hr />
<h2 id="heading-4-nested-if-statement">🔹 4. Nested <code>if</code> Statement</h2>
<p>Used when one condition depends on another condition.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">num = int(input(<span class="hljs-string">"Enter a value: "</span>))

<span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">0</span>:
    <span class="hljs-keyword">if</span> num % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>:
        print(<span class="hljs-string">"Positive and Even"</span>)
    <span class="hljs-keyword">else</span>:
        print(<span class="hljs-string">"Positive and Odd"</span>)
<span class="hljs-keyword">else</span>:
    <span class="hljs-keyword">if</span> num % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>:
        print(<span class="hljs-string">"Negative and Even"</span>)
    <span class="hljs-keyword">else</span>:
        print(<span class="hljs-string">"Negative and Odd"</span>)
</code></pre>
<hr />
<h2 id="heading-5-ternary-operator-short-hand-if">🔹 5. Ternary Operator (Short-Hand <code>if</code>)</h2>
<p>A compact way of writing <code>if-else</code> in a single line.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">num = <span class="hljs-number">13</span>
result = <span class="hljs-string">"Even"</span> <span class="hljs-keyword">if</span> num % <span class="hljs-number">2</span> == <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"Odd"</span>
print(result)
<span class="hljs-comment"># Output: Odd</span>
</code></pre>
<hr />
<h2 id="heading-mini-projects-practice">🔹 Mini Projects / Practice</h2>
<ol>
<li><p><strong>ATM Withdrawal Check</strong></p>
<ul>
<li><p>Input: account balance and withdrawal amount.</p>
</li>
<li><p>Output: whether withdrawal is possible.</p>
</li>
</ul>
</li>
<li><p><strong>Login System</strong></p>
<ul>
<li><p>Input: username &amp; password.</p>
</li>
<li><p>Output: successful login or invalid credentials.</p>
</li>
</ul>
</li>
</ol>
<hr />
<p>✅ <strong>Conclusion:</strong><br />Conditional statements help us control the flow of execution in Python. They are the foundation for problem-solving and decision-making in programs.</p>
<p>Next up: <strong>Loops in Python (</strong><code>for</code>, <code>while</code>) 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Python Sets Explained: A Beginner’s Guide to Unique & Unordered Data]]></title><description><![CDATA[When working with Python, we often deal with collections of data. Sometimes we want lists that preserve order, sometimes tuples that remain unchanged. But what if we want to store data without duplicates and perform quick operations like union or int...]]></description><link>https://python-where-my-journey-began.hashnode.dev/python-sets-explained-a-beginners-guide-to-unique-and-unordered-data</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/python-sets-explained-a-beginners-guide-to-unique-and-unordered-data</guid><category><![CDATA[Python]]></category><category><![CDATA[data structures]]></category><category><![CDATA[learn coding]]></category><category><![CDATA[learn python]]></category><category><![CDATA[python sets]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Thu, 28 Aug 2025 05:30:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756348612745/1eed0fd9-5d4b-4361-85d2-b188ad83bbc9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When working with Python, we often deal with collections of data. Sometimes we want lists that preserve order, sometimes tuples that remain unchanged. But what if we want to store data without duplicates and perform quick operations like union or intersection? That’s where <strong>sets</strong> come in.</p>
<p>In this article, we’ll explore <strong>what sets are, how to use them, and why they are so useful</strong> in Python programming.</p>
<hr />
<h2 id="heading-what-is-a-set-in-python">🔹 What is a Set in Python?</h2>
<p>A <strong>set</strong> is an <strong>unordered collection of unique elements</strong>.</p>
<ul>
<li><p><strong>Unordered</strong> → The items do not have an index or a fixed position like lists or tuples.</p>
</li>
<li><p><strong>Unique</strong> → Duplicate elements are automatically removed.</p>
</li>
</ul>
<p>Think of a set as a <strong>mathematical set</strong> — it either contains an element or it doesn’t.</p>
<h3 id="heading-example">Example:</h3>
<pre><code class="lang-python"><span class="hljs-comment"># Creating a set</span>
my_set = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>}

print(my_set)  
<span class="hljs-comment"># Output: {1, 2, 3, 4}  → duplicates are removed automatically</span>
</code></pre>
<hr />
<h2 id="heading-creating-sets">🔹 Creating Sets</h2>
<p>You can create a set in two ways:</p>
<p>Way - 1:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Using curly braces</span>
numbers = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>}
print(numbers)
<span class="hljs-comment">#output - {1, 2, 3, 4}</span>
</code></pre>
<p>Way - 2:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Using set() constructor</span>
letters = set([<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"a"</span>])
print(letters)  
<span class="hljs-comment"># Output: {'a', 'b', 'c'}</span>
</code></pre>
<p>👉 Notice how duplicates vanish!</p>
<p>⚠️ <strong>Important:</strong> While initiating an empty set it must be created using <code>set()</code> and not <code>{}</code>, because <code>{}</code> creates an empty dictionary. You can see it practically below:</p>
<pre><code class="lang-python">empty_set = set()
empty_dict = {}
print(type(empty_set))  <span class="hljs-comment"># &lt;class 'set'&gt;</span>
print(type(empty_dict)) <span class="hljs-comment"># &lt;class 'dict'&gt;</span>
</code></pre>
<hr />
<h2 id="heading-accessing-set-elements">🔹 Accessing Set Elements</h2>
<p>Since sets are <strong>unordered</strong>, you can’t access elements using an index. But you can:</p>
<ul>
<li><p><strong>Loop through a set</strong></p>
<p>  ```python
  fruits = {"apple", "banana", "cherry"}</p>
<h1 id="heading-looping">Looping</h1>
<p>  for f in fruits:
      print(f)</p>
</li>
</ul>
<p>    '''output - banana
                apple
                cherry
    '''</p>
<pre><code>
    Since sets are unordered, we will get different outputs every time we run the code.

* **Check membership** using <span class="hljs-string">`in`</span>


<span class="hljs-string">``</span><span class="hljs-string">`python
# Membership check
print("apple" in fruits)   # output - True
print("grape" in fruits)   # output - False</span>
</code></pre><hr />
<h2 id="heading-adding-and-removing-elements">🔹 Adding and Removing Elements</h2>
<p>Sets are mutable, so we can add or remove elements.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Add an element</span>
fruits.add(<span class="hljs-string">"mango"</span>)
print(fruits)

<span class="hljs-comment"># Add multiple elements</span>
fruits.update([<span class="hljs-string">"kiwi"</span>, <span class="hljs-string">"orange"</span>])
print(fruits)

<span class="hljs-comment"># Remove elements</span>
<span class="hljs-comment"># If we try to remove an element that is not present the remove function will give an error</span>
<span class="hljs-comment"># and the discard method will not produce any error.</span>
fruits.remove(<span class="hljs-string">"apple"</span>)   <span class="hljs-comment"># Error if not found</span>
fruits.discard(<span class="hljs-string">"grape"</span>)  <span class="hljs-comment"># No error if not found</span>
print(fruits)

<span class="hljs-comment"># Pop removes a random element</span>
removed = fruits.pop()
print(<span class="hljs-string">"Removed:"</span>, removed)
</code></pre>
<hr />
<h2 id="heading-set-operations-like-in-math">🔹 Set Operations (Like in Math!)</h2>
<p>One of the coolest things about sets is that they support <strong>mathematical set operations</strong>:</p>
<pre><code class="lang-python">A = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
B = {<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>}

print(A | B)  <span class="hljs-comment"># Union(Adds the elements of set A and B) → {1, 2, 3, 4, 5, 6}</span>
print(A &amp; B)  <span class="hljs-comment"># Intersection(Common elements in both sets) → {3, 4}</span>
print(A - B)  <span class="hljs-comment"># Difference(Elements present in set A that are not present in set B) → {1, 2}</span>
print(B - A)  <span class="hljs-comment"># Difference(Elements present in set B that are not present in set A) → {3, 4}</span>
print(A ^ B)  <span class="hljs-comment"># Symmetric Difference(Unique elements in both the sets) → {1, 2, 5, 6}</span>
</code></pre>
<hr />
<h2 id="heading-why-use-sets">🔹 Why Use Sets?</h2>
<p>Sets are super useful when:<br />✅ You want to remove duplicates from a collection<br />✅ You need <strong>fast membership testing</strong> (<code>in</code> is much faster in sets than in lists)<br />✅ You want to perform <strong>mathematical operations</strong> like union, intersection, and difference</p>
<h3 id="heading-example-removing-duplicates-from-a-list">Example: Removing duplicates from a list</h3>
<pre><code class="lang-python">numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
unique_numbers = list(set(numbers))
print(unique_numbers)  <span class="hljs-comment"># [1, 2, 3, 4, 5]</span>
</code></pre>
<hr />
<h2 id="heading-frozen-sets-immutable-sets">🔹 Frozen Sets (Immutable Sets)</h2>
<p>Sometimes you need an <strong>immutable set</strong> (cannot be changed). That’s where <code>frozenset()</code> comes in. Once a frozenset is created we can not manipulate any elements.</p>
<pre><code class="lang-python">fs = frozenset([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>])
print(fs)

<span class="hljs-comment"># fs.add(5)  ❌ Error → frozenset object has no attribute 'add'</span>
</code></pre>
<hr />
<h2 id="heading-conclusion">🔹 Conclusion</h2>
<p>Python Sets are a <strong>powerful and flexible data structure</strong>. They:</p>
<ul>
<li><p>Ensure uniqueness of elements</p>
</li>
<li><p>Allow fast membership checks</p>
</li>
<li><p>Support mathematical set operations</p>
</li>
<li><p>Have a special immutable version called <code>frozenset</code></p>
</li>
</ul>
<p>Next time you need to handle <strong>unique and unordered data</strong>, reach for <strong>sets</strong>! 🚀</p>
<hr />
<h3 id="heading-practice">📝 PRACTICE</h3>
<p>Now it’s time to test what you’ve learned about sets! Try solving the following tasks:</p>
<ol>
<li><p>Create a set of your 5 favorite fruits.</p>
<ul>
<li><p>Add a new fruit to the set.</p>
</li>
<li><p>Remove one fruit using <code>discard()</code>.</p>
</li>
<li><p>Try removing a fruit that does not exist and observe what happens.</p>
</li>
</ul>
</li>
<li><p>Create two sets:</p>
<ul>
<li><p><code>A = {1, 2, 3, 4, 5}</code></p>
</li>
<li><p><code>B = {4, 5, 6, 7, 8}</code><br />  Perform the following:</p>
</li>
<li><p>Union</p>
</li>
<li><p>Intersection</p>
</li>
<li><p>Difference (A - B and B - A)</p>
</li>
<li><p>Symmetric Difference</p>
</li>
</ul>
</li>
<li><p>Remove duplicates from this list using a set:</p>
<pre><code class="lang-python"> nums = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">10</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">20</span>, <span class="hljs-number">50</span>, <span class="hljs-number">30</span>]
</code></pre>
</li>
<li><p>Create a frozen set using numbers from 1–5.</p>
<ul>
<li>Try to add a number to it and see what happens.</li>
</ul>
</li>
<li><p>You have two lists of students:</p>
<pre><code class="lang-python"> math_students = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>, <span class="hljs-string">"David"</span>]
 science_students = [<span class="hljs-string">"Charlie"</span>, <span class="hljs-string">"David"</span>, <span class="hljs-string">"Eva"</span>, <span class="hljs-string">"Frank"</span>]
</code></pre>
<ul>
<li><p>Find students who are enrolled in <strong>both subjects</strong>.</p>
</li>
<li><p>Find students who are enrolled in <strong>only Math but not Science</strong>.</p>
</li>
<li><p>Find students who are in <strong>at least one subject</strong>.</p>
</li>
</ul>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Python Tuples Explained: A Beginner’s Guide to Immutable Data in Python]]></title><description><![CDATA[📘 Tuples in Python – A Complete Guide
INTRODUCTION
In this article, we are going to learn about the concept of tuples in Python. As we discussed in the earlier article about lists, we can think of tuples as lists — but unlike lists, tuples are immut...]]></description><link>https://python-where-my-journey-began.hashnode.dev/python-tuples-explained-a-beginners-guide-to-immutable-data-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/python-tuples-explained-a-beginners-guide-to-immutable-data-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[data structures]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[python tuples]]></category><category><![CDATA[learn coding]]></category><category><![CDATA[learn python]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Mon, 25 Aug 2025 16:52:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756133871091/50638f56-94a7-4ec0-b9f6-cbb359ee2af5.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-tuples-in-python-a-complete-guide">📘 Tuples in Python – A Complete Guide</h1>
<h2 id="heading-introduction">INTRODUCTION</h2>
<p>In this article, we are going to learn about the concept of tuples in Python. As we discussed in the earlier article about lists, we can think of tuples as lists — but unlike lists, tuples are <strong>immutable</strong> (not changeable).</p>
<hr />
<h2 id="heading-what-is-a-tuple">WHAT IS A TUPLE?</h2>
<p>Tuples are very useful in real-world applications. You might wonder: <em>Why are tuples useful if we cannot change them?</em></p>
<p>Well, sometimes we want data that must remain fixed. For example, think about your Google account. You use this account to log in or sign up for different platforms. If that account were to be modified, you could lose access to those platforms.</p>
<p>That’s where tuples come in — they allow us to store <strong>unchangeable, reliable data</strong>.</p>
<p>To create a tuple, we assign values inside parentheses <code>()</code> to a variable. Similar to lists, tuples can hold multiple data types.</p>
<hr />
<h2 id="heading-creating-tuples">CREATING TUPLES</h2>
<p>We can create a tuple with:</p>
<ul>
<li><p>no elements,</p>
</li>
<li><p>a single element, or</p>
</li>
<li><p>multiple elements.</p>
</li>
</ul>
<h3 id="heading-empty-tuple">Empty Tuple</h3>
<pre><code class="lang-python">tuple1 = ()
print(tuple1)
<span class="hljs-comment"># Output: ()</span>
</code></pre>
<h3 id="heading-single-element">Single Element</h3>
<pre><code class="lang-python">tuple2 = (<span class="hljs-number">5</span>,)
print(tuple2)
<span class="hljs-comment"># Output: (5,)</span>
</code></pre>
<p>⚠️ <strong>Note</strong>: Don’t forget the comma after the single element. If you write <code>(5)</code>, Python treats it as just the number <code>5</code>, not a tuple.</p>
<h3 id="heading-multiple-elements">Multiple Elements</h3>
<pre><code class="lang-python">tuple3 = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)
print(tuple3)
<span class="hljs-comment"># Output: (1, 2, 3, 4, 5)</span>
</code></pre>
<hr />
<h2 id="heading-accessing-elements">ACCESSING ELEMENTS</h2>
<p>Just like lists, tuples support indexing. The first element has index <code>0</code>.</p>
<pre><code class="lang-python">a = (<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-number">11</span>, <span class="hljs-string">"d"</span>, <span class="hljs-number">0</span>, <span class="hljs-string">"f"</span>)
print(a)        <span class="hljs-comment"># ('a', 'b', 11, 'd', 0, 'f')</span>
print(a[<span class="hljs-number">0</span>])     <span class="hljs-comment"># a</span>
print(a[<span class="hljs-number">-1</span>])    <span class="hljs-comment"># f</span>
print(a[<span class="hljs-number">1</span>:<span class="hljs-number">3</span>])   <span class="hljs-comment"># ('b', 11)</span>
</code></pre>
<hr />
<h2 id="heading-immutable">IMMUTABLE</h2>
<p>Unlike lists, tuples are <strong>immutable</strong> — we cannot modify them once created.</p>
<pre><code class="lang-python">a = (<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-number">11</span>, <span class="hljs-string">"d"</span>, <span class="hljs-number">0</span>, <span class="hljs-string">"f"</span>)
a[<span class="hljs-number">0</span>] = <span class="hljs-number">1</span>   <span class="hljs-comment"># ❌ Error</span>
</code></pre>
<hr />
<h2 id="heading-tuple-operations">TUPLE OPERATIONS</h2>
<h3 id="heading-1-concatenation">1. Concatenation</h3>
<p>Combine two or more tuples:</p>
<pre><code class="lang-python">tuple1 = (<span class="hljs-string">"a"</span>, <span class="hljs-string">"e"</span>)
tuple2 = (<span class="hljs-string">"i"</span>, <span class="hljs-string">"o"</span>, <span class="hljs-string">"u"</span>)
vowels = tuple1 + tuple2
print(vowels)
<span class="hljs-comment"># Output: ('a', 'e', 'i', 'o', 'u')</span>
</code></pre>
<h3 id="heading-2-repetition">2. Repetition</h3>
<p>Repeat elements:</p>
<pre><code class="lang-python">a = (<span class="hljs-string">"tuple"</span>,) * <span class="hljs-number">3</span>
print(a)
<span class="hljs-comment"># Output: ('tuple', 'tuple', 'tuple')</span>
</code></pre>
<h3 id="heading-3-membership">3. Membership</h3>
<p>Check if an element exists:</p>
<pre><code class="lang-python">a = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)
print(<span class="hljs-number">6</span> <span class="hljs-keyword">in</span> a)   <span class="hljs-comment"># False</span>
print(<span class="hljs-number">3</span> <span class="hljs-keyword">in</span> a)   <span class="hljs-comment"># True</span>
</code></pre>
<hr />
<h2 id="heading-tuple-methods">TUPLE METHODS</h2>
<p>Tuples have only two built-in methods:</p>
<h3 id="heading-1-count">1. <code>count()</code></h3>
<p>Returns how many times an element appears.</p>
<pre><code class="lang-python">a = (<span class="hljs-string">"m"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"o"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"z"</span>)
print(a.count(<span class="hljs-string">"m"</span>))  <span class="hljs-comment"># 1</span>
print(a.count(<span class="hljs-string">"e"</span>))  <span class="hljs-comment"># 2</span>
</code></pre>
<h3 id="heading-2-index">2. <code>index()</code></h3>
<p>Returns the position of an element (first occurrence).</p>
<pre><code class="lang-python">a = (<span class="hljs-string">"m"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"o"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"z"</span>)
print(a.index(<span class="hljs-string">"o"</span>))  <span class="hljs-comment"># 2</span>
print(a.index(<span class="hljs-string">"e"</span>))  <span class="hljs-comment"># 1</span>
</code></pre>
<hr />
<h2 id="heading-when-to-use-tuples">WHEN TO USE TUPLES?</h2>
<ul>
<li><p>When you need fixed data that should not change.</p>
</li>
<li><p>Tuples can be used as <strong>dictionary keys</strong> (lists can’t).</p>
</li>
<li><p>Useful for storing <strong>GPS coordinates</strong> like latitude and longitude (which should never change).</p>
</li>
</ul>
<hr />
<h2 id="heading-tuple-unpacking">TUPLE UNPACKING</h2>
<p>Tuple unpacking allows assigning values to multiple variables in one line.</p>
<pre><code class="lang-python">person = (<span class="hljs-string">"John"</span>, <span class="hljs-number">20</span>, <span class="hljs-string">"Degree Student"</span>)
name, age, job = person

print(name)  <span class="hljs-comment"># John</span>
print(age)   <span class="hljs-comment"># 20</span>
print(job)   <span class="hljs-comment"># Degree Student</span>
</code></pre>
<p>It can even work with flexible unpacking:</p>
<pre><code class="lang-python">numbers = (<span class="hljs-number">100</span>, <span class="hljs-number">101</span>, <span class="hljs-number">108</span>, <span class="hljs-number">130</span>, <span class="hljs-number">15</span>)
a, *b, c = numbers

print(a)  <span class="hljs-comment"># 100</span>
print(b)  <span class="hljs-comment"># [101, 108, 130]</span>
print(c)  <span class="hljs-comment"># 15</span>
</code></pre>
<hr />
<h2 id="heading-lists-vs-tuples">LISTS VS TUPLES</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>List (✅ Mutable)</td><td>Tuple (❌ Immutable)</td></tr>
</thead>
<tbody>
<tr>
<td>Mutability</td><td>✅ Mutable</td><td>❌ Immutable</td></tr>
<tr>
<td>Performance</td><td>🐢 Slower</td><td>⚡ Faster</td></tr>
<tr>
<td>Use Cases</td><td>Dynamic data</td><td>Fixed data</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-practice">PRACTICE</h2>
<p>Try the following exercises:</p>
<ol>
<li><p>Create a tuple of length 9 with any elements.</p>
</li>
<li><p>Retrieve the 4th element.</p>
</li>
<li><p>Retrieve elements from index 3 to 7.</p>
</li>
<li><p>Return the index of any element.</p>
</li>
<li><p>Use the <code>count()</code> method on any element.</p>
</li>
</ol>
<p>💡 For extra clarity: repeat the same with a list, then convert it into a tuple and compare.</p>
<hr />
<h2 id="heading-conclusion">CONCLUSION</h2>
<p>Thank you for reading this article! 🙌<br />If you found it helpful, consider subscribing to the newsletter and sharing it with friends who might benefit from it. I’d also love to hear your thoughts, ideas, or feedback — feel free to reach out anytime.</p>
]]></content:encoded></item><item><title><![CDATA[Python Dictionaries: Your Key to Organized Data]]></title><description><![CDATA[If you’ve been following along with my Python journey, you already know about strings and lists. Both are super useful, but sometimes you need a way to store data in pairs – something like a “word” and its “meaning”, or a “student” and their “marks”....]]></description><link>https://python-where-my-journey-began.hashnode.dev/python-dictionaries-your-key-to-organized-data</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/python-dictionaries-your-key-to-organized-data</guid><category><![CDATA[Python]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[beginner]]></category><category><![CDATA[coding]]></category><category><![CDATA[data structures]]></category><category><![CDATA[Python basics]]></category><category><![CDATA[Learn Code Online]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Sat, 16 Aug 2025 13:26:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1755350746158/ac09fba2-3da8-4a13-b7cc-c608b7a8924d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you’ve been following along with my Python journey, you already know about <strong>strings</strong> and <strong>lists</strong>. Both are super useful, but sometimes you need a way to <strong>store data in pairs</strong> – something like a “word” and its “meaning”, or a “student” and their “marks”.</p>
<p>That’s where <strong>Python dictionaries</strong> come in!</p>
<hr />
<h2 id="heading-what-is-a-dictionary">What is a Dictionary?</h2>
<p>A dictionary in Python is a <strong>collection of key-value pairs</strong>.</p>
<ul>
<li><p>The <strong>key</strong> is like a unique label.</p>
</li>
<li><p>The <strong>value</strong> is the data attached to that label.</p>
</li>
</ul>
<p>Think of it as a real dictionary: the <em>word</em> is the key, and its <em>definition</em> is the value.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Example</span>
student = {
    <span class="hljs-string">"name"</span>: <span class="hljs-string">"Lucky"</span>,
    <span class="hljs-string">"age"</span>: <span class="hljs-number">21</span>,
    <span class="hljs-string">"course"</span>: <span class="hljs-string">"AI"</span>
}
print(student)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">{<span class="hljs-string">'name'</span>: <span class="hljs-string">'Lucky'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">21</span>, <span class="hljs-string">'course'</span>: <span class="hljs-string">'AI'</span>}
</code></pre>
<hr />
<h2 id="heading-why-use-dictionaries">Why Use Dictionaries?</h2>
<ul>
<li><p>Quick lookups: Access values directly using keys.</p>
</li>
<li><p>Organized: Keeps data in pairs, making it easy to understand.</p>
</li>
<li><p>Flexible: Can store numbers, strings, lists, or even other dictionaries as values.</p>
</li>
</ul>
<hr />
<h2 id="heading-creating-a-dictionary">Creating a Dictionary</h2>
<p>You can create a dictionary using curly braces <code>{}</code>:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Empty dictionary</span>
my_dict = {}

<span class="hljs-comment"># Dictionary with some data</span>
fruits = {<span class="hljs-string">"apple"</span>: <span class="hljs-number">2</span>, <span class="hljs-string">"banana"</span>: <span class="hljs-number">5</span>, <span class="hljs-string">"orange"</span>: <span class="hljs-number">3</span>}
print(fruits)
</code></pre>
<hr />
<h2 id="heading-accessing-data">Accessing Data</h2>
<p>To get a value, use its key:</p>
<pre><code class="lang-python">print(fruits[<span class="hljs-string">"apple"</span>])   <span class="hljs-comment"># Output: 2</span>
</code></pre>
<p>But be careful! If the key doesn’t exist, Python throws an error.<br />To avoid that, use <code>.get()</code>:</p>
<pre><code class="lang-python">print(fruits.get(<span class="hljs-string">"mango"</span>))   <span class="hljs-comment"># Output: None</span>
</code></pre>
<hr />
<h2 id="heading-adding-and-updating-data">Adding and Updating Data</h2>
<p>You can easily add new pairs or update existing ones:</p>
<pre><code class="lang-python">fruits[<span class="hljs-string">"mango"</span>] = <span class="hljs-number">10</span>    <span class="hljs-comment"># Add new key-value pair</span>
fruits[<span class="hljs-string">"apple"</span>] = <span class="hljs-number">4</span>     <span class="hljs-comment"># Update value of 'apple'</span>
print(fruits)
</code></pre>
<hr />
<h2 id="heading-removing-data">Removing Data</h2>
<p>Dictionaries also allow you to remove pairs:</p>
<pre><code class="lang-python">fruits.pop(<span class="hljs-string">"banana"</span>)    <span class="hljs-comment"># Removes 'banana'</span>
print(fruits)

fruits.clear()          <span class="hljs-comment"># Removes everything</span>
print(fruits)
</code></pre>
<hr />
<h2 id="heading-useful-dictionary-methods">Useful Dictionary Methods</h2>
<p>Here are a few handy ones:</p>
<pre><code class="lang-python">student.keys()    <span class="hljs-comment"># All keys</span>
student.values()  <span class="hljs-comment"># All values</span>
student.items()   <span class="hljs-comment"># All key-value pairs</span>
</code></pre>
<hr />
<h2 id="heading-wrap-up">Wrap-Up 🎁</h2>
<p>Dictionaries are <strong>powerful tools</strong> in Python for managing data in a structured way.</p>
<ul>
<li><p>They store data as key-value pairs.</p>
</li>
<li><p>You can add, update, and remove items easily.</p>
</li>
<li><p>They make your code cleaner and more readable.</p>
</li>
</ul>
<p>Next time you’re working on a project, ask yourself: “Would this data make more sense as a dictionary?” Chances are, the answer will be yes!</p>
]]></content:encoded></item><item><title><![CDATA[Python Lists – Your First Step into Storing and Managing Data]]></title><description><![CDATA[Hey there! If you've mastered strings and are ready for your next Python milestone — welcome! Today, we're diving into lists, which are like super-smart containers for your data. Instead of juggling a bunch of separate variables, you can group items ...]]></description><link>https://python-where-my-journey-began.hashnode.dev/python-lists-your-first-step-into-storing-and-managing-data</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/python-lists-your-first-step-into-storing-and-managing-data</guid><category><![CDATA[Python]]></category><category><![CDATA[Python basics]]></category><category><![CDATA[python lists]]></category><category><![CDATA[beginner python]]></category><category><![CDATA[data structures]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Tue, 12 Aug 2025 10:54:06 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754995999806/aa5c9a5f-55f1-4d01-bcac-03f85aa54719.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there! If you've mastered strings and are ready for your next Python milestone — welcome! Today, we're diving into <strong>lists</strong>, which are like super-smart containers for your data. Instead of juggling a bunch of separate variables, you can group items together neatly and work with them easily.</p>
<p>When you start programming, you’ll quickly realize you need a way to store <strong>more than one value</strong> in a single variable.<br />For example: instead of creating 10 separate variables for 10 fruits, you can just use <strong>a list</strong>.</p>
<p>In Python, lists are like containers where you can store multiple items — numbers, strings, or even other lists — all in one place. And the best part? <strong>Lists are mutable</strong> — which means you can change them!</p>
<hr />
<h3 id="heading-1-creating-your-first-list">1. Creating Your First List</h3>
<p>You can make an empty list—or load it right up with a few items:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Empty list</span>
my_list = []

<span class="hljs-comment"># A list with some items</span>
fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]

<span class="hljs-comment"># Mixed kinds of data side by side</span>
mixed = [<span class="hljs-number">1</span>, <span class="hljs-string">"hello"</span>, <span class="hljs-number">3.14</span>, <span class="hljs-literal">True</span>]
</code></pre>
<hr />
<h3 id="heading-2-seeing-inside-your-list-indexing">2. Seeing Inside Your List (Indexing)</h3>
<p>Just like books on a shelf, every item has a spot—starting from <strong>0 (</strong>just like how we saw in the <a target="_blank" href="https://hashnode.com/post/cme3yvao8000002k1bjwobysf">strings article</a>.)</p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]

print(fruits[<span class="hljs-number">0</span>])   <span class="hljs-comment"># apple</span>
print(fruits[<span class="hljs-number">1</span>])   <span class="hljs-comment"># banana</span>
print(fruits[<span class="hljs-number">-1</span>])  <span class="hljs-comment"># cherry</span>
</code></pre>
<p>That <code>-1</code> grabs you the last item—handy when you don’t know how many are inside.</p>
<hr />
<h3 id="heading-3-changing-things-up">3. Changing Things Up</h3>
<p>Unlike strings, you <em>can</em> change lists. Let’s swap one fruit for another:</p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
fruits[<span class="hljs-number">1</span>] = <span class="hljs-string">"mango"</span>
print(fruits)  <span class="hljs-comment"># ['apple', 'mango', 'cherry']</span>
</code></pre>
<hr />
<h3 id="heading-4-adding-more-goodies">4. Adding More Goodies</h3>
<p>Want to expand your list? You’ve got options:</p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
fruits.append(<span class="hljs-string">"orange"</span>)         <span class="hljs-comment"># Adds at the end</span>
fruits.insert(<span class="hljs-number">1</span>, <span class="hljs-string">"grape"</span>)       <span class="hljs-comment"># Places it in position 1</span>
fruits.extend([<span class="hljs-string">"kiwi"</span>, <span class="hljs-string">"melon"</span>])  <span class="hljs-comment"># Adds several at once</span>
print(fruits) <span class="hljs-comment"># ['apple', 'grape', 'banana', 'cherry', 'orange', 'kiwi', 'melon']</span>
</code></pre>
<hr />
<h3 id="heading-5-removing-itemswith-care">5. Removing Items—With Care</h3>
<p>Lists can lose items too—pick whichever method suits your need:</p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grape"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"kiwi"</span>, <span class="hljs-string">"melon"</span>]
fruits.remove(<span class="hljs-string">"melon"</span>)  <span class="hljs-comment"># Remove by value</span>
fruits.pop(<span class="hljs-number">2</span>)           <span class="hljs-comment"># Remove by position</span>
<span class="hljs-keyword">del</span> fruits[<span class="hljs-number">0</span>]           <span class="hljs-comment"># Delete specific spot</span>
print(fruits)           <span class="hljs-comment"># ['grape', 'cherry', 'orange', 'kiwi']</span>
fruits.clear()          <span class="hljs-comment"># Empty the whole list</span>
print(fruits)           <span class="hljs-comment"># []</span>
</code></pre>
<hr />
<h3 id="heading-6-seeing-a-slice-of-the-list-slicing">6. Seeing a Slice of the List (Slicing)</h3>
<p>Want part of your list? Slicing gives you that:</p>
<p>It will return the range of values that are mentioned as indices in the square braces.</p>
<p>And while mentioning the index value the left side value of colon is used as the starting position and the right side value of colon is used as the stopping index and this index will not be considered.</p>
<p>For example if we give as list1[2:5] then it will return the list1 values that are present in the indexes 2,3,4</p>
<pre><code class="lang-python">numbers = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
print(numbers[<span class="hljs-number">1</span>:<span class="hljs-number">4</span>])   <span class="hljs-comment"># [1, 2, 3]</span>
print(numbers[:<span class="hljs-number">3</span>])    <span class="hljs-comment"># [0, 1, 2]</span>
print(numbers[::<span class="hljs-number">2</span>])   <span class="hljs-comment"># [0, 2, 4]</span>
</code></pre>
<hr />
<h3 id="heading-7-handy-list-methods">7. Handy List Methods</h3>
<p>These are daily helpers worth knowing:</p>
<pre><code class="lang-python">numbers = [<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>]

numbers.sort()             <span class="hljs-comment"># Order the list</span>
numbers.reverse()          <span class="hljs-comment"># Flip it backward</span>
print(numbers.index(<span class="hljs-number">4</span>))    <span class="hljs-comment"># Find where 4 is</span>
print(numbers.count(<span class="hljs-number">1</span>))    <span class="hljs-comment"># How many 1s?</span>
copy_list = numbers.copy() <span class="hljs-comment"># Make a safe copy</span>
</code></pre>
<hr />
<h3 id="heading-conclusion"><strong>Conclusion</strong></h3>
<p>Lists are one of the most <strong>powerful and flexible</strong> tools in Python.<br />They let you store, modify, and organize data with ease — and you’ll be using them in almost every Python program you write.</p>
<p><strong>Tip:</strong> Practice by making small programs that store multiple items in a list, like a grocery list or a favorite movies list.</p>
]]></content:encoded></item><item><title><![CDATA[Python Strings and Their Methods — A Complete Beginner’s Guide]]></title><description><![CDATA[In this article, we will be covering strings and their methods.
As we have already discussed in the earlier article on various datatypes, str is a datatype used for storing characters and words.
A string is a collection of characters. For example, th...]]></description><link>https://python-where-my-journey-began.hashnode.dev/python-strings-and-their-methods-a-complete-beginners-guide</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/python-strings-and-their-methods-a-complete-beginners-guide</guid><category><![CDATA[Python String]]></category><category><![CDATA[Python tutorial for beginners]]></category><category><![CDATA[Python String Methods]]></category><category><![CDATA[python tutorial]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Python string manipulation]]></category><category><![CDATA[learn coding]]></category><category><![CDATA[learn python]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Sat, 09 Aug 2025 08:02:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754726304240/c6f203de-ddd0-4f77-acbf-b3744c37fbfb.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we will be covering strings and their methods.</p>
<p>As we have already discussed in the <a target="_blank" href="https://hashnode.com/post/cmdzxk7cw001802ld951f0jdg">earlier article</a> on various datatypes, <code>str</code> is a datatype used for storing characters and words.</p>
<p>A string is a collection of characters. For example, the word <code>"Python"</code> is a sequence of the characters <code>"P"</code>, <code>"y"</code>, <code>"t"</code>, <code>"h"</code>, <code>"o"</code>, <code>"n"</code>. Strings are considered immutable, i.e., once a string is created, we cannot change it directly; we have to create a new string to make changes to the original string.</p>
<p>The position of the characters in a string starts from zero. For example, in the word <code>"Python"</code>:</p>
<pre><code class="lang-python"><span class="hljs-string">"P"</span> → <span class="hljs-number">0</span>
<span class="hljs-string">"y"</span> → <span class="hljs-number">1</span>
<span class="hljs-string">"t"</span> → <span class="hljs-number">2</span>
<span class="hljs-string">"h"</span> → <span class="hljs-number">3</span>
<span class="hljs-string">"o"</span> → <span class="hljs-number">4</span>
<span class="hljs-string">"n"</span> → <span class="hljs-number">5</span>
</code></pre>
<h3 id="heading-creating-strings">Creating Strings</h3>
<p>We can create strings in Python using:</p>
<ol>
<li><p>Single quotes</p>
</li>
<li><p>Double quotes</p>
</li>
<li><p>Triple quotes</p>
</li>
</ol>
<h4 id="heading-1-single-quotes">1. Single quotes:</h4>
<p>We can create strings by placing the value inside single quotes.<br />Example:</p>
<pre><code class="lang-python">name = <span class="hljs-string">'Python'</span>
print(name)
<span class="hljs-comment"># Output - Python</span>
</code></pre>
<h4 id="heading-2-double-quotes">2. Double quotes:</h4>
<p>We can create strings by placing the value inside double quotes.<br />Example:</p>
<pre><code class="lang-python">name = <span class="hljs-string">"Python"</span>
print(name)
<span class="hljs-comment"># Output - Python</span>
</code></pre>
<h4 id="heading-3-triple-quotes">3. Triple quotes:</h4>
<p>Sometimes we need to create strings that span multiple lines. In such cases, we use triple quotes.<br />Example:</p>
<pre><code class="lang-python">a = <span class="hljs-string">"""Welcome to the article
In this article, we will learn
about strings and also
different methods"""</span>
print(a)
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-plaintext">Welcome to the article
In this article, we will learn
about strings and also
different methods
</code></pre>
<p><strong>Note:</strong> While using multi-line strings, make sure you are assigning them to a variable; otherwise, they will be treated as comments.</p>
<hr />
<h3 id="heading-reading-input-from-the-user">Reading Input from the User</h3>
<p>We can read text from the user using the <code>input()</code> function:</p>
<pre><code class="lang-python">name = input(<span class="hljs-string">"Enter your name: "</span>)
print(<span class="hljs-string">f"Hello, <span class="hljs-subst">{name}</span>"</span>)
</code></pre>
<p><strong>Example Output:</strong></p>
<pre><code class="lang-plaintext">Enter your name: Prajitha
Hello, Prajitha
</code></pre>
<pre><code class="lang-python">a = int(input(<span class="hljs-string">"Enter a value: "</span>))
print(a)
</code></pre>
<p><strong>Example Output:</strong></p>
<pre><code class="lang-plaintext">Enter a value: 1
1
</code></pre>
<p>If we do not specify a datatype when taking input, Python treats it as a string by default. If we want to take an integer input, we must explicitly cast it using <code>int()</code>, as in the second example.</p>
<p>The text inside the quotes of the <code>input()</code> function appears on the screen as a prompt. It is optional—if you don’t want a prompt, you can simply write:</p>
<pre><code class="lang-python">a = int(input())
print(<span class="hljs-string">f"a : <span class="hljs-subst">{a}</span>"</span>)
</code></pre>
<pre><code class="lang-plaintext">1
a : 1
</code></pre>
<hr />
<h3 id="heading-concatenation">Concatenation</h3>
<p>As mentioned earlier, strings are immutable. To add two or more strings, we use concatenation, which creates a new string:</p>
<pre><code class="lang-python">str1 = <span class="hljs-string">"Hello"</span>
str2 = <span class="hljs-string">"World"</span>
str3 = str1 + <span class="hljs-string">" "</span> + str2
print(str3)  <span class="hljs-comment"># Output - Hello World</span>
</code></pre>
<hr />
<h3 id="heading-string-methods">String Methods</h3>
<p>Here are some commonly used string methods:</p>
<ol>
<li><p><code>upper()</code></p>
</li>
<li><p><code>lower()</code></p>
</li>
<li><p><code>capitalize()</code></p>
</li>
<li><p><code>title()</code></p>
</li>
<li><p><code>swapcase()</code></p>
</li>
<li><p><code>find()</code></p>
</li>
<li><p><code>index()</code></p>
</li>
<li><p><code>replace()</code></p>
</li>
<li><p><code>strip()</code></p>
</li>
<li><p><code>split()</code></p>
</li>
<li><p><code>startswith()</code></p>
</li>
<li><p><code>endswith()</code></p>
</li>
<li><p><code>isalnum()</code></p>
</li>
<li><p><code>isalpha()</code></p>
</li>
<li><p><code>isdigit()</code></p>
</li>
<li><p><code>islower()</code></p>
</li>
<li><p><code>isupper()</code></p>
</li>
<li><p><code>len()</code></p>
</li>
</ol>
<p>Let’s look at them in detail:</p>
<h4 id="heading-1-upper">1. <code>upper()</code></h4>
<p>Converts text to uppercase.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"string methods"</span>
print(a.upper())  <span class="hljs-comment"># Output - STRING METHODS</span>
</code></pre>
<h4 id="heading-2-lower">2. <code>lower()</code></h4>
<p>Converts text to lowercase.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"STRING METHODS"</span>
print(a.lower())  <span class="hljs-comment"># Output - string methods</span>
</code></pre>
<h4 id="heading-3-capitalize">3. <code>capitalize()</code></h4>
<p>Capitalizes the first letter and converts all other letters to lowercase.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"heLLo WOrld"</span>
print(a.capitalize())  <span class="hljs-comment"># Output - Hello world</span>
</code></pre>
<h4 id="heading-4-title">4. <code>title()</code></h4>
<p>Capitalizes the first letter of every word into uppercase.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"converts first character into uppercase"</span>
print(a.title())  <span class="hljs-comment"># Output - Converts First Character Into Uppercase</span>
</code></pre>
<h4 id="heading-5-swapcase">5. <code>swapcase()</code></h4>
<p>Swaps uppercase letters to lowercase and vice versa.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"Swaps tHE caSES"</span>
print(text.swapcase())  <span class="hljs-comment"># Output - sWAPS The CAses</span>
</code></pre>
<h4 id="heading-6-find">6. <code>find()</code></h4>
<p>Finds the position or index of a character or substring. Returns <code>-1</code> if not found.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"Finds the position of a letter"</span>
print(text.find(<span class="hljs-string">"p"</span>))    <span class="hljs-comment"># Output - 10</span>
print(text.find(<span class="hljs-string">"the"</span>))  <span class="hljs-comment"># Output - 6</span>
print(text.find(<span class="hljs-string">"x"</span>))    <span class="hljs-comment"># Output - -1</span>
</code></pre>
<h4 id="heading-7-index">7. <code>index()</code></h4>
<p>Similar to <code>find()</code>, but raises an error if the substring is not found.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"Finds the position of a letter"</span>
print(text.index(<span class="hljs-string">"F"</span>))         <span class="hljs-comment"># Output - 0</span>
print(text.index(<span class="hljs-string">"position"</span>))  <span class="hljs-comment"># Output - 10</span>
print(text.index(<span class="hljs-string">"x"</span>))         <span class="hljs-comment"># Raises ValueError</span>
</code></pre>
<h4 id="heading-8-replace">8. <code>replace()</code></h4>
<p>Replaces a substring with another.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"I love C"</span>
print(text.replace(<span class="hljs-string">"C"</span>, <span class="hljs-string">"Python"</span>))  <span class="hljs-comment"># Output - I love Python</span>
</code></pre>
<h4 id="heading-9-strip">9. <code>strip()</code></h4>
<p>Removes leading and trailing spaces.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"      Python      "</span>
print(text.strip())  <span class="hljs-comment"># Output - Python</span>
</code></pre>
<h4 id="heading-10-split">10. <code>split()</code></h4>
<p>Splits the string based on a delimiter.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"I am 20 years old"</span>
print(text.split())  <span class="hljs-comment"># Output - ['I', 'am', '20', 'years', 'old']</span>

text = <span class="hljs-string">"a#b#c#d#e"</span>
print(text.split(<span class="hljs-string">"#"</span>))  <span class="hljs-comment"># Output - ['a', 'b', 'c', 'd', 'e']</span>
</code></pre>
<h4 id="heading-11-startswith">11. <code>startswith()</code></h4>
<p>Checks if a string starts with a specific substring.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"Python has a simple syntax to code"</span>
print(text.startswith(<span class="hljs-string">"C"</span>))      <span class="hljs-comment"># Output - False</span>
print(text.startswith(<span class="hljs-string">"Python"</span>)) <span class="hljs-comment"># Output - True</span>
</code></pre>
<h4 id="heading-12-endswith">12. <code>endswith()</code></h4>
<p>Checks if a string ends with a specific substring.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"Python has a simple syntax to code"</span>
print(text.endswith(<span class="hljs-string">"program"</span>)) <span class="hljs-comment"># Output - False</span>
print(text.endswith(<span class="hljs-string">"code"</span>))    <span class="hljs-comment"># Output - True</span>
</code></pre>
<h4 id="heading-13-isalnum">13. <code>isalnum()</code></h4>
<p>Returns <code>True</code> if the string contains only letters and numbers.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"Python3"</span>
print(a.isalnum())  <span class="hljs-comment"># Output - True</span>

a = <span class="hljs-string">"Python 3"</span>
print(a.isalnum())  <span class="hljs-comment"># Output - False</span>
</code></pre>
<h4 id="heading-14-isalpha">14. <code>isalpha()</code></h4>
<p>Returns <code>True</code> if the string contains only letters.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"Python3"</span>
print(a.isalpha())  <span class="hljs-comment"># Output - False</span>

a = <span class="hljs-string">"Python"</span>
print(a.isalpha())  <span class="hljs-comment"># Output - True</span>
</code></pre>
<h4 id="heading-15-isdigit">15. <code>isdigit()</code></h4>
<p>Returns <code>True</code> if the string contains only digits.</p>
<pre><code class="lang-python">a = <span class="hljs-string">"A123"</span>
print(a.isdigit())  <span class="hljs-comment"># Output - False</span>

a = <span class="hljs-string">"1234"</span>
print(a.isdigit())  <span class="hljs-comment"># Output - True</span>
</code></pre>
<h4 id="heading-16-isupper">16. <code>isupper()</code></h4>
<p>Returns <code>True</code> if all characters are uppercase.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"PYTHON"</span>
print(text.isupper())  <span class="hljs-comment"># Output - True</span>
</code></pre>
<h4 id="heading-17-islower">17. <code>islower()</code></h4>
<p>Returns <code>True</code> if all characters are lowercase.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"python"</span>
print(text.islower())  <span class="hljs-comment"># Output - True</span>
</code></pre>
<h4 id="heading-18-len">18. <code>len()</code></h4>
<p>Returns the number of characters in a string.</p>
<pre><code class="lang-python">text = <span class="hljs-string">"Prajitha"</span>
print(len(text))  <span class="hljs-comment"># Output - 8</span>
</code></pre>
<hr />
<h2 id="heading-summary"><strong>Summary</strong></h2>
<p>Strings in Python are just pieces of text — like words, sentences, or even a single character. You can make them using single quotes, double quotes, or triple quotes (for multi-line text). Since strings are <strong>immutable</strong> (they can’t be changed directly), Python gives us lots of handy built-in methods to work with them. You can change their case (<code>upper()</code>, <code>lower()</code>), make them pretty (<code>capitalize()</code>, <code>title()</code>), find words or letters (<code>find()</code>, <code>index()</code>), replace text (<code>replace()</code>), clean up spaces (<code>strip()</code>), split them into parts (<code>split()</code>), or check what they’re made of (<code>isalnum()</code>, <code>isalpha()</code>, <code>isdigit()</code>). Mastering these tools will make working with text in Python a lot easier and way more fun!</p>
<p><strong>Note:</strong><br />The best way to master strings is by practicing them in different ways — try out the examples, mix them up, and create your own variations. Python has many other string functions beyond the ones listed here, so feel free to explore the documentation and experiment if you’re curious. The more you practice, the more comfortable and creative you’ll get with strings!</p>
]]></content:encoded></item><item><title><![CDATA[Comments and Print Statements in Python]]></title><description><![CDATA[In this article, we are going to learn about comments and print statements — two essential building blocks for any Python beginner.  

🧠 What Are Comments?
Comments are extremely useful while developing code. They act like little notes or explanatio...]]></description><link>https://python-where-my-journey-began.hashnode.dev/comments-and-print-statements-in-python</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/comments-and-print-statements-in-python</guid><category><![CDATA[python comments]]></category><category><![CDATA[print functions in python]]></category><category><![CDATA[Python]]></category><category><![CDATA[Python print formatting]]></category><category><![CDATA[python beginner]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Thu, 07 Aug 2025 14:49:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754577952351/506b41bc-fd4c-4d1e-9355-718112aa84c2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we are going to learn about <strong>comments</strong> and <strong>print statements</strong> — two essential building blocks for any Python beginner.  </p>
<hr />
<h2 id="heading-what-are-comments">🧠 What Are Comments?</h2>
<p>Comments are extremely useful while developing code. They act like little notes or explanations you can leave for yourself or others, especially when revisiting your code after days, months, or even years.</p>
<p>As your code grows into hundreds or thousands of lines, it becomes harder to remember why you wrote a particular line. That's where comments come in handy.</p>
<p>👉 <strong>Important</strong>: Comments are only visible to you (the reader) and are <strong>ignored by Python</strong> during execution.</p>
<p>There are two types of comments in Python:</p>
<ol>
<li><p><strong>Single-line Comment</strong></p>
</li>
<li><p><strong>Multi-line Comment</strong></p>
</li>
</ol>
<h3 id="heading-1-single-line-comments">🟩 1. Single-line Comments</h3>
<p>Single-line comments are used when your comment fits in a single line. You can also use them to temporarily disable a line of code (helpful during testing or debugging).</p>
<p>✅ To write a single-line comment, use the <code>#</code> symbol.</p>
<h4 id="heading-examples">📌 Examples:</h4>
<pre><code class="lang-python"><span class="hljs-comment"># This is a comment</span>
print(<span class="hljs-string">"Hello"</span>)
<span class="hljs-comment"># Output: Hello</span>
</code></pre>
<pre><code class="lang-python">print(<span class="hljs-string">"Comments Tutorial"</span>)  <span class="hljs-comment"># This is also a comment</span>
<span class="hljs-comment"># Output: Comments Tutorial</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># This is line 1 comment</span>
<span class="hljs-comment"># This is line 2 comment</span>
<span class="hljs-comment"># This is line 3 comment</span>
a = <span class="hljs-number">10</span>
print(a)
<span class="hljs-comment"># Output: 10</span>
</code></pre>
<h3 id="heading-2-multi-line-comments">🟨 2. Multi-line Comments</h3>
<p>Multi-line comments are useful when your explanation takes up more than one line. Instead of writing <code>#</code> before every line, you can use <strong>triple quotes</strong> (<code>'''</code> or <code>"""</code>).</p>
<h4 id="heading-example-with-triple-single-quotes">📌 Example with triple single quotes:</h4>
<pre><code class="lang-python"><span class="hljs-string">'''
This is line 1 comment
This is line 2 comment
This is line 3 comment
'''</span>
</code></pre>
<h4 id="heading-example-with-triple-double-quotes">📌 Example with triple double quotes:</h4>
<pre><code class="lang-python"><span class="hljs-string">"""
This is line 1 comment
This is line 2 comment
This is line 3 comment
"""</span>
</code></pre>
<p>⚠️ Note: Make sure the quotes that you are using are straight quotes ("") instead of (““).</p>
<hr />
<h2 id="heading-what-is-the-print-statement">🖨️ What Is the Print Statement?</h2>
<p>The <code>print()</code> function is used to display output in Python. There are multiple ways to use it:</p>
<h3 id="heading-1-basic-printing">✅ 1. Basic Printing</h3>
<pre><code class="lang-python">a = <span class="hljs-number">10</span>
print(a)
<span class="hljs-comment"># Output: 10</span>
</code></pre>
<h3 id="heading-2-with-descriptive-text">✅ 2. With Descriptive Text</h3>
<pre><code class="lang-python">a = <span class="hljs-number">10</span>
print(<span class="hljs-string">"a:"</span>, a)
<span class="hljs-comment"># Output: a: 10</span>
</code></pre>
<p>Adding a label helps make the output more understandable.</p>
<h3 id="heading-3-using-f-strings-formatted-strings">✅ 3. Using f-Strings (Formatted Strings)</h3>
<p>This is a modern and readable way to format output in Python.</p>
<pre><code class="lang-python">name = <span class="hljs-string">"Prajitha"</span>
print(<span class="hljs-string">f"Name: <span class="hljs-subst">{name}</span>"</span>)
<span class="hljs-comment"># Output: Name: Prajitha</span>
</code></pre>
<p>✅ Just add an <code>f</code> before the string and wrap variables inside <code>{}</code> to include them in the output.</p>
<hr />
<h2 id="heading-conclusion">✅ Conclusion</h2>
<p>That’s it for comments and print statements — two simple yet powerful tools to make your code cleaner, more understandable, and easier to debug.</p>
<p>Practice writing your own examples with comments and print statements — you'll thank yourself later when revisiting your code!</p>
]]></content:encoded></item><item><title><![CDATA[Python Variables and Data Types]]></title><description><![CDATA[Now coming to the next topic, today we are going to discuss something called as variables.
To understand clearly about variables, let us consider an example. In our college, during any exam registration or anything just like how we give our name in t...]]></description><link>https://python-where-my-journey-began.hashnode.dev/python-variables-and-data-types</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/python-variables-and-data-types</guid><category><![CDATA[Python]]></category><category><![CDATA[variables]]></category><category><![CDATA[data type in python]]></category><category><![CDATA[data types]]></category><category><![CDATA[type casting]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Wed, 06 Aug 2025 12:14:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754482286947/5aefe6e2-eb01-42b2-9f09-cf8bfde0d2fd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-now-coming-to-the-next-topic-today-we-are-going-to-discuss-something-called-as-variables">Now coming to the next topic, today we are going to discuss something called as <strong>variables</strong>.</h3>
<p>To understand clearly about variables, let us consider an example. In our college, during any exam registration or anything just like how we give our name in the name column, or our roll number in the roll number column — variables are used to store a value.</p>
<h4 id="heading-example">Example :</h4>
<pre><code class="lang-python">roll = <span class="hljs-number">11</span>  
name = <span class="hljs-string">"prajitha"</span>
</code></pre>
<p>There are different types of values that are present — for example, our name is considered to be a string since it is a word, our roll number will be an int since it consists of integer values and so on. All these types like string, integer are called as <strong>data types</strong>.</p>
<hr />
<h3 id="heading-data-types-in-python">Data Types in Python:</h3>
<ul>
<li><p><strong>int</strong> – integer values</p>
</li>
<li><p><strong>float</strong> – decimal values</p>
</li>
<li><p><strong>str</strong> – short for string, words or sentences or even letters</p>
</li>
<li><p><strong>bool</strong> – True, False etc</p>
</li>
</ul>
<p>Apart from these, there are a few more important data types in Python which we will learn in upcoming articles:</p>
<ul>
<li><p><strong>list</strong> – collection of multiple values in square brackets</p>
</li>
<li><p><strong>tuple</strong> – similar to list but immutable (cannot change)</p>
</li>
<li><p><strong>dict</strong> – key-value pairs used to store structured data</p>
</li>
<li><p><strong>set</strong> – collection of unique values</p>
</li>
</ul>
<hr />
<h3 id="heading-we-do-not-need-to-use-data-types-in-python-we-can-just-give-a-value-to-a-variable-like">We do not need to use data types in Python, we can just give a value to a variable like:</h3>
<pre><code class="lang-python">variable_name = variable_value
</code></pre>
<hr />
<h3 id="heading-but-there-are-some-rules-to-name-a-variable">But there are some rules to name a variable:</h3>
<ul>
<li><p>Variable names consist of only capital or small case letters, numbers, and underscore (_) only.</p>
</li>
<li><p>The starting letter of a variable must be a letter or an underscore, it should not start with a number.</p>
</li>
<li><p>There should not be any spaces while writing the variables.</p>
</li>
<li><p>Variable names are case-sensitive.</p>
</li>
</ul>
<h4 id="heading-examples">Examples:</h4>
<pre><code class="lang-python">first name ❌    first_name ✅  
<span class="hljs-number">1</span>st ❌           first ✅  
_value ✅        myvar, MYVAR, MyVar, myVar — all these four variable names are distinct.
</code></pre>
<hr />
<h3 id="heading-while-giving-the-values-to-the-variables-we-can-assign-only-a-single-value-to-a-variable-name">While giving the values to the variables we can assign only a single value to a variable name.</h3>
<p>And also while giving a string value or a character value, we have to give them in single or double quotes.</p>
<pre><code class="lang-python">name = <span class="hljs-string">"Prajitha"</span> <span class="hljs-keyword">or</span> name = <span class="hljs-string">'Prajitha'</span>  
letter = <span class="hljs-string">"A"</span> <span class="hljs-keyword">or</span> letter = <span class="hljs-string">'A'</span>  
letter = A ❌ invalid
</code></pre>
<p>To assign an integer or a float value or any other values to a variable we do not need to specify the datatype. We can just assign them directly.</p>
<pre><code class="lang-python">a = <span class="hljs-number">10</span>  
b = <span class="hljs-number">1.5</span>
</code></pre>
<hr />
<h3 id="heading-after-assigning-a-value-to-a-variable-we-can-use-a-function-in-python-that-tells-us-the-datatype-of-that-variables-value">After assigning a value to a variable, we can use a function in Python that tells us the datatype of that variable’s value.</h3>
<p>The function that is used is called as <code>type()</code>.</p>
<h4 id="heading-examples-1">Examples:</h4>
<pre><code class="lang-python">group = <span class="hljs-string">"BSc"</span>
print(type(group))  
<span class="hljs-comment"># output - &lt;class 'str'&gt;</span>

roll = <span class="hljs-number">11</span>
print(type(roll))  
<span class="hljs-comment"># output - &lt;class 'int'&gt;</span>

grade = <span class="hljs-string">"8.5"</span>
print(type(grade))  
<span class="hljs-comment"># output - &lt;class 'str'&gt;</span>
</code></pre>
<p>In the third example if you observe, even if we have given <code>8.5</code> as value, it showed that the datatype is <code>str</code>, because we have given <code>8.5</code> in double quotes. So anything that is mentioned inside single or double quotes is considered as <strong>string</strong>.</p>
<hr />
<h3 id="heading-we-can-also-convert-the-datatype-of-one-variable-into-another-datatype-like-converting-a-variable-of-datatype-int-to-a-datatype-str-this-process-of-converting-is-called-as-type-casting">We can also convert the datatype of one variable into another datatype like converting a variable of datatype <code>int</code> to a datatype <code>str</code>. This process of converting is called as <strong>type casting</strong>.</h3>
<p>It is very easy to perform type casting in Python.</p>
<h4 id="heading-examples-2">Examples:</h4>
<pre><code class="lang-python">value_before = <span class="hljs-number">5.2</span>
print(type(value_before))  
<span class="hljs-comment"># output - &lt;class 'float'&gt;</span>

value_after = int(value_before)
print(value_after)  
<span class="hljs-comment"># output - 5</span>

print(type(value_after))  
<span class="hljs-comment"># output - &lt;class 'int'&gt;</span>
</code></pre>
<pre><code class="lang-python">value_before = <span class="hljs-string">"123"</span>
print(type(value_before))  
<span class="hljs-comment"># output - &lt;class 'str'&gt;</span>

value_after = int(value_before)
print(type(value_after))  
<span class="hljs-comment"># output - &lt;class 'int'&gt;</span>
</code></pre>
<pre><code class="lang-python">s = <span class="hljs-string">"abc"</span>
int(s)
<span class="hljs-comment"># output - ❌ It will show error as we are not performing type casting correctly.</span>
</code></pre>
<hr />
<h3 id="heading-in-upcoming-articles-we-will-cover">In upcoming articles, we will cover:</h3>
<ul>
<li><p>How to use <code>list</code>, <code>tuple</code>, <code>dict</code>, and <code>set</code> in real life</p>
</li>
<li><p>How to work with multiple variables at once</p>
</li>
<li><p>Advanced type conversions and error handling</p>
</li>
</ul>
<hr />
<h3 id="heading-summary">Summary:</h3>
<ul>
<li><p>Variables are used to store a value in Python.</p>
</li>
<li><p>Data types tell us what kind of value the variable holds.</p>
</li>
<li><p>Python allows us to assign values without specifying the data type.</p>
</li>
<li><p>Use <code>type()</code> to check the data type.</p>
</li>
<li><p>Use type casting to convert one data type into another.</p>
</li>
<li><p>Follow naming rules while creating variables.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How Python Runs Your Code: Compilation Process Explained (With First Program)]]></title><description><![CDATA[In my earlier article (https://hashnode.com/post/cmdxai7na000502le26b681e2), I talked about how programming languages act as translators between humans and machines — converting binary into human-readable form and vice versa. This enables communicati...]]></description><link>https://python-where-my-journey-began.hashnode.dev/how-python-runs-your-code-compilation-process-explained-with-first-program</link><guid isPermaLink="true">https://python-where-my-journey-began.hashnode.dev/how-python-runs-your-code-compilation-process-explained-with-first-program</guid><category><![CDATA[Python]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[compilation steps]]></category><category><![CDATA[code execution]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Tammana Prajitha]]></dc:creator><pubDate>Tue, 05 Aug 2025 15:02:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754405466454/19f0debb-e532-443c-a1f5-74b3bdb8e398.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In my earlier article (<a target="_blank" href="https://hashnode.com/post/cmdxai7na000502le26b681e2">https://hashnode.com/post/cmdxai7na000502le26b681e2</a>), I talked about how programming languages act as translators between humans and machines — converting binary into human-readable form and vice versa. This enables <strong>communication between humans and computers</strong>.</p>
<h3 id="heading-terminology-alert">💡 Terminology Alert!</h3>
<ul>
<li><p><strong>High-Level Language</strong>: Language that is understandable by humans (e.g., Python, Java)</p>
</li>
<li><p><strong>Low-Level Language</strong>: Language understandable by machines — usually binary (0’s and 1’s)</p>
</li>
</ul>
<hr />
<h2 id="heading-but-how-does-this-translation-happen">🤔 But How Does This Translation Happen?</h2>
<p>This process of converting high-level language to low-level (machine) language is called <strong>compilation</strong>.</p>
<p>Now let’s understand <strong>how Python handles this process</strong> step by step.</p>
<hr />
<h2 id="heading-python-compilation-stages-5-step-process">🔁 Python Compilation Stages (5-Step Process)</h2>
<ol>
<li><p><strong>Source Code</strong></p>
</li>
<li><p><strong>Python Interpreter</strong></p>
</li>
<li><p><strong>Bytecode</strong></p>
</li>
<li><p><strong>Python Virtual Machine (PVM)</strong></p>
</li>
<li><p><strong>Output</strong></p>
</li>
</ol>
<hr />
<h3 id="heading-1-source-code">🔹 1. Source Code</h3>
<p>This is the code written by a developer — it’s the <strong>.py file</strong> you create.</p>
<p>Just like we save images as <code>.jpg</code> or <code>.png</code>, Python source code must be saved with the <code>.py</code> extension.</p>
<p>Example:</p>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello, Python!!"</span>)
</code></pre>
<p>Save this as <a target="_blank" href="http://hello.py"><code>hello.py</code></a>.</p>
<hr />
<h3 id="heading-2-python-interpreter">🔸 2. Python Interpreter</h3>
<p>The <strong>interpreter</strong> is what starts the process.</p>
<ul>
<li><p>It <strong>reads the source code</strong></p>
</li>
<li><p><strong>Compiles</strong> it to an intermediate format called <strong>bytecode</strong>, which is not human-readable but still not binary.</p>
</li>
<li><p>Then passes it on to the next stage</p>
</li>
</ul>
<p>This is what allows your Python code to be run and understood by your system.</p>
<hr />
<h3 id="heading-3-bytecode-pyc">🔹 3. Bytecode (.pyc)</h3>
<p>Once compiled, Python generates a <strong>bytecode file</strong> automatically.</p>
<ul>
<li><p>It will be saved as a <code>.pyc</code> file</p>
</li>
<li><p>Stored inside a folder called <code>__pycache__</code></p>
</li>
<li><p>Example: <code>__pycache__/hello.cpython-311.pyc</code></p>
</li>
</ul>
<p>This file contains a translated version of your original code — not in binary, but in a form Python can process faster.</p>
<p>🧠 <strong>Why it's useful</strong>:<br />If you run the same program multiple times, Python won’t compile it again — it just reuses the <code>.pyc</code> file, saving time.</p>
<hr />
<h3 id="heading-4-python-virtual-machine-pvm">🔸 4. Python Virtual Machine (PVM)</h3>
<p>The <strong>Python Virtual Machine</strong> now steps in.</p>
<ul>
<li><p>It reads and <strong>executes the bytecode</strong></p>
</li>
<li><p>Executes it <strong>line by line</strong></p>
</li>
<li><p>This is where all the actual actions (like printing, calculations, etc.) happen</p>
</li>
</ul>
<p>You can think of the PVM as <strong>Python’s runtime engine</strong>.</p>
<hr />
<h3 id="heading-5-output">🔹 5. Output</h3>
<p>After all the behind-the-scenes work is done, Python finally shows you the <strong>output</strong> on your screen.</p>
<p>In our case:</p>
<pre><code class="lang-python">Hello, Python!!
</code></pre>
<hr />
<h3 id="heading-lets-see-this-in-action">🧪 Let’s See This in Action</h3>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello, Python!!"</span>)
</code></pre>
<ol>
<li><p>Save this code as <a target="_blank" href="http://hello.py"><code>hello.py</code></a></p>
</li>
<li><p>Run it using:</p>
<pre><code class="lang-python"> python hello.py
</code></pre>
</li>
<li><p>Python creates a <code>.pyc</code> file like:<br /> <code>__pycache__/hello.cpython-311.pyc</code></p>
</li>
<li><p>The PVM executes the bytecode</p>
</li>
<li><p>Output appears:</p>
<pre><code class="lang-python"> Hello, Python!!
</code></pre>
</li>
</ol>
<hr />
<h2 id="heading-in-short">🔁 In Short</h2>
<p>Your code (.py)</p>
<p>↓</p>
<p>Compiled to Bytecode (.pyc)</p>
<p>↓</p>
<p>Run by PVM</p>
<p>↓</p>
<p>Output on Screen</p>
<hr />
<h3 id="heading-final-thoughts">🧠 Final Thoughts</h3>
<p>Knowing what happens behind the scenes gives you <strong>more control and confidence</strong> as a Python programmer. You’re not just writing code — you understand how it runs.</p>
<p>In the next article, we’ll take our first step into actual Python coding with <strong>variables and data</strong>.</p>
<p>Thanks for reading! 😊</p>
]]></content:encoded></item></channel></rss>