{"id":76,"date":"2025-10-21T04:34:17","date_gmt":"2025-10-21T04:34:17","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=76"},"modified":"2025-12-17T07:47:54","modified_gmt":"2025-12-17T07:47:54","slug":"lesson-16-comprehensions-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=76","title":{"rendered":"Lesson 16: Comprehensions in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To learn how to create <strong>lists, dictionaries, and sets efficiently<\/strong> using comprehensions, making Python code more concise and readable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is a Comprehension?<\/strong><\/h2>\n\n\n\n<p>A <strong>comprehension<\/strong> is a compact way to <strong>create collections<\/strong> (list, dict, set) in a single line using a <strong>for loop<\/strong> and optional <strong>condition<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. List Comprehension<\/strong><\/h2>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;expression for item in iterable if condition]\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 1: Squares of Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares = &#91;x**2 for x in range(1, 6)]\nprint(squares)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9, 16, 25]\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 2: Even Numbers Only<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>evens = &#91;x for x in range(10) if x % 2 == 0]\nprint(evens)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;0, 2, 4, 6, 8]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Dictionary Comprehension<\/strong><\/h2>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{key_expression: value_expression for item in iterable if condition}\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 1: Square Dictionary<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares_dict = {x: x**2 for x in range(1, 6)}\nprint(squares_dict)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 2: Filter Odd Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>odd_dict = {x: x**3 for x in range(10) if x % 2 != 0}\nprint(odd_dict)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{1: 1, 3: 27, 5: 125, 7: 343, 9: 729}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>4. Set Comprehension<\/strong><\/h2>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{expression for item in iterable if condition}\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example: Unique Squares<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#91;1, 2, 2, 3, 4, 4, 5]\nunique_squares = {x**2 for x in nums}\nprint(unique_squares)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{1, 4, 9, 16, 25}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f <strong>5. Nested Comprehension<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>Example: Multiplication Table<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>table = &#91;&#91;i*j for j in range(1, 6)] for i in range(1, 6)]\nfor row in table:\n    print(row)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 2, 3, 4, 5]\n&#91;2, 4, 6, 8, 10]\n&#91;3, 6, 9, 12, 15]\n&#91;4, 8, 12, 16, 20]\n&#91;5, 10, 15, 20, 25]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd01 <strong>6. Conditional Expressions in Comprehension<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>Example: Label Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>labels = &#91;\"Even\" if x % 2 == 0 else \"Odd\" for x in range(1, 11)]\nprint(labels)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even']\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>7. Real-Life Example \u2013 Filtering Names<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>names = &#91;\"Ali\", \"Sara\", \"John\", \"Lily\", \"Bob\"]\nshort_names = &#91;name for name in names if len(name) &lt;= 3]\nprint(short_names)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Ali', 'Bob']<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To learn how to create lists, dictionaries, and sets efficiently using comprehensions, making Python code more concise and readable. \ud83e\udde9 1. What Is a Comprehension? A comprehension is a compact way to create collections (list, dict, set) in a single line using a for loop and optional condition. \u2699\ufe0f 2. List Comprehension [&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-76","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\/76","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=76"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}