{"id":17,"date":"2025-10-19T02:48:00","date_gmt":"2025-10-19T02:48:00","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=17"},"modified":"2025-12-17T10:42:26","modified_gmt":"2025-12-17T10:42:26","slug":"lesson-2-python-syntax-and-variables","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=17","title":{"rendered":"Lesson 2: Python Syntax and Variables"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this lesson, you\u2019ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python\u2019s basic syntax and indentation rules<\/li>\n\n\n\n<li>Naming conventions for variables<\/li>\n\n\n\n<li>How to declare, assign, and use variables<\/li>\n\n\n\n<li>Data types and dynamic typing in Python<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcd8 <strong>1. What is Python Syntax?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong> is the set of rules that defines how Python code must be written and formatted.<br>Python emphasizes <strong>readability and simplicity<\/strong> \u2014 indentation is used instead of braces <code>{}<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Indentation and Code Blocks<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, indentation (spaces at the start of a line) defines code blocks.<br>You must use <strong>consistent spaces<\/strong> \u2014 typically <strong>4 spaces per block<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if True:\n    print(\"This is inside the block.\")\n    print(\"Indentation defines scope.\")\nprint(\"This is outside the block.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is inside the block.\nIndentation defines scope.\nThis is outside the block.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c <strong>Incorrect Example (will cause error):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if True:\nprint(\"Error due to missing indentation\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Error:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IndentationError: expected an indented block\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde9 <strong>3. Comments in Python<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Comments are ignored by the interpreter and used to explain code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Type<\/th><th>Syntax<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>Single-line<\/td><td><code>#<\/code><\/td><td><code># This is a comment<\/code><\/td><\/tr><tr><td>Multi-line<\/td><td><code>'''...'''<\/code> or <code>\"\"\"...\"\"\"<\/code><\/td><td><code>''' This is a multi-line comment '''<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This is a single-line comment\n'''\nThis is a\nmulti-line comment\n'''\nprint(\"Comments make code easier to read.\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 <strong>4. Variables in Python<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>variable<\/strong> is a name that stores a value in memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable_name = value\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Sameer\"\nage = 25\nis_student = True\n\nprint(name)\nprint(age)\nprint(is_student)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\n25\nTrue\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>5. Variable Naming Rules<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Rule<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>Must start with a letter or underscore<\/td><td><code>_user<\/code>, <code>name<\/code> \u2705<\/td><\/tr><tr><td>Cannot start with a number<\/td><td><code>1name<\/code> \u274c<\/td><\/tr><tr><td>Case-sensitive<\/td><td><code>Name<\/code> \u2260 <code>name<\/code><\/td><\/tr><tr><td>Can contain letters, digits, underscores<\/td><td><code>user_name<\/code>, <code>age2<\/code> \u2705<\/td><\/tr><tr><td>Avoid reserved keywords<\/td><td><code>if<\/code>, <code>class<\/code>, <code>True<\/code>, etc. \u274c<\/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\">\ud83e\uddee <strong>6. Multiple Assignments<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python allows multiple assignments in one line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Examples:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Assign same value\nx = y = z = 10\nprint(x, y, z)\n\n# Assign different values\na, b, c = 1, 2, 3\nprint(a, b, c)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 10 10\n1 2 3\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd22 <strong>7. Dynamic Typing<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You don\u2019t need to declare variable types.<br>Python automatically assigns types based on values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10         # int\nx = \"Python\"   # now string\nx = 3.14       # now float\n\nprint(x)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3.14\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcca <strong>8. Basic Data Types<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Type<\/th><th>Example<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>int<\/code><\/td><td><code>x = 10<\/code><\/td><td>Integer numbers<\/td><\/tr><tr><td><code>float<\/code><\/td><td><code>y = 3.14<\/code><\/td><td>Decimal numbers<\/td><\/tr><tr><td><code>str<\/code><\/td><td><code>name = \"Alice\"<\/code><\/td><td>String\/text<\/td><\/tr><tr><td><code>bool<\/code><\/td><td><code>is_ready = True<\/code><\/td><td>True or False<\/td><\/tr><tr><td><code>list<\/code><\/td><td><code>fruits = [\"apple\", \"banana\"]<\/code><\/td><td>Ordered, mutable collection<\/td><\/tr><tr><td><code>tuple<\/code><\/td><td><code>colors = (\"red\", \"blue\")<\/code><\/td><td>Ordered, immutable collection<\/td><\/tr><tr><td><code>dict<\/code><\/td><td><code>student = {\"name\": \"Ali\", \"age\": 21}<\/code><\/td><td>Key-value pairs<\/td><\/tr><tr><td><code>set<\/code><\/td><td><code>nums = {1,2,3}<\/code><\/td><td>Unordered unique items<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10\nb = 3.5\nc = \"Python\"\nd = True\n\nprint(type(a))\nprint(type(b))\nprint(type(c))\nprint(type(d))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;class 'int'&gt;\n&lt;class 'float'&gt;\n&lt;class 'str'&gt;\n&lt;class 'bool'&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddea <strong>9. Practice Tasks<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Task 1:<\/strong> Create variables for your name, age, and city. Print them.<br>\u2705 <strong>Task 2:<\/strong> Swap two variable values (e.g., <code>x=5<\/code>, <code>y=10<\/code> \u2192 <code>x=10<\/code>, <code>y=5<\/code>).<br>\u2705 <strong>Task 3:<\/strong> Assign multiple values to variables in one line.<br>\u2705 <strong>Task 4:<\/strong> Create variables of all 4 basic types and print their data types.<br>\u2705 <strong>Task 5:<\/strong> Write a Python script that prints your name and year of birth using variables.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddf1 <strong>10. Summary<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Concept<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>Indentation<\/td><td>Defines code structure<\/td><\/tr><tr><td>Comment<\/td><td>Adds explanation, ignored by interpreter<\/td><\/tr><tr><td>Variable<\/td><td>Stores data in memory<\/td><\/tr><tr><td>Dynamic Typing<\/td><td>Type changes automatically<\/td><\/tr><tr><td>Multiple Assignment<\/td><td>Assign values in one line<\/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\">\ud83e\uddee <strong>11. Mini Project: Variable Display App<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Goal:<\/strong><br>Create a simple program that displays user info dynamically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Personal Info Display\nname = \"Sameer Pasha\"\nprofession = \"Python Developer\"\nexperience = 2\n\nprint(\"Name:\", name)\nprint(\"Profession:\", profession)\nprint(\"Experience:\", experience, \"years\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Sameer Pasha\nProfession: Python Developer\nExperience: 2 years\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-063f6739-bf4e-4735-a775-c13c5f280951\" href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_2_Python_Syntax_and_Variables_Practice_Sheet.docx\">Lesson_2_Python_Syntax_and_Variables_Practice_Sheet<\/a><a href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_2_Python_Syntax_and_Variables_Practice_Sheet.docx\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-063f6739-bf4e-4735-a775-c13c5f280951\">Download<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/codetypingpro.com\/?p=317\" data-type=\"post\" data-id=\"317\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">more examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective By the end of this lesson, you\u2019ll understand: \ud83d\udcd8 1. What is Python Syntax? Syntax is the set of rules that defines how Python code must be written and formatted.Python emphasizes readability and simplicity \u2014 indentation is used instead of braces {}. \u2699\ufe0f 2. Indentation and Code Blocks In Python, indentation (spaces [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":4,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":326,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions\/326"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}