<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pawankashap</title>
    <description>The latest articles on DEV Community by Pawankashap (@pawankashap).</description>
    <link>https://dev.to/pawankashap</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1082701%2F01a06f3c-1979-431d-8c27-b68aea898722.jpeg</url>
      <title>DEV Community: Pawankashap</title>
      <link>https://dev.to/pawankashap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pawankashap"/>
    <language>en</language>
    <item>
      <title>SQLAlchemy data models relationship</title>
      <dc:creator>Pawankashap</dc:creator>
      <pubDate>Sun, 08 Oct 2023 22:25:33 +0000</pubDate>
      <link>https://dev.to/pawankashap/sqlalchemy-data-models-relationship-5ci8</link>
      <guid>https://dev.to/pawankashap/sqlalchemy-data-models-relationship-5ci8</guid>
      <description>&lt;p&gt;There are many benefits to using SQLAlchemy, including simplified database connection management and seamless integration with libraries like Pandas. If you are in the business of building applications, I would venture to guess that using an ORM to manage your app's data is a top priority and a great use case for SQLAlchemy.&lt;br&gt;
As someone with a strong background in data, it's understandable to find database model management easier than SQL queries as a software engineer. However, the use of foreign keys and distinguishing between one-to-many and many-to-many relationships may seem like unnecessary abstractions. It's important to remember that these elements are not limitations of SQL and serve a specific purpose in executing JOINs between tables.&lt;br&gt;
The purpose of utilizing an ORM is to reduce the workload of application developers by converting database concepts into code that can be easily replicated within our application. In this session, we will be examining how to define SQLAlchemy data models, with a focus on effectively managing table relationships.&lt;br&gt;
We will be discussing vanilla SQLAlchemy. If you want to learn about implementing SQLAlchemy data models in Flask, check out this post after.&lt;/p&gt;

&lt;h1&gt;
  
  
  Definition of a Basic Model
&lt;/h1&gt;

&lt;p&gt;Before explaining how to define relationships, let's review the process of creating a basic database model.&lt;/p&gt;

&lt;p&gt;To create SQLAlchemy database models, we need to create classes that extend an SQLAlchemy base, which is usually a declarative_base(). We import this base from the sqlalchemy package. The database models are made up of Columns, which are specific data types used in SQLAlchemy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9hTLS7l0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj5uu1tutzg72f4t2fcx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9hTLS7l0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj5uu1tutzg72f4t2fcx.png" alt="Image description" width="669" height="715"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above creates a model that makes use of all available column types in SQLAlchemy. Each model we create corresponds with a table, where each Column object represents a column in the resulting table. When our app initializes SQLAlchemy, tables will be created in our database to match each model, provided they don't already exist.&lt;/p&gt;

&lt;p&gt;We set optional built-in variables in our model such as &lt;strong&gt;tablename&lt;/strong&gt; and &lt;strong&gt;table_args&lt;/strong&gt;. The former determines the name of the resulting database table, while the latter allows us to set which Postgres schema our table will belong to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating Tables From Our Models&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After creating our model, we must create the corresponding table in our database. To achieve this, we can execute the create_all() method on the Base object, which our model inherits from. Below is a script that automates this task:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--acizHSmu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bky09l0cnnadmjc2j82t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--acizHSmu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bky09l0cnnadmjc2j82t.png" alt="Image description" width="669" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have saved the ExampleModel class to a file named models.py, from where we import Base. After creating the engine, the line Base.metadata.create_all(engine) generates all the tables related to our models.&lt;/p&gt;

&lt;p&gt;You can also take a look at the SQL query that has been produced by our example model:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FdQUNLwW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6gumkpn71n4rhc1khmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FdQUNLwW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6gumkpn71n4rhc1khmw.png" alt="Image description" width="670" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  One-to-Many &amp;amp; Many-to-One Relationships
&lt;/h2&gt;

&lt;p&gt;One-to-many or many-to-one relationships are widely used in databases. For instance, a customer can have multiple orders, or a sports player can belong to a single team. To illustrate the latter, we'll create some models.&lt;/p&gt;

