{"id":1445,"date":"2026-03-27T13:02:49","date_gmt":"2026-03-27T13:02:49","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=1445"},"modified":"2026-03-29T15:33:09","modified_gmt":"2026-03-29T15:33:09","slug":"learn-python-basics","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/learn-python-basics\/","title":{"rendered":"Learn Python &#8211; Basics"},"content":{"rendered":"\n<p>Welcome to the world of Python! Whether you&#8217;re a student, a professional switching careers, or just curious about coding, Python is one of the best languages to start with. It&#8217;s simple, readable, and incredibly powerful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Python?<\/h2>\n\n\n\n<p>Python is a&nbsp;<strong>high-level, interpreted programming language<\/strong>. That means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>High-level<\/strong>: You write code in a way that&#8217;s close to human language.<\/li>\n\n\n\n<li><strong>Interpreted<\/strong>: Python runs your code line-by-line, making it easier to debug.<\/li>\n\n\n\n<li><strong>Versatile<\/strong>: Used in web development, data science, AI, automation, and more.<\/li>\n<\/ul>\n\n\n\n<p>Python is known for its&nbsp;<strong>clean syntax<\/strong>, which makes it ideal for beginners.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up Python<\/h2>\n\n\n\n<p>To start coding:<\/p>\n\n\n\n<p><strong>Install Python<\/strong>&nbsp;from python.org. Once installed, open terminal and check if python has been installed successfully.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python --version<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<p>Let&#8217;s write our first Python file, say&nbsp;<code>hello.py<\/code>, which can be done in any text editor like notepad or vim\/nano (in linux). Now type the following code in the file and save it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># hello.py\nprint(\"Hello, world!\")\n<\/code><\/pre>\n\n\n\n<p>Now, open your command line or terminal, navigate to the directory where you saved your file, and run following command to execute the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python hello.py<\/code><\/pre>\n\n\n\n<p>The output should be: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, World!<\/code><\/pre>\n\n\n\n<p>And just like that, you&#8217;ve run your first Python script!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Basics<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.&nbsp;<strong>Variables and Data Types<\/strong><\/h3>\n\n\n\n<p>Variables are like containers that store data. You can name them anything (following naming rules), and Python automatically figures out the type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"       # String (text)\nage = 25             # Integer (whole number)\nheight = 5.6         # Float (decimal number)\nis_student = True    # Boolean (True\/False)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Strings are text.<\/li>\n\n\n\n<li>Integers are whole numbers.<\/li>\n\n\n\n<li>Floats are decimal numbers.<\/li>\n\n\n\n<li>Booleans are either&nbsp;<code>True<\/code>&nbsp;or&nbsp;<code>False<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.&nbsp;<strong>Operators<\/strong><\/h3>\n\n\n\n<p>Operators let you perform actions on variables.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Arithmetic Operators:\nx = 10 + 5   # Addition\ny = 10 - 3   # Subtraction\nz = 4 * 2    # Multiplication\na = 10 \/ 2   # Division\n\n# Comparison Operators:\n# Used to compare values. They return True or False.\n\nprint(x &gt; y)   # Greater than\nprint(x == y)  # Equal to\n\n# Logical Operators:\n# Used to combine conditions.\n\nprint(True and False)  # False\nprint(True or False)   # True<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.&nbsp;<strong>Control Flow<\/strong><\/h3>\n\n\n\n<p>Control flow lets your program make decisions and repeat actions.<\/p>\n\n\n\n<p><strong><code>if<\/code>\u00a0Statement:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if age &gt; 18:\n    print(\"You are an adult.\")\nelse:\n    print(\"You are a minor.\")\n<\/code><\/pre>\n\n\n\n<p><strong><code>elif<\/code><\/strong> <strong>Statement<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 200\nb = 33\nif b > a:\n  print(\"b is greater than a\")\nelif a == b:\n  print(\"a and b are equal\")\nelse:\n  print(\"a is greater than b\")<\/code><\/pre>\n\n\n\n<p><strong><code>for<\/code>&nbsp;Loop:<\/strong><\/p>\n\n\n\n<p>Used to repeat actions a fixed number of times.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)\n<\/code><\/pre>\n\n\n\n<p>This prints numbers from 0 to 4.<\/p>\n\n\n\n<p><strong><code>while<\/code>&nbsp;Loop:<\/strong><\/p>\n\n\n\n<p>Repeats as long as a condition is true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Keep asking for input until the user enters \"stop\"\nuser_input = \"\"\nwhile user_input != \"stop\":\n    user_input = input(\"Enter a command (type 'stop' to exit): \")\n    print(f\"You entered: {user_input}\")\n\n<\/code><\/pre>\n\n\n\n<p><strong>Important Note: <\/strong>In\u00a0Python a\u00a0<strong><code>for<\/code>\u00a0loop<\/strong>\u00a0is used for\u00a0<strong>definite iteration<\/strong>\u00a0over a sequence of items (like a list, string, or range), where the number of iterations is known in advance. A\u00a0<strong><code>while<\/code>\u00a0loop<\/strong>\u00a0is used for\u00a0<strong>indefinite iteration<\/strong>, repeating a block of code as long as a specified condition remains\u00a0<code>True<\/code>, making it ideal when the number of iterations is unknown beforehand.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Data Structures<\/h2>\n\n\n\n<p>Python has built-in structures to store collections of data.<\/p>\n\n\n\n<p>1.&nbsp;<strong>List<\/strong>&nbsp;\u2013 Ordered, mutable<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nprint(fruits&#91;0])  # apple\n<\/code><\/pre>\n\n\n\n<p>2.&nbsp;<strong>Tuple<\/strong>&nbsp;\u2013 Ordered, immutable<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>point = (10, 20)\n<\/code><\/pre>\n\n\n\n<p>3. <strong>Dictionary<\/strong>&nbsp;\u2013 Key-value pairs, just like JSON object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25}\nprint(person&#91;\"name\"])  # Alice\n<\/code><\/pre>\n\n\n\n<p>4.&nbsp;<strong>Set<\/strong>&nbsp;\u2013 Unordered, unique items<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unique_numbers = {1, 2, 3, 2}\nprint(unique_numbers)  # {1, 2, 3}\n<\/code><\/pre>\n\n\n\n<p>Here\u2019s a <strong>comparative table<\/strong> that clearly explains the differences between Python\u2019s core data structures \u2014 <strong>List<\/strong>, <strong>Tuple<\/strong>, <strong>Set<\/strong>, and <strong>Dictionary<\/strong> \u2014 with detailed descriptions and examples:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pythondatastructurescomparison\">Python Data Structures Comparison<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th><strong>List<\/strong><\/th><th><strong>Tuple<\/strong><\/th><th><strong>Set<\/strong><\/th><th><strong>Dictionary<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Definition<\/strong><\/td><td>Ordered, mutable collection<\/td><td>Ordered, immutable collection<\/td><td>Unordered, mutable, unique items<\/td><td>Unordered collection of key-value pairs<\/td><\/tr><tr><td><strong>Syntax<\/strong><\/td><td><code>fruits = [\"apple\", \"banana\"]<\/code><\/td><td><code>point = (10, 20)<\/code><\/td><td><code>nums = {1, 2, 3}<\/code><\/td><td><code>person = {\"name\": \"Alice\", \"age\": 25}<\/code><\/td><\/tr><tr><td><strong>Ordering<\/strong><\/td><td>Maintains order<\/td><td>Maintains order<\/td><td>No guaranteed order<\/td><td>No guaranteed order (Python 3.7+ maintains insertion order)<\/td><\/tr><tr><td><strong>Mutability<\/strong><\/td><td>\u2705 Can be changed<\/td><td>\u274c Cannot be changed<\/td><td>\u2705 Can be changed<\/td><td>\u2705 Can be changed<\/td><\/tr><tr><td><strong>Duplicates Allowed<\/strong><\/td><td>\u2705 Yes<\/td><td>\u2705 Yes<\/td><td>\u274c No<\/td><td>\u2705 Keys must be unique; values can repeat<\/td><\/tr><tr><td><strong>Indexing<\/strong><\/td><td>\u2705 Yes (<code>fruits[0]<\/code>)<\/td><td>\u2705 Yes (<code>point[1]<\/code>)<\/td><td>\u274c No indexing<\/td><td>\u2705 Keys used for access (<code>person[\"name\"]<\/code>)<\/td><\/tr><tr><td><strong>Use Case<\/strong><\/td><td>Store ordered items like a list<\/td><td>Fixed data like coordinates<\/td><td>Unique items like tags or IDs<\/td><td>Map relationships like name-age pairs<\/td><\/tr><tr><td><strong>Common Methods<\/strong><\/td><td><code>.append()<\/code>, <code>.remove()<\/code>, <code>.sort()<\/code><\/td><td><code>.count()<\/code>, <code>.index()<\/code><\/td><td><code>.add()<\/code>, <code>.remove()<\/code>, <code>.union()<\/code><\/td><td><code>.keys()<\/code>, <code>.values()<\/code>, <code>.items()<\/code><\/td><\/tr><tr><td><strong>Performance<\/strong><\/td><td>Slower than tuple for iteration<\/td><td>Faster for iteration<\/td><td>Fast membership tests<\/td><td>Fast lookups via keys<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"summary\"> Summary<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>lists<\/strong> when you need an ordered, changeable collection.<\/li>\n\n\n\n<li>Use <strong>tuples<\/strong> when you want to protect data from being modified.<\/li>\n\n\n\n<li>Use <strong>sets<\/strong> when you need to store unique items and perform fast membership checks.<\/li>\n\n\n\n<li>Use <strong>dictionaries<\/strong> when you want to associate keys with values for fast lookups.<\/li>\n<\/ul>\n\n\n\n<p>Would you like a visual diagram of this comparison or a downloadable Markdown\/PDF version for your blog or documentation?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functions<\/h2>\n\n\n\n<p>Functions are reusable blocks of code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name):\n    return f\"Hello, {name}!\"\n\nprint(greet(\"Alice\"))<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>def<\/code>&nbsp;defines a function.<\/li>\n\n\n\n<li><code>return<\/code>&nbsp;sends back a result.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Mini Project: Age Calculator<\/h2>\n\n\n\n<p>Let\u2019s build a simple python project step by step and run it.<\/p>\n\n\n\n<p>1. Setup a project directory let&#8217;s say age-calculator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir age-calculator<\/code><\/pre>\n\n\n\n<p>2. Create a <span style=\"background-color: initial; font-family: inherit; font-size: inherit; text-align: initial; color: initial;\"><code>main.py<\/code><\/span> file in age-calculator directory and add following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Enter your name: \")\nage = int(input(\"Enter your age: \"))\nyears_to_100 = 100 - age\nprint(f\"Hi {name}, you'll turn 100 in {years_to_100} years!\")<\/code><\/pre>\n\n\n\n<p>3. Execute the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>D:\\Python\\age-calculator> python main.py\n# Output\nEnter your name: Tom\nEnter your age: 25\nHi Tom, you'll turn 100 in 75 years!<\/code><\/pre>\n\n\n\n<p>In the program-<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>input()<\/code>&nbsp;gets user input.<\/li>\n\n\n\n<li><code>int()<\/code>&nbsp;converts input to a number.<\/li>\n\n\n\n<li><code>f\"\"<\/code>&nbsp;is an f-string for formatting.<\/li>\n<\/ul>\n\n\n\n<p>That&#8217;s it! We have created our first python project.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the world of Python! Whether you&#8217;re a student, a professional switching careers, or just curious about coding, Python is one of the best languages to start with. It&#8217;s&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[292,293,294],"class_list":["post-1445","post","type-post","status-publish","format-standard","hentry","category-python","tag-learn-python","tag-python-basics","tag-python-for-beginners","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1445"}],"version-history":[{"count":56,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1445\/revisions"}],"predecessor-version":[{"id":1567,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1445\/revisions\/1567"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}