{"id":16,"date":"2025-10-19T02:50:39","date_gmt":"2025-10-19T02:50:39","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=16"},"modified":"2025-12-17T10:56:35","modified_gmt":"2025-12-17T10:56:35","slug":"lesson-3-operators-and-expressions","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=16","title":{"rendered":"Lesson 3: Operators and Expressions"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand different types of <strong>operators<\/strong> in Python and how to use them in <strong>expressions<\/strong> to perform calculations or logic.<\/p>\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>1. What Are Operators?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Operators are special symbols that perform operations on variables and values.<br><strong>Expressions<\/strong> are combinations of variables, operators, and values that produce a result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10\ny = 5\nresult = x + y\nprint(result)  # Output: 15\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\">\u2699\ufe0f <strong>2. Types of Operators<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>A. Arithmetic Operators<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used for mathematical operations.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td>Addition<\/td><td><code>5 + 3<\/code><\/td><td><code>8<\/code><\/td><\/tr><tr><td><code>-<\/code><\/td><td>Subtraction<\/td><td><code>5 - 3<\/code><\/td><td><code>2<\/code><\/td><\/tr><tr><td><code>*<\/code><\/td><td>Multiplication<\/td><td><code>5 * 3<\/code><\/td><td><code>15<\/code><\/td><\/tr><tr><td><code>\/<\/code><\/td><td>Division<\/td><td><code>10 \/ 3<\/code><\/td><td><code>3.3333<\/code><\/td><\/tr><tr><td><code>\/\/<\/code><\/td><td>Floor Division<\/td><td><code>10 \/\/ 3<\/code><\/td><td><code>3<\/code><\/td><\/tr><tr><td><code>%<\/code><\/td><td>Modulus (Remainder)<\/td><td><code>10 % 3<\/code><\/td><td><code>1<\/code><\/td><\/tr><tr><td><code>**<\/code><\/td><td>Exponentiation<\/td><td><code>2 ** 3<\/code><\/td><td><code>8<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = 10, 3\nprint(a + b, a - b, a * b, a \/ b, a \/\/ b, a % b, a ** b)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>B. Comparison Operators<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to compare two values. They return either <strong>True<\/strong> or <strong>False<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operator<\/th><th>Meaning<\/th><th>Example<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td><code>==<\/code><\/td><td>Equal to<\/td><td><code>5 == 5<\/code><\/td><td>True<\/td><\/tr><tr><td><code>!=<\/code><\/td><td>Not equal to<\/td><td><code>5 != 3<\/code><\/td><td>True<\/td><\/tr><tr><td><code>&gt;<\/code><\/td><td>Greater than<\/td><td><code>7 &gt; 4<\/code><\/td><td>True<\/td><\/tr><tr><td><code>&lt;<\/code><\/td><td>Less than<\/td><td><code>3 &lt; 5<\/code><\/td><td>True<\/td><\/tr><tr><td><code>&gt;=<\/code><\/td><td>Greater or equal<\/td><td><code>5 &gt;= 5<\/code><\/td><td>True<\/td><\/tr><tr><td><code>&lt;=<\/code><\/td><td>Less or equal<\/td><td><code>4 &lt;= 3<\/code><\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x, y = 10, 20\nprint(x &gt; y)  # False\nprint(x != y) # True\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C. Logical Operators<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to combine conditional statements.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td><code>and<\/code><\/td><td>True if both are true<\/td><td><code>x &gt; 5 and y &lt; 30<\/code><\/td><td>True<\/td><\/tr><tr><td><code>or<\/code><\/td><td>True if one is true<\/td><td><code>x &gt; 15 or y &lt; 30<\/code><\/td><td>True<\/td><\/tr><tr><td><code>not<\/code><\/td><td>Reverses the result<\/td><td><code>not(x &gt; 5)<\/code><\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x, y = 10, 20\nprint(x &gt; 5 and y &lt; 25)  # True\nprint(x &gt; 15 or y &lt; 25)  # True\nprint(not(x &gt; 5))        # False\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>D. Assignment Operators<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to assign values to variables.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operator<\/th><th>Example<\/th><th>Equivalent To<\/th><\/tr><\/thead><tbody><tr><td><code>=<\/code><\/td><td><code>x = 10<\/code><\/td><td><code>x = 10<\/code><\/td><\/tr><tr><td><code>+=<\/code><\/td><td><code>x += 2<\/code><\/td><td><code>x = x + 2<\/code><\/td><\/tr><tr><td><code>-=<\/code><\/td><td><code>x -= 3<\/code><\/td><td><code>x = x - 3<\/code><\/td><\/tr><tr><td><code>*=<\/code><\/td><td><code>x *= 2<\/code><\/td><td><code>x = x * 2<\/code><\/td><\/tr><tr><td><code>\/=<\/code><\/td><td><code>x \/= 2<\/code><\/td><td><code>x = x \/ 2<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 5\nx += 3\nprint(x)  # Output: 8\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>E. Bitwise Operators (Advanced)<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to perform operations on binary numbers.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td><code>&amp;<\/code><\/td><td>AND<\/td><td><code>5 &amp; 3<\/code><\/td><td><code>1<\/code><\/td><\/tr><tr><td>`<\/td><td>`<\/td><td>OR<\/td><td>`5<\/td><\/tr><tr><td><code>^<\/code><\/td><td>XOR<\/td><td><code>5 ^ 3<\/code><\/td><td><code>6<\/code><\/td><\/tr><tr><td><code>~<\/code><\/td><td>NOT<\/td><td><code>~5<\/code><\/td><td><code>-6<\/code><\/td><\/tr><tr><td><code>&lt;&lt;<\/code><\/td><td>Left shift<\/td><td><code>5 &lt;&lt; 1<\/code><\/td><td><code>10<\/code><\/td><\/tr><tr><td><code>&gt;&gt;<\/code><\/td><td>Right shift<\/td><td><code>5 &gt;&gt; 1<\/code><\/td><td><code>2<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = 5, 3\nprint(a &amp; b, a | b, a ^ b)\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. Expressions<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An <strong>expression<\/strong> is a combination of values, variables, and operators that evaluates to a result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10\ny = 5\nz = (x + y) * 2\nprint(z)  # Output: 30\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>4. Practice Ideas<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a Python program to calculate the area of a rectangle using variables.<\/li>\n\n\n\n<li>Check if a number is positive and even using comparison and logical operators.<\/li>\n\n\n\n<li>Create a program that swaps two variables using assignment operators.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-3d1394b0-c240-44aa-ba5f-bf013860794a\" href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_3_Operators_and_Expressions_Practice_Sheet.docx\">Lesson_3_Operators_and_Expressions_Practice_Sheet<\/a><a href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_3_Operators_and_Expressions_Practice_Sheet.docx\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-3d1394b0-c240-44aa-ba5f-bf013860794a\">Download<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/codetypingpro.com\/?p=324\" data-type=\"post\" data-id=\"324\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">more examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand different types of operators in Python and how to use them in expressions to perform calculations or logic. \ud83e\udde0 1. What Are Operators? Operators are special symbols that perform operations on variables and values.Expressions are combinations of variables, operators, and values that produce a result. Example: \u2699\ufe0f 2. Types of [&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-16","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\/16","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=16"}],"version-history":[{"count":2,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":329,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/329"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}