&lt;p&gt;Below is a code snippet that may appear confusing initially, but don't worry, it's not easy for anyone to understand at first glance. We'll work together to make sense of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ESJH6BD9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz1as4nq8j9c69mmg2za.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ESJH6BD9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz1as4nq8j9c69mmg2za.png" alt="Image description" width="668" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right away, we can identify a couple of key elements. We have two distinct models with their own Columns: one for players and another for teams. There are also two new features to take note of.&lt;/p&gt;

&lt;p&gt;Firstly, we've introduced the concept of Foreign keys on the PlayerModel's team_id column. If you're familiar with SQL, you'll be well-versed in this area. If not, consider it like this: a foreign key is a characteristic of a column that signifies a relationship between tables. Usually, items from one table are closely associated with those in another table, such as when customers "own" orders or when teams "own" players. In our scenario, we're stating that each player is associated with a team, as depicted by their team_id. This enables us to combine data from both our players and team tables.&lt;/p&gt;

&lt;p&gt;A new concept that we're introducing here is relationships. These relationships work alongside foreign keys and enable us to build connections between two models in our application (not the database). It's worth noting that the foreign key value we use is 'example.sqlalchemy_tutorial_teams.id'. In this context, 'example' refers to our Postgres schema and 'sqlalchemy_tutorial_teams' is the table name for our teams table. When it comes to the relationship value we pass, we use "TeamModel" which is the class name of the target data model, not the table name. Essentially, foreign keys convey the relationships we're building to SQL, while relationships inform our application of the same. Both need to be done.&lt;/p&gt;

&lt;p&gt;The purpose of this is to make it simple to perform JOINs in our app. If we were to use an ORM, we wouldn't be able to specify which columns to join on by simply saying "join this model with that model". However, by defining our relationships in our models, we can easily join two tables together without the need for any additional details. SQLAlchemy is able to do this by examining the data models we've set up, which are enforced by the foreign keys and relationships we've defined. This saves us the hassle of handling data-related logic while creating our app's business logic, as we're able to define these relationships upfront.&lt;/p&gt;

&lt;p&gt;If the tables already exist, SQLAlchemy will not create new ones from the data models. This means that if there are any issues with relationships during the first run of the app, those errors will still persist on the second run, even if we believe we have corrected them. To address unexpected error messages, it may be helpful to remove the SQL tables before running the app again after making changes to the model.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Back References *&lt;/em&gt;&lt;br&gt;
When we specify relationships on a data model, it enables us to access properties of the joined model by using a property on the original model. For instance, if we join our PlayerModel with our TeamModel, we can retrieve properties of a player's team by using PlayerModel.team.name. Here, team refers to the name of our relationship and name is a property of the associated model.&lt;/p&gt;

&lt;p&gt;One-directional relationships are created where team details can be accessed through a player, but not the other way around. Adding a back reference can solve this issue.&lt;/p&gt;

&lt;p&gt;When creating a relationship, we can set the backref attribute to make it bi-directional. Here's how we modify the previous relationship:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v1sObsUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmjmmud2pg0c9kwrmiyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v1sObsUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmjmmud2pg0c9kwrmiyo.png" alt="Image description" width="666" height="87"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, with a backref, we can access a team's player details by calling TeamModel.player.&lt;/p&gt;

&lt;h2&gt;
  
  
  JOIN operation.
&lt;/h2&gt;

&lt;p&gt;After successfully establishing a connection between two data models, the most effective way to verify your work is by performing a JOIN on these models. We won't delve into the creation of complex SQLAlchemy ORM queries here, but we can use this method to confirm our progress.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ASccxvhK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf5pzsviubr5zybj6dd4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ASccxvhK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf5pzsviubr5zybj6dd4.png" alt="Image description" width="668" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we JOIN the TeamModel and PlayerModel, we reference everything as a property of PlayerModel. Here is the sample data output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BKfLv1GN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89c1gepkit052s9x9dw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BKfLv1GN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89c1gepkit052s9x9dw8.png" alt="Image description" width="668" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Many-to-Many Relationships
&lt;/h1&gt;

&lt;p&gt;Establishing foreign key relationships is beneficial when we anticipate a table in our relationship to have only one record per multiple records in another table (for example, one player per team). However, when players can belong to several teams, things become intricate.&lt;/p&gt;

&lt;p&gt;You may have already noticed that many-to-many relationships occur between tables where any number of records from table 1 can be linked to any number of records from table 2. SQLAlchemy makes use of association tables to establish these relationships. An association table is a SQL table that is specifically designed to clarify these connections and we will create one now.&lt;/p&gt;

