{"id":36,"date":"2026-07-17T11:20:12","date_gmt":"2026-07-17T08:20:12","guid":{"rendered":"https:\/\/danielmarcu.com\/blog\/?p=36"},"modified":"2026-07-17T11:20:12","modified_gmt":"2026-07-17T08:20:12","slug":"python-tutorial-from-0","status":"publish","type":"post","link":"https:\/\/danielmarcu.com\/blog\/?p=36","title":{"rendered":"Python Tutorial from 0"},"content":{"rendered":"<h1>The Complete Python Beginner&#8217;s Guide: From Zero to Your First Real Programs<\/h1>\n<p>This is a full walkthrough for learning Python from scratch \u2014 covering setup, core syntax, data structures, control flow, functions, files, error handling, and how to actually organize and run your own programs. Written for total beginners, but dense enough to get you writing real code, not just toy examples.<\/p>\n<p><a href=\"#\" id=\"toggle-full-article\" onclick=\"document.getElementById('full-article').style.display='block'; this.parentElement.style.display='none'; return false;\">Click to read the full guide \u2192<\/a><\/p>\n<div id=\"full-article\" style=\"display:none;\">\n<h2>Step 1: Install Python<\/h2>\n<p>Download Python from <strong>python.org<\/strong> (get the latest 3.x version \u2014 Python 2 is long dead and shouldn&#8217;t be used). During installation on Windows, check the box that says <strong>&#8220;Add Python to PATH&#8221;<\/strong> \u2014 skipping this is the single most common reason beginners can&#8217;t run Python from the command line afterward.<\/p>\n<p>Verify it worked by opening a terminal (Command Prompt, PowerShell, or Terminal on Mac\/Linux) and typing:<\/p>\n<pre><code>python --version<\/code><\/pre>\n<p>On some systems (especially Mac\/Linux) you may need <code>python3<\/code> instead of <code>python<\/code>. If neither works, the PATH wasn&#8217;t set correctly and you&#8217;ll need to reinstall or manually add it.<\/p>\n<h2>Step 2: Pick an Editor<\/h2>\n<p>You technically only need a text editor and the terminal, but a proper code editor makes life much easier:<\/p>\n<ul>\n<li><strong>VS Code<\/strong> \u2014 free, extremely popular, install the official Python extension for syntax highlighting, debugging, and autocomplete.<\/li>\n<li><strong>PyCharm (Community Edition)<\/strong> \u2014 free, more full-featured out of the box, heavier to run.<\/li>\n<li><strong>IDLE<\/strong> \u2014 comes bundled with Python itself; barebones but works fine for quick scripts while learning.<\/li>\n<\/ul>\n<p>Create a folder for your practice scripts and save every file with a <code>.py<\/code> extension.<\/p>\n<h2>Step 3: Run Your First Script<\/h2>\n<p>Create a file called <code>hello.py<\/code> with:<\/p>\n<pre><code>print(\"Hello, world!\")<\/code><\/pre>\n<p>Run it from the terminal by navigating to that folder and typing:<\/p>\n<pre><code>python hello.py<\/code><\/pre>\n<p><code>print()<\/code> is a function \u2014 it takes whatever is inside the parentheses and displays it. You&#8217;ll use it constantly for both real output and debugging.<\/p>\n<h2>Step 4: Variables and Basic Types<\/h2>\n<p>A variable is just a name pointing to a value. Python doesn&#8217;t require declaring a type up front:<\/p>\n<pre><code>name = \"Alex\"        # string (text)\r\nage = 25              # integer (whole number)\r\nheight = 5.9          # float (decimal number)\r\nis_student = True     # boolean (True\/False)<\/code><\/pre>\n<p>Check any variable&#8217;s type with the built-in <code>type()<\/code> function:<\/p>\n<pre><code>print(type(age))   # &lt;class 'int'&gt;<\/code><\/pre>\n<p>Combine text and variables using an f-string (the modern, preferred way):<\/p>\n<pre><code>print(f\"{name} is {age} years old.\")<\/code><\/pre>\n<h2>Step 5: Core Data Structures<\/h2>\n<p>These four structures cover the vast majority of everyday Python code:<\/p>\n<ul>\n<li><strong>List<\/strong> \u2014 an ordered, changeable collection: <code>fruits = [\"apple\", \"banana\", \"cherry\"]<\/code>. Access items by index: <code>fruits[0]<\/code> gives <code>\"apple\"<\/code> (Python counts from 0).<\/li>\n<li><strong>Tuple<\/strong> \u2014 like a list, but unchangeable once created: <code>point = (4, 7)<\/code>. Used when data shouldn&#8217;t be modified accidentally.<\/li>\n<li><strong>Dictionary<\/strong> \u2014 key-value pairs, like a lookup table: <code>person = {\"name\": \"Alex\", \"age\": 25}<\/code>. Access with <code>person[\"name\"]<\/code>.<\/li>\n<li><strong>Set<\/strong> \u2014 an unordered collection of unique values, useful for removing duplicates or checking membership fast: <code>unique_ids = {1, 2, 3}<\/code>.<\/li>\n<\/ul>\n<p>Useful list operations you&#8217;ll use constantly:<\/p>\n<pre><code>fruits.append(\"mango\")     # add to the end\r\nfruits.remove(\"banana\")    # remove a specific value\r\nlen(fruits)                 # get the count of items\r\n\"apple\" in fruits            # check membership -&gt; True\/False<\/code><\/pre>\n<h2>Step 6: Conditionals<\/h2>\n<p>Conditionals let your program make decisions:<\/p>\n<pre><code>age = 20\r\n\r\nif age &lt; 13:\r\n    print(\"Child\")\r\nelif age &lt; 20:\r\n    print(\"Teenager\")\r\nelse:\r\n    print(\"Adult\")<\/code><\/pre>\n<p>Two things trip up beginners immediately: Python uses <strong>indentation<\/strong> (not curly braces) to define code blocks, and every conditional line ends with a colon. Getting indentation wrong causes actual errors, not just style complaints.<\/p>\n<h2>Step 7: Loops<\/h2>\n<p>Loops repeat code. The two you&#8217;ll use constantly:<\/p>\n<pre><code># for loop - repeat for each item in a collection\r\nfor fruit in fruits:\r\n    print(fruit)\r\n\r\n# while loop - repeat as long as a condition is true\r\ncount = 0\r\nwhile count &lt; 5:\r\n    print(count)\r\n    count += 1<\/code><\/pre>\n<p>Two useful loop-control keywords: <code>break<\/code> exits a loop early, <code>continue<\/code> skips to the next iteration without finishing the current one.<\/p>\n<p>The <code>range()<\/code> function is the standard way to loop a specific number of times:<\/p>\n<pre><code>for i in range(5):     # 0, 1, 2, 3, 4\r\n    print(i)<\/code><\/pre>\n<h2>Step 8: Functions<\/h2>\n<p>Functions let you package reusable logic instead of copy-pasting code:<\/p>\n<pre><code>def greet(name):\r\n    return f\"Hello, {name}!\"\r\n\r\nmessage = greet(\"Alex\")\r\nprint(message)<\/code><\/pre>\n<ul>\n<li><code>def<\/code> starts a function definition, followed by the name and parameters in parentheses.<\/li>\n<li><code>return<\/code> sends a value back to wherever the function was called \u2014 without it, the function returns <code>None<\/code>.<\/li>\n<li>Give parameters default values when useful: <code>def greet(name=\"friend\"):<\/code> lets you call <code>greet()<\/code> with no arguments.<\/li>\n<\/ul>\n<h2>Step 9: String Manipulation<\/h2>\n<p>Strings come with a huge set of built-in methods you&#8217;ll use constantly:<\/p>\n<pre><code>text = \"  Hello World  \"\r\n\r\ntext.strip()          # removes leading\/trailing whitespace\r\ntext.lower()           # \"hello world\"\r\ntext.upper()           # \"HELLO WORLD\"\r\ntext.replace(\"World\", \"Python\")\r\ntext.split(\" \")        # splits into a list: [\"Hello\", \"World\"]\r\nlen(text)               # length of the string<\/code><\/pre>\n<p>Slicing lets you grab parts of a string (or list): <code>text[0:5]<\/code> gives the first 5 characters. Negative indices count from the end: <code>text[-1]<\/code> is the last character.<\/p>\n<h2>Step 10: Working with Files<\/h2>\n<p>Reading and writing files is a core everyday task:<\/p>\n<pre><code># Writing to a file\r\nwith open(\"notes.txt\", \"w\") as file:\r\n    file.write(\"Hello, file!\")\r\n\r\n# Reading from a file\r\nwith open(\"notes.txt\", \"r\") as file:\r\n    content = file.read()\r\n    print(content)<\/code><\/pre>\n<p>Always use the <code>with<\/code> statement (called a context manager) for file handling \u2014 it automatically closes the file when you&#8217;re done, even if an error occurs partway through, which prevents corrupted or locked files.<\/p>\n<p>File modes worth knowing: <code>\"r\"<\/code> read, <code>\"w\"<\/code> write (overwrites existing content), <code>\"a\"<\/code> append (adds to the end without erasing).<\/p>\n<h2>Step 11: Handling Errors Gracefully<\/h2>\n<p>Real programs deal with things going wrong \u2014 bad input, missing files, network failures. Use <code>try\/except<\/code> to handle errors instead of letting your program crash:<\/p>\n<pre><code>try:\r\n    number = int(input(\"Enter a number: \"))\r\n    print(100 \/ number)\r\nexcept ValueError:\r\n    print(\"That wasn't a valid number.\")\r\nexcept ZeroDivisionError:\r\n    print(\"Can't divide by zero.\")<\/code><\/pre>\n<p>Catching specific error types (like <code>ValueError<\/code> or <code>ZeroDivisionError<\/code>) rather than a bare <code>except:<\/code> is better practice \u2014 it means you handle the errors you expect and still see unexpected ones clearly instead of silently swallowing bugs.<\/p>\n<h2>Step 12: Organizing Code into Modules<\/h2>\n<p>As programs grow, splitting code across multiple files keeps things manageable. Any <code>.py<\/code> file can be imported into another:<\/p>\n<pre><code># math_helpers.py\r\ndef square(x):\r\n    return x * x<\/code><\/pre>\n<pre><code># main.py\r\nfrom math_helpers import square\r\nprint(square(5))   # 25<\/code><\/pre>\n<p>Python&#8217;s standard library also ships with useful built-in modules you&#8217;ll reach for constantly: <code>random<\/code> (random numbers\/choices), <code>datetime<\/code> (dates and times), <code>os<\/code> (file system and environment access), and <code>math<\/code> (extended math functions).<\/p>\n<h2>Step 13: Installing Third-Party Packages<\/h2>\n<p>The standard library covers a lot, but the real power of Python comes from its package ecosystem. Install packages using <strong>pip<\/strong>, Python&#8217;s package manager, from the terminal:<\/p>\n<pre><code>pip install requests<\/code><\/pre>\n<p>Then use it in your code:<\/p>\n<pre><code>import requests\r\nresponse = requests.get(\"https:\/\/example.com\")\r\nprint(response.status_code)<\/code><\/pre>\n<p>For any project with dependencies, use a <strong>virtual environment<\/strong> to keep packages isolated per-project instead of installed globally:<\/p>\n<pre><code>python -m venv myenv\r\nmyenv\\Scripts\\activate      # Windows\r\nsource myenv\/bin\/activate    # Mac\/Linux<\/code><\/pre>\n<p>This avoids version conflicts between different projects that need different package versions.<\/p>\n<h2>Step 14: Object-Oriented Basics<\/h2>\n<p>Classes let you bundle data and behavior together, which becomes essential as programs grow beyond simple scripts:<\/p>\n<pre><code>class Dog:\r\n    def __init__(self, name, breed):\r\n        self.name = name\r\n        self.breed = breed\r\n\r\n    def bark(self):\r\n        return f\"{self.name} says woof!\"\r\n\r\nmy_dog = Dog(\"Rex\", \"Labrador\")\r\nprint(my_dog.bark())<\/code><\/pre>\n<ul>\n<li><code>__init__<\/code> is the constructor \u2014 it runs automatically when you create a new instance of the class.<\/li>\n<li><code>self<\/code> refers to the specific instance being worked with; it&#8217;s always the first parameter of a method.<\/li>\n<li>You create (&#8220;instantiate&#8221;) an object by calling the class name like a function: <code>Dog(\"Rex\", \"Labrador\")<\/code>.<\/li>\n<\/ul>\n<h2>Step 15: Debugging Effectively<\/h2>\n<p>Bugs are inevitable \u2014 here&#8217;s how to find them efficiently rather than guessing:<\/p>\n<ul>\n<li><strong>Read the traceback carefully.<\/strong> Python&#8217;s error messages point to the exact file and line number where things broke, plus the error type \u2014 this is almost always your fastest lead.<\/li>\n<li><strong>Use print statements strategically<\/strong> to check variable values at different points in your code when something isn&#8217;t behaving as expected.<\/li>\n<li><strong>Use your editor&#8217;s debugger<\/strong> (VS Code and PyCharm both have one built in) to pause execution and inspect variables live, rather than relying only on print statements for complex bugs.<\/li>\n<li><strong>Isolate the problem<\/strong> \u2014 comment out sections or test a small snippet in isolation to narrow down exactly where things go wrong.<\/li>\n<\/ul>\n<h2>Step 16: Common Beginner Mistakes to Avoid<\/h2>\n<ul>\n<li><strong>Mixing up <code>=<\/code> and <code>==<\/code><\/strong> \u2014 a single equals sign assigns a value; double equals compares two values. Using one where you meant the other is one of the most common early bugs.<\/li>\n<li><strong>Inconsistent indentation<\/strong> \u2014 mixing tabs and spaces, or misaligning blocks, causes real errors in Python, unlike languages where whitespace is cosmetic.<\/li>\n<li><strong>Modifying a list while looping over it<\/strong> \u2014 this causes unpredictable skipped items; loop over a copy (<code>for item in list.copy():<\/code>) if you need to modify the original during iteration.<\/li>\n<li><strong>Not using virtual environments<\/strong> \u2014 installing every package globally eventually causes version conflicts between unrelated projects.<\/li>\n<li><strong>Copy-pasting code without understanding it<\/strong> \u2014 especially early on, type things out and test small pieces individually; it builds the mental model you&#8217;ll need for debugging your own original code later.<\/li>\n<\/ul>\n<p>That covers the full pipeline: setup, syntax fundamentals, data structures, control flow, functions, file handling, error handling, modules and packages, basic object-oriented programming, and debugging. From here, it&#8217;s repetition \u2014 building small real projects, hitting real bugs, and fixing them \u2014 until the language becomes second nature.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Complete Python Beginner&#8217;s Guide: From Zero to Your First Real Programs This is a full walkthrough for learning Python from scratch \u2014 covering setup, core syntax, data structures, control flow, functions, files, error handling, and how to actually organize and run your own programs. Written for total beginners, but dense enough to get you &hellip; <a href=\"https:\/\/danielmarcu.com\/blog\/?p=36\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python Tutorial from 0&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":1,"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danielmarcu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}