<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Priyamaurya on Medium]]></title>
        <description><![CDATA[Stories by Priyamaurya on Medium]]></description>
        <link>https://medium.com/@priyamaurya7781?source=rss-161c6e4b116a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5sfbR-8bXOjyTF38</url>
            <title>Stories by Priyamaurya on Medium</title>
            <link>https://medium.com/@priyamaurya7781?source=rss-161c6e4b116a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 21:22:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@priyamaurya7781/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Python’s Built-in Data Structures: Lists, Tuples, Dictionaries, and Sets]]></title>
            <link>https://medium.com/@priyamaurya7781/understanding-pythons-built-in-data-structures-lists-tuples-dictionaries-and-sets-ff44836a630d?source=rss-161c6e4b116a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff44836a630d</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[basic-python]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Priyamaurya]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 13:00:43 GMT</pubDate>
            <atom:updated>2026-11-04T13:00:43.624Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Introduction</strong></blockquote><p>Python provides several built-in data structures that make handling data much easier. Among them, Lists, Tuples, Dictionaries, and Sets are some of the most commonly used.</p><p>At first, they might look quite similar because all of them can store multiple values. However, each one has its own purpose and behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o8vHFYYLuBehf_7g" /><figcaption>Photo by <a href="https://unsplash.com/@agforl24?utm_source=medium&amp;utm_medium=referral">Tai Bui</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote><strong>List</strong></blockquote><p>A list is an ordered and mutable collection of elements in Python. It allows us to store multiple values in a single variable.<br>One of the best things about lists is that they can <strong>contain different types of data, such as integers, strings, floats, and even other lists</strong>.</p><p>Lists are created using square brackets ‘[]’.</p><p>Example:</p><pre>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;mango&quot;, &quot;orange&quot;]<br>print(fruits)</pre><p>Output:</p><pre>[&#39;apple&#39;, &#39;banana&#39;, &#39;mango&#39;, &#39;orange&#39;]</pre><h4>Important Features of List</h4><p>1. Ordered<br>Lists maintain the order in which elements are inserted. Each element has an index, starting from 0.</p><pre>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;mango&quot;]<br>print(fruits[0])<br>print(fruits[2])</pre><p>Output:</p><pre>apple<br>mango</pre><p>2. Mutable<br>Lists are mutable, which means we can modify their elements after creating them.</p><pre>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;mango&quot;]<br>fruits[1] = &quot;grapes&quot;<br>print(fruits)</pre><p>Output:</p><pre>[&#39;apple&#39;, &#39;grapes&#39;, &#39;mango&#39;]</pre><p>3. Allows Duplicate Values<br>A list can contain duplicate elements.</p><pre>numbers = [10, 20, 10, 30, 20]<br>print(numbers)</pre><p>Output:</p><pre>[10, 20, 10, 30, 20]</pre><p>4. Allows Different Data Types</p><pre>my_list = [10, &quot;Python&quot;, 3.5, True]<br>print(my_list)</pre><p><strong>Common List Methods</strong><br>Python provides several built-in methods to work with lists</p><p>append() — Adds an element at the end <br>insert() — Adds an element at a specific position <br>remove() — Removes a particular element <br>pop() — Removes an element by index (last element by default) <br>sort() — Sorts the list <br>reverse() — Reverses the list <br>clear() — Removes all elements</p><p>Example:</p><pre>numbers = [10, 20, 30]<br>numbers.append(40)<br>numbers.insert(1, 15)<br>numbers.remove(30)<br>print(numbers)</pre><p>Output:</p><pre>[10, 15, 20, 40]</pre><p>When should we use lists?<br>Lists are useful when we need to store multiple values and expect to add, remove, or update elements frequently. For example, storing student names, shopping items, or marks.</p><blockquote><strong>Tuple</strong></blockquote><p>A tuple is an ordered and immutable collection of elements. Just like lists, tuples can store multiple values of different data types.<br>The main difference is that tuples cannot be modified after they are created.</p><p>Tuples are created using parentheses ‘()’.</p><p>Example:</p><pre>student = (&quot;Priya&quot;, 22, &quot;Computer Science&quot;)<br>print(student)</pre><p>Output:</p><pre>(&#39;Priya&#39;, 22, &#39;Computer Science&#39;)</pre><h4>Important Features of Tuples</h4><p>1. Ordered<br>Tuples maintain the order of elements, and we can access them using indexing.</p><pre>student = (&quot;Priya&quot;, 22, &quot;Computer Science&quot;)<br>print(student[0])<br>print(student[1])</pre><p>Output:</p><pre>Priya<br>22</pre><p>2. Immutable<br>Once a tuple is created, its elements cannot be changed, added, or removed.</p><pre>numbers = (10, 20, 30)<br># This will raise a TypeError<br>numbers[1] = 50</pre><p>3. Allows Duplicate Values</p><pre>numbers = (10, 20, 10, 30)<br>print(numbers)</pre><p>Output:</p><pre>(10, 20, 10, 30)</pre><p>4. Allows Different Data Types</p><pre>my_tuple = (10, &quot;Python&quot;, 5.5, True)<br>print(my_tuple)</pre><p>Important Tuple Methods<br>Since tuples are immutable, they have fewer built-in methods than lists.</p><p>count() — Returns how many times an element appears <br>index() — Returns the index of an element</p><p>Example:</p><pre>numbers = (10, 20, 30, 20, 40)<br>print(numbers.count(20))<br>print(numbers.index(30))</pre><p>Output:</p><pre>2<br>2</pre><p>A small tip: If you want to create a tuple containing only one element, you must include a comma.<br>a = (10,)<br>Without the comma, Python treats it as an integer inside parentheses rather than a tuple.</p><p>When should we use tuples?<br>Tuples are useful when we have data that should remain unchanged. For example, storing coordinates, dates, or fixed configuration values.</p><blockquote><strong>Dictionary</strong></blockquote><p>A dictionary is a collection of data stored in the form of key-value pairs. Instead of accessing elements using their index, we access them using their keys.<br>Dictionaries are created using curly brackets ‘{}’ with each key and value separated by a colon ‘:’.</p><p>Example:</p><pre>student = {<br> &quot;name&quot;: &quot;Priya&quot;,<br> &quot;age&quot;: 22,<br> &quot;course&quot;: &quot;Python&quot;,<br> &quot;marks&quot;: 85<br>}<br>print(student)</pre><p>Output:</p><pre>{&#39;name&#39;: &#39;Priya&#39;, &#39;age&#39;: 22, &#39;course&#39;: &#39;Python&#39;, &#39;marks&#39;: 85}</pre><p>Here, “name” is a key and “Priya” is its value.</p><h4>Important Features of Dictionaries</h4><p>1. Stored as Key-Value Pairs<br>Every dictionary element consists of a key and its corresponding value.</p><pre>student = {&quot;name&quot;: &quot;Priya&quot;, &quot;age&quot;: 22}<br>print(student[&quot;name&quot;])</pre><p>Output:</p><pre>Priya</pre><p>2. Mutable<br>We can add, update, and remove elements from a dictionary.</p><pre>student = {&quot;name&quot;: &quot;Priya&quot;, &quot;age&quot;: 22}<br># Updating a value<br>student[&quot;age&quot;] = 23<br># Adding a new key-value pair<br>student[&quot;city&quot;] = &quot;Varanasi&quot;<br>print(student)</pre><p>Output:</p><pre>{&#39;name&#39;: &#39;Priya&#39;, &#39;age&#39;: 23, &#39;city&#39;: &#39;Varanasi&#39;}</pre><p>3. Keys Must Be Unique<br>A dictionary cannot have duplicate keys. If we use the same key more than once, the latest value will overwrite the previous one.</p><pre>student = {<br> &quot;name&quot;: &quot;Priya&quot;,<br> &quot;age&quot;: 22,<br> &quot;name&quot;: &quot;Shikha&quot;<br>}<br>print(student)</pre><p>Output:</p><pre>{&#39;name&#39;: &#39;Shikha&#39;, &#39;age&#39;: 22}</pre><p>4. Values Can Be Duplicated<br>Different keys can have the same value.</p><pre>marks = {<br> &quot;Maths&quot;: 90,<br> &quot;Physics&quot;: 90,<br> &quot;Chemistry&quot;: 85<br>}</pre><p>Common Dictionary Methods</p><p>keys() — Returns all keys <br>values() — Returns all values<br>items() — Returns key-value pairs <br>get() — Returns the value of a key <br>update() — Updates the dictionary <br>pop() — Removes a specified key <br>clear() — Removes all elements</p><p>Example:</p><pre>student = {<br> &quot;name&quot;: &quot;Priya&quot;,<br> &quot;age&quot;: 22,<br> &quot;course&quot;: &quot;Python&quot;<br>}<br>print(student.keys())<br>print(student.values())<br>print(student.get(&quot;name&quot;))</pre><p>Output:</p><pre>dict_keys([&#39;name&#39;, &#39;age&#39;, &#39;course&#39;])<br>dict_values([&#39;Priya&#39;, 22, &#39;Python&#39;])<br>Priya</pre><p>When should we use dictionaries?<br>Dictionaries are useful when we need to store related information and retrieve it using meaningful keys. For example, student records, employee details, product information, and user profiles.</p><blockquote><strong>Set</strong></blockquote><p>A set is an unordered collection of unique elements. The most important thing about sets is that they do not allow duplicate values.<br>Sets are created using curly brackets ‘{}’.</p><p>Example:</p><pre>numbers = {10, 20, 30, 40, 20, 10}<br>print(numbers)</pre><p>Output (order may vary):</p><pre>{10, 20, 30, 40}</pre><p>Notice that duplicate values are automatically removed.</p><h4>Important Features of Sets</h4><p>1. Does Not Allow Duplicates<br>Every element in a set must be unique.</p><pre>numbers = {1, 2, 2, 3, 3, 4}<br>print(numbers)</pre><p>Output:</p><pre>{1, 2, 3, 4}</pre><p>2. Unordered<br>Sets do not maintain a guaranteed insertion order. Therefore, we cannot access elements using indexing.</p><pre>numbers = {10, 20, 30}<br># This will raise a TypeError<br>print(numbers[0])</pre><p>3. Mutable<br>We can add or remove elements from a set after creating it.</p><pre>numbers = {10, 20, 30}<br>numbers.add(40)<br>numbers.remove(20)<br>print(numbers)</pre><p>Output (order may vary):</p><pre>{10, 30, 40}</pre><p>4. Supports Mathematical Operations<br>This is one of the most useful features of sets. Python allows us to perform operations like union, intersection, and difference.</p><p>Let’s understand them with an example.</p><p>A = {1, 2, 3, 4}<br>B = {3, 4, 5, 6}</p><p>Union: Returns all unique elements from both sets.</p><pre>print(A | B)</pre><p>Output:</p><pre>{1, 2, 3, 4, 5, 6}</pre><p>Intersection: Returns only the common elements.</p><pre>print(A &amp; B)</pre><p>Output:</p><pre>{3, 4}</pre><p>Difference: Returns elements present in the first set but not in the second.</p><pre>print(A - B)</pre><p>Output:</p><pre>{1, 2}</pre><p>Common Set Methods</p><p>add() — Adds an element <br>update() — Adds multiple elements <br>remove() — Removes an element (raises an error if missing) <br>discard() — Removes an element without an error if missing <br>union() — Combines two sets <br>intersection() — Returns common elements <br>difference() — Returns elements present only in the first set <br>clear() — Removes all elements</p><p>Note: An empty set is created using ‘set()’, because ‘{}’ creates an empty dictionary.</p><p>When should we use sets?<br>Sets are useful when we want to remove duplicate values, find common elements between collections, or perform mathematical set operations.<br>For example, finding unique email addresses or identifying common students between two classes.</p><blockquote><strong>Conclusion</strong></blockquote><p>Lists, Tuples, Dictionaries, and Sets are four important data structures in Python. Although they all help us store multiple values, each has a different purpose.</p><p>- Use Lists when you need an ordered collection that can be modified.<br>- Use Tuples when you want to store data that should remain unchanged.<br>- Use Dictionaries when you want to store information in key-value pairs.<br>- Use Sets when you need unique elements and want to perform set operations.</p><p>The best way to get comfortable with them is to practise different examples and solve small problems using each one.</p><h4>Keep practising, keep experimenting, and happy coding!</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff44836a630d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn Python lambda functions with simple explanations and examples for beginners.]]></title>
            <link>https://medium.com/@priyamaurya7781/learn-python-lambda-functions-with-simple-explanations-and-examples-for-beginners-df2c797f304f?source=rss-161c6e4b116a------2</link>
            <guid isPermaLink="false">https://medium.com/p/df2c797f304f</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Priyamaurya]]></dc:creator>
            <pubDate>Thu, 22 Oct 2026 14:17:38 GMT</pubDate>
            <atom:updated>2026-10-22T14:17:38.306Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Understanding Lambda Functions in Python: Syntax, Examples, and Uses</blockquote><p>When I started learning Python, I was familiar with defining functions using the def keyword. But when I came across lambda functions, I wondered why we needed another way to create functions when regular functions were already doing the job. Initially, the syntax seemed a little confusing, especially when lambda functions were used with map(), filter(), and reduce(). So, I decided to explore this concept in detail and understand not just how lambda functions work, but also when and why we should use them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Bc-LdYjDbQLEzC-B" /><figcaption>Photo by <a href="https://unsplash.com/@emilep?utm_source=medium&amp;utm_medium=referral">Emile Perron</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong><em>Introduction</em></strong></h3><p>Python is known for its simple syntax and readability. One of its interesting features is the lambda function, which allows us to create small functions in a single line of code.<br>When learning Python, we usually begin by defining functions using the ‘def’ keyword. These functions help us organize code, reuse logic, and perform specific tasks. However, Python also provides another way to create functions using the ‘lambda’ keyword.</p><p>For example, suppose we want to calculate the square of a number.</p><p>We can write a regular function like this:</p><p>def square(n):<br> return n * n<br>print(square(5))</p><p>Output:<br>25</p><p>But we can write the same logic using a lambda function:</p><p>square = lambda n: n * n<br>print(square(5))</p><p>Output:<br>25</p><p>Both approaches produce the same result, but their syntax is different.</p><p>This raises some interesting questions:</p><ul><li>What exactly is a lambda function?<br>- How does it work internally?<br>- What is the difference between ‘lambda’ and ‘def’?<br>- Why do we use lambda functions with map(), filter(), and reduce()?<br>- When should we use lambda functions, and when should we avoid them?<br>In this article, we will explore these questions step by step, starting with the fundamentals and gradually moving toward practical examples.</li></ul><h3><strong>What Is a Lambda Function in Python?</strong></h3><p>A lambda function is a <strong>small anonymous function</strong> that is defined using the <strong>‘lambda’ keyword.</strong></p><p>Let’s understand the important terms in this definition.</p><p><strong><em>Anonymous function</em></strong>: A function created without a conventional function name. Although a lambda expression can be assigned to a variable, the function itself does not have a name assigned through a ‘def’ declaration.</p><p><strong><em>Single expression</em></strong>: A lambda function contains one expression whose result becomes the function’s return value.</p><p>For example:</p><p>lambda x: x + 10<br>This lambda function accepts a parameter ‘x’ and adds ‘10’ to it.</p><p>However, simply writing the expression does not mean we have called the function. To execute it, we need to call the resulting function object.</p><p>One way to do that is to assign it to a variable:<br>add_ten = lambda x: x + 10</p><p>print(add_ten(5))<br>print(add_ten(20))</p><p>Output:<br>15<br>30</p><p>Here is what happens:</p><p>1. lambda x: x + 10 creates a function.<br>2. add_ten refers to that function.<br>3. When we write add_ten(5), the value ‘5’ is passed to ‘x’.<br>4. Python evaluates ‘x + 10’.<br>5. The result is returned automatically.</p><p>Notice that we did not write a ‘return’ statement. Lambda functions automatically return the value of their expression.</p><p>Why do we need lambda functions?<br>They allow us to write a function in a single line, making the code more concise and convenient.<br>For example, if we want to square a number or check whether a number is even, we can use a lambda function instead of creating a separate function for that task.</p><h3><strong>Understanding the Syntax of a Lambda Function</strong></h3><p>The general syntax is:</p><p><strong>lambda arguments: expression</strong></p><p>A lambda function consists of three main parts:</p><p>‘lambda’: The keyword used to create the function.<br>‘arguments’: The parameters that receive input values.<br>‘expression’: The operation whose result is returned.</p><h3>Lambda Functions vs. Regular Functions</h3><p>To understand lambda functions properly, we should compare them with regular functions.</p><p>Example 1: Using `def`</p><p>def add(a, b):<br> return a + b<br>print(add(10, 20))</p><p>Output:<br>30</p><p>Example 2: Using ‘lambda’</p><p>add = lambda a, b: a + b<br>print(add(10, 20))</p><p>Output:<br>30</p><p>Both functions behave the same way for these inputs.</p><p>However, they differ in how they are defined and what kinds of code they can contain.<br>A regular function is defined using the def keyword and is suitable for complex operations and reusable code. A lambda function is defined using the lambda keyword and is used for small, simple operations in a single expression. Regular functions can contain multiple statements, while lambda functions are limited to a single expression.</p><h3>Lambda Functions With Multiple Arguments</h3><p>A common misconception is that lambda functions can accept only one argument. That is incorrect.</p><p>A lambda function can accept zero or more parameters, subject to Python’s usual parameter rules.</p><p>Example 1: Finding the maximum of two numbers</p><p>maximum = lambda a, b: a if a &gt; b else b<br>print(maximum(15, 9))</p><p>Output:<br>15</p><h3>Understanding Lambda Functions With map()</h3><p>Now we come to one of the most useful applications of lambda functions.</p><p>The ‘map()’ function applies a given function to every item in an iterable, such as a list or tuple.</p><p>Syntax: <strong>map(function, iterable)</strong></p><p>Here:<br>- ‘function’ is the operation to apply.<br>- ‘iterable’ contains the values to process.<br>- ‘map()’ returns an iterator in Python 3.</p><p>Example 1: Square every number in a list</p><p>numbers = [1, 2, 3, 4, 5]<br>result = list(map(lambda x: x * x, numbers))<br>print(result)</p><p>Output:<br>[1, 4, 9, 16, 25]</p><p>Example 2: Convert strings to integers</p><p>numbers = [“10”, “20”, “30”, “40”]<br>result = list(map(lambda x: int(x), numbers))<br>print(result)</p><p>Output:<br>[10, 20, 30, 40]</p><h3>Understanding Lambda Functions With filter()</h3><p>The ‘filter()’ function selects items from an iterable based on a condition.</p><p>Syntax:<strong> filter(function, iterable)</strong></p><p>The function should return a truthy or falsy result for each item. Only the items whose results are truthy are retained.</p><p>Example 1: Filter even numbers</p><p>numbers = [1, 2, 3, 4, 5, 6, 7, 8]<br>result = list(filter(lambda x: x % 2 == 0, numbers))<br>print(result)</p><p>Output:<br>[2, 4, 6, 8]</p><p>Example 2: Filter words that start with a particular letter</p><p>words = [“apple”, “banana”, “avocado”, “mango”, “apricot”]<br>result = list(filter(lambda word: word.startswith(“a”), words))<br>print(result)</p><p>Output:<br>[‘apple’, ‘avocado’, ‘apricot’]</p><h3>Lambda Functions With ‘reduce()’</h3><p>reduce() combines the elements of an iterable into a single result by repeatedly applying a function.<br>The reduce() function is available in the ‘functools’ module.</p><p>Syntax: <strong>reduce(function, iterable)</strong></p><p>Example 1: Calculate the sum of all numbers</p><p>from functools import reduce<br>numbers = [1, 2, 3, 4, 5]<br>result = reduce(lambda a, b: a + b, numbers)<br>print(result)</p><p>Output:<br>15</p><p>How does it work?<br>The lambda accepts two values, a and b, and adds them.</p><p>reduce() performs the following steps:</p><p>1. Add 1 and 2 to get ‘3’.<br>2. Add the result 3 and the next number 3 to get ‘6’.<br>3. Add 6 and 4 to get ‘10’.<br>4. Add 10 and 5 to get ‘15’.</p><p>The final result is 15.</p><h3>Can Lambda Functions Have Default Arguments?</h3><p>Yes. Lambda functions support default arguments just like regular functions.</p><p>For example:</p><p>greet = lambda name=”Guest”: “Hello, “ + name</p><p>print(greet())<br>print(greet(“Priya”))</p><p>Output:</p><p>Hello, Guest<br>Hello, Priya</p><p>When no argument is provided, the default value ‘Guest’ is used.</p><p>When ‘Priya’ is provided, it replaces the default value.</p><h3>What Lambda Functions Cannot Do</h3><p>Understanding the limitations of lambda functions is just as important as understanding their uses.</p><p>1. They <strong>cannot contain multiple statements</strong> in their body</p><p>lambda x:<br> y = x + 2<br> y * 3</p><p>This is <strong>invalid </strong>because assignment is a statement, and the lambda body must be an expression.</p><p>2. They <strong>cannot contain ‘return’</strong> statements</p><p>lambda x: return x * 2</p><p>A lambda returns the value of its expression automatically.</p><p>Correct:</p><p>double = lambda x: x * 2<br>print(double(5))</p><p>Output:<br>10</p><p>3. They are <strong>not ideal for complex logic</strong></p><p>You can write complicated expressions using a lambda, but that doesn’t mean you should.<br>For example, nested conditional expressions might technically work, but they can make code difficult to maintain.<br>A regular function is often a better choice when logic requires multiple steps, meaningful variable names, exception handling, or detailed documentation.</p><p>And that’s it!! I hope this article helped you understand Python lambda functions and when to use them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df2c797f304f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>