<?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 Shadrul on Medium]]></title>
        <description><![CDATA[Stories by Shadrul on Medium]]></description>
        <link>https://medium.com/@shadrul.100?source=rss-43217e16a1e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*OngNyJQr8dREdX4N</url>
            <title>Stories by Shadrul on Medium</title>
            <link>https://medium.com/@shadrul.100?source=rss-43217e16a1e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:26:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shadrul.100/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[Dictionaries in {Python}]]></title>
            <link>https://medium.com/@shadrul.100/dictionaries-in-python-e26099965df5?source=rss-43217e16a1e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e26099965df5</guid>
            <category><![CDATA[dictionary]]></category>
            <category><![CDATA[toilet-seat-articles]]></category>
            <category><![CDATA[dictionaries-in-python]]></category>
            <category><![CDATA[python-dictionaries]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Shadrul]]></dc:creator>
            <pubDate>Mon, 06 Feb 2023 15:41:46 GMT</pubDate>
            <atom:updated>2023-02-06T15:41:46.677Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KfSqSw7gfdDbs9wBizHqhw.png" /><figcaption>dictionaries in python</figcaption></figure><blockquote>Dictionaries are a collection of key-value pairs in Python, used to store and retrieve data efficiently. They are mutable, meaning that you can change their content, add new elements, or remove existing ones. In this article, we will discuss dictionaries in Python and their operations, including time complexities of each operation.</blockquote><p>A dictionary is defined using curly braces {} and is separated into key-value pairs using colons. For example, consider the following dictionary:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}</pre><p>In this example, the keys are “name”, “age”, and “country”, while the values are “John Doe”, 30, and “USA”, respectively. You can access the value of a particular key using the square bracket notation, as shown below:</p><pre>print(person[&quot;name&quot;]) # Output: John Doe</pre><p>In addition to accessing values, you can also change the value of a key, add new keys, or remove existing ones. Here are some examples:</p><pre>person[&quot;age&quot;] = 35 # change the value of the key &quot;age&quot;<br>person[&quot;gender&quot;] = &quot;male&quot; # add a new key-value pair<br>del person[&quot;country&quot;] # remove the key-value pair with key &quot;country&quot;</pre><p>Now that we have a basic understanding of dictionaries, let’s discuss the time complexities of various operations in dictionaries.</p><ol><li><strong>Accessing an element</strong>: The time complexity of accessing an element in a dictionary is <strong>O(1)</strong>. This means that, regardless of the size of the dictionary, accessing an element takes constant time.</li></ol><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br>print(person[&quot;name&quot;]) # Output: John Doe</pre><p>2. <strong>Adding an element</strong>: The time complexity of adding an element to a dictionary is <strong>O(1)</strong>. This means that adding an element takes constant time, regardless of the size of the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>person[&quot;gender&quot;] = &quot;male&quot; # add a new key-value pair<br>print(person) # Output: {&#39;name&#39;: &#39;John Doe&#39;, &#39;age&#39;: 30, &#39;country&#39;: &#39;USA&#39;, &#39;gender&#39;: &#39;male&#39;}</pre><p>3. <strong>Updating an element</strong>: The time complexity of updating an element in a dictionary is also <strong>O(1)</strong>. This means that updating an element takes constant time, regardless of the size of the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br>person[&quot;age&quot;] = 35 # change the value of the key &quot;age&quot;<br>print(person) # Output: {&#39;name&#39;: &#39;John Doe&#39;, &#39;age&#39;: 35, &#39;country&#39;: &#39;USA&#39;}</pre><p>4. <strong>Removing an element</strong>: The time complexity of removing an element from a dictionary is<strong> O(1)</strong>. This means that removing an element takes constant time, regardless of the size of the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>del person[&quot;country&quot;] # remove the key-value pair with key &quot;country&quot; <br>print(person) # Output: {&#39;name&#39;: &#39;John Doe&#39;, &#39;age&#39;: 30}</pre><p>In addition to the operations discussed above, dictionaries in Python also have several built-in methods that can be used to manipulate their contents. Some of the most commonly used methods are:</p><ol><li><strong>clear()</strong>: This method removes all key-value pairs from the dictionary. The time complexity of this operation is <strong>O(n)</strong>, where n is the number of elements in the dictionary.</li></ol><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>person.clear()<br>print(person) # Output: {}</pre><p>2.<strong> copy()</strong> : This method returns a shallow copy of the dictionary. The time complexity of this operation is <strong>O(n)</strong>, where n is the number of elements in the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br>person_copy = person.copy()<br>print(person_copy) # Output: {&#39;name&#39;: &#39;John Doe&#39;, &#39;age&#39;: 30, &#39;country&#39;: &#39;USA&#39;}</pre><p>3. <strong>get(key, default)</strong>: This method returns the value of the specified key, or the default value if the key is not found. The time complexity of this operation is <strong>O(1)</strong>.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>print(person.get(&quot;gender&quot;, &quot;unknown&quot;)) # Output: unknown</pre><p>4. <strong>items()</strong> : This method returns a list of tuples, each containing a key-value pair from the dictionary. The time complexity of this operation is <strong>O(n)</strong>, where n is the number of elements in the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>print(person.items()) # Output: [(&#39;name&#39;, &#39;John Doe&#39;), (&#39;age&#39;, 30), (&#39;country&#39;, &#39;USA&#39;)]</pre><p>5. <strong>keys()</strong> : This method returns a list of all the keys in the dictionary. The time complexity of this operation is <strong>O(n)</strong>, where n is the number of elements in the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br>print(person.keys()) # Output: [&#39;name&#39;, &#39;age&#39;, &#39;country&#39;]</pre><p>5. <strong>values() </strong>: This method returns a list of all the values in the dictionary. The time complexity of this operation is<strong> O(n)</strong>, where n is the number of elements in the dictionary.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>print(person.values()) # Output: [&#39;John Doe&#39;, 30, &#39;USA&#39;]</pre><p>In Python, you can iterate over the elements of a dictionary in several ways. Here are some of the most common methods:</p><ol><li><strong>Using </strong><strong>for loop</strong>: You can use a for loop to iterate over the keys of a dictionary, and use the keys to access the corresponding values.</li></ol><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>for key in person:<br>    print(key, &quot;:&quot;, person[key])</pre><p>Output:</p><pre>name : John Doe<br>age : 30<br>country : USA</pre><p><strong>2. Using </strong><strong>items() method</strong>: You can use the items() method to get a list of tuples, each containing a key-value pair from the dictionary, and then iterate over the tuples.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>for key, value in person.items():<br>    print(key, &quot;:&quot;, value)</pre><p>Output:</p><pre>name : John Doe<br>age : 30<br>country : USA</pre><p><strong>3. Using </strong><strong>keys() method</strong>: You can use the keys() method to get a list of all the keys in the dictionary, and then iterate over the keys.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>for key in person.keys():<br>    print(key, &quot;:&quot;, person[key])</pre><p>Output:</p><pre>name : John Doe<br>age : 30<br>country : USA</pre><p><strong>4. Using </strong><strong>values() method</strong>: You can use the values() method to get a list of all the values in the dictionary, and then iterate over the values.</p><p>Example:</p><pre>person = {<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;age&quot;: 30,<br>    &quot;country&quot;: &quot;USA&quot;<br>}<br><br>for value in person.values():<br>    print(value)</pre><p>Output:</p><pre>John Doe<br>30<br>USA</pre><h4><strong>DefaultDict</strong></h4><p>A defaultdict is a subclass of the built-in Python dict class that returns a <strong>default value for any key</strong> <strong>that doesn&#39;t exist in the dictionary</strong>. The default value is specified when you create the defaultdict object, and it can be any type of object, including a list or dict.</p><p>Here’s an example of how you can use a defaultdict to count the occurrences of words in a list of strings:</p><pre>from collections import defaultdict<br><br>words = [&quot;apple&quot;, &quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;, &quot;banana&quot;]<br>word_counts = defaultdict(int)<br>for word in words:<br>    word_counts[word] += 1<br>print(word_counts)</pre><p>Output:</p><pre>defaultdict(int, {&#39;apple&#39;: 2, &#39;banana&#39;: 2, &#39;cherry&#39;: 1})</pre><p>In this example, the defaultdict is created with the default value of int, which is 0. When a new word is encountered, the value for that word is set to 0, and then increased by 1 for each subsequent occurrence of that word. This eliminates the need to check whether a key exists in the dictionary before incrementing its value, since the defaultdict will always return a default value of 0 if the key doesn&#39;t exist.</p><blockquote><strong>Take Away :- Try using normal dict in place of defaultdict and notice the error.</strong></blockquote><p>You can also specify a custom default value by passing a function as the argument to the defaultdict constructor. For example, you could use a lambda function to return an empty list as the default value:</p><pre>from collections import defaultdict<br><br>words = [&quot;apple&quot;, &quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;, &quot;banana&quot;]<br>word_lists = defaultdict(list)<br>for word in words:<br>    word_lists[word].append(word)<br>print(word_lists)</pre><p>Output:</p><pre>defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;apple&#39;: [&#39;apple&#39;, &#39;apple&#39;], &#39;banana&#39;: [&#39;banana&#39;, &#39;banana&#39;], &#39;cherry&#39;: [&#39;cherry&#39;]})</pre><p>In this example, the defaultdict is created with a default value of an empty list, which is returned by the lambda function. Each time a new word is encountered, a new list is created for that word, and the word is appended to the list. This allows you to build up a list of words for each unique word in the input list.</p><blockquote>In conclusion, there are several ways to iterate over the elements of a dictionary in Python, and the method you choose will depend on your specific use case. Whether you need to access the keys, values, or both, these methods will help you iterate over the elements of a dictionary efficiently.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e26099965df5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comprehensions in {Python}]]></title>
            <link>https://medium.com/@shadrul.100/comprehensions-in-python-29eee72e5f?source=rss-43217e16a1e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/29eee72e5f</guid>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[list-comprehension]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[toilet-seat-articles]]></category>
            <dc:creator><![CDATA[Shadrul]]></dc:creator>
            <pubDate>Sun, 05 Feb 2023 06:12:08 GMT</pubDate>
            <atom:updated>2023-02-05T06:12:08.980Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bRz2HSZMHOn80vEho0fL3A.jpeg" /><figcaption>comprehensions in python</figcaption></figure><p>Comprehensions in Python are a concise and efficient way to manipulate lists, dictionaries, and sets. They are used to transform one iterable into another and can be used to simplify complex code into a single line. In this article, we will take a deep dive into comprehensions in Python, understand their syntax, and learn how to use them in different use cases.</p><h3>List comprehensions</h3><p>List comprehensions are a concise way to create lists in Python. They provide an elegant way to perform operations on a list and return a new list as the result. The syntax of a list comprehension is as follows:</p><pre>[expression for item in iterable if condition]</pre><p>The expression is evaluated for each item in the iterable and the results are collected into a new list. The condition is an optional clause that is used to filter the items in the iterable.</p><p>Let’s consider a simple example to understand the concept of list comprehensions. Let’s say we have a list of numbers and we want to create a new list that contains the square of each number in the original list. We can write the following code to do this:</p><pre>numbers = [1, 2, 3, 4, 5]<br>squared_numbers = [x**2 for x in numbers]<br>print(squared_numbers)</pre><p>Output:</p><pre>[1, 4, 9, 16, 25]</pre><p>In the above example, the expression x**2 is evaluated for each item in the numbers list and the results are collected into a new list. The code is concise and easy to understand, making it a great alternative to traditional for loops.</p><p>Let’s consider another example where we want to create a new list that contains only even numbers from the original list. We can write the following code to do this:</p><pre>numbers = [1, 2, 3, 4, 5]<br>even_numbers = [x for x in numbers if x % 2 == 0]<br>print(even_numbers)</pre><p>Output:</p><pre>[2, 4]</pre><p>In the above example, the expression x is evaluated for each item in the numbers list, and only the even numbers are collected into a new list. The condition x % 2 == 0 is used to filter the items in the list.</p><h3>Dictionary comprehensions</h3><p>Dictionary comprehensions are similar to list comprehensions, but they create dictionaries instead of lists. The syntax of a dictionary comprehension is as follows:</p><pre>{key_expression: value_expression for item in iterable if condition}</pre><p>Let’s consider an example to understand the concept of dictionary comprehensions. Let’s say we have a list of numbers and we want to create a dictionary where the keys are the numbers and the values are the squares of the numbers. We can write the following code to do this:</p><pre>numbers = [1, 2, 3, 4, 5]<br>squared_numbers = {x: x**2 for x in numbers}<br>print(squared_numbers)</pre><p>Output:</p><pre>{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</pre><p>In the above example, the key_expression is x and the value_expression is x**2. The code is concise and easy to understand, making it a great alternative to traditional dictionary construction using for loops.</p><h3>Set comprehensions</h3><p>Set comprehensions are similar to list and dictionary comprehensions, but they create sets instead of lists or dictionaries. The syntax of a set comprehension is as follows:</p><pre>{expression for item in iterable if condition}</pre><p>Let’s consider an example to understand the concept of set comprehensions. Let’s say we have a list of numbers and we want to create a set that contains the square of each number in the original list. We can write the following code to do this:</p><pre>numbers = [1, 2, 3, 4, 5]<br>squared_numbers = {x**2 for x in numbers}<br>print(squared_numbers)</pre><p>Output:</p><pre>{1, 4, 9, 16, 25}</pre><p>In the above example, the expression x**2 is evaluated for each item in the numbers list and the results are collected into a new set. The code is concise and easy to understand, making it a great alternative to traditional set construction using for loops.</p><p>In conclusion, comprehensions in Python are a concise and efficient way to manipulate lists, dictionaries, and sets. They provide an elegant way to perform operations on iterables and return new iterables as the result. The syntax of comprehensions is simple and easy to understand, making it a great alternative to traditional for loops. Whether you’re working with lists, dictionaries, or sets, comprehensions can help you simplify your code and make it easier to understand.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29eee72e5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading and Writing Files in Python: A Guide with Examples]]></title>
            <link>https://medium.com/@shadrul.100/reading-and-writing-files-in-python-a-guide-with-examples-f3c77a46c4f7?source=rss-43217e16a1e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3c77a46c4f7</guid>
            <category><![CDATA[toilet-seat-articles]]></category>
            <category><![CDATA[files-in-python]]></category>
            <category><![CDATA[python-file-handling]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-file-operation]]></category>
            <dc:creator><![CDATA[Shadrul]]></dc:creator>
            <pubDate>Sat, 04 Feb 2023 15:40:33 GMT</pubDate>
            <atom:updated>2023-02-04T15:40:33.472Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K7VZYSk5Z_2p9YDVdPnQ5w.jpeg" /><figcaption>files in python</figcaption></figure><p>Whether you’re working with text, binary, or CSV files, Python has you covered with built-in functions and third-party libraries. In this article, we will explore various operations you can perform on files in Python, including reading and writing, with detailed examples.</p><h4>Reading Files in Python</h4><p>In Python, there are several ways to read files, including reading the entire contents of the file as a string, reading the file line by line, or reading the file as a list of lines. The most common method for reading files is using the open() function, which opens a file for reading or writing and returns a file object.</p><p>Here’s an example of how to read an entire file as a string:</p><pre>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<br>    contents = file.read()<br>    print(contents)</pre><blockquote>The with statement is a convenient way to manage file objects, as it automatically closes the file when you&#39;re done with it. The open() function takes two arguments: the name of the file you want to open and the mode in which you want to open it. The mode &quot;r&quot; stands for reading, and there are several other modes you can use, such as &quot;w&quot; for writing, &quot;a&quot; for appending, and &quot;b&quot; for binary.</blockquote><p>If you want to read a file line by line, you can use a for loop:</p><pre>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<br>    for line in file:<br>        print(line)</pre><p>Alternatively, you can read the file into a list of lines and then access each line individually:</p><pre>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<br>    lines = file.readlines()<br>    for line in lines:<br>        print(line)</pre><h4>Reading Binary Files in Python</h4><p>Binary files are different from text files in that they store data in a raw, binary format, rather than as human-readable text. Examples of binary files include images, audio, and video files. To read binary files in Python, you must use the &quot;b&quot; mode when opening the file:</p><pre>with open(&quot;example.bin&quot;, &quot;rb&quot;) as file:<br>    contents = file.read()<br>    print(contents)</pre><h4>Reading CSV Files in Python</h4><p>CSV stands for Comma-Separated Values, and it’s a widely used file format for storing and exchanging tabular data. To read CSV files in Python, you can use the csv library, which provides several functions for reading and writing CSV files.</p><p>Here’s an example of how to read a CSV file into a list of dictionaries:</p><pre>import csv<br>with open(&quot;example.csv&quot;, &quot;r&quot;) as file:<br>    reader = csv.DictReader(file)<br>    rows = [row for row in reader]<br>    print(rows)</pre><p>The csv.DictReader function takes a file object and returns a reader object, which can be used to iterate over the rows in the file. Each row is returned as a dictionary, with the keys representing the headers in the CSV file and the values representing the corresponding values in each row. You can access individual values by referencing the keys of the dictionary.</p><h4>Writing Files in Python</h4><p>In addition to reading files, Python also provides several methods for writing files. You can write data to a file by using the write() method of a file object, or by using the csv library for writing CSV files.</p><p>Here’s an example of how to write a string to a text file:</p><pre>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<br>    file.write(&quot;Hello, World!&quot;)</pre><p>In this example, the &quot;w&quot; mode is used, which means that the file will be opened for writing and any existing content in the file will be overwritten. If you want to append data to the file instead of overwriting it, you can use the &quot;a&quot; mode:</p><pre>with open(&quot;example.txt&quot;, &quot;a&quot;) as file:<br>    file.write(&quot;\nHello again!&quot;)</pre><p>To write binary data to a file, you can use the &quot;wb&quot; mode:</p><pre>with open(&quot;example.bin&quot;, &quot;wb&quot;) as file:<br>    file.write(b&quot;\x01\x02\x03&quot;)</pre><h4><strong>Writing CSV Files in Python</strong></h4><p>To write a CSV file in Python, you can use the csv library and its writer and DictWriter classes. Here&#39;s an example of how to write a list of dictionaries to a CSV file:</p><pre>import csv<br>data = [<br>    {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;},<br>    {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;London&quot;},<br>    {&quot;name&quot;: &quot;Jim&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Paris&quot;},<br>]<br>with open(&quot;example.csv&quot;, &quot;w&quot;) as file:<br>    writer = csv.DictWriter(file, fieldnames=[&quot;name&quot;, &quot;age&quot;, &quot;city&quot;])<br>    writer.writeheader()<br>    writer.writerows(data)</pre><p>In this example, the csv.DictWriter class is used to write the data to the file. The fieldnames argument is a list of the keys in the dictionaries, which will be used as the headers in the CSV file. The writeheader() method writes the headers to the file, and the writerows() method writes each row of data.</p><p>Conclusion</p><p>In this article, we have covered various operations for reading and writing files in Python, including text, binary, and CSV files. Whether you’re reading an entire file into memory, reading it line by line, or writing data to a file, Python provides a variety of methods and libraries to help you get the job done.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3c77a46c4f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Variables in {Python}]]></title>
            <link>https://medium.com/@shadrul.100/variables-in-python-5d4789aaf35e?source=rss-43217e16a1e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d4789aaf35e</guid>
            <category><![CDATA[python-variable]]></category>
            <category><![CDATA[toilet-seat-articles]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[variables-in-python]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Shadrul]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 20:55:53 GMT</pubDate>
            <atom:updated>2023-01-23T20:55:53.738Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*mB_Trf1NFTe66Wkr8uSc3w.jpeg" /><figcaption>Python Variables</figcaption></figure><blockquote>Variables are a fundamental concept in programming, and Python is no exception. In Python, a variable is a named location in memory where a value can be stored and retrieved later. Variables are used to store data that can be manipulated, processed, or used to control the flow of a program.</blockquote><p>In Python, variables are defined using the assignment operator (=), which assigns a value to a variable. For example, the following code defines a variable called “x” and assigns it the value of 5:</p><pre>x = 5</pre><p>Once a variable has been defined, its value can be accessed by referencing the variable name. For example, the following code prints the value of “x” to the console:</p><pre>print(x) # Output: 5</pre><blockquote>Please note that every variable in python is a <strong>Object, </strong>so if you try to print the type of any variable using the function <strong>type</strong>; it prints out the corresponding class.</blockquote><pre>print(type(x)) # Output: &lt;class &#39;int&#39;&gt;</pre><p>It’s important to note that variables in Python are not explicitly typed. This means that a variable can hold any type of data, such as a number, a string, or a list. For example, the following code assigns a string value to the variable “name”:</p><pre>name = &quot;John Smith&quot;</pre><p>And the following code assigns a list value to the variable “numbers”:</p><pre>numbers = [1, 2, 3, 4, 5]</pre><p>A variable can also be reassigned to a new value at any time. For example, the following code first assigns the value of 5 to “x”, and then reassigns it the value of 10:</p><pre>x = 5<br>x = 10</pre><p>It’s also possible to perform mathematical operations on variables and store the result in a new variable or the same variable. For example, the following code defines a variable “a” with value 2, and a variable “b” with value 3, then creates a new variable “c” which value is the sum of “a” and “b”:</p><pre>a = 2<br>b = 3<br>c = a + b</pre><p>In summary, variables are an essential concept in programming and Python provides a simple and flexible way to work with them. They allow you to store, manipulate and retrieve data in your code, and they are essential to control the flow of a program.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d4789aaf35e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Types in {Python}]]></title>
            <link>https://medium.com/@shadrul.100/data-types-in-python-f1b7f3d871ec?source=rss-43217e16a1e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1b7f3d871ec</guid>
            <category><![CDATA[data-type]]></category>
            <category><![CDATA[python-data-type]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[toilet-seat-articles]]></category>
            <dc:creator><![CDATA[Shadrul]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 20:40:29 GMT</pubDate>
            <atom:updated>2023-01-23T20:40:29.849Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*wcU5s8FrJeTt2lRt3MDtVw.png" /><figcaption>Data Types in Python</figcaption></figure><p>Python is a powerful, high-level programming language. One of the key features of Python is its support for a wide range of data types, which allows programmers to efficiently store and manipulate various types of data in their programs.</p><p>There are several built-in data types in Python, including:</p><ol><li><strong>Numbers</strong>: Python supports three types of numbers: <strong>integers</strong>, <strong>floating-point numbers</strong>, and <strong>complex numbers</strong>. Integers are whole numbers, such as 1, 2, and 3, while floating-point numbers are numbers with decimal points, such as 3.14 or 2.718. Complex numbers are numbers in the form of a+bi, where a and b are real numbers and i is the imaginary unit. For example, 3+4i is a complex number.</li></ol><p>Example:</p><pre>x = 5<br>y = 2.5<br>z = 3+4j<br>print(type(x))<br>print(type(y))<br>print(type(z))</pre><p>Output:</p><pre>&lt;class &#39;int&#39;&gt;<br>&lt;class &#39;float&#39;&gt;<br>&lt;class &#39;complex&#39;&gt;</pre><blockquote>Since in Python everything is “Object” that’s why when we try to print type of any datatype it prints out it’s corresponding class.</blockquote><p>2. <strong>Strings</strong>: A string is a sequence of characters enclosed in <strong>single or double quotes</strong>. Strings are used to represent text in Python. They can be concatenated, sliced, and indexed.</p><p>Example:</p><pre>name = &quot;John Smith&quot;<br>age = &quot;25&quot;<br>print(name)<br>print(age)</pre><p>Output:</p><pre>John Smith<br>25</pre><p>3. <strong>Lists</strong>: A list is a collection of items enclosed in square brackets. Lists are <strong>ordered</strong>, <strong>mutable</strong>, and can contain elements of <strong>different data types</strong>. Lists are commonly used to store multiple values in a single variable.</p><p>Example:</p><pre>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;orange&quot;]<br>print(fruits)</pre><p>Output:</p><pre>[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]</pre><p><strong>4. Tuples</strong>: A tuple is similar to a list but enclosed in parentheses. Tuples are <strong>ordered</strong>, <strong>immutable</strong>, and can contain elements of <strong>different data types.</strong> Tuples are commonly used to store multiple values in a single variable that should not be modified.</p><p>Example:</p><pre>coordinates = (4, 5)<br>print(coordinates)</pre><p>Output:</p><pre>(4, 5)</pre><p><strong>5. Dictionaries</strong>: A dictionary is a collection of<strong> key-value pairs</strong> enclosed in curly braces. Dictionaries are <strong>unordered</strong>, <strong>mutable</strong>, and can contain elements of <strong>different data types</strong>. They are commonly used to store data in a more structured way.</p><p>Example:</p><pre>person = {&quot;name&quot;: &quot;John Smith&quot;, &quot;age&quot;: 25}<br>print(person)</pre><p>Output:</p><pre>{&#39;name&#39;: &#39;John Smith&#39;, &#39;age&#39;: 25}</pre><p>6. <strong>Sets</strong>: A set is a collection of <strong>unique items</strong> enclosed in curly braces. Sets are <strong>unordered</strong> and can contain elements of d<strong>ifferent data types</strong>. They are commonly used to perform mathematical set operations such as union, intersection, and difference.</p><p>Example:</p><pre>numbers = {1, 2, 3, 4, 5}<br>print(numbers)</pre><p>Output:</p><pre>{1, 2, 3, 4, 5}</pre><p>7. <strong>Boolean</strong>: Boolean data type has two logical values, <strong>True</strong> and <strong>False</strong>.</p><p>Example:</p><pre>isApplicable = True<br>print(isApplicable)</pre><p>Output:</p><pre>True</pre><p>In conclusion, Python provides a wide range of data types that can be used to efficiently store and manipulate various types of data in a program. Understanding and correctly using the different data types available in Python can greatly enhance the efficiency and readability of your code. It is important to choose the right data type for the task at hand and use it appropriately to get the most out of Python’s capabilities.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1b7f3d871ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Packages in {GO}]]></title>
            <link>https://medium.com/@shadrul.100/packages-in-go-d9235ed290d8?source=rss-43217e16a1e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9235ed290d8</guid>
            <category><![CDATA[toilet-seat-articles]]></category>
            <category><![CDATA[go-packages]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[go-tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Shadrul]]></dc:creator>
            <pubDate>Sun, 22 Jan 2023 22:05:34 GMT</pubDate>
            <atom:updated>2023-01-22T22:05:34.819Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*cHaMGgjIkC-384zHvfYX9g.png" /><figcaption>Go Packages</figcaption></figure><p>In the Go programming language, a package is a collection of Go source files in the same directory that are compiled together. Packages are used to organize and reuse code, and they provide a way to encapsulate and protect the implementation details of a set of related functions, types, and variables.</p><blockquote>Go has a built-in concept of packages, which makes it easy to organize and share code. Each Go source file starts with a package declaration that specifies the package to which the file belongs. For example, the following code declares that the file is part of the “<strong>mypackage</strong>” package:</blockquote><pre>package mypackage</pre><p>All the Go source files in the same directory that have the same package declaration belong to the same package.</p><blockquote>A package can have one or more exported (public) and unexported (private) elements. Exported elements start with an uppercase letter, while unexported elements start with a lowercase letter. Exported elements can be accessed from other packages, while unexported elements can only be accessed within the same package.</blockquote><p>To use a package in a Go program, you must import it using the import keyword. For example, to use the “mypackage” package in a program, you would use the following import statement:</p><pre>import &quot;mypackage&quot;</pre><p>Once imported, you can use the exported elements of the package by prefixing them with the package name. For example, if “mypackage” has an exported function called “MyFunction”, you would call it like this:</p><pre>mypackage.MyFunction()</pre><p>It’s also possible to import a package with a different name, using the following syntax:</p><pre>import myalias &quot;mypackage&quot;</pre><p>This can be useful to avoid naming conflicts or to provide more meaningful names for the imported package.</p><blockquote>Go also has a special package called “main”, which is used to define the entry point of a Go program. A Go program must have exactly one package called “main”, and it must contain a function called “main” that serves as the entry point of the program.</blockquote><p>Here is an example of a simple Go program that uses a package called “mymath” to calculate the square of a number:</p><pre>package main<br><br>import &quot;mymath&quot;<br><br>func main() {<br>    result := mymath.Square(5)<br>    println(result)<br>}</pre><p>And the package mymath:</p><pre>package mymath<br><br>func Square(x int) int {<br>    return x * x<br>}</pre><p>In this example, the “main” package imports the “mymath” package and uses its Square function to calculate the square of the number 5.</p><p>In Go, a program that contains a package named “main” is considered an <strong>executable</strong> program. An example of a simple executable program in Go is as follows:</p><pre>package main<br><br>import &quot;fmt&quot;<br>func main() {<br>    fmt.Println(&quot;!... Hello World ...!&quot;)<br>}</pre><p>To create an executable file from this program, you can use the command</p><pre>go build main.go</pre><p>This will create a new file named “main” in the same directory, which can be executed by using the command.</p><pre>go run main.go</pre><p>On the other hand, a program that does not contain a package named “main” is considered a <strong>reusable</strong> program. These programs cannot be directly executed and are intended to be used as a library for other programs. An example of a reusable program in Go would be:</p><pre>package shadrul<br><br>import &quot;fmt&quot;<br>func someFunc() {<br>    fmt.Println(&quot;This is a reusable program&quot;)<br>}</pre><p>If you try to build this file with the command</p><pre>go build shadrul.go</pre><p>you will not see an executable file created. Instead, this file can be imported into another program and its functions can be used.</p><blockquote>In summary, packages in Go provide a way to organize and reuse code, and they allow for encapsulation and protection of implementation details. Packages are defined by a package declaration and consist of one or more Go source files in the same directory. Packages can have exported and unexported elements, and they can be imported and used in other Go programs. The “main” package is a special package that is used to define the entry point of a Go program.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9235ed290d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>