{"id":3044,"date":"2026-06-20T03:46:31","date_gmt":"2026-06-20T03:46:31","guid":{"rendered":"https:\/\/tucumandevelopers.com\/index.php\/2026\/06\/20\/python-for-beginners-part-2-variables-data-types-numbers\/"},"modified":"2026-06-20T03:46:31","modified_gmt":"2026-06-20T03:46:31","slug":"python-for-beginners-part-2-variables-data-types-numbers","status":"publish","type":"post","link":"https:\/\/tucumandevelopers.com\/index.php\/2026\/06\/20\/python-for-beginners-part-2-variables-data-types-numbers\/","title":{"rendered":"Python for Beginners \u2014 Part 2: Variables, Data Types &#038; Numbers"},"content":{"rendered":"<div>\n<div>\n<div data-article-id=\"3945695\" id=\"article-body\">\n<p><em>Part 2 of a beginner-friendly series on learning Python from scratch.<\/em><\/p>\n<p>In <a href=\"https:\/\/dev.to\/ramesh_s_a8f0867d239e927c\/python-for-beginners-part-1-getting-started-syntax-42ho\">Part 1<\/a>, we installed Python, wrote our first program, and learned the syntax rules that hold everything together. Now it&#8217;s time to start storing and working with information \u2014 which means variables and data types.<\/p>\n<h2> <a name=\"what-is-a-variable\" href=\"#what-is-a-variable\"> <\/a> What is a Variable? <\/h2>\n<p>A variable is a name that points to a value stored in memory. Think of it as a labeled container you can put something into, and refer back to later by name. <\/p>\n<div>\n<pre><code><span>name<\/span> <span>=<\/span> <span>\"<\/span><span>Ramesh<\/span><span>\"<\/span> <span>age<\/span> <span>=<\/span> <span>25<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Unlike many other languages, Python doesn&#8217;t need you to declare a variable&#8217;s type ahead of time. You just assign a value with <code>=<\/code>, and Python figures out the type on its own. This is called <strong>dynamic typing<\/strong>. <\/p>\n<div>\n<pre><code><span>x<\/span> <span>=<\/span> <span>5<\/span> <span># x is an integer <\/span><span>x<\/span> <span>=<\/span> <span>\"<\/span><span>hello<\/span><span>\"<\/span> <span># now x is a string \u2014 totally legal in Python <\/span><\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>This flexibility is convenient, but it also means you need to be a little more careful \u2014 Python won&#8217;t stop you from changing a variable&#8217;s type halfway through your program, even if that wasn&#8217;t your intention.<\/p>\n<h2> <a name=\"variable-naming-rules\" href=\"#variable-naming-rules\"> <\/a> Variable Naming Rules <\/h2>\n<p>Python is strict about how variable names can look:<\/p>\n<ul>\n<li>Must start with a letter or an underscore (<code>_<\/code>) \u2014 never a number.<\/li>\n<li>Can only contain letters, numbers, and underscores.<\/li>\n<li>Cannot be a Python keyword (<code>class<\/code>, <code>for<\/code>, <code>if<\/code>, etc.).<\/li>\n<li>Are case-sensitive \u2014 <code>age<\/code>, <code>Age<\/code>, and <code>AGE<\/code> are three different variables. <\/li>\n<\/ul>\n<div>\n<pre><code><span>age<\/span> <span>=<\/span> <span>25<\/span> <span># valid <\/span><span>_age<\/span> <span>=<\/span> <span>25<\/span> <span># valid <\/span><span>age2<\/span> <span>=<\/span> <span>25<\/span> <span># valid <\/span><span>2<\/span><span>age<\/span> <span>=<\/span> <span>25<\/span> <span># invalid \u2014 cannot start with a number <\/span><span>my<\/span><span>-<\/span><span>age<\/span> <span>=<\/span> <span>25<\/span> <span># invalid \u2014 hyphens aren't allowed <\/span><\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<h3> <a name=\"naming-conventions\" href=\"#naming-conventions\"> <\/a> Naming conventions <\/h3>\n<p>Python&#8217;s style guide (PEP 8) recommends <code>snake_case<\/code> for variable names \u2014 lowercase words separated by underscores: <\/p>\n<div>\n<pre><code><span>first_name<\/span> <span>=<\/span> <span>\"<\/span><span>Ramesh<\/span><span>\"<\/span> <span>total_score<\/span> <span>=<\/span> <span>95<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<h2> <a name=\"assigning-multiple-variables\" href=\"#assigning-multiple-variables\"> <\/a> Assigning Multiple Variables <\/h2>\n<p>Python lets you assign several variables in a single line, which keeps code compact and readable. <\/p>\n<div>\n<pre><code><span># One value to multiple variables <\/span><span>x<\/span> <span>=<\/span> <span>y<\/span> <span>=<\/span> <span>z<\/span> <span>=<\/span> <span>10<\/span> <span># Multiple values to multiple variables <\/span><span>name<\/span><span>,<\/span> <span>age<\/span><span>,<\/span> <span>city<\/span> <span>=<\/span> <span>\"<\/span><span>Ramesh<\/span><span>\"<\/span><span>,<\/span> <span>25<\/span><span>,<\/span> <span>\"<\/span><span>Chennai<\/span><span>\"<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<h2> <a name=\"data-types-in-python\" href=\"#data-types-in-python\"> <\/a> Data Types in Python <\/h2>\n<p>Every value in Python belongs to a data type, which determines what kind of operations you can perform on it. Here are the core built-in types you&#8217;ll use constantly:<\/p>\n<div>\n<table>\n<thead>\n<tr>\n<th>Type<\/th>\n<th>Example<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>str<\/code><\/td>\n<td><code>\"hello\"<\/code><\/td>\n<td>Text<\/td>\n<\/tr>\n<tr>\n<td><code>int<\/code><\/td>\n<td><code>25<\/code><\/td>\n<td>Whole numbers<\/td>\n<\/tr>\n<tr>\n<td><code>float<\/code><\/td>\n<td><code>3.14<\/code><\/td>\n<td>Decimal numbers<\/td>\n<\/tr>\n<tr>\n<td><code>bool<\/code><\/td>\n<td> <code>True<\/code> \/ <code>False<\/code> <\/td>\n<td>Logical values<\/td>\n<\/tr>\n<tr>\n<td><code>list<\/code><\/td>\n<td><code>[1, 2, 3]<\/code><\/td>\n<td>Ordered, changeable collection<\/td>\n<\/tr>\n<tr>\n<td><code>tuple<\/code><\/td>\n<td><code>(1, 2, 3)<\/code><\/td>\n<td>Ordered, unchangeable collection<\/td>\n<\/tr>\n<tr>\n<td><code>dict<\/code><\/td>\n<td><code>{\"a\": 1}<\/code><\/td>\n<td>Key-value pairs<\/td>\n<\/tr>\n<tr>\n<td><code>set<\/code><\/td>\n<td><code>{1, 2, 3}<\/code><\/td>\n<td>Unordered, unique values<\/td>\n<\/tr>\n<tr>\n<td><code>NoneType<\/code><\/td>\n<td><code>None<\/code><\/td>\n<td>Represents &#8220;no value&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>We&#8217;ll dive deep into collections (list, tuple, dict, set) in Part 5. For now, let&#8217;s focus on the basics \u2014 strings, numbers, and booleans.<\/p>\n<h3> <a name=\"checking-a-variables-type\" href=\"#checking-a-variables-type\"> <\/a> Checking a variable&#8217;s type <\/h3>\n<p>Use the built-in <code>type()<\/code> function any time you want to confirm what you&#8217;re working with: <\/p>\n<div>\n<pre><code><span>x<\/span> <span>=<\/span> <span>25<\/span> <span>print<\/span><span>(<\/span><span>type<\/span><span>(<\/span><span>x<\/span><span>))<\/span> <span># &lt;class 'int'&gt; <\/span> <span>y<\/span> <span>=<\/span> <span>\"<\/span><span>hello<\/span><span>\"<\/span> <span>print<\/span><span>(<\/span><span>type<\/span><span>(<\/span><span>y<\/span><span>))<\/span> <span># &lt;class 'str'&gt; <\/span><\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>This is one of the most useful debugging habits you can build early on.<\/p>\n<h2> <a name=\"numbers-in-python\" href=\"#numbers-in-python\"> <\/a> Numbers in Python <\/h2>\n<p>Python has three numeric types you&#8217;ll run into regularly:<\/p>\n<ul>\n<li> <strong><code>int<\/code><\/strong> \u2014 whole numbers, positive or negative, with no limit on size: <code>10<\/code>, <code>-45<\/code>, <code>1000000<\/code> <\/li>\n<li> <strong><code>float<\/code><\/strong> \u2014 numbers with a decimal point: <code>3.14<\/code>, <code>-0.5<\/code>, <code>2.0<\/code> <\/li>\n<li> <strong><code>complex<\/code><\/strong> \u2014 numbers with an imaginary part, written with a <code>j<\/code>: <code>3 + 4j<\/code> (rare for beginners, but good to know it exists) <\/li>\n<\/ul>\n<div>\n<pre><code><span>x<\/span> <span>=<\/span> <span>10<\/span> <span># int <\/span><span>y<\/span> <span>=<\/span> <span>3.14<\/span> <span># float <\/span><span>z<\/span> <span>=<\/span> <span>3<\/span> <span>+<\/span> <span>4j<\/span> <span># complex <\/span> <span>print<\/span><span>(<\/span><span>type<\/span><span>(<\/span><span>x<\/span><span>),<\/span> <span>type<\/span><span>(<\/span><span>y<\/span><span>),<\/span> <span>type<\/span><span>(<\/span><span>z<\/span><span>))<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<h3> <a name=\"basic-arithmetic\" href=\"#basic-arithmetic\"> <\/a> Basic arithmetic <\/h3>\n<p>Python supports all the math operations you&#8217;d expect: <\/p>\n<div>\n<pre><code><span>a<\/span> <span>=<\/span> <span>10<\/span> <span>b<\/span> <span>=<\/span> <span>3<\/span> <span>print<\/span><span>(<\/span><span>a<\/span> <span>+<\/span> <span>b<\/span><span>)<\/span> <span># 13 \u2192 addition <\/span><span>print<\/span><span>(<\/span><span>a<\/span> <span>-<\/span> <span>b<\/span><span>)<\/span> <span># 7 \u2192 subtraction <\/span><span>print<\/span><span>(<\/span><span>a<\/span> <span>*<\/span> <span>b<\/span><span>)<\/span> <span># 30 \u2192 multiplication <\/span><span>print<\/span><span>(<\/span><span>a<\/span> <span>\/<\/span> <span>b<\/span><span>)<\/span> <span># 3.333... \u2192 division (always returns a float) <\/span><span>print<\/span><span>(<\/span><span>a<\/span> <span>\/\/<\/span> <span>b<\/span><span>)<\/span> <span># 3 \u2192 floor division (drops the decimal) <\/span><span>print<\/span><span>(<\/span><span>a<\/span> <span>%<\/span> <span>b<\/span><span>)<\/span> <span># 1 \u2192 modulus (remainder) <\/span><span>print<\/span><span>(<\/span><span>a<\/span> <span>**<\/span> <span>b<\/span><span>)<\/span> <span># 1000 \u2192 exponent (a to the power of b) <\/span><\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Note that <code>\/<\/code> always returns a <code>float<\/code>, even if the result is a whole number: <\/p>\n<div>\n<pre><code><span>print<\/span><span>(<\/span><span>10<\/span> <span>\/<\/span> <span>2<\/span><span>)<\/span> <span># 5.0, not 5 <\/span><\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<h2> <a name=\"type-casting\" href=\"#type-casting\"> <\/a> Type Casting <\/h2>\n<p>Sometimes you need to convert a value from one type to another \u2014 this is called <strong>casting<\/strong>. Python gives you simple functions for this: <\/p>\n<div>\n<pre><code><span>x<\/span> <span>=<\/span> <span>\"<\/span><span>25<\/span><span>\"<\/span> <span>y<\/span> <span>=<\/span> <span>int<\/span><span>(<\/span><span>x<\/span><span>)<\/span> <span># converts string \"25\" to integer 25 <\/span> <span>a<\/span> <span>=<\/span> <span>25<\/span> <span>b<\/span> <span>=<\/span> <span>str<\/span><span>(<\/span><span>a<\/span><span>)<\/span> <span># converts integer 25 to string \"25\" <\/span> <span>c<\/span> <span>=<\/span> <span>\"<\/span><span>3.14<\/span><span>\"<\/span> <span>d<\/span> <span>=<\/span> <span>float<\/span><span>(<\/span><span>c<\/span><span>)<\/span> <span># converts string \"3.14\" to float 3.14 <\/span><\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>This comes up constantly in real programs \u2014 for example, when you take user input (which always arrives as a string) and need to do math with it: <\/p>\n<div>\n<pre><code><span>user_input<\/span> <span>=<\/span> <span>input<\/span><span>(<\/span><span>\"<\/span><span>Enter your age: <\/span><span>\"<\/span><span>)<\/span> <span># this is a string, even if you type \"25\" <\/span><span>age<\/span> <span>=<\/span> <span>int<\/span><span>(<\/span><span>user_input<\/span><span>)<\/span> <span># now it's a usable integer <\/span><span>print<\/span><span>(<\/span><span>age<\/span> <span>+<\/span> <span>5<\/span><span>)<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>If you try to do math directly on the unconverted string, Python will raise a <code>TypeError<\/code> \u2014 so casting isn&#8217;t optional here, it&#8217;s required.<\/p>\n<h2> <a name=\"why-this-matters\" href=\"#why-this-matters\"> <\/a> Why This Matters <\/h2>\n<p>Dynamic typing is one of the reasons Python feels fast to write in \u2014 you spend less time declaring types and more time solving the actual problem. But that same flexibility is also where beginners get tripped up: a variable that started as a number can quietly become a string somewhere in your code, and the bug only shows up when you try to do math on it. Getting comfortable with <code>type()<\/code> and casting early will save you a lot of confusion later.<\/p>\n<h2> <a name=\"whats-next\" href=\"#whats-next\"> <\/a> What&#8217;s Next <\/h2>\n<p>In Part 3, we&#8217;ll cover <strong>strings and booleans<\/strong> \u2014 how to slice and format text, the most useful string methods, and how Python handles <code>True<\/code>\/<code>False<\/code> logic.<\/p>\n<hr>\n<p><em>This is Part 2 of an 8-part beginner Python series. Catch up on <a href=\"https:\/\/dev.to\/ramesh_s_a8f0867d239e927c\/python-for-beginners-part-1-getting-started-syntax-42ho\">Part 1: Getting Started &amp; Syntax<\/a>, or continue to Part 3 once it&#8217;s live.<\/em><\/p>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Fuente: <a href=\"https:\/\/dev.to\/ramesh_s_a8f0867d239e927c\/python-for-beginners-part-2-variables-data-types-numbers-mja\">Art\u00edculo original<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part 2 of a beginner-friendly series on learning Python from scratch. In Part 1, we installed Python, wrote our first program, and learned the syntax rules that hold everything together. Now it&#8217;s time to start storing and working with information \u2014 which means variables and data types. What is a Variable? A variable is a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3043,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[41],"tags":[],"class_list":["post-3044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devto"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/3044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/comments?post=3044"}],"version-history":[{"count":0,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/3044\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media\/3043"}],"wp:attachment":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media?parent=3044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/categories?post=3044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/tags?post=3044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}