Understanding Data Types in Python
Gamya
·
2026-05-01
·
via DEV Community
<h1> Python Basics #2 🐍🔥 </h1> <p>In the previous article of this Python Basics series, we learned about variables and how Python stores information in memory.</p> <p>But here’s the thing:</p> <p>Python doesn’t just store <em>one kind</em> of information.</p> <p>Sometimes you want to store:</p> <ul> <li>Numbers</li> <li>Decimal values</li> <li>Text</li> <li>True/False conditions</li> <li>Collections of data</li> <li>And much more</li> </ul> <p>To handle all of this properly, Python uses something called <strong>data types</strong>.</p> <p>Understanding data types is one of the most important steps in becoming comfortable with programming.</p> <p>And once you truly understand them, writing code becomes <em>way</em> less confusing.</p> <p>So today, let’s break down Python data types in the simplest way possible — with examples ⚔️</p> <h1> What Is a Data Type? </h1> <p>A <strong>data type</strong> tells Python what kind of information a variable is storing.</p> <p>Think of it like character classes in an RPG game.</p> <p>Different characters have different abilities:</p> <ul> <li>A swordsman attacks differently</li> <li>A mage uses magic</li> <li>A healer restores health</li> </ul> <p>Similarly, different data types behave differently in Python.</p> <p>For example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">power_level</span> <span class="o">=</span> <span class="mi">9000</span> </code></pre> </div> <p>Python understands this is a number.</p> <p>But here:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">anime_name</span> <span class="o">=</span> <span class="sh">"</span><span class="s">Dragon Ball</span><span class="sh">"</span> </code></pre> </div> <p>Python understands this is text.</p> <p>The data type helps Python decide:</p> <ul> <li>What operations are allowed</li> <li>How memory should be used</li> <li>How the data should behave</li> </ul> <h1> The Main Data Types Beginners Should Know </h1> <p>Today we’ll focus on three important beginner-friendly data types:</p> <ol> <li>Integers (<code>int</code>)</li> <li>Floating-point numbers (<code>float</code>)</li> <li>Boolean values (<code>bool</code>)</li> </ol> <p>These are used <em>everywhere</em> in Python projects.</p> <h1> 1. Integers (<code>int</code>) 🔢 </h1> <p>An <strong>integer</strong> is a whole number with no decimal point.</p> <p>Examples:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">episodes</span> <span class="o">=</span> <span class="mi">1000</span> <span class="n">score</span> <span class="o">=</span> <span class="mi">95</span> <span class="n">age</span> <span class="o">=</span> <span class="mi">17</span> </code></pre> </div> <p>All of these are integers.</p> <p>Integers can be:</p> <ul> <li>Positive</li> <li>Negative</li> <li>Zero</li> </ul> <p>Example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">health</span> <span class="o">=</span> <span class="mi">100</span> <span class="n">enemy_damage</span> <span class="o">=</span> <span class="o">-</span><span class="mi">25</span> <span class="n">coins</span> <span class="o">=</span> <span class="mi">0</span> </code></pre> </div> <h1> Example 🎌 </h1> <p>Imagine you're creating an anime fighting game.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">naruto_clones</span> <span class="o">=</span> <span class="mi">2000</span> </code></pre> </div> <p>Since shadow clones are counted as whole numbers, Python stores this as an integer.</p> <p>You can perform calculations with integers too.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">goku_power</span> <span class="o">=</span> <span class="mi">9000</span> <span class="n">goku_power</span> <span class="o">=</span> <span class="n">goku_power</span> <span class="o">+</span> <span class="mi">5000</span> <span class="nf">print</span><span class="p">(</span><span class="n">goku_power</span><span class="p">)</span> </code></pre> </div> <p>Output:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="mi">14000</span> </code></pre> </div> <p>This is how games and apps constantly update values internally.</p> <h1> Why Integers Matter </h1> <p>Integers are commonly used for:</p> <ul> <li>Scores</li> <li>Health points</li> <li>Ages</li> <li>Inventory counts</li> <li>Likes on social media</li> <li>Followers</li> <li>Levels in games</li> </ul> <p>Basically, whenever you need <strong>whole numbers</strong>, integers are usually the answer.</p> <h1> 2. Floating-Point Numbers (<code>float</code>) 🌊 </h1> <p>A <strong>floating-point number</strong> (or simply a <em>float</em>) is a number that contains a decimal point.</p> <p>Examples:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">pi</span> <span class="o">=</span> <span class="mf">3.14</span> <span class="n">rating</span> <span class="o">=</span> <span class="mf">9.8</span> <span class="n">temperature</span> <span class="o">=</span> <span class="mf">36.5</span> </code></pre> </div> <p>Unlike integers, floats can store fractional values.</p> <h1> Why Are They Called “Floating-Point”? </h1> <p>The decimal point can “float” to different positions.</p> <p>For example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="mf">1.5</span> <span class="mf">10.75</span> <span class="mf">999.999</span> <span class="mf">0.0001</span> </code></pre> </div> <p>The decimal point moves around depending on the number.</p> <p>That’s why they’re called floating-point numbers.</p> <h1> Example ⚡ </h1> <p>Imagine tracking a character’s speed multiplier.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">speed_multiplier</span> <span class="o">=</span> <span class="mf">1.75</span> </code></pre> </div> <p>Or maybe an anime rating app:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">demon_slayer_rating</span> <span class="o">=</span> <span class="mf">9.4</span> </code></pre> </div> <p>Floats are perfect for precise values.</p> <h1> Integer vs Float </h1> <p>Let’s compare them side-by-side.</p> <div class="table-wrapper-paragraph"><table> <thead> <tr> <th>Integer</th> <th>Float</th> </tr> </thead> <tbody> <tr> <td><code>7</code></td> <td><code>7.0</code></td> </tr> <tr> <td><code>100</code></td> <td><code>100.5</code></td> </tr> <tr> <td><code>-3</code></td> <td><code>-3.14</code></td> </tr> </tbody> </table></div> <p>Even though <code>7</code> and <code>7.0</code> look similar, Python treats them differently.</p> <h1> Checking Data Types in Python </h1> <p>Python provides a very useful function called <code>type()</code>.</p> <p>Example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">power_level</span> <span class="o">=</span> <span class="mi">9000</span> <span class="nf">print</span><span class="p">(</span><span class="nf">type</span><span class="p">(</span><span class="n">power_level</span><span class="p">))</span> </code></pre> </div> <p>Output:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="o"><</span><span class="k">class</span> <span class="err">'</span><span class="nc">int</span><span class="sh">'</span><span class="s">> </span></code></pre> </div> <p>Now with a float:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">rating</span> <span class="o">=</span> <span class="mf">9.7</span> <span class="nf">print</span><span class="p">(</span><span class="nf">type</span><span class="p">(</span><span class="n">rating</span><span class="p">))</span> </code></pre> </div> <p>Output:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="o"><</span><span class="k">class</span> <span class="err">'</span><span class="nc">float</span><span class="sh">'</span><span class="s">> </span></code></pre> </div> <p>This is extremely useful when debugging programs.</p> <h1> 3. Boolean Values (<code>bool</code>) ✅❌ </h1> <p>A Boolean variable can only store one of two values:</p> <ul> <li><code>True</code></li> <li><code>False</code></li> </ul> <p>That’s it.</p> <p>Nothing in between.</p> <h1> Real-Life Example </h1> <p>Think about simple yes/no situations:</p> <ul> <li>Is the player alive?</li> <li>Has the mission been completed?</li> <li>Is the villain defeated?</li> <li>Is the account verified?</li> </ul> <p>All of these can be represented using Booleans.</p> <h1> Example 🎭 </h1> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">is_hokage</span> <span class="o">=</span> <span class="bp">True</span> </code></pre> </div> <p>Or:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">is_akatsuki_member</span> <span class="o">=</span> <span class="bp">False</span> </code></pre> </div> <p>Booleans are heavily used in decision-making systems.</p> <h1> Why Booleans Are So Important </h1> <p>Booleans power the logic behind applications.</p> <p>For example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">is_logged_in</span> <span class="o">=</span> <span class="bp">True</span> <span class="k">if</span> <span class="n">is_logged_in</span><span class="p">:</span> <span class="nf">print</span><span class="p">(</span><span class="sh">"</span><span class="s">Welcome back!</span><span class="sh">"</span><span class="p">)</span> </code></pre> </div> <p>Output:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">Welcome</span> <span class="n">back</span><span class="err">!</span> </code></pre> </div> <p>This is how apps decide what should happen next.</p> <h1> Important Note About Boolean Values </h1> <p>In Python:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="bp">True</span> <span class="bp">False</span> </code></pre> </div> <p>must start with capital letters.</p> <p>Wrong:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">true</span> <span class="n">false</span> </code></pre> </div> <p>Correct:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="bp">True</span> <span class="bp">False</span> </code></pre> </div> <p>Python is case-sensitive.</p> <h1> Combining Data Types Together </h1> <p>Real programs usually use multiple data types together.</p> <p>Example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">character_name</span> <span class="o">=</span> <span class="sh">"</span><span class="s">Luffy</span><span class="sh">"</span> <span class="n">bounty</span> <span class="o">=</span> <span class="mi">3000000000</span> <span class="n">pirate_king_candidate</span> <span class="o">=</span> <span class="bp">True</span> <span class="n">power_multiplier</span> <span class="o">=</span> <span class="mf">1.8</span> </code></pre> </div> <p>Here we have:</p> <div class="table-wrapper-paragraph"><table> <thead> <tr> <th>Variable</th> <th>Data Type</th> </tr> </thead> <tbody> <tr> <td><code>character_name</code></td> <td>String</td> </tr> <tr> <td><code>bounty</code></td> <td>Integer</td> </tr> <tr> <td><code>pirate_king_candidate</code></td> <td>Boolean</td> </tr> <tr> <td><code>power_multiplier</code></td> <td>Float</td> </tr> </tbody> </table></div> <p>This is how real applications organize information.</p> <h1> Beginner Mistakes to Avoid 🚨 </h1> <h1> 1. Mixing Up Strings and Numbers </h1> <p>Wrong:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">power</span> <span class="o">=</span> <span class="sh">"</span><span class="s">9000</span><span class="sh">"</span> <span class="o">+</span> <span class="mi">1000</span> </code></pre> </div> <p>This causes an error because <code>"9000"</code> is text, not a number.</p> <p>Correct:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">power</span> <span class="o">=</span> <span class="mi">9000</span> <span class="o">+</span> <span class="mi">1000</span> </code></pre> </div> <h1> 2. Forgetting Decimal Points </h1> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">rating</span> <span class="o">=</span> <span class="mi">9</span> </code></pre> </div> <p>This is an integer.</p> <p>But:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">rating</span> <span class="o">=</span> <span class="mf">9.0</span> </code></pre> </div> <p>This is a float.</p> <p>Tiny difference.</p> <p>Big meaning.</p> <h1> 3. Using Quotes Around Boolean Values </h1> <p>Wrong:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">is_alive</span> <span class="o">=</span> <span class="sh">"</span><span class="s">True</span><span class="sh">"</span> </code></pre> </div> <p>This becomes text, not a Boolean.</p> <p>Correct:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">is_alive</span> <span class="o">=</span> <span class="bp">True</span> </code></pre> </div> <h1> Fun Mini Project 🎮 </h1> <p>Let’s create a tiny anime character profile.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">character_name</span> <span class="o">=</span> <span class="sh">"</span><span class="s">Gojo</span><span class="sh">"</span> <span class="n">age</span> <span class="o">=</span> <span class="mi">28</span> <span class="n">power_level</span> <span class="o">=</span> <span class="mi">9999</span> <span class="n">cursed_energy_multiplier</span> <span class="o">=</span> <span class="mf">2.5</span> <span class="n">is_strongest_sorcerer</span> <span class="o">=</span> <span class="bp">True</span> <span class="nf">print</span><span class="p">(</span><span class="n">character_name</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">age</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">power_level</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">cursed_energy_multiplier</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">is_strongest_sorcerer</span><span class="p">)</span> </code></pre> </div> <p>Output:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">Gojo</span> <span class="mi">28</span> <span class="mi">9999</span> <span class="mf">2.5</span> <span class="bp">True</span> </code></pre> </div> <p>Simple.</p> <p>But this is already how real applications begin.</p> <h1> Quick Summary 🧠 </h1> <div class="table-wrapper-paragraph"><table> <thead> <tr> <th>Data Type</th> <th>Example</th> <th>Purpose</th> </tr> </thead> <tbody> <tr> <td>Integer (<code>int</code>)</td> <td><code>100</code></td> <td>Whole numbers</td> </tr> <tr> <td>Float (<code>float</code>)</td> <td><code>9.5</code></td> <td>Decimal numbers</td> </tr> <tr> <td>Boolean (<code>bool</code>)</td> <td><code>True</code></td> <td>True/False logic</td> </tr> </tbody> </table></div> <h1> Final Thoughts </h1> <p>Understanding data types is a huge step in learning Python.</p> <p>At first, they may seem small and unimportant.</p> <p>But almost every Python program depends on them.</p> <p>Games.<br> Web apps.<br> AI systems.<br> Automation scripts.<br> Data science projects.</p> <p>Everything uses data types.</p> <p>So spend time practicing them.</p> <p>Create weird variables.<br> Experiment with anime examples.<br> Break things.<br> Fix things.</p> <p>That’s how real learning happens.</p> <h1> Practice Challenge ⚔️ </h1> <p>Try creating variables for:</p> <ul> <li>Your favorite anime</li> <li>Its rating</li> <li>Number of episodes</li> <li>Whether it is completed or ongoing</li> </ul> <p>Example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">anime_name</span> <span class="o">=</span> <span class="sh">"</span><span class="s">Attack on Titan</span><span class="sh">"</span> <span class="n">rating</span> <span class="o">=</span> <span class="mf">9.9</span> <span class="n">episodes</span> <span class="o">=</span> <span class="mi">89</span> <span class="n">is_completed</span> <span class="o">=</span> <span class="bp">True</span> </code></pre> </div> <p>Then use <code>type()</code> on each variable to see what Python says.</p> <h1> Wrapping Up </h1> <p>This is article #2 in the <strong>Python Basics</strong> series.</p> <p>In the next article, we’ll explore:</p> <p>👉 Strings in Python<br> 👉 String operations<br> 👉 Concatenation<br> 👉 Useful string methods<br> 👉 Common beginner mistakes</p> <p>Stay tuned 🐍✨</p> <p>Happy coding!</p>
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。