&lt;p&gt;Take a look at how the association_table variable is defined below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b4DLx0qQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3seb0ln51n46lu9uo41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b4DLx0qQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3seb0ln51n46lu9uo41.png" alt="Image description" width="487" height="767"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have implemented a new data type, called Table, to create a many-to-many association between two tables. To define this association, we provide the name of the resulting table as the first parameter, which we have named "association". Additionally, we associate this new table with the same declarative base as our data models, by passing Base.metadata. Finally, we create two columns as foreign keys, linking the team_id column of PlayerModel with the id column of TeamModel.&lt;/p&gt;

&lt;p&gt;What we're doing is creating a third table that links our two existing tables. Instead of creating a whole new data model, we're opting for the simpler solution of an association table. Moving forward, we can now directly access the association table to retrieve information from both our players and teams tables.&lt;/p&gt;

&lt;p&gt;To implement an association table, the final step is to establish a relationship on the data model. It's worth noting that we set a relationship on the PlayerModel as we did before, but this time we assigned the secondary attribute to the name of the association table.&lt;/p&gt;

</description>
      <category>python</category>
      <category>backenddevelopment</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Data Structure in Python</title>
      <dc:creator>Pawankashap</dc:creator>
      <pubDate>Sun, 27 Aug 2023 02:23:49 +0000</pubDate>
      <link>https://dev.to/pawankashap/data-structure-in-python-4250</link>
      <guid>https://dev.to/pawankashap/data-structure-in-python-4250</guid>
      <description>&lt;p&gt;Python is a versatile and dynamic programming language that offers a range of built-in data structures. These data structures allow developers to organize and manipulate data efficiently, providing the basis for building high-performance algorithms. In this article, we will delve into some of the most commonly used data structures in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lists:&lt;/strong&gt;&lt;br&gt;
Lists in Python are highly versatile data structures that let you organize a collection of items in a specific order, regardless of their type. The great thing about these lists is that they can be changed, allowing you to modify their contents even after they have been created.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aE7PesW1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xieqllz6trljc9r35nez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aE7PesW1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xieqllz6trljc9r35nez.png" alt="Image description" width="649" height="48"&gt;&lt;/a&gt;&lt;br&gt;
The list named "my_list" has a combination of various data types such as integers 1, 2, and 3, the string "hello", and the boolean value True. Lists are collections of items that can be modified and hold elements of different types. It is possible to access, modify, add, and remove elements within a list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tuples:&lt;/strong&gt;&lt;br&gt;
Tuples, like lists, are collections of items in a specific order. However, unlike lists, tuples cannot be altered once they are created, making them immutable. Tuples are frequently utilized to represent predetermined sets of values.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QzOWhmmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcor197n35bmncc3quec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QzOWhmmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcor197n35bmncc3quec.png" alt="Image description" width="649" height="53"&gt;&lt;/a&gt;&lt;br&gt;
You have created a tuple named my_tuple with four elements in this code, which are the integers 1, 2, and 3, and the string "world". Tuples are defined using parentheses ( ) and the elements inside the tuple are separated by commas.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dictionaries:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Associative arrays, also known as dictionaries or maps, allow for the storing of key-value pairs. They provide quick access to values by utilizing unique keys. Dictionaries are useful for tasks that involve converting one set of values into another.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VwUihRai--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5m3z991z9x9fj91wqxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VwUihRai--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5m3z991z9x9fj91wqxh.png" alt="Image description" width="644" height="53"&gt;&lt;/a&gt;&lt;br&gt;
My_dict is a dictionary that stores information using key-value pairs. Each key such as "name", "age", and "city" is associated with a corresponding value like "Alice", 30, and "New York". By using dictionaries, one can retrieve values quickly using their keys. That makes them perfect for organizing and accessing structured data. In this particular case, the dictionary stores information about a person's name, age, and city.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sets:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Collections of unique elements are called sets. These sets are especially helpful when you want to remove duplicates from a list or check for membership in a constant amount of time.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BN1buQhz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erc5ptdizq8rorpmhvfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BN1buQhz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erc5ptdizq8rorpmhvfm.png" alt="Image description" width="648" height="49"&gt;&lt;/a&gt;&lt;br&gt;
In the set called "my_set", there are five unique values: the integers 1, 2, 3, 4, and 5. Sets are collections that do not have a specific order and eliminate duplicates, guaranteeing that each element only appears once. Sets are useful when working with separate values or performing set operations, such as intersection, union, and difference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stacks and Queues:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python may not have dedicated classes for stacks and queues, but you can easily create them using lists. Lists come equipped with useful functions that allow you to replicate the behavior of both a stack (Last-In-First-Out) and a queue (First-In-First-Out).&lt;/p&gt;

&lt;p&gt;Example (Stack):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s----emtnt4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfvltzz8v6kzgylr1km4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s----emtnt4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfvltzz8v6kzgylr1km4.png" alt="Image description" width="649" height="123"&gt;&lt;/a&gt;&lt;br&gt;
To create a stack data structure, start by initializing an empty list called "stack". Use the "append()" method to add values to the stack. For example, "stack.append(1)" adds the value 1 to the top of the stack. Adding "stack.append(2)" places 2 on top of 1. To retrieve the top item from the stack, use the "pop()" method. This removes and retrieves the top item, which in this case is 2. Store the retrieved value in a variable called "top_item".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked Lists:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python allows the use of custom classes to create linked lists. Each node in these lists contains data and a reference to the next node. Linked lists are highly efficient for insertion and deletion operations, making them very useful.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yhhi0azm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yysgijrtaomxq4xqb7dh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yhhi0azm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yysgijrtaomxq4xqb7dh.png" alt="Image description" width="647" height="244"&gt;&lt;/a&gt;&lt;br&gt;
To represent individual nodes in a linked list, you can define a class called Node. Each node contains data and a reference to the next node in the list. The constructor for the Node class is the &lt;strong&gt;init&lt;/strong&gt; method, which initializes a new node with the provided data. The data attribute of the node is set to the value passed when creating the node. Additionally, the next attribute is initialized as None, indicating that the node doesn't yet have a reference to the next node in the list. To create nodes with specific data values, you can use the syntax Node(value). For example, Node(1) creates a node named node1 with data value 1. To link nodes together, you can set the next attribute of one node to point to the other node. For instance, node1.next = node2 creates a link between node1 and node2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For developers, Python's built-in data structures can be highly beneficial when dealing with various computational issues. Understanding the distinct features and appropriate usage of each structure can assist in making informed decisions about which one to implement in different situations. Whether working on simple scripts or intricate applications, mastering these structures can enhance the ability to write efficient and effective Python code.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>programming</category>
    </item>
    <item>
      <title>React's Hooks useState and useEffect</title>
      <dc:creator>Pawankashap</dc:creator>
      <pubDate>Sun, 02 Jul 2023 05:44:04 +0000</pubDate>
      <link>https://dev.to/pawankashap/understanding-reacts-usestate-and-useeffect-hooks-39mp</link>
      <guid>https://dev.to/pawankashap/understanding-reacts-usestate-and-useeffect-hooks-39mp</guid>
      <description>&lt;p&gt;Hooks were first introduced in version 16.8 of React, and they have revolutionized the way we write React code. With Hooks, you can easily extract stateful logic from functional components, making it possible to reuse and test it independently.&lt;/p&gt;

&lt;p&gt;React developers should have a strong grasp of two critical Hooks: useState and useEffect. These Hooks are essential for managing state and performing side effects in functional components. In this blog post, we will explore the useState and useEffect hooks, their usage, and how they simplify React development.&lt;/p&gt;

&lt;h1&gt;
  
  
  useState Hook
&lt;/h1&gt;

&lt;p&gt;When working with React applications, managing state can be done using either class components or the useState hook. While both options serve the same purpose, there are variations in syntax, lifecycle methods, and code organization that set them apart.&lt;/p&gt;

&lt;p&gt;Example of a counter using a class component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R4VYWcfR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8kfdfitngs2kedfb3ux.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R4VYWcfR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8kfdfitngs2kedfb3ux.png" alt="Image description" width="646" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s look at doing the same thing with the React useState Hook, you can easily incorporate a state in a functional component. This hook provides an array consisting of two elements - the current state value and a function that can be used to update the state.&lt;/p&gt;

&lt;p&gt;Example of a counter using the useState hook:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ceV2Ne8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tq93grxen23n1cgc3y8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ceV2Ne8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tq93grxen23n1cgc3y8y.png" alt="Image description" width="646" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To implement the functionality, the initial step is to import the useState Hook from React. Then, within the Counter function, we need to invoke the useState Hook. This will provide us with the current state and the corresponding function that modifies it, as demonstrated in the example with count and &lt;em&gt;setCount&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It's important to keep in mind that variables typically disappear upon exiting a function. However, React is unique in that it preserves state variables, including the count variable.&lt;/p&gt;

&lt;p&gt;With the useState Hook, you have the ability to declare state variables of any type. Additionally, this Hook can be utilized multiple times within a single component.&lt;/p&gt;

&lt;h1&gt;
  
  
  useEffect Hook
&lt;/h1&gt;

&lt;p&gt;React's useEffect hook is a powerful feature that allows you to handle side effects in functional components. Side effects are tasks that are executed outside the scope of the component's rendering, such as data fetching, subscribing to events, or interacting with the browser's DOM. The useEffect hook replaces the lifecycle methods in class components and provides a clean and declarative way to manage side effects in React applications.&lt;/p&gt;

&lt;p&gt;Here's an example that demonstrates the usage of useEffect:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j-UbKsn1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvuazfvaxkp8x4slqrup.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j-UbKsn1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvuazfvaxkp8x4slqrup.png" alt="Image description" width="649" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the previous example, a timer is initiated using the useEffect hook to increment the seconds state every second. To ensure that the effect runs only once upon component mount, an empty dependency array [] is passed as the second argument.&lt;/p&gt;

&lt;p&gt;The function passed to useEffect can contain any code that should be executed as a side effect, such as data fetching, API calls, subscribing to events, or modifying the DOM. The cleanup function, if provided, is returned from the effect function and will be called when the component unmounts or when the dependencies change.&lt;/p&gt;

&lt;p&gt;React components can have two types of side effects: ones that require cleanup and ones that do not. In the previous example, no cleanup was necessary. However, setting up a subscription, closing a socket, and clearing timers are a few examples of side effects that do require cleanup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useEffect with cleanup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The cleanup function within useEffect plays a crucial role in releasing resources, preventing memory leaks, avoiding unexpected behavior, maintaining a clean state, and optimizing resource utilization. By managing side effects properly, it helps to ensure that your React components perform well, are predictable, and resilient.&lt;/p&gt;

&lt;p&gt;Optional cleanup functions are available to clean up any resources or subscriptions that may have been created by the effectful function. These functions are executed either when the component is unmounted or when the dependencies of the useEffect hook change.&lt;/p&gt;

&lt;p&gt;Here's an example of using useEffect with cleanup:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fu1qlrgs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgfs2hhnyo1hnyhvzhuw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fu1qlrgs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgfs2hhnyo1hnyhvzhuw.png" alt="Image description" width="643" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our program utilizes a state variable, "count," which initializes at 0 and increments every second through the use of setInterval. Within the effect function, we establish the interval via setInterval and assign its reference to the "interval" variable. The cleanup function is then returned from the effect function, which clears the interval using clearInterval and logs a console message. As the dependency array is empty ([]), the effect will only run once after the initial render.&lt;/p&gt;

&lt;p&gt;Once the component is mounted, the effect function runs and sets up the interval. Every second, the setInterval function increases the count state variable. The component then re-renders with the latest count value.&lt;/p&gt;

&lt;p&gt;When the component is unmounted or if any dependencies change, the cleanup function is activated. This function uses clearInterval to stop the interval and prevent it from incrementing the count. Moreover, it writes the message "Interval cleared" to the console. By utilizing clearInterval in the cleanup function, we can guarantee that the interval is correctly cleared and won't continue to run when the component is not in use. This prevents memory leaks and ensures that the component functions correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Empty Dependency Array&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;To pass an empty array of dependencies as the second argument, we can do it in this way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o-gDlV44--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shfllpnp8s1ptr6osats.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o-gDlV44--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shfllpnp8s1ptr6osats.png" alt="Image description" width="474" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you use an empty dependency array ([]) as the second argument with the useEffect hook, it indicates that the effect should only run once during the initial rendering of the component. This approach guarantees that the effect is not reliant on any particular values or state variables, and as such, it won't need to be re-executed if any dependencies change.&lt;/p&gt;

&lt;p&gt;Here's an example of fetching data from an API using useEffect:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L7JVnDTC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ptda22pwtsva6nupqux.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L7JVnDTC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ptda22pwtsva6nupqux.png" alt="Image description" width="641" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To store the data retrieved from the API, we utilize the useState function to create a state variable named "data". We make use of the useEffect hook, which is passed an empty dependency array ([]) as its second argument, to retrieve data from the API via axios.get. This guarantees that the effect runs only once during the component's initial rendering. Using setData, we store the fetched data in the "data" state variable. Finally, we map and display the names of the items in the JSX.&lt;/p&gt;

&lt;p&gt;To ensure that the useEffect hook runs only once when the component is mounted, you can provide an empty dependency array. This hook fetches data from the API and updates the data state variable. As a result, the effect will not be triggered again during subsequent re-renders of the component. This prevents unnecessary API calls and ensures that the data is fetched only once.&lt;/p&gt;

&lt;p&gt;In summary, React functional components have been transformed by the introduction of useState and useEffect hooks. These hooks offer a simpler and more effective way to manage state and handle side effects, resulting in cleaner and easier-to-maintain code. By gaining a thorough understanding of these hooks, developers can effortlessly create highly interactive and efficient React applications.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Scope</title>
      <dc:creator>Pawankashap</dc:creator>
      <pubDate>Sun, 14 May 2023 21:45:00 +0000</pubDate>
      <link>https://dev.to/pawankashap/javascript-scope-122d</link>
      <guid>https://dev.to/pawankashap/javascript-scope-122d</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this blog post, I'll explain the scope and how to use it to your advantage when writing code. I'll also look at some common scoping issues and how to solve them. Let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In JavaScript, scope refers to the current context of your code. This context determines where you can access certain variables and functions. In other words, where you decide to define a variable or function in JavaScript impacts where you can access it later. So, if you define a variable inside a function, you can only access it inside that function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are 3 types of scope in JavaScript&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;    Global scope&lt;/li&gt;
&lt;li&gt;    Function scope&lt;/li&gt;
&lt;li&gt;    Block scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Global Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Global scope means that a variable or function is available anywhere in your code. This is the default scope for variables and functions in JavaScript.&lt;br&gt;
Let's take a look at an example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nKApK6an--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90tuntneqm4yt8tcu188.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nKApK6an--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90tuntneqm4yt8tcu188.png" alt="Image description" width="317" height="92"&gt;&lt;/a&gt;&lt;br&gt;
In the code snippet above, I have defined a variable called 'animal' with a value of 'cat'. I have also defined a function called 'printAnimal', which prints the value of 'animal' to the console. Because I have defined 'animal' in the global scope, I can access it inside the 'printAnimal' function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Function scope is similar to a local scope in that variables and &lt;br&gt;
each function creates a new scope. However, there is one key difference: Variables defined inside a function are not accessible from outside the function.&lt;br&gt;
Let's take a look at an example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LlSRP4y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdrgrn9rm5fimt5ruziw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LlSRP4y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdrgrn9rm5fimt5ruziw.png" alt="Image description" width="422" height="99"&gt;&lt;/a&gt;&lt;br&gt;
In the code snippet above, I have defined a function called 'printAnimal'. I have also defined a variable called 'animal' with a value of 'cat'.&lt;br&gt;
Because I have defined 'animal' inside the 'printAnimal' function, I can only access it inside that function. If I try to access 'animal' outside of the function, I will get an error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can use let and const keywords inside the Block Scope in JavaScript. Variables declared inside a { } block cannot be accessed from outside the block&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jgZrZcBo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae0wbaulj231evhvgx8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jgZrZcBo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae0wbaulj231evhvgx8m.png" alt="Image description" width="398" height="91"&gt;&lt;/a&gt;&lt;br&gt;
In the code snippet above, I have defined a variable called 'animal' with a value of 'dog'. I have also used the 'let' keyword to create a block scope for the variable inside the { } block.&lt;br&gt;
Because I have defined 'animal' using the 'let' keyword, I can access it inside the code block. However, 'animal' is not available in the global scope. Therefore, if I try to access 'animal' outside of the code block, I will get an error.&lt;/p&gt;

</description>
      <category>scope</category>
      <category>functional</category>
      <category>javascript</category>
      <category>variables</category>
    </item>
  </channel>
</rss>
